utils-uniapp 1.1.12 → 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 +79 -56
- package/lib/http/index.js +1 -1
- package/package.json +1 -1
package/lib/http/index.d.ts
CHANGED
|
@@ -15,23 +15,29 @@ export declare const CONTENT_TYPES: Readonly<{
|
|
|
15
15
|
*/
|
|
16
16
|
FORM_URLENCODED: string;
|
|
17
17
|
/**
|
|
18
|
-
* 文件上传表单 multipart/form
|
|
18
|
+
* 文件上传表单 multipart/form-data
|
|
19
19
|
*/
|
|
20
20
|
FORM_DATA: string;
|
|
21
21
|
}>;
|
|
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
|
|
|
@@ -53,16 +59,17 @@ export interface CacheAdapter {
|
|
|
53
59
|
* 设置缓存
|
|
54
60
|
* @param key 缓存key
|
|
55
61
|
* @param value 缓存存储内容
|
|
56
|
-
* @param expire
|
|
62
|
+
* @param expire 过期秒数;语义由适配器实现;request库仅透传数值,不做过期逻辑
|
|
57
63
|
*/
|
|
58
64
|
set(key: string, value: unknown, expire: number): void;
|
|
59
65
|
}
|
|
60
66
|
|
|
61
67
|
/**
|
|
62
68
|
* 外部传入缓存配置入参(仅调用方使用,业务传入)
|
|
63
|
-
* @description 开启请求缓存 isCache=true 时,必须传入对应 mode 的适配器 localAdapter / sessionAdapter
|
|
64
69
|
*/
|
|
65
70
|
export interface CacheConfigInput {
|
|
71
|
+
/** 是否开启缓存 */
|
|
72
|
+
enable?: boolean;
|
|
66
73
|
/**
|
|
67
74
|
* 自定义缓存业务key;不填默认由url生成,**不包含请求参数**,同URL不同参数务必手动指定key
|
|
68
75
|
*/
|
|
@@ -72,33 +79,35 @@ export interface CacheConfigInput {
|
|
|
72
79
|
*/
|
|
73
80
|
prefix?: string;
|
|
74
81
|
/**
|
|
75
|
-
*
|
|
76
|
-
* - local:持久存储
|
|
77
|
-
* - session:会话存储(H5会话销毁;小程序降级持久)
|
|
78
|
-
*/
|
|
79
|
-
mode?: 'local' | 'session';
|
|
80
|
-
/**
|
|
81
|
-
* 缓存过期时间,单位秒;语义由外部CacheAdapter实现,约定 <=0代表永不过期;库默认兜底1小时(3600s)
|
|
82
|
+
* 缓存过期时间,单位秒;库默认兜底1小时(3600s)
|
|
82
83
|
*/
|
|
83
84
|
expire?: number;
|
|
84
85
|
/**
|
|
85
|
-
*
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
*
|
|
90
|
-
|
|
91
|
-
|
|
86
|
+
* 缓存适配器实例;enable=true开启缓存时必填
|
|
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 当前传入的缓存配置
|
|
93
|
+
*/
|
|
94
|
+
adapter?: CacheAdapter | null | ((ctx: {
|
|
95
|
+
url: string;
|
|
96
|
+
config: Partial<UniApp.RequestOptions | UniApp.UploadFileOptions | UniApp.DownloadFileOptions>;
|
|
97
|
+
options: RequestOptions;
|
|
98
|
+
cacheConfig: CacheConfig;
|
|
99
|
+
}) => CacheAdapter | null);
|
|
92
100
|
}
|
|
93
101
|
|
|
94
102
|
/**
|
|
95
103
|
* normalizeOpts 之后内部运行时缓存配置对象【内部类型,不对外导出】
|
|
96
104
|
* @description adapter 字段由内部派生生成,外部禁止传入、禁止读取使用
|
|
105
|
+
* @internal
|
|
97
106
|
*/
|
|
98
107
|
interface CacheConfig {
|
|
108
|
+
enable: boolean;
|
|
99
109
|
key: string;
|
|
100
110
|
prefix: string;
|
|
101
|
-
mode: 'local' | 'session';
|
|
102
111
|
expire: number;
|
|
103
112
|
readonly adapter: CacheAdapter | null;
|
|
104
113
|
}
|
|
@@ -109,24 +118,28 @@ interface CacheConfig {
|
|
|
109
118
|
* @param opts normalizeOpts合并后的完整配置
|
|
110
119
|
* @returns 处理后的请求参数
|
|
111
120
|
*/
|
|
112
|
-
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>;
|
|
113
125
|
|
|
114
126
|
/**
|
|
115
127
|
* 响应拦截器【标准化后】
|
|
116
|
-
* @param res uni
|
|
128
|
+
* @param res uni原始响应对象,实际类型为 UniAnyResult
|
|
117
129
|
* @param opts normalizeOpts合并后的完整配置
|
|
118
130
|
* @param reloadFn 重发当前请求函数,可为null
|
|
119
131
|
* @returns 处理后业务数据
|
|
120
132
|
*/
|
|
121
|
-
type ResInterceptor = (res:
|
|
133
|
+
type ResInterceptor = (res: unknown, opts: RequestOptions, reloadFn: (() => Promise<unknown>) | null) => Promise<unknown> | unknown;
|
|
122
134
|
|
|
123
135
|
/**
|
|
124
136
|
* 错误拦截器【标准化后】
|
|
125
137
|
* @param err 捕获到的异常对象,可能为接口回调失败载荷 RequestFailPayload 或 JS运行异常 Error
|
|
126
138
|
* @param opts normalizeOpts合并后的完整配置
|
|
139
|
+
* @param reloadFn 重发当前请求函数,可为null
|
|
127
140
|
* @returns 返回非undefined代表错误已接管;返回undefined会继续抛出异常
|
|
128
141
|
*/
|
|
129
|
-
type ErrInterceptor = (err: RequestFailPayload | Error, opts: RequestOptions) => Promise<
|
|
142
|
+
type ErrInterceptor = (err: RequestFailPayload | Error, opts: RequestOptions, reloadFn: (() => Promise<unknown>) | null) => Promise<unknown> | unknown;
|
|
130
143
|
|
|
131
144
|
/**
|
|
132
145
|
* 用户传入拦截器:支持单个函数 / 函数数组,交由 normalizeInterceptor + composeAsync合并
|
|
@@ -137,6 +150,7 @@ export type UserErrInterceptor = ErrInterceptor | ErrInterceptor[];
|
|
|
137
150
|
|
|
138
151
|
/**
|
|
139
152
|
* normalizeOpts标准化之后的请求额外配置对象【内部运行时】
|
|
153
|
+
* @internal
|
|
140
154
|
*/
|
|
141
155
|
interface RequestOptions {
|
|
142
156
|
/**
|
|
@@ -148,11 +162,7 @@ interface RequestOptions {
|
|
|
148
162
|
*/
|
|
149
163
|
showError: boolean;
|
|
150
164
|
/**
|
|
151
|
-
*
|
|
152
|
-
*/
|
|
153
|
-
isCache: boolean;
|
|
154
|
-
/**
|
|
155
|
-
* 缓存配置;adapter为内部运行时派生字段,外部请勿传入、请勿访问;开启isCache:true,必须传入对应模式的适配器,否则运行时抛出异常
|
|
165
|
+
* 缓存配置;adapter为内部运行时派生字段,外部请勿传入、请勿访问;开启缓存必须传入适配器,否则运行时抛出异常
|
|
156
166
|
*/
|
|
157
167
|
cacheConfig: CacheConfig;
|
|
158
168
|
/**
|
|
@@ -176,7 +186,7 @@ interface RequestOptions {
|
|
|
176
186
|
*/
|
|
177
187
|
errInterceptor: ErrInterceptor;
|
|
178
188
|
/**
|
|
179
|
-
*
|
|
189
|
+
* 允许扩展自定义字段;尽量避免滥用
|
|
180
190
|
*/
|
|
181
191
|
[key: string]: any;
|
|
182
192
|
}
|
|
@@ -184,7 +194,6 @@ interface RequestOptions {
|
|
|
184
194
|
/**
|
|
185
195
|
* 业务侧传入配置【外部用户参数,支持拦截器数组】
|
|
186
196
|
* 经过内部 normalizeOpts 之后转换为 RequestOptions
|
|
187
|
-
* @description isCache 设置为 true 时,cacheConfig 需要传入对应 mode 的适配器 localAdapter / sessionAdapter,否则运行时抛出异常
|
|
188
197
|
*/
|
|
189
198
|
export interface UserRequestOpts {
|
|
190
199
|
/**
|
|
@@ -196,11 +205,7 @@ export interface UserRequestOpts {
|
|
|
196
205
|
*/
|
|
197
206
|
showError?: boolean;
|
|
198
207
|
/**
|
|
199
|
-
*
|
|
200
|
-
*/
|
|
201
|
-
isCache?: boolean;
|
|
202
|
-
/**
|
|
203
|
-
* 缓存配置,用户传入部分字段即可;isCache=true必须传入对应mode适配器
|
|
208
|
+
* 缓存配置,用户传入部分字段即可;开启缓存时cacheConfig.adapter必须提供适配器
|
|
204
209
|
*/
|
|
205
210
|
cacheConfig?: Partial<CacheConfigInput>;
|
|
206
211
|
/**
|
|
@@ -224,7 +229,7 @@ export interface UserRequestOpts {
|
|
|
224
229
|
*/
|
|
225
230
|
errInterceptor?: UserErrInterceptor;
|
|
226
231
|
/**
|
|
227
|
-
*
|
|
232
|
+
* 允许扩展自定义字段;尽量避免滥用
|
|
228
233
|
*/
|
|
229
234
|
[key: string]: any;
|
|
230
235
|
}
|
|
@@ -270,11 +275,6 @@ export type DownloadConfig = Partial<Omit<UniApp.DownloadFileOptions, 'url' | 'h
|
|
|
270
275
|
* @description 注意:当前实现token由请求内部source生成,**不支持外部传入自定义cancelToken**
|
|
271
276
|
*/
|
|
272
277
|
export declare const CancelToken: {
|
|
273
|
-
/**
|
|
274
|
-
* 保存token与uni任务映射
|
|
275
|
-
*/
|
|
276
|
-
caches: Record<string, UniTask>;
|
|
277
|
-
|
|
278
278
|
/**
|
|
279
279
|
* 注册请求任务
|
|
280
280
|
* @param token 取消令牌
|
|
@@ -291,6 +291,7 @@ export declare const CancelToken: {
|
|
|
291
291
|
/**
|
|
292
292
|
* 根据token终止请求
|
|
293
293
|
* @param token 取消令牌
|
|
294
|
+
* @internal 内部方法,业务请勿直接调用
|
|
294
295
|
*/
|
|
295
296
|
_cancel(token: string): void;
|
|
296
297
|
|
|
@@ -310,10 +311,13 @@ export declare const CancelToken: {
|
|
|
310
311
|
* 包装uni异步API为Promise
|
|
311
312
|
* @param type uni接口名称 request | uploadFile | downloadFile
|
|
312
313
|
* @param opts 请求参数,可以传入 cancelToken 用于请求取消
|
|
313
|
-
* @description
|
|
314
|
+
* @description 返回uni原始成功回调对象;catch捕获为 RequestFailPayload | Error
|
|
314
315
|
* @returns uni接口回调结果Promise
|
|
315
316
|
*/
|
|
316
|
-
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>;
|
|
317
321
|
|
|
318
322
|
/**
|
|
319
323
|
* 底层直接展示loading,不做计数
|
|
@@ -337,6 +341,14 @@ export declare function startLoading(title?: string): void;
|
|
|
337
341
|
*/
|
|
338
342
|
export declare function endLoading(): void;
|
|
339
343
|
|
|
344
|
+
/**
|
|
345
|
+
* 请求返回Promise扩展类型,携带取消能力
|
|
346
|
+
*/
|
|
347
|
+
export interface RequestPromise<T> extends Promise<T> {
|
|
348
|
+
cancel(): void;
|
|
349
|
+
token: string;
|
|
350
|
+
}
|
|
351
|
+
|
|
340
352
|
/**
|
|
341
353
|
* createRequest返回的请求实例对象,支持泛型T指定响应类型
|
|
342
354
|
*/
|
|
@@ -345,32 +357,32 @@ export interface RequestInstance {
|
|
|
345
357
|
* 基础请求调用
|
|
346
358
|
* @param url 请求地址
|
|
347
359
|
* @param method http方法 get/post/put/delete等
|
|
348
|
-
* @param params
|
|
360
|
+
* @param params 请求body/query参数,库不做类型校验
|
|
349
361
|
* @param [config] 当前请求配置,覆盖实例defaultConfig(baseURL、headers等)
|
|
350
362
|
* @param [options] 当前请求扩展选项,覆盖实例defaultOpts(loading、缓存、拦截器)
|
|
351
|
-
* @returns Promise
|
|
363
|
+
* @returns Promise业务响应数据,附加cancel、token属性
|
|
352
364
|
*/
|
|
353
|
-
<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>;
|
|
354
366
|
|
|
355
367
|
/**
|
|
356
368
|
* get快捷方法
|
|
357
369
|
* @param url 请求地址
|
|
358
|
-
* @param params
|
|
370
|
+
* @param params 请求body/query参数,库不做类型校验
|
|
359
371
|
* @param [config] 当前请求配置
|
|
360
372
|
* @param [options] 当前请求扩展选项
|
|
361
|
-
* @returns Promise
|
|
373
|
+
* @returns Promise业务响应数据,附加cancel、token属性
|
|
362
374
|
*/
|
|
363
|
-
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>;
|
|
364
376
|
|
|
365
377
|
/**
|
|
366
378
|
* post快捷方法
|
|
367
379
|
* @param url 请求地址
|
|
368
|
-
* @param params
|
|
380
|
+
* @param params 请求body/query参数,库不做类型校验
|
|
369
381
|
* @param [config] 当前请求配置
|
|
370
382
|
* @param [options] 当前请求扩展选项
|
|
371
|
-
* @returns Promise
|
|
383
|
+
* @returns Promise业务响应数据,附加cancel、token属性
|
|
372
384
|
*/
|
|
373
|
-
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>;
|
|
374
386
|
}
|
|
375
387
|
|
|
376
388
|
/**
|
|
@@ -384,24 +396,34 @@ export declare function createRequest(defaultConfig: HttpRequestConfig, defaultO
|
|
|
384
396
|
/**
|
|
385
397
|
* 创建上传文件实例
|
|
386
398
|
* @param defaultConfig 实例默认配置(baseURL、headers等)
|
|
387
|
-
* @param defaultOpts 实例默认扩展选项
|
|
399
|
+
* @param defaultOpts 实例默认扩展选项(loading、缓存、拦截器)
|
|
388
400
|
* @returns 上传调用函数
|
|
389
401
|
*/
|
|
390
402
|
export declare function createUploadFile(
|
|
391
403
|
defaultConfig: UploadConfig,
|
|
392
404
|
defaultOpts?: UserRequestOpts
|
|
393
|
-
): <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>;
|
|
394
416
|
|
|
395
417
|
/**
|
|
396
418
|
* 创建下载文件实例
|
|
397
419
|
* @param defaultConfig 实例默认配置(baseURL、headers等)
|
|
398
|
-
* @param defaultOpts 实例默认扩展选项
|
|
420
|
+
* @param defaultOpts 实例默认扩展选项(loading、缓存、拦截器)
|
|
399
421
|
* @returns 下载调用函数
|
|
400
422
|
*/
|
|
401
423
|
export declare function createDownloadFile(
|
|
402
424
|
defaultConfig: DownloadConfig,
|
|
403
425
|
defaultOpts?: UserRequestOpts
|
|
404
|
-
): <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>;
|
|
405
427
|
|
|
406
428
|
export type {
|
|
407
429
|
CacheAdapter,
|
|
@@ -411,6 +433,7 @@ export type {
|
|
|
411
433
|
UserErrInterceptor,
|
|
412
434
|
UserRequestOpts,
|
|
413
435
|
RequestInstance,
|
|
436
|
+
RequestPromise,
|
|
414
437
|
HttpRequestConfig,
|
|
415
438
|
UploadConfig,
|
|
416
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;
|