UNPKG

admin-on-rest-fr05t1k

Version:

A frontend Framework for building admin applications on top of REST services, using ES6, React and Material UI

38 lines (34 loc) 1.36 kB
import HttpError from './HttpError'; export const fetchJson = (url, options = {}) => { const requestHeaders = options.headers || new Headers({ Accept: 'application/json', }); if (!(options && options.body && options.body instanceof FormData)) { requestHeaders.set('Content-Type', 'application/json'); } if (options.user && options.user.authenticated && options.user.token) { requestHeaders.set('Authorization', options.user.token); } return fetch(url, { ...options, headers: requestHeaders }) .then(response => response.text().then(text => ({ status: response.status, statusText: response.statusText, headers: response.headers, body: text, }))) .then(({ status, statusText, headers, body }) => { let json; try { json = JSON.parse(body); } catch (e) { // not json, no big deal } if (status < 200 || status >= 300) { return Promise.reject(new HttpError((json && json.message) || statusText, status)); } return { status, headers, body, json }; }); }; export const queryParameters = data => Object.keys(data) .map(key => [key, data[key]].map(encodeURIComponent).join('=')) .join('&');