interface HttpOptions { method?: string; headers?: any; body?: any; [x: string]: any; } export function httpBase(endpoint: string, { body, ...customConfig }: HttpOptions = {}) { const headers = { 'content-type': 'application/json' } const config: HttpOptions = { method: customConfig.method, credentials: "include", ...customConfig, headers: { ...headers, ...customConfig.headers, }, } if (body) { config.body = JSON.stringify(body) } return window .fetch(endpoint, config) .then(async response => { if (response.ok) { const contentType = response.headers.get("content-type"); if (contentType && contentType.indexOf("application/json") !== -1) { return await response.json(); } else { return await response.text(); } } else { const errorResponse = await response; return Promise.reject(errorResponse); } }) } function httpGet(endpoint: string, { ...config }: HttpOptions = {}) { return httpBase(endpoint, { method: 'GET', ...config }); } function httpPut(endpoint: string, { ...config }: HttpOptions = {}) { return httpBase(endpoint, { method: 'PUT', ...config }); } function httpDelete(endpoint: string, { ...config }: HttpOptions = {}) { return httpBase(endpoint, { method: 'DELETE', ...config }); } function httpPost(endpoint: string, { ...config }: HttpOptions = {}) { return httpBase(endpoint, { method: 'POST', ...config }); } export const http = { get: httpGet, put: httpPut, delete: httpDelete, post: httpPost }