utils-uniapp 1.1.13 → 1.1.15
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 +65 -24
- package/lib/http/index.js +1 -1
- package/package.json +1 -1
package/lib/http/index.d.ts
CHANGED
|
@@ -22,16 +22,22 @@ export declare const CONTENT_TYPES: Readonly<{
|
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
24
|
* uni请求任务对象,依赖uni‑app全局UniApp命名空间
|
|
25
|
+
* @internal
|
|
25
26
|
*/
|
|
26
27
|
type UniTask = UniApp.RequestTask | UniApp.UploadTask | UniApp.DownloadTask;
|
|
27
28
|
|
|
29
|
+
/** @internal */
|
|
28
30
|
type UniRequestResult = UniApp.RequestSuccessCallbackResult;
|
|
31
|
+
/** @internal */
|
|
29
32
|
type UniUploadResult = UniApp.UploadFileSuccessCallbackResult;
|
|
33
|
+
/** @internal */
|
|
30
34
|
type UniDownloadResult = UniApp.DownloadFileSuccessCallbackResult;
|
|
35
|
+
/** @internal */
|
|
31
36
|
type UniAnyResult = UniRequestResult | UniUploadResult | UniDownloadResult;
|
|
32
37
|
|
|
33
38
|
/**
|
|
34
39
|
* 请求失败载荷【内部类型,不对外导出】
|
|
40
|
+
* @internal
|
|
35
41
|
*/
|
|
36
42
|
type RequestFailPayload = {
|
|
37
43
|
type: 'fail';
|
|
@@ -45,7 +51,7 @@ export interface CacheAdapter {
|
|
|
45
51
|
/**
|
|
46
52
|
* 获取缓存
|
|
47
53
|
* @param key 缓存key
|
|
48
|
-
* @returns 缓存值,不存在返回undefined
|
|
54
|
+
* @returns 缓存值,不存在返回undefined;调用方需要自行做类型断言
|
|
49
55
|
*/
|
|
50
56
|
get(key: string): unknown;
|
|
51
57
|
|
|
@@ -79,17 +85,24 @@ export interface CacheConfigInput {
|
|
|
79
85
|
/**
|
|
80
86
|
* 缓存适配器实例;enable=true开启缓存时必填
|
|
81
87
|
* 支持直接传入实例,或同步获取实例的函数;禁止async异步函数
|
|
88
|
+
* @param ctx 上下文对象
|
|
89
|
+
* @param ctx.url 完整请求url
|
|
90
|
+
* @param ctx.config 组装完成即将传给uni的请求参数(request/upload/download)
|
|
91
|
+
* @param ctx.options normalizeOpts合并后的用户请求扩展配置
|
|
92
|
+
* @param ctx.cacheConfig 当前传入的缓存配置
|
|
82
93
|
*/
|
|
83
94
|
adapter?: CacheAdapter | null | ((ctx: {
|
|
84
95
|
url: string;
|
|
85
|
-
config:
|
|
86
|
-
options:
|
|
96
|
+
config: Partial<UniApp.RequestOptions | UniApp.UploadFileOptions | UniApp.DownloadFileOptions>;
|
|
97
|
+
options: RequestOptions;
|
|
98
|
+
cacheConfig: CacheConfig;
|
|
87
99
|
}) => CacheAdapter | null);
|
|
88
100
|
}
|
|
89
101
|
|
|
90
102
|
/**
|
|
91
103
|
* normalizeOpts 之后内部运行时缓存配置对象【内部类型,不对外导出】
|
|
92
104
|
* @description adapter 字段由内部派生生成,外部禁止传入、禁止读取使用
|
|
105
|
+
* @internal
|
|
93
106
|
*/
|
|
94
107
|
interface CacheConfig {
|
|
95
108
|
enable: boolean;
|
|
@@ -105,24 +118,28 @@ interface CacheConfig {
|
|
|
105
118
|
* @param opts normalizeOpts合并后的完整配置
|
|
106
119
|
* @returns 处理后的请求参数
|
|
107
120
|
*/
|
|
108
|
-
type ReqInterceptor = (
|
|
121
|
+
type ReqInterceptor = (
|
|
122
|
+
invokeArgs: Partial<UniApp.RequestOptions | UniApp.UploadFileOptions | UniApp.DownloadFileOptions>,
|
|
123
|
+
opts: RequestOptions
|
|
124
|
+
) => Promise<Partial<UniApp.RequestOptions | UniApp.UploadFileOptions | UniApp.DownloadFileOptions>> | Partial<UniApp.RequestOptions | UniApp.UploadFileOptions | UniApp.DownloadFileOptions>;
|
|
109
125
|
|
|
110
126
|
/**
|
|
111
127
|
* 响应拦截器【标准化后】
|
|
112
|
-
* @param res uni
|
|
128
|
+
* @param res uni原始响应对象,实际类型为 UniAnyResult
|
|
113
129
|
* @param opts normalizeOpts合并后的完整配置
|
|
114
130
|
* @param reloadFn 重发当前请求函数,可为null
|
|
115
131
|
* @returns 处理后业务数据
|
|
116
132
|
*/
|
|
117
|
-
type ResInterceptor = (res:
|
|
133
|
+
type ResInterceptor = (res: unknown, opts: RequestOptions, reloadFn: (() => Promise<unknown>) | null) => Promise<unknown> | unknown;
|
|
118
134
|
|
|
119
135
|
/**
|
|
120
136
|
* 错误拦截器【标准化后】
|
|
121
137
|
* @param err 捕获到的异常对象,可能为接口回调失败载荷 RequestFailPayload 或 JS运行异常 Error
|
|
122
138
|
* @param opts normalizeOpts合并后的完整配置
|
|
139
|
+
* @param reloadFn 重发当前请求函数,可为null
|
|
123
140
|
* @returns 返回非undefined代表错误已接管;返回undefined会继续抛出异常
|
|
124
141
|
*/
|
|
125
|
-
type ErrInterceptor = (err: RequestFailPayload | Error, opts: RequestOptions) => Promise<
|
|
142
|
+
type ErrInterceptor = (err: RequestFailPayload | Error, opts: RequestOptions, reloadFn: (() => Promise<unknown>) | null) => Promise<unknown> | unknown;
|
|
126
143
|
|
|
127
144
|
/**
|
|
128
145
|
* 用户传入拦截器:支持单个函数 / 函数数组,交由 normalizeInterceptor + composeAsync合并
|
|
@@ -133,6 +150,7 @@ export type UserErrInterceptor = ErrInterceptor | ErrInterceptor[];
|
|
|
133
150
|
|
|
134
151
|
/**
|
|
135
152
|
* normalizeOpts标准化之后的请求额外配置对象【内部运行时】
|
|
153
|
+
* @internal
|
|
136
154
|
*/
|
|
137
155
|
interface RequestOptions {
|
|
138
156
|
/**
|
|
@@ -168,7 +186,7 @@ interface RequestOptions {
|
|
|
168
186
|
*/
|
|
169
187
|
errInterceptor: ErrInterceptor;
|
|
170
188
|
/**
|
|
171
|
-
*
|
|
189
|
+
* 允许扩展自定义字段;尽量避免滥用
|
|
172
190
|
*/
|
|
173
191
|
[key: string]: any;
|
|
174
192
|
}
|
|
@@ -211,7 +229,7 @@ export interface UserRequestOpts {
|
|
|
211
229
|
*/
|
|
212
230
|
errInterceptor?: UserErrInterceptor;
|
|
213
231
|
/**
|
|
214
|
-
*
|
|
232
|
+
* 允许扩展自定义字段;尽量避免滥用
|
|
215
233
|
*/
|
|
216
234
|
[key: string]: any;
|
|
217
235
|
}
|
|
@@ -273,6 +291,7 @@ export declare const CancelToken: {
|
|
|
273
291
|
/**
|
|
274
292
|
* 根据token终止请求
|
|
275
293
|
* @param token 取消令牌
|
|
294
|
+
* @internal 内部方法,业务请勿直接调用
|
|
276
295
|
*/
|
|
277
296
|
_cancel(token: string): void;
|
|
278
297
|
|
|
@@ -292,10 +311,13 @@ export declare const CancelToken: {
|
|
|
292
311
|
* 包装uni异步API为Promise
|
|
293
312
|
* @param type uni接口名称 request | uploadFile | downloadFile
|
|
294
313
|
* @param opts 请求参数,可以传入 cancelToken 用于请求取消
|
|
295
|
-
* @description
|
|
314
|
+
* @description 返回uni原始成功回调对象;catch捕获为 RequestFailPayload | Error
|
|
296
315
|
* @returns uni接口回调结果Promise
|
|
297
316
|
*/
|
|
298
|
-
export declare function promisifyRequest
|
|
317
|
+
export declare function promisifyRequest(
|
|
318
|
+
type: 'request' | 'uploadFile' | 'downloadFile',
|
|
319
|
+
opts: Partial<UniApp.RequestOptions | UniApp.UploadFileOptions | UniApp.DownloadFileOptions>
|
|
320
|
+
): Promise<UniAnyResult>;
|
|
299
321
|
|
|
300
322
|
/**
|
|
301
323
|
* 底层直接展示loading,不做计数
|
|
@@ -319,6 +341,14 @@ export declare function startLoading(title?: string): void;
|
|
|
319
341
|
*/
|
|
320
342
|
export declare function endLoading(): void;
|
|
321
343
|
|
|
344
|
+
/**
|
|
345
|
+
* 请求返回Promise扩展类型,携带取消能力
|
|
346
|
+
*/
|
|
347
|
+
export interface RequestPromise<T> extends Promise<T> {
|
|
348
|
+
cancel(): void;
|
|
349
|
+
token: string;
|
|
350
|
+
}
|
|
351
|
+
|
|
322
352
|
/**
|
|
323
353
|
* createRequest返回的请求实例对象,支持泛型T指定响应类型
|
|
324
354
|
*/
|
|
@@ -327,32 +357,32 @@ export interface RequestInstance {
|
|
|
327
357
|
* 基础请求调用
|
|
328
358
|
* @param url 请求地址
|
|
329
359
|
* @param method http方法 get/post/put/delete等
|
|
330
|
-
* @param params
|
|
360
|
+
* @param params 请求body/query参数,库不做类型校验
|
|
331
361
|
* @param [config] 当前请求配置,覆盖实例defaultConfig(baseURL、headers等)
|
|
332
362
|
* @param [options] 当前请求扩展选项,覆盖实例defaultOpts(loading、缓存、拦截器)
|
|
333
|
-
* @returns Promise
|
|
363
|
+
* @returns Promise业务响应数据,附加cancel、token属性
|
|
334
364
|
*/
|
|
335
|
-
<T = any>(url: string, method: string, params: any, config?: HttpRequestConfig, options?: UserRequestOpts):
|
|
365
|
+
<T = any>(url: string, method: string, params: any, config?: HttpRequestConfig, options?: UserRequestOpts): RequestPromise<T>;
|
|
336
366
|
|
|
337
367
|
/**
|
|
338
368
|
* get快捷方法
|
|
339
369
|
* @param url 请求地址
|
|
340
|
-
* @param params
|
|
370
|
+
* @param params 请求body/query参数,库不做类型校验
|
|
341
371
|
* @param [config] 当前请求配置
|
|
342
372
|
* @param [options] 当前请求扩展选项
|
|
343
|
-
* @returns Promise
|
|
373
|
+
* @returns Promise业务响应数据,附加cancel、token属性
|
|
344
374
|
*/
|
|
345
|
-
get<T = any>(url: string, params: any, config?: HttpRequestConfig, options?: UserRequestOpts):
|
|
375
|
+
get<T = any>(url: string, params: any, config?: HttpRequestConfig, options?: UserRequestOpts): RequestPromise<T>;
|
|
346
376
|
|
|
347
377
|
/**
|
|
348
378
|
* post快捷方法
|
|
349
379
|
* @param url 请求地址
|
|
350
|
-
* @param params
|
|
380
|
+
* @param params 请求body/query参数,库不做类型校验
|
|
351
381
|
* @param [config] 当前请求配置
|
|
352
382
|
* @param [options] 当前请求扩展选项
|
|
353
|
-
* @returns Promise
|
|
383
|
+
* @returns Promise业务响应数据,附加cancel、token属性
|
|
354
384
|
*/
|
|
355
|
-
post<T = any>(url: string, params: any, config?: HttpRequestConfig, options?: UserRequestOpts):
|
|
385
|
+
post<T = any>(url: string, params: any, config?: HttpRequestConfig, options?: UserRequestOpts): RequestPromise<T>;
|
|
356
386
|
}
|
|
357
387
|
|
|
358
388
|
/**
|
|
@@ -366,24 +396,34 @@ export declare function createRequest(defaultConfig: HttpRequestConfig, defaultO
|
|
|
366
396
|
/**
|
|
367
397
|
* 创建上传文件实例
|
|
368
398
|
* @param defaultConfig 实例默认配置(baseURL、headers等)
|
|
369
|
-
* @param defaultOpts 实例默认扩展选项
|
|
399
|
+
* @param defaultOpts 实例默认扩展选项(loading、缓存、拦截器)
|
|
370
400
|
* @returns 上传调用函数
|
|
371
401
|
*/
|
|
372
402
|
export declare function createUploadFile(
|
|
373
403
|
defaultConfig: UploadConfig,
|
|
374
404
|
defaultOpts?: UserRequestOpts
|
|
375
|
-
): <T = any>(
|
|
405
|
+
): <T = any>(
|
|
406
|
+
url: string,
|
|
407
|
+
params: {
|
|
408
|
+
filePath: string;
|
|
409
|
+
/** keyName 映射到 uni uploadFile 的 name 字段 */
|
|
410
|
+
keyName?: string;
|
|
411
|
+
[key: string]: any;
|
|
412
|
+
},
|
|
413
|
+
config?: UploadConfig,
|
|
414
|
+
options?: UserRequestOpts
|
|
415
|
+
) => RequestPromise<T>;
|
|
376
416
|
|
|
377
417
|
/**
|
|
378
418
|
* 创建下载文件实例
|
|
379
419
|
* @param defaultConfig 实例默认配置(baseURL、headers等)
|
|
380
|
-
* @param defaultOpts 实例默认扩展选项
|
|
420
|
+
* @param defaultOpts 实例默认扩展选项(loading、缓存、拦截器)
|
|
381
421
|
* @returns 下载调用函数
|
|
382
422
|
*/
|
|
383
423
|
export declare function createDownloadFile(
|
|
384
424
|
defaultConfig: DownloadConfig,
|
|
385
425
|
defaultOpts?: UserRequestOpts
|
|
386
|
-
): <T = any>(url: string, params?: Record<string, any>, config?: DownloadConfig, options?: UserRequestOpts) =>
|
|
426
|
+
): <T = any>(url: string, params?: Record<string, any>, config?: DownloadConfig, options?: UserRequestOpts) => RequestPromise<T>;
|
|
387
427
|
|
|
388
428
|
export type {
|
|
389
429
|
CacheAdapter,
|
|
@@ -393,6 +433,7 @@ export type {
|
|
|
393
433
|
UserErrInterceptor,
|
|
394
434
|
UserRequestOpts,
|
|
395
435
|
RequestInstance,
|
|
436
|
+
RequestPromise,
|
|
396
437
|
HttpRequestConfig,
|
|
397
438
|
UploadConfig,
|
|
398
439
|
DownloadConfig
|
package/lib/http/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
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"],S=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"}),C=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 A(e,o){return new Promise(function(a,n){var r=o.cancelToken,c=t._objectWithoutProperties(o,M),l=uni[e](t._objectSpread2(t._objectSpread2({},c),{},{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&&C.delete(r)}}));r&&C.add(r,l)})}var T=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]:"加载中...";T++,T===1&&D(e)}function $(){T>0&&T--,T===0&&W()}function N(e,o){return function(a,n,r,c){return e(a,o,n,r,c)}}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=I(o.headers)(),r=I(a.headers)(),c=t._objectSpread2(t._objectSpread2(t._objectSpread2({},o),a),{},{headers:t._objectSpread2(t._objectSpread2({},n),r)}),l=c.headers,s=t._objectWithoutProperties(c,G),i=e.startsWith("http")?e:c.baseURL?"".concat(c.baseURL.replace(/\/+$/,""),"/").concat(e.replace(/^\/+/,"")):e;return{urlRes:i,headers:l,configRes:s}}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,c=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:c,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 q(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(c){return c},resInterceptor:function(c){return c},errInterceptor:function(){}},o),a);return["reqInterceptor","resInterceptor","errInterceptor"].forEach(function(r){var c=[].concat(t._toConsumableArray(P(o[r])),t._toConsumableArray(P(a[r])));n[r]=c.length?U.composeAsync.apply(void 0,t._toConsumableArray(c)):function(l){return l}}),n}function L(e,o){var a=Array.from(o);return function(){return e.apply(void 0,t._toConsumableArray(H.cloneDeepWithDescriptors(a)))}}function F(e,o,a,n,r){return w.apply(this,arguments)}function w(){return w=t._asyncToGenerator(t._regenerator().m(function e(o,a,n,r,c){var l,s,i,f,d,v,h;return t._regenerator().w(function(u){for(;;)switch(u.p=u.n){case 0:return l=null,r.loading&&(z(r.loadingText),l=function(){$(),l=null}),u.p=1,u.n=2,r.reqInterceptor(n,r);case 2:return s=u.v,u.n=3,o(a,s);case 3:return i=u.v,u.n=4,r.resInterceptor(i,r,c);case 4:return f=u.v,u.a(2,f);case 5:return u.p=5,h=u.v,u.n=6,r.errInterceptor(h,r,c);case 6:if(d=u.v,d===void 0){u.n=7;break}return u.a(2,d);case 7:return u.a(2,Promise.reject(h));case 8:return u.p=8,(v=l)===null||v===void 0||v(),u.f(8);case 9:return u.a(2)}},e,null,[[1,5,8,9]])})),w.apply(this,arguments)}function B(e,o){function a(n,r,c,l,s){var i=C.source(),f=L(a,arguments),d=E(n,e,l),v=d.urlRes,h=d.headers,u=d.configRes,g=q("加载中...",o,s),k=r.toUpperCase()==="GET",R=k?S.JSON:g.qs?S.FORM_URLENCODED:S.JSON,m=t._objectSpread2({cancelToken:i.token,method:r,url:v,data:c,header:t._objectSpread2({"Content-Type":R},h)},u),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=F(A,"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,c,l){var s=C.source(),i=L(a,arguments),f=E(n,e,c),d=f.urlRes,v=f.headers,h=f.configRes,u=q("上传中...",o,l),g=t._objectSpread2({keyName:"file"},r),k=g.keyName,R=k===void 0?"file":k,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=F(A,"uploadFile",_,u,i);return y.cancel=s.cancel,y.token=s.token,y}}function X(e,o){return function a(n,r,c,l){var s=C.source(),i=L(a,arguments),f=E(n,e,c),d=f.urlRes,v=f.headers,h=f.configRes,u=q("下载中...",o,l),g=d;if(r&&t._typeof(r)==="object"&&!Array.isArray(r)){var k=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("&");k&&(g+=(g.includes("?")?"&":"?")+k)}var R=t._objectSpread2({cancelToken:s.token,url:g,header:t._objectSpread2({},v)},h),m=F(A,"downloadFile",R,u,i);return m.cancel=s.cancel,m.token=s.token,m}}exports.CONTENT_TYPES=S;exports.CancelToken=C;exports.createDownloadFile=X;exports.createRequest=B;exports.createUploadFile=V;exports.endLoading=$;exports.hideLoading=W;exports.promisifyRequest=A;exports.showLoading=D;exports.startLoading=z;
|