UNPKG

rxrest

Version:

RxRest a reactive REST utility

125 lines 3.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const index_1 = require("./index"); const utils_1 = require("./utils"); const fetch_1 = require("./fetch"); /** * RxRestConfiguration */ class RxRestConfiguration { constructor() { this.$headers = new Headers(); this.$queryParams = new URLSearchParams(); this.identifier = 'id'; this.requestInterceptors = []; this.responseInterceptors = []; this.errorInterceptors = []; this.abortCallback = () => null; this.uuid = false; this.fetch = fetch_1.fetch; } /** * requestBodyHandler * JSONify the body if it's an `RxRestItem` or an `Object` * * @param {FormData|URLSearchParams|Body|Blob|undefined} body * @returns {any} */ _requestBodyHandler(body) { if (!body) { return undefined; } if (body instanceof FormData || body instanceof URLSearchParams) { return body; } return body instanceof index_1.RxRestItem ? body.json() : JSON.stringify(body); } /** * responseBodyHandler * transforms the response to a json object * * @param {Response} body * @reject when response is an error * @returns {Promise<any>} */ _responseBodyHandler(body) { return body.text() .then(text => { return { body: text ? JSON.parse(text) : null, metadata: null }; }); } get responseBodyHandler() { return this._responseBodyHandler; } set responseBodyHandler(responseBodyHandler) { this._responseBodyHandler = responseBodyHandler; } get requestBodyHandler() { return this._requestBodyHandler; } set requestBodyHandler(requestBodyHandler) { this._requestBodyHandler = requestBodyHandler; } /** * set baseURL * * @param {String} base * @returns */ set baseURL(base) { if (base.charAt(base.length - 1) !== '/') { base += '/'; } this.$baseURL = base; } /** * get baseURL * * @returns {string} */ get baseURL() { return this.$baseURL; } /** * Set global query params * @param {Object|URLSearchParams} params */ set queryParams(params) { if (params instanceof URLSearchParams) { this.$queryParams = params; return; } if (typeof params === 'string') { this.$queryParams = new URLSearchParams(params); return; } this.$queryParams = utils_1.objectToMap(new URLSearchParams(), params); } /** * Get global query params * @return {URLSearchParams} */ get queryParams() { return this.$queryParams; } /** * set global headers * @param {Object|Headers} params */ set headers(params) { if (params instanceof Headers) { this.$headers = params; return; } this.$headers = utils_1.objectToMap(new Headers(), params); } /** * Get global headers * @return Headers */ get headers() { return this.$headers; } } exports.RxRestConfiguration = RxRestConfiguration; //# sourceMappingURL=RxRestConfiguration.js.map