Created
February 5, 2019 16:24
-
-
Save baruchvlz/6e7d6df1deef629270e0436504bc154c 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
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