Skip to content

Instantly share code, notes, and snippets.

@baruchvlz
Created February 5, 2019 16:24
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 baruchvlz/6e7d6df1deef629270e0436504bc154c to your computer and use it in GitHub Desktop.
Save baruchvlz/6e7d6df1deef629270e0436504bc154c to your computer and use it in GitHub Desktop.
export function fetchJSON(url: string, method: API_METHODS, data?: any): Promise<any> {
return fetch(url, {
method,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then((res: any) => {
if (res.status >= 400) {
return Promise.reject(res);
}
return res;
})
.then(res => res.json())
.then((json: any) => {
if (json && json.errors && json.errors.length) {
return Promise.reject(json);
}
return json;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment