You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
trezor-wallet/src/js/utils/networkUtils.js

39 lines
1.2 KiB

/* @flow */
'use strict';
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);
} else if (type === 'binary') {
return await response.arrayBuffer();
} else {
return await response.text();
}
} else {
throw new Error(`${ url } ${ response.statusText }`);
}
// return fetch(url, { credentials: 'same-origin' }).then((response) => {
// if (response.status === 200) {
// return response.text().then(result => (json ? JSON.parse(result) : result));
// } else {
// throw new Error(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);
} else {
throw new Error('jsonRequest error: ' + response.toString() );
}
};