ra-core
Version:
Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React
88 lines • 3.78 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.flattenObject = exports.queryParameters = exports.fetchJson = exports.createHeadersFromOptions = void 0;
const HttpError_1 = __importDefault(require("./HttpError.cjs"));
const query_string_1 = require("query-string");
const createHeadersFromOptions = (options) => {
const requestHeaders = (options.headers ||
new Headers({
Accept: 'application/json',
}));
const hasBody = options && options.body;
const isContentTypeSet = requestHeaders.has('Content-Type');
const isGetMethod = !options?.method || options?.method === 'GET';
const isFormData = options?.body instanceof FormData;
const shouldSetContentType = hasBody && !isContentTypeSet && !isGetMethod && !isFormData;
if (shouldSetContentType) {
requestHeaders.set('Content-Type', 'application/json');
}
if (options.user && options.user.authenticated && options.user.token) {
requestHeaders.set('Authorization', options.user.token);
}
return requestHeaders;
};
exports.createHeadersFromOptions = createHeadersFromOptions;
/**
* Utility function to make HTTP calls. It's similar to the HTML5 `fetch()`, except it handles JSON decoding and HTTP error codes automatically.
*
* @param url the URL to call
* @param options the options to pass to the HTTP call
* @param options.user the user object, used for the Authorization header
* @param options.user.token the token to pass as the Authorization header
* @param options.user.authenticated whether the user is authenticated or not (the Authorization header will be set only if this is true)
* @param options.headers the headers to pass to the HTTP call
*
* @returns {Promise} the Promise for a response object containing the following properties:
* - status: the HTTP status code
* - headers: the HTTP headers
* - body: the response body
* - json: the response body parsed as JSON
*/
const fetchJson = (url, options = {}) => {
const requestHeaders = (0, exports.createHeadersFromOptions)(options);
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_1.default((json && json.message) || statusText, status, json));
}
return Promise.resolve({ status, headers, body, json });
});
};
exports.fetchJson = fetchJson;
exports.queryParameters = query_string_1.stringify;
const isValidObject = value => {
if (!value) {
return false;
}
const isArray = Array.isArray(value);
const isBuffer = typeof Buffer !== 'undefined' && Buffer.isBuffer(value);
const isObject = Object.prototype.toString.call(value) === '[object Object]';
const hasKeys = !!Object.keys(value).length;
return !isArray && !isBuffer && isObject && hasKeys;
};
const flattenObject = (value, path = []) => {
if (isValidObject(value)) {
return Object.assign({}, ...Object.keys(value).map(key => (0, exports.flattenObject)(value[key], path.concat([key]))));
}
else {
return path.length ? { [path.join('.')]: value } : value;
}
};
exports.flattenObject = flattenObject;
//# sourceMappingURL=fetch.js.map