UNPKG

miniprogram-request

Version:
145 lines 6.47 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.Http = void 0; var miniprogram_network_life_cycle_1 = require("miniprogram-network-life-cycle"); var transform_1 = require("./transform"); /** * 小程序HTTP 请求生命周期封装 * @example * `const http = new Http({ baseURL: 'https://api.newfuture.cc/', retry: 3 });` * @template TExt 扩展参数属性类型 */ var Http = /** @class */ (function (_super) { __extends(Http, _super); /** * 新建 Http实列 * @param config 全局默认配置 * @param request 请求处理方法,默认使用请求队列处理 * @param listeners 请求事件监听 */ function Http(config, request, listeners) { return _super.call(this, // tslint:disable-next-line: no-use-before-declare request || wx.request, // tslint:disable-next-line: no-object-literal-type-assertion config || { transformSend: transform_1.transformRequestSendDefault }, listeners) || this; } Http.prototype.request = function () { var argNum = arguments.length; // tslint:disable-next-line: no-unsafe-any var options = argNum === 1 ? arguments[0] : (arguments[3] || {}); if (argNum > 1) { options.method = arguments[0]; options.url = arguments[1]; if (argNum > 2) { // tslint:disable-next-line: no-unsafe-any options.data = arguments[2]; } } return this.process(options); }; /** * GET 操作 * @param action 请求操作URL,支持{name}格式参数 * @param data 可转为query string * @param config 可覆盖默认配置 * @template TReturn Promise 返回的格式类型,默认返回微信原始返回数据格式 * @template TData get query data请求参数的格式类型,默认 any * @template TParams 路径参数(如`/items/{id}`或者`/{0}/{1}`)的格式类型,默认 任意object或数组 */ // tslint:disable-next-line: no-reserved-keywords Http.prototype.get = function (action, data, config) { return this.request('GET', action, data, config); }; /** * POST 操作 * @param action 请求操作URL,支持{name}格式参数 * @param data 操作数据,默认会以json方式上传 * @param config 可覆盖默认配置 * @template TReturn Promise 返回的格式类型,默认返回微信原始返回数据格式 * @template TData post data参数格式类型,默认 any * @template TParams 路径参数(如`/items/{id}`或者`/{0}/{1}`)的格式类型,默认 任意object或数组 */ Http.prototype.post = function (action, data, config) { return this.request('POST', action, data, config); }; /** * PUT 操作 * @param action 请求操作URL,支持{name}格式参数 * @param data 操作数据,默认会以json方式上传 * @param config 可覆盖默认配置 * @template TReturn Promise 返回的格式类型,默认返回微信原始返回数据格式 * @template TData post data数据格式类型,默认 any * @template TParams 路径参数(如`/items/{id}`或者`/{0}/{1}`)的格式类型,默认 任意object或数组 */ Http.prototype.put = function (action, data, config) { return this.request('PUT', action, data, config); }; /** * DELETE 操作 * @param action 请求操作URL,支持{name}格式参数 * @param data 可转为query string * @param config 可覆盖默认配置 * @template TReturn Promise 返回的格式类型,默认返回微信原始返回数据格式 * @template TData put query data参数格式类型,默认 any * @template TParams 路径参数(如`/items/{id}`或者`/{0}/{1}`)的格式类型,默认 任意object或数组 */ // tslint:disable-next-line: no-reserved-keywords Http.prototype.delete = function (action, data, config) { return this.request('DELETE', action, data, config); }; /** * HEAD 操作 * @param action 请求操作URL,支持{name}格式参数 * @param data 可转为query string * @param config 可覆盖默认配置 * @template TReturn Promise 返回的格式类型,默认返回微信原始返回数据格式 * @template TData head query data参数格式类型,默认 any * @template TParams 路径参数(如`/items/{id}`或者`/{0}/{1}`)的格式类型,默认 任意object或数组 */ Http.prototype.head = function (action, data, config) { return this.request('HEAD', action, data, config); }; /** * Patch 操作 * 由于小程序不支持PATCH 方法 * 采用X-HTTP-Method-Override兼容处理,需要服务器端支持 * @param action 请求操作URL,支持{name}格式参数 * @param data 操作数据,默认会以json方式上传 * @param config 可覆盖默认配置 * @template TReturn Promise 返回的格式类型,默认返回微信原始返回数据格式 * @template TData patch data参数格式类型,默认 any * @template TParams 路径参数(如`/items/{id}`或者`/{0}/{1}`)的格式类型,默认 任意object或数组 */ Http.prototype.patch = function (action, data, config) { if (!config) { // tslint:disable-next-line: no-parameter-reassignment config = { headers: { 'X-HTTP-Method-Override': 'PATCH' } }; } else if (!config.headers) { config.headers = { 'X-HTTP-Method-Override': 'PATCH' }; } else { config.headers['X-HTTP-Method-Override'] = 'PATCH'; } return this.request('POST', action, data, config); }; return Http; }(miniprogram_network_life_cycle_1.LifeCycle)); exports.Http = Http; //# sourceMappingURL=http.js.map