utils-uniapp 1.1.16 → 1.1.18
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 +15 -12
- package/lib/http/index.js +1 -1
- package/package.json +1 -1
package/lib/http/index.d.ts
CHANGED
|
@@ -44,6 +44,9 @@ type RequestFailPayload = {
|
|
|
44
44
|
res: UniApp.GeneralCallbackResult;
|
|
45
45
|
};
|
|
46
46
|
|
|
47
|
+
/** 请求配置 */
|
|
48
|
+
type RequestConfig = Partial<UniApp.RequestOptions | UniApp.UploadFileOption | UniApp.DownloadFileOption>
|
|
49
|
+
|
|
47
50
|
/**
|
|
48
51
|
* 缓存适配器抽象接口,对接外部缓存实现
|
|
49
52
|
*/
|
|
@@ -93,7 +96,7 @@ export interface CacheConfigInput {
|
|
|
93
96
|
*/
|
|
94
97
|
adapter?: CacheAdapter | null | ((ctx: {
|
|
95
98
|
url: string;
|
|
96
|
-
config:
|
|
99
|
+
config: RequestConfig;
|
|
97
100
|
options: RequestOptions;
|
|
98
101
|
cacheConfig: CacheConfig;
|
|
99
102
|
}) => CacheAdapter | null);
|
|
@@ -105,10 +108,10 @@ export interface CacheConfigInput {
|
|
|
105
108
|
* @internal
|
|
106
109
|
*/
|
|
107
110
|
interface CacheConfig {
|
|
108
|
-
enable: boolean;
|
|
109
|
-
key: string;
|
|
110
|
-
prefix: string;
|
|
111
|
-
expire: number;
|
|
111
|
+
readonly enable: boolean;
|
|
112
|
+
readonly key: string;
|
|
113
|
+
readonly prefix: string;
|
|
114
|
+
readonly expire: number;
|
|
112
115
|
readonly adapter: CacheAdapter | null;
|
|
113
116
|
}
|
|
114
117
|
|
|
@@ -121,10 +124,10 @@ interface CacheConfig {
|
|
|
121
124
|
*/
|
|
122
125
|
type ReqInterceptor = (
|
|
123
126
|
payload: {
|
|
124
|
-
config:
|
|
125
|
-
|
|
127
|
+
config: RequestConfig;
|
|
128
|
+
options: RequestOptions;
|
|
126
129
|
}
|
|
127
|
-
) => Promise<
|
|
130
|
+
) => Promise<RequestConfig> | RequestConfig;
|
|
128
131
|
|
|
129
132
|
/**
|
|
130
133
|
* 响应拦截器【标准化后】
|
|
@@ -137,7 +140,7 @@ type ReqInterceptor = (
|
|
|
137
140
|
*/
|
|
138
141
|
type ResInterceptor = (payload: {
|
|
139
142
|
res: unknown;
|
|
140
|
-
config:
|
|
143
|
+
config: RequestConfig;
|
|
141
144
|
options: RequestOptions;
|
|
142
145
|
reloadFn: (() => Promise<unknown>) | null;
|
|
143
146
|
}) => Promise<unknown> | unknown;
|
|
@@ -153,7 +156,7 @@ type ResInterceptor = (payload: {
|
|
|
153
156
|
*/
|
|
154
157
|
type ErrInterceptor = (payload: {
|
|
155
158
|
err: RequestFailPayload | Error;
|
|
156
|
-
config:
|
|
159
|
+
config: RequestConfig;
|
|
157
160
|
options: RequestOptions;
|
|
158
161
|
reloadFn: (() => Promise<unknown>) | null;
|
|
159
162
|
}) => Promise<unknown> | unknown;
|
|
@@ -259,7 +262,7 @@ export type HttpRequestConfig = Partial<Omit<UniApp.RequestOptions, 'url' | 'hea
|
|
|
259
262
|
/** 库自定义:请求基础地址,非uni.request原生参数 */
|
|
260
263
|
baseURL?: string;
|
|
261
264
|
/** 库自定义:headers支持对象或者返回对象的工厂函数 */
|
|
262
|
-
headers?: Record<string, string> | (() => Record<string, string>);
|
|
265
|
+
headers?: Record<string, string> | ((ctx: RequestConfig & { headers?: Record<string, string> }) => Record<string, string>);
|
|
263
266
|
[key: string]: any;
|
|
264
267
|
};
|
|
265
268
|
|
|
@@ -333,7 +336,7 @@ export declare const CancelToken: {
|
|
|
333
336
|
*/
|
|
334
337
|
export declare function promisifyRequest(
|
|
335
338
|
type: 'request' | 'uploadFile' | 'downloadFile',
|
|
336
|
-
opts:
|
|
339
|
+
opts: RequestConfig
|
|
337
340
|
): Promise<UniAnyResult>;
|
|
338
341
|
|
|
339
342
|
/**
|
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"],
|
|
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({config:n,options:r});case 2:return s=c.v,c.n=3,o(a,s);case 3:return i=c.v,c.n=4,r.resInterceptor({res: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({err: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;
|