1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-12-04 06:08:10 +00:00
trezor-wallet/src/utils/networkUtils.js
2019-03-04 13:33:02 +01:00

28 lines
862 B
JavaScript

/* @flow */
import 'whatwg-fetch';
export const httpRequest = async (url: string, type: string = 'text'): any => {
const response: Response = await fetch(url, { credentials: 'same-origin' });
if (response.ok) {
if (type === 'json') {
const txt: string = await response.text();
return JSON.parse(txt);
}
if (type === 'binary') {
await response.arrayBuffer();
}
await response.text();
}
throw new Error(`${url} ${response.statusText}`);
};
export const JSONRequest = async (url: string): Promise<JSON> => {
const response: Response = await fetch(url, { credentials: 'same-origin' });
if (response.ok) {
const txt: string = await response.text();
return JSON.parse(txt);
}
throw new Error(`jsonRequest error: ${response.toString()}`);
};