Last active
February 26, 2020 08:43
-
-
Save maylencita/b83f60a2435f879244ae128d1681f633 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'whatwg-fetch'; | |
import { User, Channel, Question, Answer } from './models' | |
interface ServiceState { | |
users: Array<User>, | |
channels: Array<Channel>, | |
user: User | |
} | |
const jsonParsingError = { | |
error: 'Parsing Error', | |
message: 'Impossible to parse server`s response' | |
} | |
type Method = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | |
interface RequestOptions { | |
body?: {} | |
headers?: HeadersInit | |
} | |
interface getResponse<T> { | |
body: T; | |
} | |
class BasicHttpService { | |
get = <T>(url: string, options?: object) => fetchJson<T>(url, 'GET', options) | |
post = <T>(url: string, options?: object, body?: object) => fetchJson<T>(url, 'POST', { ...options, body }) | |
put = <T>(url: string, options?: object, body?: object) => fetchJson<T>(url, 'PUT', { ...options, body }) | |
delete = <T>(url: string, options?: object) => fetchJson<T>(url, 'DELETE', options) | |
} | |
export default class ChatService extends BasicHttpService { | |
serviceUrl = 'http://localhost:3001' | |
public loginUser = (userName: string) => this.post<ServiceState>(`${this.serviceUrl}/login`, {}, { name: userName, avatar: '^^' }) | |
} | |
async function fetchJson<T>(url: string, method: Method, options?: RequestOptions) { | |
const jsonBody = (options && options.body) ? JSON.stringify(options.body) : undefined | |
// tslint:disable:no-any | |
const newOptions: any = { | |
method, | |
body: jsonBody, | |
headers: { | |
'content-type': 'application/json', | |
'pragma': 'no-cache', | |
'cache-control': 'no-cache' | |
}, | |
credentials: 'include' | |
} | |
// tslint:disable:no-any | |
const response = await fetch(encodeURI(url), newOptions) | |
const value = await filterStatus(response) | |
return value as T | |
} | |
async function filterStatus(response: Response) { | |
if (response.status >= 200 && response.status < 300) { | |
return response.json() | |
} else { | |
try { | |
const j = await response.json(); | |
return await Promise.reject(j); | |
} | |
catch (err) { | |
return await Promise.reject(err instanceof Error ? jsonParsingError : err); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment