1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-12-04 14:18:12 +00:00
trezor-wallet/src/utils/networkUtils.js

28 lines
862 B
JavaScript
Raw Normal View History

2017-12-13 11:01:37 +00:00
/* @flow */
2018-07-30 10:52:13 +00:00
2017-12-13 11:01:37 +00:00
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);
2019-03-04 12:33:02 +00:00
}
if (type === 'binary') {
await response.arrayBuffer();
2017-12-13 11:01:37 +00:00
}
await response.text();
2017-12-13 11:01:37 +00:00
}
2018-07-30 10:52:13 +00:00
throw new Error(`${url} ${response.statusText}`);
2018-04-16 21:19:50 +00:00
};
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);
}
2018-07-30 10:52:13 +00:00
throw new Error(`jsonRequest error: ${response.toString()}`);
2019-03-04 12:33:02 +00:00
};