utils-uniapp 1.1.18 → 1.1.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/http/index.d.ts +27 -26
- package/lib/http/index.js +1 -1
- package/package.json +1 -1
package/lib/http/index.d.ts
CHANGED
|
@@ -45,7 +45,7 @@ type RequestFailPayload = {
|
|
|
45
45
|
};
|
|
46
46
|
|
|
47
47
|
/** 请求配置 */
|
|
48
|
-
type RequestConfig = Partial<UniApp.RequestOptions | UniApp.UploadFileOption | UniApp.DownloadFileOption
|
|
48
|
+
type RequestConfig = Partial<UniApp.RequestOptions | UniApp.UploadFileOption | UniApp.DownloadFileOption>;
|
|
49
49
|
|
|
50
50
|
/**
|
|
51
51
|
* 缓存适配器抽象接口,对接外部缓存实现
|
|
@@ -117,49 +117,50 @@ interface CacheConfig {
|
|
|
117
117
|
|
|
118
118
|
/**
|
|
119
119
|
* 请求前置拦截器【标准化后】
|
|
120
|
-
* @param
|
|
121
|
-
* @param
|
|
122
|
-
* @param payload.opts normalizeOpts合并后的完整配置
|
|
120
|
+
* @param invokeArgs 组装完成即将传给uni的请求参数(原invokeArgs)
|
|
121
|
+
* @param options normalizeOpts合并后的完整配置
|
|
123
122
|
* @returns 处理后的请求参数
|
|
124
123
|
*/
|
|
125
124
|
type ReqInterceptor = (
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
options: RequestOptions;
|
|
129
|
-
}
|
|
125
|
+
invokeArgs: RequestConfig,
|
|
126
|
+
options: RequestOptions
|
|
130
127
|
) => Promise<RequestConfig> | RequestConfig;
|
|
131
128
|
|
|
132
129
|
/**
|
|
133
130
|
* 响应拦截器【标准化后】
|
|
134
|
-
* @param
|
|
135
|
-
* @param payload
|
|
131
|
+
* @param res uni原始响应对象,实际类型为 UniAnyResult
|
|
132
|
+
* @param payload 拦截器载荷对象
|
|
136
133
|
* @param payload.config 组装完成即将传给uni的请求参数
|
|
137
134
|
* @param payload.options normalizeOpts合并后的完整配置
|
|
138
135
|
* @param payload.reloadFn 重发当前请求函数,可为null
|
|
139
136
|
* @returns 处理后业务数据
|
|
140
137
|
*/
|
|
141
|
-
type ResInterceptor = (
|
|
142
|
-
res: unknown
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
138
|
+
type ResInterceptor = (
|
|
139
|
+
res: unknown,
|
|
140
|
+
payload: {
|
|
141
|
+
config: RequestConfig;
|
|
142
|
+
options: RequestOptions;
|
|
143
|
+
reloadFn: (() => Promise<unknown>) | null;
|
|
144
|
+
}
|
|
145
|
+
) => Promise<unknown> | unknown;
|
|
147
146
|
|
|
148
147
|
/**
|
|
149
148
|
* 错误拦截器【标准化后】
|
|
150
|
-
* @param
|
|
151
|
-
* @param payload
|
|
149
|
+
* @param err 捕获到的异常对象,可能为接口回调失败载荷 RequestFailPayload 或 JS运行异常 Error
|
|
150
|
+
* @param payload 拦截器载荷对象
|
|
152
151
|
* @param payload.config 组装完成即将传给uni的请求参数
|
|
153
152
|
* @param payload.options normalizeOpts合并后的完整配置
|
|
154
153
|
* @param payload.reloadFn 重发当前请求函数,可为null
|
|
155
154
|
* @returns 返回非undefined代表错误已接管;返回undefined会继续抛出异常
|
|
156
155
|
*/
|
|
157
|
-
type ErrInterceptor = (
|
|
158
|
-
err: RequestFailPayload | Error
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
156
|
+
type ErrInterceptor = (
|
|
157
|
+
err: RequestFailPayload | Error,
|
|
158
|
+
payload: {
|
|
159
|
+
config: RequestConfig;
|
|
160
|
+
options: RequestOptions;
|
|
161
|
+
reloadFn: (() => Promise<unknown>) | null;
|
|
162
|
+
}
|
|
163
|
+
) => Promise<unknown> | unknown;
|
|
163
164
|
|
|
164
165
|
/**
|
|
165
166
|
* 用户传入拦截器:支持单个函数 / 函数数组,交由 normalizeInterceptor + composeAsync合并
|
|
@@ -274,7 +275,7 @@ export type UploadConfig = Partial<Omit<UniApp.UploadFileOption, 'url' | 'header
|
|
|
274
275
|
/** 库自定义:请求基础地址 */
|
|
275
276
|
baseURL?: string;
|
|
276
277
|
/** 库自定义:headers支持对象或者返回对象的工厂函数 */
|
|
277
|
-
headers?: Record<string, string> | (() => Record<string, string>);
|
|
278
|
+
headers?: Record<string, string> | ((ctx: RequestConfig & { headers?: Record<string, string> }) => Record<string, string>);
|
|
278
279
|
[key: string]: any;
|
|
279
280
|
};
|
|
280
281
|
|
|
@@ -286,7 +287,7 @@ export type DownloadConfig = Partial<Omit<UniApp.DownloadFileOption, 'url' | 'he
|
|
|
286
287
|
/** 库自定义:请求基础地址 */
|
|
287
288
|
baseURL?: string;
|
|
288
289
|
/** 库自定义:headers支持对象或者返回对象的工厂函数 */
|
|
289
|
-
headers?: Record<string, string> | (() => Record<string, string>);
|
|
290
|
+
headers?: Record<string, string> | ((ctx: RequestConfig & { headers?: Record<string, string> }) => Record<string, string>);
|
|
290
291
|
[key: string]: any;
|
|
291
292
|
};
|
|
292
293
|
|
package/lib/http/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("../_rollupPluginBabelHelpers-fa19b5b2.js"),H=require("../object/index.js"),U=require("../magic/index.js");var M=["cancelToken"],G=["headers"],J=["adapter"],x=["keyName","filePath"],T=Object.freeze({JSON:"application/json;charset=UTF-8",TEXT:"text/plain;charset=UTF-8",FORM_URLENCODED:"application/x-www-form-urlencoded;charset=UTF-8",FORM_DATA:"multipart/form-data"}),k=Object.seal(function(){var e=Object.create(null);function o(a){var n=e[a];typeof(n==null?void 0:n.abort)=="function"&&n.abort()}return Object.seal({add:function(n,r){e[n]=r},delete:function(n){Reflect.deleteProperty(e,n)},_cancel:o,cancelAll:function(){Object.values(e).forEach(function(n){typeof(n==null?void 0:n.abort)=="function"&&n.abort()}),Object.keys(e).forEach(function(n){return delete e[n]})},source:function(){var n="".concat(Date.now(),"-").concat(U.getRandomStr(16));return{token:n,cancel:function(){return o(n)}}}})}());function w(e,o){return new Promise(function(a,n){var r=o.cancelToken,u=t._objectWithoutProperties(o,M),l=uni[e](t._objectSpread2(t._objectSpread2({},u),{},{success:function(i){if(i.statusCode>=200&&i.statusCode<300)return a(i);var f=new Error("HTTP status ".concat(i.statusCode));n(Object.assign(f,{type:"fail",res:i}))},fail:function(i){var f=new Error(i.errMsg||"request fail");n(Object.assign(f,{type:"fail",res:i}))},complete:function(){r&&k.delete(r)}}));r&&k.add(r,l)})}var S=0,O=!1;function D(e){O||(O=!0,uni.showLoading({title:e,mask:!0}))}function W(){O&&(O=!1,uni.hideLoading())}function z(){var e=arguments.length>0&&arguments[0]!==void 0?arguments[0]:"加载中...";S++,S===1&&D(e)}function $(){S>0&&S--,S===0&&W()}function N(e,o){return function(a,n,r,u){return e(a,o,n,r,u)}}function I(e){return typeof e=="function"?e:function(){return e||{}}}function E(e){var o=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{},a=arguments.length>2&&arguments[2]!==void 0?arguments[2]:{},n=t._objectSpread2(t._objectSpread2({},o),a);n.headers;var r=t._objectWithoutProperties(n,G),u=I(o.headers)(t._objectSpread2({url:e},r)),l=I(a.headers)(t._objectSpread2(t._objectSpread2({url:e},r),{},{headers:t._objectSpread2({},u)})),s=t._objectSpread2(t._objectSpread2({},u),l),i=e.startsWith("http")?e:r.baseURL?"".concat(r.baseURL.replace(/\/+$/,""),"/").concat(e.replace(/^\/+/,"")):e;return{urlRes:i,headers:s,configRes:r}}function P(e){return typeof e=="function"?[e]:Array.isArray(e)?e.filter(function(o){return typeof o=="function"}):[]}function Y(e){var o=e.url,a=e.invokeArgs,n=a===void 0?{}:a,r=e.options,u=r===void 0?{}:r,l=e.cacheConfig,s=l===void 0?{}:l,i=s.adapter,f=t._objectWithoutProperties(s,J),d=t._objectSpread2({enable:!1,key:"",prefix:"api",expire:60*60,adapter:null},f);if(!d.key){var v=(d.prefix||"api").replace(/:$/,"");d.key="".concat(v,":").concat(o)}if(d.enable&&(typeof i=="function"&&(i=i({url:o,config:n,options:u,cacheConfig:d})),!(i&&typeof i.get=="function"&&typeof i.set=="function")))throw new Error("开启缓存(cacheConfig.enable=true)时,请在cacheConfig传入对应adapter的适配器,适配器必须实现 get(key)、set(key,value,expire) 方法");return Object.freeze(t._objectSpread2(t._objectSpread2({},d),{},{adapter:i}))}function F(e){var o=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{},a=arguments.length>2&&arguments[2]!==void 0?arguments[2]:{},n=t._objectSpread2(t._objectSpread2({qs:!0,showError:!0,cacheConfig:{},loadingText:e,loading:!0,reqInterceptor:function(u){return u},resInterceptor:function(u){return u},errInterceptor:function(){}},o),a);return["reqInterceptor","resInterceptor","errInterceptor"].forEach(function(r){var u=[].concat(t._toConsumableArray(P(o[r])),t._toConsumableArray(P(a[r])));n[r]=u.length?U.composeAsync.apply(void 0,t._toConsumableArray(u)):function(l){return l}}),n}function q(e,o){var a=Array.from(o);return function(){return e.apply(void 0,t._toConsumableArray(H.cloneDeepWithDescriptors(a)))}}function L(e,o,a,n,r){return A.apply(this,arguments)}function A(){return A=t._asyncToGenerator(t._regenerator().m(function e(o,a,n,r,u){var l,s,i,f,d,v,h;return t._regenerator().w(function(c){for(;;)switch(c.p=c.n){case 0:return l=null,r.loading&&(z(r.loadingText),l=function(){$(),l=null}),c.p=1,c.n=2,r.reqInterceptor(
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("../_rollupPluginBabelHelpers-fa19b5b2.js"),H=require("../object/index.js"),U=require("../magic/index.js");var M=["cancelToken"],G=["headers"],J=["adapter"],x=["keyName","filePath"],T=Object.freeze({JSON:"application/json;charset=UTF-8",TEXT:"text/plain;charset=UTF-8",FORM_URLENCODED:"application/x-www-form-urlencoded;charset=UTF-8",FORM_DATA:"multipart/form-data"}),k=Object.seal(function(){var e=Object.create(null);function o(a){var n=e[a];typeof(n==null?void 0:n.abort)=="function"&&n.abort()}return Object.seal({add:function(n,r){e[n]=r},delete:function(n){Reflect.deleteProperty(e,n)},_cancel:o,cancelAll:function(){Object.values(e).forEach(function(n){typeof(n==null?void 0:n.abort)=="function"&&n.abort()}),Object.keys(e).forEach(function(n){return delete e[n]})},source:function(){var n="".concat(Date.now(),"-").concat(U.getRandomStr(16));return{token:n,cancel:function(){return o(n)}}}})}());function w(e,o){return new Promise(function(a,n){var r=o.cancelToken,u=t._objectWithoutProperties(o,M),l=uni[e](t._objectSpread2(t._objectSpread2({},u),{},{success:function(i){if(i.statusCode>=200&&i.statusCode<300)return a(i);var f=new Error("HTTP status ".concat(i.statusCode));n(Object.assign(f,{type:"fail",res:i}))},fail:function(i){var f=new Error(i.errMsg||"request fail");n(Object.assign(f,{type:"fail",res:i}))},complete:function(){r&&k.delete(r)}}));r&&k.add(r,l)})}var S=0,O=!1;function D(e){O||(O=!0,uni.showLoading({title:e,mask:!0}))}function W(){O&&(O=!1,uni.hideLoading())}function z(){var e=arguments.length>0&&arguments[0]!==void 0?arguments[0]:"加载中...";S++,S===1&&D(e)}function $(){S>0&&S--,S===0&&W()}function N(e,o){return function(a,n,r,u){return e(a,o,n,r,u)}}function I(e){return typeof e=="function"?e:function(){return e||{}}}function E(e){var o=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{},a=arguments.length>2&&arguments[2]!==void 0?arguments[2]:{},n=t._objectSpread2(t._objectSpread2({},o),a);n.headers;var r=t._objectWithoutProperties(n,G),u=I(o.headers)(t._objectSpread2({url:e},r)),l=I(a.headers)(t._objectSpread2(t._objectSpread2({url:e},r),{},{headers:t._objectSpread2({},u)})),s=t._objectSpread2(t._objectSpread2({},u),l),i=e.startsWith("http")?e:r.baseURL?"".concat(r.baseURL.replace(/\/+$/,""),"/").concat(e.replace(/^\/+/,"")):e;return{urlRes:i,headers:s,configRes:r}}function P(e){return typeof e=="function"?[e]:Array.isArray(e)?e.filter(function(o){return typeof o=="function"}):[]}function Y(e){var o=e.url,a=e.invokeArgs,n=a===void 0?{}:a,r=e.options,u=r===void 0?{}:r,l=e.cacheConfig,s=l===void 0?{}:l,i=s.adapter,f=t._objectWithoutProperties(s,J),d=t._objectSpread2({enable:!1,key:"",prefix:"api",expire:60*60,adapter:null},f);if(!d.key){var v=(d.prefix||"api").replace(/:$/,"");d.key="".concat(v,":").concat(o)}if(d.enable&&(typeof i=="function"&&(i=i({url:o,config:n,options:u,cacheConfig:d})),!(i&&typeof i.get=="function"&&typeof i.set=="function")))throw new Error("开启缓存(cacheConfig.enable=true)时,请在cacheConfig传入对应adapter的适配器,适配器必须实现 get(key)、set(key,value,expire) 方法");return Object.freeze(t._objectSpread2(t._objectSpread2({},d),{},{adapter:i}))}function F(e){var o=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{},a=arguments.length>2&&arguments[2]!==void 0?arguments[2]:{},n=t._objectSpread2(t._objectSpread2({qs:!0,showError:!0,cacheConfig:{},loadingText:e,loading:!0,reqInterceptor:function(u){return u},resInterceptor:function(u){return u},errInterceptor:function(){}},o),a);return["reqInterceptor","resInterceptor","errInterceptor"].forEach(function(r){var u=[].concat(t._toConsumableArray(P(o[r])),t._toConsumableArray(P(a[r])));n[r]=u.length?U.composeAsync.apply(void 0,t._toConsumableArray(u)):function(l){return l}}),n}function q(e,o){var a=Array.from(o);return function(){return e.apply(void 0,t._toConsumableArray(H.cloneDeepWithDescriptors(a)))}}function L(e,o,a,n,r){return A.apply(this,arguments)}function A(){return A=t._asyncToGenerator(t._regenerator().m(function e(o,a,n,r,u){var l,s,i,f,d,v,h;return t._regenerator().w(function(c){for(;;)switch(c.p=c.n){case 0:return l=null,r.loading&&(z(r.loadingText),l=function(){$(),l=null}),c.p=1,c.n=2,r.reqInterceptor(n,r);case 2:return s=c.v,c.n=3,o(a,s);case 3:return i=c.v,c.n=4,r.resInterceptor(i,{config:n,options:r,reloadFn:u});case 4:return f=c.v,c.a(2,f);case 5:return c.p=5,h=c.v,c.n=6,r.errInterceptor(h,{config:n,options:r,reloadFn:u});case 6:if(d=c.v,d===void 0){c.n=7;break}return c.a(2,d);case 7:return c.a(2,Promise.reject(h));case 8:return c.p=8,(v=l)===null||v===void 0||v(),c.f(8);case 9:return c.a(2)}},e,null,[[1,5,8,9]])})),A.apply(this,arguments)}function B(e,o){function a(n,r,u,l,s){var i=k.source(),f=q(a,arguments),d=E(n,e,l),v=d.urlRes,h=d.headers,c=d.configRes,g=F("加载中...",o,s),C=r.toUpperCase()==="GET",R=C?T.JSON:g.qs?T.FORM_URLENCODED:T.JSON,m=t._objectSpread2({cancelToken:i.token,method:r,url:v,data:u,header:t._objectSpread2({"Content-Type":R},h)},c),p=Y({url:n,invokeArgs:m,options:g,cacheConfig:t._objectSpread2(t._objectSpread2({},o.cacheConfig),s==null?void 0:s.cacheConfig)}),b=p.enable;if(b){var _=p.adapter.get(p.key);if(_)return Promise.resolve(_)}var y=L(w,"request",m,t._objectSpread2(t._objectSpread2({},g),{},{cacheConfig:p}),f).then(function(j){return b&&p.adapter.set(p.key,j,p.expire),j});return y.cancel=i.cancel,y.token=i.token,y}return a.get=N(a,"get"),a.post=N(a,"post"),a}function V(e,o){return function a(n,r,u,l){var s=k.source(),i=q(a,arguments),f=E(n,e,u),d=f.urlRes,v=f.headers,h=f.configRes,c=F("上传中...",o,l),g=t._objectSpread2({keyName:"file"},r),C=g.keyName,R=C===void 0?"file":C,m=g.filePath,p=t._objectWithoutProperties(g,x);function b(j){return j?!!(j.startsWith("wxfile://tmp")||j.startsWith("http://tmp")||j.startsWith("http://temp")||j.startsWith("blob:")||j.startsWith("_doc/")):!1}if(!b(m))return Promise.resolve({data:m});var _=t._objectSpread2({cancelToken:s.token,url:d,filePath:m,name:R,formData:p,header:t._objectSpread2({},v)},h),y=L(w,"uploadFile",_,c,i);return y.cancel=s.cancel,y.token=s.token,y}}function X(e,o){return function a(n,r,u,l){var s=k.source(),i=q(a,arguments),f=E(n,e,u),d=f.urlRes,v=f.headers,h=f.configRes,c=F("下载中...",o,l),g=d;if(r&&t._typeof(r)==="object"&&!Array.isArray(r)){var C=Object.entries(r).filter(function(p){var b=t._slicedToArray(p,2),_=b[1];return _!=null&&_!==""}).map(function(p){var b=t._slicedToArray(p,2),_=b[0],y=b[1];return"".concat(encodeURIComponent(_),"=").concat(encodeURIComponent(y))}).join("&");C&&(g+=(g.includes("?")?"&":"?")+C)}var R=t._objectSpread2({cancelToken:s.token,url:g,header:t._objectSpread2({},v)},h),m=L(w,"downloadFile",R,c,i);return m.cancel=s.cancel,m.token=s.token,m}}exports.CONTENT_TYPES=T;exports.CancelToken=k;exports.createDownloadFile=X;exports.createRequest=B;exports.createUploadFile=V;exports.endLoading=$;exports.hideLoading=W;exports.promisifyRequest=w;exports.showLoading=D;exports.startLoading=z;
|