MyCrypto/common/api/utils.ts

28 lines
628 B
TypeScript
Raw Normal View History

2017-12-11 12:44:53 -05:00
import { indexOf } from 'lodash';
export const filter = (i: any, arr: any[]) => {
return -1 !== indexOf(arr, i) ? true : false;
};
export function checkHttpStatus(response: Response) {
if (response.status >= 200 && response.status < 300) {
return response;
} else {
return new Error(response.statusText);
2017-07-03 22:28:56 -05:00
}
2017-04-12 00:04:27 -05:00
}
export function parseJSON(response: Response) {
return response.json();
2017-04-12 00:04:27 -05:00
}
export async function handleJSONResponse(response: Response, errorMessage: string) {
if (response.ok) {
return await response.json();
}
if (errorMessage) {
throw new Error(errorMessage);
}
return false;
}