Skip to content

Instantly share code, notes, and snippets.

@maylencita
Last active February 26, 2020 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maylencita/b83f60a2435f879244ae128d1681f633 to your computer and use it in GitHub Desktop.
Save maylencita/b83f60a2435f879244ae128d1681f633 to your computer and use it in GitHub Desktop.
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