UNPKG

@ace-fetch/uni-app

Version:

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

116 lines (115 loc) 4.84 kB
"use strict"; 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); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.UinAppClient = void 0; var dispatchRequest_1 = require("./dispatchRequest"); var interceptorManager_1 = require("./interceptorManager"); var mergeConfig_1 = require("./mergeConfig"); var UinAppClient = /** @class */ (function () { function UinAppClient(defaults) { this.defaults = defaults; this.interceptors = { request: new interceptorManager_1.InterceptorManager(), response: new interceptorManager_1.InterceptorManager(), }; } UinAppClient.prototype.request = function (configOrUrl, config) { if (typeof configOrUrl === 'string') { config = config || {}; config.url = configOrUrl; } else { config = configOrUrl || {}; } config = (0, mergeConfig_1.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_1.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 = (0, dispatchRequest_1.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; }()); exports.UinAppClient = UinAppClient;