2017-12-13 11:01:37 +00:00
|
|
|
/* @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 {
|
2018-02-20 09:30:36 +00:00
|
|
|
throw new Error(`${ url } ${ response.statusText }`);
|
2017-12-13 11:01:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
// }
|
|
|
|
// })
|
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);
|
|
|
|
} else {
|
|
|
|
throw new Error('jsonRequest error: ' + response.toString() );
|
|
|
|
}
|
2017-12-13 11:01:37 +00:00
|
|
|
};
|