utils-uniapp 1.1.9 → 1.1.10

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.
@@ -1,19 +1,28 @@
1
1
  /**
2
- * 依赖说明:需要安装 @types/uni‑app,提供 UniApp.* 命名空间类型
3
2
  * 请求Content‑Type常量
4
3
  */
5
4
  export declare const CONTENT_TYPES: Readonly<{
6
- /** json格式 application/json;charset=UTF-8 */
5
+ /**
6
+ * json格式 application/json;charset=UTF-8
7
+ */
7
8
  JSON: string;
8
- /** 纯文本 text/plain;charset=UTF-8 */
9
+ /**
10
+ * 纯文本 text/plain;charset=UTF-8
11
+ */
9
12
  TEXT: string;
10
- /** url编码表单 application/x-www-form-urlencoded;charset=UTF-8 */
13
+ /**
14
+ * url编码表单 application/x-www-form-urlencoded;charset=UTF-8
15
+ */
11
16
  FORM_URLENCODED: string;
12
- /** 文件上传表单 multipart/form‑data */
17
+ /**
18
+ * 文件上传表单 multipart/form‑data
19
+ */
13
20
  FORM_DATA: string;
14
21
  }>;
15
22
 
16
- /** uni请求任务对象,依赖uni‑app全局UniApp命名空间 */
23
+ /**
24
+ * uni请求任务对象,依赖uni‑app全局UniApp命名空间
25
+ */
17
26
  type UniTask = UniApp.RequestTask | UniApp.UploadTask | UniApp.DownloadTask;
18
27
 
19
28
  type UniRequestResult = UniApp.RequestSuccessCallbackResult;
@@ -22,15 +31,15 @@ type UniDownloadResult = UniApp.DownloadFileSuccessCallbackResult;
22
31
  type UniAnyResult = UniRequestResult | UniUploadResult | UniDownloadResult;
23
32
 
24
33
  /**
25
- * 请求失败载荷
34
+ * 请求失败载荷【内部类型,不对外导出】
26
35
  */
27
- export type RequestFailPayload = {
36
+ type RequestFailPayload = {
28
37
  type: 'fail';
29
38
  res: UniApp.GeneralCallbackResult;
30
39
  };
31
40
 
32
41
  /**
33
- * 缓存适配器抽象接口,对接外部缓存实现(如前面StorageCache)
42
+ * 缓存适配器抽象接口,对接外部缓存实现
34
43
  */
35
44
  export interface CacheAdapter {
36
45
  /**
@@ -39,11 +48,12 @@ export interface CacheAdapter {
39
48
  * @returns 缓存值,不存在返回undefined
40
49
  */
41
50
  get(key: string): unknown;
51
+
42
52
  /**
43
53
  * 设置缓存
44
54
  * @param key 缓存key
45
55
  * @param value 缓存存储内容
46
- * @param expire 过期秒数,<=0永不过期
56
+ * @param expire 过期秒数;语义由适配器实现,约定 <=0 代表永不过期;request库仅透传数值,不做过期逻辑
47
57
  */
48
58
  set(key: string, value: unknown, expire: number): void;
49
59
  }
@@ -68,7 +78,7 @@ export interface CacheConfigInput {
68
78
  */
69
79
  mode?: 'local' | 'session';
70
80
  /**
71
- * 缓存过期时间,单位秒;<=0代表永不过期,默认1小时过期
81
+ * 缓存过期时间,单位秒;语义由外部CacheAdapter实现,约定 <=0代表永不过期;库默认兜底1小时(3600s)
72
82
  */
73
83
  expire?: number;
74
84
  /**
@@ -82,32 +92,15 @@ export interface CacheConfigInput {
82
92
  }
83
93
 
84
94
  /**
85
- * normalizeOpts 之后内部运行时缓存配置对象
95
+ * normalizeOpts 之后内部运行时缓存配置对象【内部类型,不对外导出】
86
96
  * @description adapter 字段由内部派生生成,外部禁止传入、禁止读取使用
87
97
  */
88
- export interface CacheConfig {
89
- /**
90
- * 缓存key;默认仅由url生成,**不包含请求参数**,同URL不同参数需手动指定key
91
- */
98
+ interface CacheConfig {
92
99
  key: string;
93
- /**
94
- * 缓存key前缀
95
- */
96
100
  prefix: string;
97
- /**
98
- * 缓存模式
99
- */
100
101
  mode: 'local' | 'session';
101
- /**
102
- * 过期时间,单位秒;<=0代表永不过期,默认1小时过期
103
- */
104
102
  expire: number;
105
- /**
106
- * ⚠️ adapter 为内部运行时派生字段,**外部禁止传入、禁止访问**;
107
- * 开启 isCache:true,必须传入 CacheConfigInput 对应模式的适配器,否则运行时抛出异常
108
- */
109
- adapter: CacheAdapter | null;
110
- [key: string]: unknown;
103
+ readonly adapter: CacheAdapter | null;
111
104
  }
112
105
 
113
106
  /**
@@ -116,7 +109,7 @@ export interface CacheConfig {
116
109
  * @param opts normalizeOpts合并后的完整配置
117
110
  * @returns 处理后的请求参数
118
111
  */
119
- export type ReqInterceptor = (invokeArgs: Record<string, unknown>, opts: RequestOptions) => Promise<Record<string, unknown>> | Record<string, unknown>;
112
+ type ReqInterceptor = (invokeArgs: Record<string, any>, opts: RequestOptions) => Promise<Record<string, any>> | Record<string, any>;
120
113
 
121
114
  /**
122
115
  * 响应拦截器【标准化后】
@@ -125,15 +118,15 @@ export type ReqInterceptor = (invokeArgs: Record<string, unknown>, opts: Request
125
118
  * @param reloadFn 重发当前请求函数,可为null
126
119
  * @returns 处理后业务数据
127
120
  */
128
- export type ResInterceptor = (res: unknown, opts: RequestOptions, reloadFn: (() => Promise<unknown>) | null) => Promise<unknown> | unknown;
121
+ type ResInterceptor = (res: any, opts: RequestOptions, reloadFn: (() => Promise<any>) | null) => Promise<any> | any;
129
122
 
130
123
  /**
131
124
  * 错误拦截器【标准化后】
132
- * @param err 捕获到的异常对象
125
+ * @param err 捕获到的异常对象,可能为接口回调失败载荷 RequestFailPayload 或 JS运行异常 Error
133
126
  * @param opts normalizeOpts合并后的完整配置
134
127
  * @returns 返回非undefined代表错误已接管;返回undefined会继续抛出异常
135
128
  */
136
- export type ErrInterceptor = (err: RequestFailPayload | Error, opts: RequestOptions) => Promise<unknown> | unknown;
129
+ type ErrInterceptor = (err: RequestFailPayload | Error, opts: RequestOptions) => Promise<any> | any;
137
130
 
138
131
  /**
139
132
  * 用户传入拦截器:支持单个函数 / 函数数组,交由 normalizeInterceptor + composeAsync合并
@@ -145,27 +138,47 @@ export type UserErrInterceptor = ErrInterceptor | ErrInterceptor[];
145
138
  /**
146
139
  * normalizeOpts标准化之后的请求额外配置对象【内部运行时】
147
140
  */
148
- export interface RequestOptions {
149
- /** 是否使用url‑encoded表单编码 */
141
+ interface RequestOptions {
142
+ /**
143
+ * 是否使用url‑encoded表单编码
144
+ */
150
145
  qs: boolean;
151
- /** 是否展示错误toast提示 */
146
+ /**
147
+ * 是否展示错误toast提示
148
+ */
152
149
  showError: boolean;
153
- /** 是否开启请求缓存;开启时cacheConfig必须提供对应mode的适配器 */
150
+ /**
151
+ * 是否开启请求缓存;开启时cacheConfig必须提供对应mode的适配器
152
+ */
154
153
  isCache: boolean;
155
- /** 缓存配置;adapter为内部运行时派生字段,外部请勿传入、请勿访问;开启isCache:true,必须传入对应模式的适配器,否则运行时抛出异常 */
154
+ /**
155
+ * 缓存配置;adapter为内部运行时派生字段,外部请勿传入、请勿访问;开启isCache:true,必须传入对应模式的适配器,否则运行时抛出异常
156
+ */
156
157
  cacheConfig: CacheConfig;
157
- /** loading提示文字 */
158
+ /**
159
+ * loading提示文字
160
+ */
158
161
  loadingText: string;
159
- /** 是否开启全局loading弹窗 */
162
+ /**
163
+ * 是否开启全局loading弹窗
164
+ */
160
165
  loading: boolean;
161
- /** 请求前置拦截器(已合并为单个函数) */
166
+ /**
167
+ * 请求前置拦截器(已合并为单个函数)
168
+ */
162
169
  reqInterceptor: ReqInterceptor;
163
- /** 响应拦截器(已合并为单个函数) */
170
+ /**
171
+ * 响应拦截器(已合并为单个函数)
172
+ */
164
173
  resInterceptor: ResInterceptor;
165
- /** 错误拦截器(已合并为单个函数) */
174
+ /**
175
+ * 错误拦截器(已合并为单个函数)
176
+ */
166
177
  errInterceptor: ErrInterceptor;
167
- /** 允许扩展自定义字段 */
168
- [key: string]: unknown;
178
+ /**
179
+ * 允许扩展自定义字段
180
+ */
181
+ [key: string]: any;
169
182
  }
170
183
 
171
184
  /**
@@ -174,34 +187,71 @@ export interface RequestOptions {
174
187
  * @description isCache 设置为 true 时,cacheConfig 需要传入对应 mode 的适配器 localAdapter / sessionAdapter,否则运行时抛出异常
175
188
  */
176
189
  export interface UserRequestOpts {
177
- /** 是否使用url‑encoded表单编码 */
190
+ /**
191
+ * 是否使用url‑encoded表单编码
192
+ */
178
193
  qs?: boolean;
179
- /** 是否展示错误toast提示 */
194
+ /**
195
+ * 是否展示错误toast提示
196
+ */
180
197
  showError?: boolean;
181
- /** 是否开启请求缓存;开启时cacheConfig必须提供对应mode的适配器(localAdapter/sessionAdapter) */
198
+ /**
199
+ * 是否开启请求缓存;开启时cacheConfig必须提供对应mode的适配器(localAdapter/sessionAdapter)
200
+ */
182
201
  isCache?: boolean;
183
- /** 缓存配置,用户传入部分字段即可;isCache=true必须传入对应mode适配器 */
202
+ /**
203
+ * 缓存配置,用户传入部分字段即可;isCache=true必须传入对应mode适配器
204
+ */
184
205
  cacheConfig?: Partial<CacheConfigInput>;
185
- /** loading提示文字 */
206
+ /**
207
+ * loading提示文字
208
+ */
186
209
  loadingText?: string;
187
- /** 是否开启全局loading弹窗 */
210
+ /**
211
+ * 是否开启全局loading弹窗
212
+ */
188
213
  loading?: boolean;
189
- /** 请求前置拦截器,支持单个函数或函数数组 */
214
+ /**
215
+ * 请求前置拦截器,支持单个函数或函数数组
216
+ */
190
217
  reqInterceptor?: UserReqInterceptor;
191
- /** 响应拦截器,支持单个函数或函数数组 */
218
+ /**
219
+ * 响应拦截器,支持单个函数或函数数组
220
+ */
192
221
  resInterceptor?: UserResInterceptor;
193
- /** 错误拦截器,支持单个函数或函数数组 */
222
+ /**
223
+ * 错误拦截器,支持单个函数或函数数组
224
+ */
194
225
  errInterceptor?: UserErrInterceptor;
195
- /** 允许扩展自定义字段 */
196
- [key: string]: unknown;
226
+ /**
227
+ * 允许扩展自定义字段
228
+ */
229
+ [key: string]: any;
197
230
  }
198
231
 
232
+ /**
233
+ * 请求config:uni.request配置,排除内部强制覆盖字段,允许业务扩展
234
+ */
235
+ export type HttpRequestConfig = Omit<UniApp.RequestOptions, 'url'|'header'|'method'|'data'|'cancelToken'> & {[key:string]:any};
236
+
237
+ /**
238
+ * 上传config:uni.uploadFile配置,排除内部强制覆盖字段
239
+ */
240
+ export type UploadConfig = Omit<UniApp.UploadFileOptions, 'url'|'header'|'filePath'|'name'|'formData'|'cancelToken'> & {[key:string]:any};
241
+
242
+ /**
243
+ * 下载config:uni.downloadFile配置,排除内部强制覆盖字段
244
+ */
245
+ export type DownloadConfig = Omit<UniApp.DownloadFileOptions, 'url'|'header'|'cancelToken'> & {[key:string]:any};
246
+
199
247
  /**
200
248
  * 请求取消控制器(仿axios CancelToken)
201
- * 注意:当前实现token由请求内部source生成,**不支持外部传入自定义cancelToken**
249
+ * @description 注意:当前实现token由请求内部source生成,**不支持外部传入自定义cancelToken**
202
250
  */
203
251
  export declare const CancelToken: {
204
- /** 保存token与uni任务映射 */
252
+ /**
253
+ * 保存token与uni任务映射
254
+ */
205
255
  caches: Record<string, UniTask>;
206
256
 
207
257
  /**
@@ -239,10 +289,10 @@ export declare const CancelToken: {
239
289
  * 包装uni异步API为Promise
240
290
  * @param type uni接口名称 request | uploadFile | downloadFile
241
291
  * @param opts 请求参数,可以传入 cancelToken 用于请求取消
242
- * @description 泛型仅为类型断言,运行时不会做类型转换
292
+ * @description TS 无法表达 Promise reject 类型;本泛型T仅代表resolve成功返回类型;catch捕获为 RequestFailPayload | Error
243
293
  * @returns uni接口回调结果Promise
244
294
  */
245
- export declare function promisifyRequest<T = UniAnyResult>(type: 'request' | 'uploadFile' | 'downloadFile', opts: Record<string, unknown>): Promise<T>;
295
+ export declare function promisifyRequest<T = UniAnyResult>(type: 'request' | 'uploadFile' | 'downloadFile', opts: Record<string, any>): Promise<T>;
246
296
 
247
297
  /**
248
298
  * 底层直接展示loading,不做计数
@@ -267,7 +317,7 @@ export declare function startLoading(title?: string): void;
267
317
  export declare function endLoading(): void;
268
318
 
269
319
  /**
270
- * createRequest返回的请求实例对象
320
+ * createRequest返回的请求实例对象,支持泛型T指定响应类型
271
321
  */
272
322
  export interface RequestInstance {
273
323
  /**
@@ -279,7 +329,7 @@ export interface RequestInstance {
279
329
  * @param [options] 当前请求扩展选项,覆盖实例defaultOpts(loading、缓存、拦截器)
280
330
  * @returns Promise业务响应数据
281
331
  */
282
- (url: string, method: string, params?: unknown, config?: Record<string, unknown>, options?: UserRequestOpts): Promise<unknown>;
332
+ <T = any>(url: string, method: string, params: any, config?: HttpRequestConfig, options?: UserRequestOpts): Promise<T>;
283
333
 
284
334
  /**
285
335
  * get快捷方法
@@ -287,8 +337,9 @@ export interface RequestInstance {
287
337
  * @param params 请求参数
288
338
  * @param [config] 当前请求配置
289
339
  * @param [options] 当前请求扩展选项
340
+ * @returns Promise业务响应数据
290
341
  */
291
- get(url: string, params?: unknown, config?: Record<string, unknown>, options?: UserRequestOpts): Promise<unknown>;
342
+ get<T = any>(url: string, params: any, config?: HttpRequestConfig, options?: UserRequestOpts): Promise<T>;
292
343
 
293
344
  /**
294
345
  * post快捷方法
@@ -296,8 +347,9 @@ export interface RequestInstance {
296
347
  * @param params 请求参数
297
348
  * @param [config] 当前请求配置
298
349
  * @param [options] 当前请求扩展选项
350
+ * @returns Promise业务响应数据
299
351
  */
300
- post(url: string, params?: unknown, config?: Record<string, unknown>, options?: UserRequestOpts): Promise<unknown>;
352
+ post<T = any>(url: string, params: any, config?: HttpRequestConfig, options?: UserRequestOpts): Promise<T>;
301
353
  }
302
354
 
303
355
  /**
@@ -306,7 +358,7 @@ export interface RequestInstance {
306
358
  * @param defaultOpts 实例默认扩展选项(loading、缓存、拦截器)
307
359
  * @returns RequestInstance 请求实例
308
360
  */
309
- export declare function createRequest(defaultConfig: Record<string, unknown>, defaultOpts?: UserRequestOpts): RequestInstance;
361
+ export declare function createRequest(defaultConfig: HttpRequestConfig, defaultOpts?: UserRequestOpts): RequestInstance;
310
362
 
311
363
  /**
312
364
  * 创建上传文件实例
@@ -315,9 +367,14 @@ export declare function createRequest(defaultConfig: Record<string, unknown>, de
315
367
  * @returns 上传调用函数
316
368
  */
317
369
  export declare function createUploadFile(
318
- defaultConfig: Record<string, unknown>,
370
+ defaultConfig: UploadConfig,
319
371
  defaultOpts?: UserRequestOpts
320
- ): (url: string, params: { filePath: string; keyName?: string; [key: string]: unknown }, config?: Record<string, unknown>, options?: UserRequestOpts) => Promise<unknown>;
372
+ ): <T = any>(
373
+ url: string,
374
+ params: { filePath: string; keyName?: string; [key: string]: any },
375
+ config?: UploadConfig,
376
+ options?: UserRequestOpts
377
+ ) => Promise<T>;
321
378
 
322
379
  /**
323
380
  * 创建下载文件实例
@@ -326,23 +383,24 @@ export declare function createUploadFile(
326
383
  * @returns 下载调用函数
327
384
  */
328
385
  export declare function createDownloadFile(
329
- defaultConfig: Record<string, unknown>,
386
+ defaultConfig: DownloadConfig,
330
387
  defaultOpts?: UserRequestOpts
331
- ): (url: string, params?: Record<string, unknown>, config?: Record<string, unknown>, options?: UserRequestOpts) => Promise<unknown>;
388
+ ): <T = any>(
389
+ url: string,
390
+ params?: Record<string, any>,
391
+ config?: DownloadConfig,
392
+ options?: UserRequestOpts
393
+ ) => Promise<T>;
332
394
 
333
- // 导出全部业务可用类型
334
395
  export type {
335
- ReqInterceptor,
336
- ResInterceptor,
337
- ErrInterceptor,
396
+ CacheAdapter,
397
+ CacheConfigInput,
338
398
  UserReqInterceptor,
339
399
  UserResInterceptor,
340
400
  UserErrInterceptor,
341
- CacheAdapter,
342
- CacheConfigInput,
343
- CacheConfig,
344
- RequestFailPayload,
345
- RequestOptions,
346
401
  UserRequestOpts,
347
- RequestInstance
402
+ RequestInstance,
403
+ HttpRequestConfig,
404
+ UploadConfig,
405
+ DownloadConfig
348
406
  };
package/lib/http/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const w=require("../index-1b48ae95.js"),P=require("../object/index.js"),k=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"}),y={caches:Object.create(null),add(e,t){this.caches[e]=t},delete(e){Reflect.deleteProperty(this.caches,e)},_cancel(e){const t=this.caches[e];typeof(t==null?void 0:t.abort)=="function"&&t.abort()},cancelAll(){Object.values(this.caches).forEach(e=>{typeof(e==null?void 0:e.abort)=="function"&&e.abort()}),this.caches=Object.create(null)},source(){const e=`${Date.now()}-${w.getRandomStr(16)}`;return{token:e,cancel:()=>this._cancel(e)}}};function O(e,t){return new Promise((r,n)=>{const i=uni[e]({...t,success(o){if(o.statusCode>=200&&o.statusCode<300)return r(o);n({type:"fail",res:o})},fail(o){n({type:"fail",res:o})},complete(){t.cancelToken&&y.delete(t.cancelToken)}});t.cancelToken&&y.add(t.cancelToken,i)})}let R=0,x=!1;function U(e){x||(x=!0,uni.showLoading({title:e,mask:!0}))}function D(){x&&(x=!1,uni.hideLoading())}function L(){let e=arguments.length>0&&arguments[0]!==void 0?arguments[0]:"加载中...";R++,R===1&&U(e)}function N(){R>0&&R--,R===0&&D()}function j(e,t){return function(r,n,i,o){return e(r,t,n,i,o)}}function A(e){return typeof e=="function"?e:()=>e||{}}function F(e){let t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{},r=arguments.length>2&&arguments[2]!==void 0?arguments[2]:{};const n=A(t.headers)(),i=A(r.headers)(),o={...t,...r,headers:{...n,...i}},{headers:s,...u}=o;return{urlRes:e.startsWith("http")?e:o.baseURL?o.baseURL+e:e,headers:s,configRes:u}}function I(e){return typeof e=="function"?[e]:Array.isArray(e)?e.filter(t=>typeof t=="function"):[]}function q(e){let t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{},r=arguments.length>2&&arguments[2]!==void 0?arguments[2]:{};const n={qs:!0,showError:!0,isCache:!1,cacheConfig:{},loadingText:e.loadingText,loading:!0,reqInterceptor:a=>a,resInterceptor:a=>a,errInterceptor:()=>{},...t,...r},i={...t.cacheConfig,...r.cacheConfig},{localAdapter:o,sessionAdapter:s,...u}=i,c={key:"",prefix:"api",mode:"local",expire:60*60,...u};if(!c.key){const a=(c.prefix||"api").replace(/:$/,"");c.key=`${a}:${e.url}`}if(c.adapter=c.mode==="session"?s:o,Object.defineProperty(c,"adapter",{writable:!1,configurable:!1}),n.cacheConfig=c,n.isCache){const a=c.adapter;if(!(a&&typeof a.get=="function"&&typeof a.set=="function"))throw new Error("开启缓存(isCache=true)时,请在cacheConfig传入对应mode的适配器,适配器必须实现 get(key)、set(key,value,expire) 方法")}return["reqInterceptor","resInterceptor","errInterceptor"].forEach(a=>{const l=[...I(t[a]),...I(r[a])];n[a]=l.length?w.composeAsync(...l):g=>g}),n}function E(e,t){const r=Array.from(t);return()=>e(...P.cloneDeepWithDescriptors(r))}function _(e,t,r,n,i){return v.apply(this,arguments)}function v(){return v=w._asyncToGenerator(function*(e,t,r,n,i){let o;n.loading&&(L(n.loadingText),o=()=>{N(),o=null});try{var s;const c=yield n.reqInterceptor(r,n),a=yield e(t,c);return(s=o)===null||s===void 0||s(),yield n.resInterceptor(a,n,i)}catch(c){var u;(u=o)===null||u===void 0||u();const a=yield n.errInterceptor(c,n);return a!==void 0?a:Promise.reject(c)}}),v.apply(this,arguments)}function S(e,t){function r(n,i,o,s,u){const c=y.source(),a=E(r,arguments),l=q({url:n,loadingText:"加载中..."},t,u),{urlRes:g,headers:C,configRes:h}=F(n,e,s),f=l.isCache,d=l.cacheConfig;if(f){const T=d.adapter.get(d.key);if(T)return Promise.resolve(T)}const m=i.toUpperCase()==="GET"?k.JSON:l.qs?k.FORM_URLENCODED:k.JSON,b={cancelToken:c.token,method:i,url:g,data:o,header:{"Content-Type":m,...C},...h};return _(O,"request",b,l,a).then(T=>(f&&d.adapter.set(d.key,T,d.expire),T))}return r.get=j(r,"get"),r.post=j(r,"post"),r}function $(e,t){return function r(n,i,o,s){const u=y.source(),c=E(r,arguments),a=q({loadingText:"上传中..."},t,s),{urlRes:l,headers:g,configRes:C}=F(n,e,o),{keyName:h="file",filePath:f,...d}={keyName:"file",...i};if(!f||!f.startsWith("http://tmp")&&!f.startsWith("wxfile://tmp"))return Promise.resolve({data:f});const p={cancelToken:u.token,url:l,filePath:f,name:h,formData:d,header:{...g},...C};return _(O,"uploadFile",p,a,c)}}function z(e,t){return function r(n,i,o,s){const u=y.source(),c=E(r,arguments),a=q({loadingText:"下载中..."},t,s),{urlRes:l,headers:g,configRes:C}=F(n,e,o);let h=l;if(i&&typeof i=="object"){const d=Object.entries(i).filter(p=>{let[,m]=p;return m!=null&&m!==""}).map(p=>{let[m,b]=p;return`${encodeURIComponent(m)}=${encodeURIComponent(b)}`}).join("&");d&&(h+=(h.includes("?")?"&":"?")+d)}const f={cancelToken:u.token,url:h,header:{...g},...C};return _(O,"downloadFile",f,a,c)}}exports.CONTENT_TYPES=k;exports.CancelToken=y;exports.createDownloadFile=z;exports.createRequest=S;exports.createUploadFile=$;exports.endLoading=N;exports.hideLoading=D;exports.promisifyRequest=O;exports.showLoading=U;exports.startLoading=L;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const v=require("../index-1b48ae95.js"),P=require("../object/index.js"),k=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"}),y={caches:Object.create(null),add(e,t){this.caches[e]=t},delete(e){Reflect.deleteProperty(this.caches,e)},_cancel(e){const t=this.caches[e];typeof(t==null?void 0:t.abort)=="function"&&t.abort()},cancelAll(){Object.values(this.caches).forEach(e=>{typeof(e==null?void 0:e.abort)=="function"&&e.abort()}),this.caches=Object.create(null)},source(){const e=`${Date.now()}-${v.getRandomStr(16)}`;return{token:e,cancel:()=>this._cancel(e)}}};function O(e,t){return new Promise((r,n)=>{const i=uni[e]({...t,success(o){if(o.statusCode>=200&&o.statusCode<300)return r(o);n({type:"fail",res:o})},fail(o){n({type:"fail",res:o})},complete(){t.cancelToken&&y.delete(t.cancelToken)}});t.cancelToken&&y.add(t.cancelToken,i)})}let R=0,x=!1;function L(e){x||(x=!0,uni.showLoading({title:e,mask:!0}))}function U(){x&&(x=!1,uni.hideLoading())}function D(){let e=arguments.length>0&&arguments[0]!==void 0?arguments[0]:"加载中...";R++,R===1&&L(e)}function N(){R>0&&R--,R===0&&U()}function j(e,t){return function(r,n,i,o){return e(r,t,n,i,o)}}function A(e){return typeof e=="function"?e:()=>e||{}}function F(e){let t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{},r=arguments.length>2&&arguments[2]!==void 0?arguments[2]:{};const n=A(t.headers)(),i=A(r.headers)(),o={...t,...r,headers:{...n,...i}},{headers:l,...s}=o;return{urlRes:e.startsWith("http")?e:o.baseURL?o.baseURL+e:e,headers:l,configRes:s}}function I(e){return typeof e=="function"?[e]:Array.isArray(e)?e.filter(t=>typeof t=="function"):[]}function _(e){let t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{},r=arguments.length>2&&arguments[2]!==void 0?arguments[2]:{};const n={qs:!0,showError:!0,isCache:!1,cacheConfig:{},loadingText:e.loadingText,loading:!0,reqInterceptor:c=>c,resInterceptor:c=>c,errInterceptor:()=>{},...t,...r},i={...t.cacheConfig,...r.cacheConfig},{localAdapter:o,sessionAdapter:l,...s}=i,a={key:"",prefix:"api",mode:"local",expire:60*60,...s};if(!a.key){const c=(a.prefix||"api").replace(/:$/,"");a.key=`${c}:${e.url}`}if(a.adapter=a.mode==="session"?l:o,Object.defineProperty(a,"adapter",{writable:!1,configurable:!1}),n.cacheConfig=a,n.isCache){const c=a.adapter;if(!(c&&typeof c.get=="function"&&typeof c.set=="function"))throw new Error("开启缓存(isCache=true)时,请在cacheConfig传入对应mode的适配器,适配器必须实现 get(key)、set(key,value,expire) 方法")}return["reqInterceptor","resInterceptor","errInterceptor"].forEach(c=>{const u=[...I(t[c]),...I(r[c])];n[c]=u.length?v.composeAsync(...u):g=>g}),n}function q(e,t){const r=Array.from(t);return()=>e(...P.cloneDeepWithDescriptors(r))}function E(e,t,r,n,i){return w.apply(this,arguments)}function w(){return w=v._asyncToGenerator(function*(e,t,r,n,i){let o;n.loading&&(D(n.loadingText),o=()=>{N(),o=null});try{const s=yield n.reqInterceptor(r,n),a=yield e(t,s);return yield n.resInterceptor(a,n,i)}catch(s){const a=yield n.errInterceptor(s,n);return a!==void 0?a:Promise.reject(s)}finally{var l;(l=o)===null||l===void 0||l()}}),w.apply(this,arguments)}function S(e,t){function r(n,i,o,l,s){const a=y.source(),c=q(r,arguments),u=_({url:n,loadingText:"加载中..."},t,s),{urlRes:g,headers:C,configRes:h}=F(n,e,l),f=u.isCache,d=u.cacheConfig;if(f){const T=d.adapter.get(d.key);if(T)return Promise.resolve(T)}const m=i.toUpperCase()==="GET"?k.JSON:u.qs?k.FORM_URLENCODED:k.JSON,b={cancelToken:a.token,method:i,url:g,data:o,header:{"Content-Type":m,...C},...h};return E(O,"request",b,u,c).then(T=>(f&&d.adapter.set(d.key,T,d.expire),T))}return r.get=j(r,"get"),r.post=j(r,"post"),r}function $(e,t){return function r(n,i,o,l){const s=y.source(),a=q(r,arguments),c=_({loadingText:"上传中..."},t,l),{urlRes:u,headers:g,configRes:C}=F(n,e,o),{keyName:h="file",filePath:f,...d}={keyName:"file",...i};if(!f||!f.startsWith("http://tmp")&&!f.startsWith("wxfile://tmp"))return Promise.resolve({data:f});const p={cancelToken:s.token,url:u,filePath:f,name:h,formData:d,header:{...g},...C};return E(O,"uploadFile",p,c,a)}}function z(e,t){return function r(n,i,o,l){const s=y.source(),a=q(r,arguments),c=_({loadingText:"下载中..."},t,l),{urlRes:u,headers:g,configRes:C}=F(n,e,o);let h=u;if(i&&typeof i=="object"){const d=Object.entries(i).filter(p=>{let[,m]=p;return m!=null&&m!==""}).map(p=>{let[m,b]=p;return`${encodeURIComponent(m)}=${encodeURIComponent(b)}`}).join("&");d&&(h+=(h.includes("?")?"&":"?")+d)}const f={cancelToken:s.token,url:h,header:{...g},...C};return E(O,"downloadFile",f,c,a)}}exports.CONTENT_TYPES=k;exports.CancelToken=y;exports.createDownloadFile=z;exports.createRequest=S;exports.createUploadFile=$;exports.endLoading=N;exports.hideLoading=U;exports.promisifyRequest=O;exports.showLoading=L;exports.startLoading=D;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utils-uniapp",
3
- "version": "1.1.9",
3
+ "version": "1.1.10",
4
4
  "description": "aubyn's utils for uniapp",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",