UNPKG

@ace-fetch/uni-app

Version:

uni-app adapter for @ace-fetch/core.

113 lines (112 loc) 4.62 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; import { dispatchRequest } from './dispatchRequest'; import { InterceptorManager } from './interceptorManager'; import { mergeConfig } from './mergeConfig'; var UinAppClient = /** @class */ (function () { function UinAppClient(defaults) { this.defaults = defaults; this.interceptors = { request: new InterceptorManager(), response: new InterceptorManager(), }; } UinAppClient.prototype.request = function (configOrUrl, config) { if (typeof configOrUrl === 'string') { config = config || {}; config.url = configOrUrl; } else { config = configOrUrl || {}; } config = mergeConfig(this.defaults, config); // Set config.method if (config.method) { config.method = config.method.toUpperCase(); } else if (this.defaults.method) { config.method = this.defaults.method.toUpperCase(); } else { config.method = 'GET'; } // filter out skipped interceptors var requestInterceptorChain = [], responseInterceptorChain = []; var synchronousRequestInterceptors = true; this.interceptors.request.forEach(function unshiftRequestInterceptors(interceptor) { if (typeof interceptor.runWhen === 'function' && interceptor.runWhen(config) === false) { return; } synchronousRequestInterceptors = synchronousRequestInterceptors && interceptor.synchronous; requestInterceptorChain.unshift(interceptor.fulfilled, interceptor.rejected); }); this.interceptors.response.forEach(function pushResponseInterceptors(interceptor) { responseInterceptorChain.push(interceptor.fulfilled, interceptor.rejected); }); var promise; if (!synchronousRequestInterceptors) { var chain = [dispatchRequest, undefined]; Array.prototype.unshift.apply(chain, requestInterceptorChain); chain = chain.concat(responseInterceptorChain); promise = Promise.resolve(config); while (chain.length) { promise = promise.then(chain.shift(), chain.shift()); } return promise; } var newConfig = config; while (requestInterceptorChain.length) { var onFulfilled = requestInterceptorChain.shift(); var onRejected = requestInterceptorChain.shift(); try { newConfig = onFulfilled === null || onFulfilled === void 0 ? void 0 : onFulfilled(newConfig); } catch (error) { onRejected === null || onRejected === void 0 ? void 0 : onRejected(error); break; } } try { promise = dispatchRequest(newConfig); } catch (error) { return Promise.reject(error); } while (responseInterceptorChain.length) { promise = promise.then(responseInterceptorChain.shift(), responseInterceptorChain.shift()); } return promise; }; UinAppClient.prototype.get = function (url, config) { return this.request(__assign({ url: url, method: 'GET' }, config)); }; UinAppClient.prototype.delete = function (url, config) { return this.request(__assign({ url: url, method: 'DELETE' }, config)); }; UinAppClient.prototype.head = function (url, config) { return this.request(__assign({ url: url, method: 'HEAD' }, config)); }; UinAppClient.prototype.options = function (url, config) { return this.request(__assign({ url: url, method: 'OPTIONS' }, config)); }; UinAppClient.prototype.post = function (url, data, config) { return this.request(__assign({ url: url, method: 'POST', data: data }, config)); }; UinAppClient.prototype.put = function (url, data, config) { return this.request(__assign({ url: url, method: 'PUT', data: data }, config)); }; UinAppClient.prototype.patch = function (url, data, config) { throw new Error('Method not implemented.'); }; return UinAppClient; }()); export { UinAppClient };