utils-uniapp 1.1.21 → 1.1.23
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 +122 -50
- package/lib/http/index.js +1 -1
- package/lib/magic/index.d.ts +21 -27
- package/lib/magic/index.js +1 -1
- package/lib/pageStack/index.d.ts +1 -1
- package/package.json +1 -1
package/lib/http/index.d.ts
CHANGED
|
@@ -40,16 +40,20 @@ type UniAnyResult = UniRequestResult | UniUploadResult | UniDownloadResult;
|
|
|
40
40
|
* @internal
|
|
41
41
|
*/
|
|
42
42
|
type RequestFailPayload = {
|
|
43
|
+
/** 失败类型标识 */
|
|
43
44
|
type: 'fail';
|
|
45
|
+
/** uni原始回调结果 */
|
|
44
46
|
res: UniApp.GeneralCallbackResult;
|
|
45
47
|
};
|
|
46
48
|
|
|
47
|
-
/**
|
|
49
|
+
/**
|
|
50
|
+
* 标准化后的请求配置
|
|
51
|
+
*/
|
|
48
52
|
type NormalizeReqConfig = Partial<UniApp.RequestOptions | UniApp.UploadFileOption | UniApp.DownloadFileOption> & {
|
|
49
53
|
/** 库自定义:请求基础地址,非uni.request原生参数 */
|
|
50
54
|
baseURL: string;
|
|
51
55
|
/** 用于取消请求的token */
|
|
52
|
-
cancelToken: string
|
|
56
|
+
cancelToken: string;
|
|
53
57
|
};
|
|
54
58
|
|
|
55
59
|
/**
|
|
@@ -97,81 +101,119 @@ export interface CacheConfigInput {
|
|
|
97
101
|
* @param ctx.url 完整请求url
|
|
98
102
|
* @param ctx.config 组装完成即将传给uni的请求参数(request/upload/download)
|
|
99
103
|
* @param ctx.options normalizeOpts合并后的用户请求扩展配置
|
|
100
|
-
* @param ctx.cacheConfig
|
|
104
|
+
* @param ctx.cacheConfig 当前待生成的运行时缓存配置
|
|
101
105
|
*/
|
|
102
|
-
adapter?: CacheAdapter | null | ((ctx: {
|
|
103
|
-
url: string;
|
|
104
|
-
config: NormalizeReqConfig;
|
|
105
|
-
options: RequestOptions;
|
|
106
|
-
cacheConfig: CacheConfig;
|
|
107
|
-
}) => CacheAdapter | null);
|
|
106
|
+
adapter?: CacheAdapter | null | ((ctx: { url: string; config: NormalizeReqConfig; options: RequestOptions; cacheConfig: CacheConfig }) => CacheAdapter | null);
|
|
108
107
|
}
|
|
109
108
|
|
|
109
|
+
/**
|
|
110
|
+
* 重载请求函数类型
|
|
111
|
+
* @template T
|
|
112
|
+
*/
|
|
113
|
+
type ReloadFn = <T>(...newArgvs: Parameters<RequestInstance>) => RequestPromise<T>;
|
|
114
|
+
|
|
110
115
|
/**
|
|
111
116
|
* normalizeOpts 之后内部运行时缓存配置对象【内部类型,不对外导出】
|
|
112
117
|
* @description adapter 字段由内部派生生成,外部禁止传入、禁止读取使用
|
|
113
118
|
* @internal
|
|
114
119
|
*/
|
|
115
120
|
interface CacheConfig {
|
|
121
|
+
/** 是否启用缓存 */
|
|
116
122
|
readonly enable: boolean;
|
|
123
|
+
/** 最终生成的缓存key */
|
|
117
124
|
readonly key: string;
|
|
125
|
+
/** 缓存key前缀 */
|
|
118
126
|
readonly prefix: string;
|
|
127
|
+
/** 缓存有效期,单位秒 */
|
|
119
128
|
readonly expire: number;
|
|
129
|
+
/** 缓存适配器实例 */
|
|
120
130
|
readonly adapter: CacheAdapter | null;
|
|
121
131
|
}
|
|
122
132
|
|
|
133
|
+
/** 拦截器返回类型 */
|
|
134
|
+
type InterceptorReturn<T> = Promise<T> | T;
|
|
123
135
|
/**
|
|
124
136
|
* 请求前置拦截器【标准化后】
|
|
125
|
-
* @
|
|
126
|
-
* @param
|
|
127
|
-
* @
|
|
137
|
+
* @description 若拦截器返回 null / undefined,会自动使用原始 invokeArgs;有返回值则使用拦截器输出,必须保持 NormalizeReqConfig 结构
|
|
138
|
+
* @param ctx 上下文
|
|
139
|
+
* @param ctx.invokeArgs 组装完成即将传给uni的请求参数(原invokeArgs)
|
|
140
|
+
* @param ctx.options normalizeOpts合并后的完整配置
|
|
141
|
+
* @param next 执行下一个拦截器,返回处理后的请求参数
|
|
142
|
+
* @returns 处理后的请求参数;返回 null/undefined 将自动兜底为原始 invokeArgs
|
|
128
143
|
*/
|
|
129
144
|
type ReqInterceptor = (
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
145
|
+
ctx: {
|
|
146
|
+
/** 组装完成即将传给uni的请求参数 */
|
|
147
|
+
invokeArgs: NormalizeReqConfig;
|
|
148
|
+
/** normalizeOpts合并后的完整配置 */
|
|
149
|
+
options: RequestOptions;
|
|
150
|
+
},
|
|
151
|
+
next: () => InterceptorReturn<NormalizeReqConfig>
|
|
152
|
+
) => InterceptorReturn<NormalizeReqConfig>;
|
|
133
153
|
|
|
134
154
|
/**
|
|
135
155
|
* 响应拦截器【标准化后】
|
|
136
|
-
* @
|
|
137
|
-
* @param
|
|
138
|
-
* @param
|
|
139
|
-
* @param
|
|
140
|
-
* @param
|
|
141
|
-
* @
|
|
156
|
+
* @description 若拦截器返回 null / undefined,会自动使用原始 res;有返回值则使用拦截器输出作为最终业务数据
|
|
157
|
+
* @param ctx 上下文
|
|
158
|
+
* @param ctx.res uni原始响应对象,实际类型为 UniAnyResult
|
|
159
|
+
* @param ctx.invokeArgs 组装完成即将传给uni的请求参数
|
|
160
|
+
* @param ctx.options normalizeOpts合并后的完整配置
|
|
161
|
+
* @param ctx.reload 重发当前请求函数
|
|
162
|
+
* @param next 执行下一个拦截器,返回处理后业务数据
|
|
163
|
+
* @returns 处理后业务数据;返回 null/undefined 将自动兜底为原始 res
|
|
142
164
|
*/
|
|
143
165
|
type ResInterceptor = (
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
166
|
+
ctx: {
|
|
167
|
+
/** uni原始响应对象 */
|
|
168
|
+
res: UniAnyResult;
|
|
169
|
+
/** 组装完成即将传给uni的请求参数 */
|
|
170
|
+
invokeArgs: NormalizeReqConfig;
|
|
171
|
+
/** normalizeOpts合并后的完整配置 */
|
|
147
172
|
options: RequestOptions;
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
173
|
+
/** 重发当前请求函数 */
|
|
174
|
+
reload: ReloadFn;
|
|
175
|
+
},
|
|
176
|
+
next: () => InterceptorReturn<any>
|
|
177
|
+
) => InterceptorReturn<any>;
|
|
151
178
|
|
|
179
|
+
/** 错误类型:接口失败载荷 | JS异常 */
|
|
180
|
+
type Err = RequestFailPayload | Error;
|
|
152
181
|
/**
|
|
153
182
|
* 错误拦截器【标准化后】
|
|
154
|
-
* @
|
|
155
|
-
* @param
|
|
156
|
-
* @param
|
|
157
|
-
* @param
|
|
158
|
-
* @param
|
|
159
|
-
* @
|
|
183
|
+
* @description 若拦截器返回 null / undefined,会自动抛出原始 err;有返回值则使用拦截器输出作为拒绝原因
|
|
184
|
+
* @param ctx 上下文
|
|
185
|
+
* @param ctx.err 捕获到的异常对象,可能为接口回调失败载荷 RequestFailPayload 或 JS运行异常 Error
|
|
186
|
+
* @param ctx.invokeArgs 组装完成即将传给uni的请求参数
|
|
187
|
+
* @param ctx.options normalizeOpts合并后的完整配置
|
|
188
|
+
* @param ctx.reload 重发当前请求函数
|
|
189
|
+
* @param next 执行下一个拦截器,返回错误载荷
|
|
190
|
+
* @returns 错误载荷;返回 null/undefined 将自动兜底为原始 err
|
|
160
191
|
*/
|
|
161
192
|
type ErrInterceptor = (
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
193
|
+
ctx: {
|
|
194
|
+
/** 捕获到的异常对象 */
|
|
195
|
+
err: Err;
|
|
196
|
+
/** 组装完成即将传给uni的请求参数 */
|
|
197
|
+
invokeArgs: NormalizeReqConfig;
|
|
198
|
+
/** normalizeOpts合并后的完整配置 */
|
|
165
199
|
options: RequestOptions;
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
200
|
+
/** 重发当前请求函数 */
|
|
201
|
+
reload: ReloadFn;
|
|
202
|
+
},
|
|
203
|
+
next: () => InterceptorReturn<Err>
|
|
204
|
+
) => InterceptorReturn<Err>;
|
|
169
205
|
|
|
170
206
|
/**
|
|
171
207
|
* 用户传入拦截器:支持单个函数 / 函数数组,交由 normalizeInterceptor + composeAsync合并
|
|
172
208
|
*/
|
|
173
209
|
export type UserReqInterceptor = ReqInterceptor | ReqInterceptor[];
|
|
210
|
+
/**
|
|
211
|
+
* 用户传入响应拦截器:支持单个函数 / 函数数组,交由 normalizeInterceptor + composeAsync合并
|
|
212
|
+
*/
|
|
174
213
|
export type UserResInterceptor = ResInterceptor | ResInterceptor[];
|
|
214
|
+
/**
|
|
215
|
+
* 用户传入错误拦截器:支持单个函数 / 函数数组,交由 normalizeInterceptor + composeAsync合并
|
|
216
|
+
*/
|
|
175
217
|
export type UserErrInterceptor = ErrInterceptor | ErrInterceptor[];
|
|
176
218
|
|
|
177
219
|
/**
|
|
@@ -243,15 +285,40 @@ export interface UserRequestOpts {
|
|
|
243
285
|
*/
|
|
244
286
|
loading?: boolean;
|
|
245
287
|
/**
|
|
246
|
-
*
|
|
288
|
+
* 请求前置拦截器
|
|
289
|
+
* - 支持单个拦截器函数 / 拦截器函数数组,多个拦截器按顺序链式执行
|
|
290
|
+
* - 拦截器返回 null / undefined 时,自动沿用原始 invokeArgs
|
|
291
|
+
* @param ctx 上下文
|
|
292
|
+
* @param ctx.invokeArgs 组装完成即将传给uni的请求参数
|
|
293
|
+
* @param ctx.options normalizeOpts合并后的完整配置
|
|
294
|
+
* @param next 执行下一个拦截器,返回处理后的请求参数
|
|
295
|
+
* @returns 处理后的请求参数,返回 null/undefined 将自动兜底原始invokeArgs
|
|
247
296
|
*/
|
|
248
297
|
reqInterceptor?: UserReqInterceptor;
|
|
249
298
|
/**
|
|
250
|
-
*
|
|
299
|
+
* 响应拦截器
|
|
300
|
+
* - 支持单个拦截器函数 / 拦截器函数数组,多个拦截器按顺序链式执行
|
|
301
|
+
* - 拦截器返回 null / undefined 时,自动沿用原始响应res
|
|
302
|
+
* @param ctx 上下文
|
|
303
|
+
* @param ctx.res uni原始响应对象
|
|
304
|
+
* @param ctx.invokeArgs 组装完成即将传给uni的请求参数
|
|
305
|
+
* @param ctx.options normalizeOpts合并后的完整配置
|
|
306
|
+
* @param ctx.reload 重发当前请求函数
|
|
307
|
+
* @param next 执行下一个拦截器,返回处理后业务数据
|
|
308
|
+
* @returns 处理后业务数据,返回 null/undefined 将自动兜底原始res
|
|
251
309
|
*/
|
|
252
310
|
resInterceptor?: UserResInterceptor;
|
|
253
311
|
/**
|
|
254
|
-
*
|
|
312
|
+
* 错误拦截器
|
|
313
|
+
* - 支持单个拦截器函数 / 拦截器函数数组,多个拦截器按顺序链式执行
|
|
314
|
+
* - 拦截器返回 null / undefined 时,自动抛出原始err
|
|
315
|
+
* @param ctx 上下文
|
|
316
|
+
* @param ctx.err 捕获到的异常对象(RequestFailPayload | Error)
|
|
317
|
+
* @param ctx.invokeArgs 组装完成即将传给uni的请求参数
|
|
318
|
+
* @param ctx.options normalizeOpts合并后的完整配置
|
|
319
|
+
* @param ctx.reload 重发当前请求函数
|
|
320
|
+
* @param next 执行下一个拦截器,返回错误载荷
|
|
321
|
+
* @returns 错误载荷,返回 null/undefined 将自动兜底原始err
|
|
255
322
|
*/
|
|
256
323
|
errInterceptor?: UserErrInterceptor;
|
|
257
324
|
/**
|
|
@@ -264,11 +331,12 @@ export interface UserRequestOpts {
|
|
|
264
331
|
* 运行时 headers 上下文对象
|
|
265
332
|
* 用于 headers 工厂函数回调,提供请求运行时信息,支持业务自定义扩展字段
|
|
266
333
|
* @template T - 业务原始配置泛型,会合并到上下文对象
|
|
267
|
-
* @property
|
|
334
|
+
* @property url - 请求完整地址
|
|
268
335
|
*/
|
|
269
336
|
export type RuntimeHeadersCtx<T = {}> = {
|
|
337
|
+
/** 请求完整地址 */
|
|
270
338
|
url: string;
|
|
271
|
-
} & T
|
|
339
|
+
} & T;
|
|
272
340
|
|
|
273
341
|
/**
|
|
274
342
|
* 请求config:uni.request配置,排除内部强制覆盖字段,允许业务扩展
|
|
@@ -279,6 +347,7 @@ export type HttpRequestConfig = Partial<Omit<UniApp.RequestOptions, 'url' | 'hea
|
|
|
279
347
|
baseURL?: string;
|
|
280
348
|
/** 库自定义:headers支持对象或者返回对象的工厂函数 */
|
|
281
349
|
headers?: Record<string, string> | ((ctx: RuntimeHeadersCtx<HttpRequestConfig>) => Record<string, string>);
|
|
350
|
+
/** 自定义扩展字段 */
|
|
282
351
|
[key: string]: any;
|
|
283
352
|
};
|
|
284
353
|
|
|
@@ -291,6 +360,7 @@ export type UploadConfig = Partial<Omit<UniApp.UploadFileOption, 'url' | 'header
|
|
|
291
360
|
baseURL?: string;
|
|
292
361
|
/** 库自定义:headers支持对象或者返回对象的工厂函数 */
|
|
293
362
|
headers?: Record<string, string> | ((ctx: RuntimeHeadersCtx<UploadConfig>) => Record<string, string>);
|
|
363
|
+
/** 自定义扩展字段 */
|
|
294
364
|
[key: string]: any;
|
|
295
365
|
};
|
|
296
366
|
|
|
@@ -303,6 +373,7 @@ export type DownloadConfig = Partial<Omit<UniApp.DownloadFileOption, 'url' | 'he
|
|
|
303
373
|
baseURL?: string;
|
|
304
374
|
/** 库自定义:headers支持对象或者返回对象的工厂函数 */
|
|
305
375
|
headers?: Record<string, string> | ((ctx: RuntimeHeadersCtx<DownloadConfig>) => Record<string, string>);
|
|
376
|
+
/** 自定义扩展字段 */
|
|
306
377
|
[key: string]: any;
|
|
307
378
|
};
|
|
308
379
|
|
|
@@ -347,13 +418,10 @@ export declare const CancelToken: {
|
|
|
347
418
|
* 包装uni异步API为Promise
|
|
348
419
|
* @param type uni接口名称 request | uploadFile | downloadFile
|
|
349
420
|
* @param opts 请求参数,可以传入 cancelToken 用于请求取消
|
|
350
|
-
* @description 返回uni原始成功回调对象;catch捕获为 RequestFailPayload | Error
|
|
421
|
+
* @description resolve 返回uni原始成功回调对象;catch捕获为 RequestFailPayload | Error
|
|
351
422
|
* @returns uni接口回调结果Promise
|
|
352
423
|
*/
|
|
353
|
-
export declare function promisifyRequest(
|
|
354
|
-
type: 'request' | 'uploadFile' | 'downloadFile',
|
|
355
|
-
opts: NormalizeReqConfig
|
|
356
|
-
): Promise<UniAnyResult>;
|
|
424
|
+
export declare function promisifyRequest(type: 'request' | 'uploadFile' | 'downloadFile', opts: NormalizeReqConfig): Promise<UniAnyResult>;
|
|
357
425
|
|
|
358
426
|
/**
|
|
359
427
|
* 底层直接展示loading,不做计数
|
|
@@ -381,7 +449,9 @@ export declare function endLoading(): void;
|
|
|
381
449
|
* 请求返回Promise扩展类型,携带取消能力
|
|
382
450
|
*/
|
|
383
451
|
export interface RequestPromise<T> extends Promise<T> {
|
|
452
|
+
/** 取消当前请求 */
|
|
384
453
|
cancel(): void;
|
|
454
|
+
/** 当前请求的取消令牌 */
|
|
385
455
|
token: string;
|
|
386
456
|
}
|
|
387
457
|
|
|
@@ -441,9 +511,11 @@ export declare function createUploadFile(
|
|
|
441
511
|
): <T = any>(
|
|
442
512
|
url: string,
|
|
443
513
|
params: {
|
|
514
|
+
/** 文件本地路径 */
|
|
444
515
|
filePath: string;
|
|
445
|
-
/** keyName 映射到 uni
|
|
516
|
+
/** keyName 映射到 uni.uploadFile 的 name 字段(后端接收的表单字段名) */
|
|
446
517
|
keyName?: string;
|
|
518
|
+
/** 额外表单参数 */
|
|
447
519
|
[key: string]: any;
|
|
448
520
|
},
|
|
449
521
|
config?: UploadConfig,
|
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 n=require("../_rollupPluginBabelHelpers-fa19b5b2.js"),$=require("../object/index.js"),G=require("../magic/index.js");var H=["cancelToken"],M=["headers"],J=["adapter"],Y=["keyName","filePath"];function B(){for(var t=arguments.length,c=new Array(t),i=0;i<t;i++)c[i]=arguments[i];if(c.some(function(r){return typeof r!="function"}))throw new TypeError("composeAsync 参数必须是函数");return c.length===0?n._asyncToGenerator(n._regenerator().m(function r(){var e,a,s,u=arguments;return n._regenerator().w(function(o){for(;;)switch(o.n){case 0:for(e=u.length,a=new Array(e),s=0;s<e;s++)a[s]=u[s];return o.a(2,a.length===1?a[0]:a)}},r)})):c.reduce(function(r,e){return n._asyncToGenerator(n._regenerator().m(function a(){var s=this,u,o,f,d=arguments;return n._regenerator().w(function(p){for(;;)switch(p.n){case 0:for(u=d.length,o=new Array(u),f=0;f<u;f++)o[f]=d[f];return p.a(2,e.call.apply(e,[this].concat(o,[n._asyncToGenerator(n._regenerator().m(function h(){return n._regenerator().w(function(l){for(;;)switch(l.n){case 0:return l.a(2,r.apply(s,o))}},h)}))])))}},a,this)}))},n._asyncToGenerator(n._regenerator().m(function r(){var e,a,s,u=arguments;return n._regenerator().w(function(o){for(;;)switch(o.n){case 0:for(e=u.length,a=new Array(e),s=0;s<e;s++)a[s]=u[s];return o.a(2,a.length===1?a[0]:a)}},r)})))}var R=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 t=Object.create(null);function c(i){var r=t[i];typeof(r==null?void 0:r.abort)=="function"&&r.abort()}return Object.seal({add:function(r,e){t[r]=e},delete:function(r){Reflect.deleteProperty(t,r)},_cancel:c,cancelAll:function(){Object.values(t).forEach(function(r){typeof(r==null?void 0:r.abort)=="function"&&r.abort()}),Object.keys(t).forEach(function(r){return delete t[r]})},source:function(){var r="".concat(Date.now(),"-").concat(G.getRandomStr(16));return{token:r,cancel:function(){return c(r)}}}})}());function A(t,c){return new Promise(function(i,r){var e=c.cancelToken,a=n._objectWithoutProperties(c,H),s=uni[t](n._objectSpread2(n._objectSpread2({},a),{},{success:function(o){if(o.statusCode>=200&&o.statusCode<300)return i(o);var f=Object.assign(new Error("HTTP status ".concat(o.statusCode)),{type:"fail",res:o});r(f)},fail:function(o){var f=Object.assign(new Error(o.errMsg||"request fail"),{type:"fail",res:o});r(f)},complete:function(){e&&C.delete(e)}}));e&&C.add(e,s)})}var T=0,S=!1;function U(t){S||(S=!0,uni.showLoading({title:t,mask:!0}))}function D(){S&&(S=!1,uni.hideLoading())}function W(){var t=arguments.length>0&&arguments[0]!==void 0?arguments[0]:"加载中...";T++,T===1&&U(t)}function z(){T>0&&T--,T===0&&D()}function N(t,c){return function(i,r,e,a){return t(i,c,r,e,a)}}function I(t){return typeof t=="function"?t:function(){return t||{}}}function E(t){var c=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{},i=arguments.length>2&&arguments[2]!==void 0?arguments[2]:{},r=n._objectSpread2(n._objectSpread2({},c),i);r.headers;var e=n._objectWithoutProperties(r,M),a=I(c.headers)(n._objectSpread2({url:t},e)),s=I(i.headers)(n._objectSpread2(n._objectSpread2({url:t},e),{},{headers:n._objectSpread2({},a)})),u=n._objectSpread2(n._objectSpread2({},a),s),o=t.startsWith("http")?t:e.baseURL?"".concat(e.baseURL.replace(/\/+$/,""),"/").concat(t.replace(/^\/+/,"")):t;return{urlRes:o,headers:u,configRes:e}}function P(t){return typeof t=="function"?[t]:Array.isArray(t)?t.filter(function(c){return typeof c=="function"}):[]}function V(t){var c=t.url,i=t.invokeArgs,r=i===void 0?{}:i,e=t.options,a=e===void 0?{}:e,s=t.cacheConfig,u=s===void 0?{}:s,o=u.adapter,f=n._objectWithoutProperties(u,J),d=n._objectSpread2({enable:!1,key:"",prefix:"api",expire:60*60,adapter:null},f);if(!d.key){var p=(d.prefix||"api").replace(/:$/,"");d.key="".concat(p,":").concat(c)}if(d.enable&&(typeof o=="function"&&(o=o({url:c,config:r,options:a,cacheConfig:d})),!(o&&typeof o.get=="function"&&typeof o.set=="function")))throw new Error("开启缓存(cacheConfig.enable=true)时,请在cacheConfig传入对应adapter的适配器,适配器必须实现 get(key)、set(key,value,expire) 方法");return Object.freeze(n._objectSpread2(n._objectSpread2({},d),{},{adapter:o}))}function q(t){var c=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{},i=arguments.length>2&&arguments[2]!==void 0?arguments[2]:{},r=n._objectSpread2(n._objectSpread2({qs:!0,showError:!0,cacheConfig:{},loadingText:t,loading:!0,reqInterceptor:function(a){return a.invokeArgs},resInterceptor:function(a){return a.res},errInterceptor:function(a){return a.err}},c),i);return["reqInterceptor","resInterceptor","errInterceptor"].forEach(function(e){var a=[].concat(n._toConsumableArray(P(c[e])),n._toConsumableArray(P(i[e])));a.length&&(r[e]=B.apply(void 0,n._toConsumableArray(a)))}),r}function F(t,c){var i=Array.from(c);return function(){for(var r=arguments.length,e=new Array(r),a=0;a<r;a++)e[a]=arguments[a];return t.apply(void 0,n._toConsumableArray($.cloneDeepWithDescriptors(e.length?e:i)))}}function L(t,c,i,r,e){return O.apply(this,arguments)}function O(){return O=n._asyncToGenerator(n._regenerator().m(function t(c,i,r,e,a){var s,u,o,f,d,p,h;return n._regenerator().w(function(l){for(;;)switch(l.p=l.n){case 0:return s=null,e.loading&&(W(e.loadingText),s=function(){z(),s=null}),l.p=1,l.n=2,e.reqInterceptor({invokeArgs:r,options:e});case 2:return u=l.v,l.n=3,c(i,u!=null?u:r);case 3:return o=l.v,l.n=4,e.resInterceptor({res:o,invokeArgs:r,options:e,reload:a});case 4:return f=l.v,l.a(2,f!=null?f:o);case 5:return l.p=5,h=l.v,l.n=6,e.errInterceptor({err:h,invokeArgs:r,options:e,reload:a});case 6:return d=l.v,l.a(2,Promise.reject(d!=null?d:h));case 7:return l.p=7,(p=s)===null||p===void 0||p(),l.f(7);case 8:return l.a(2)}},t,null,[[1,5,7,8]])})),O.apply(this,arguments)}function X(t,c){function i(r,e,a,s,u){var o=C.source(),f=F(i,arguments),d=E(r,t,s),p=d.urlRes,h=d.headers,l=d.configRes,v=q("加载中...",c,u),w=e.toUpperCase()==="GET",k=w?R.JSON:v.qs?R.FORM_URLENCODED:R.JSON,m=n._objectSpread2({cancelToken:o.token,method:e,url:p,data:a,header:n._objectSpread2({"Content-Type":k},h)},l),g=V({url:r,invokeArgs:m,options:v,cacheConfig:n._objectSpread2(n._objectSpread2({},c.cacheConfig),u==null?void 0:u.cacheConfig)}),_=g.enable;if(_){var b=g.adapter.get(g.key);if(b)return Promise.resolve(b)}var y=L(A,"request",m,n._objectSpread2(n._objectSpread2({},v),{},{cacheConfig:g}),f).then(function(j){return _&&g.adapter.set(g.key,j,g.expire),j});return y.cancel=o.cancel,y.token=o.token,y}return i.get=N(i,"get"),i.post=N(i,"post"),i}function x(t,c){return function i(r,e,a,s){var u=C.source(),o=F(i,arguments),f=E(r,t,a),d=f.urlRes,p=f.headers,h=f.configRes,l=q("上传中...",c,s),v=n._objectSpread2({keyName:"file"},e),w=v.keyName,k=w===void 0?"file":w,m=v.filePath,g=n._objectWithoutProperties(v,Y);function _(j){return j?!!(j.startsWith("wxfile://tmp")||j.startsWith("http://tmp")||j.startsWith("http://temp")||j.startsWith("blob:")||j.startsWith("_doc/")):!1}if(!_(m))return Promise.resolve({data:m});var b=n._objectSpread2({cancelToken:u.token,url:d,filePath:m,name:k,formData:g,header:n._objectSpread2({},p)},h),y=L(A,"uploadFile",b,l,o);return y.cancel=u.cancel,y.token=u.token,y}}function K(t,c){return function i(r,e,a,s){var u=C.source(),o=F(i,arguments),f=E(r,t,a),d=f.urlRes,p=f.headers,h=f.configRes,l=q("下载中...",c,s),v=d;if(e&&n._typeof(e)==="object"&&!Array.isArray(e)){var w=Object.entries(e).filter(function(g){var _=n._slicedToArray(g,2),b=_[1];return b!=null&&b!==""}).map(function(g){var _=n._slicedToArray(g,2),b=_[0],y=_[1];return"".concat(encodeURIComponent(b),"=").concat(encodeURIComponent(y))}).join("&");w&&(v+=(v.includes("?")?"&":"?")+w)}var k=n._objectSpread2({cancelToken:u.token,url:v,header:n._objectSpread2({},p)},h),m=L(A,"downloadFile",k,l,o);return m.cancel=u.cancel,m.token=u.token,m}}exports.CONTENT_TYPES=R;exports.CancelToken=C;exports.createDownloadFile=K;exports.createRequest=X;exports.createUploadFile=x;exports.endLoading=z;exports.hideLoading=D;exports.promisifyRequest=A;exports.showLoading=U;exports.startLoading=W;
|
package/lib/magic/index.d.ts
CHANGED
|
@@ -1,20 +1,25 @@
|
|
|
1
|
+
type AnyFn<T = any> = (...args: any[]) => T;
|
|
2
|
+
type FirstFn = (next: <T>(e: T) => T, ...args: any[]) => any;
|
|
3
|
+
type NextFn = (next: AnyFn, ...args: any[]) => any;
|
|
4
|
+
type DropFirst<T extends readonly any[]> = T extends [any, ...infer Rest] ? Rest : [];
|
|
5
|
+
type DropRes<T extends AnyFn> = T extends (...args: infer A) => infer P ? (...argvs: DropFirst<A>) => P : never;
|
|
1
6
|
// 基础组合类型
|
|
2
7
|
type Compose<T extends any[]> = T extends [infer F]
|
|
3
8
|
? F
|
|
4
|
-
: T extends [infer
|
|
5
|
-
?
|
|
6
|
-
? Compose<
|
|
7
|
-
? (...args: Parameters<
|
|
9
|
+
: T extends [...infer Left, infer Last]
|
|
10
|
+
? Last extends NextFn
|
|
11
|
+
? Compose<Left> extends AnyFn
|
|
12
|
+
? (...args: DropFirst<Parameters<Extract<Last, AnyFn>>>) => ReturnType<Compose<Left>>
|
|
8
13
|
: never
|
|
9
14
|
: never
|
|
10
15
|
: never;
|
|
11
16
|
|
|
12
17
|
type ComposeAsync<T extends any[]> = T extends [infer F]
|
|
13
18
|
? F
|
|
14
|
-
: T extends [infer
|
|
15
|
-
?
|
|
16
|
-
? ComposeAsync<
|
|
17
|
-
? (...args: Parameters<
|
|
19
|
+
: T extends [...infer Left, infer Last]
|
|
20
|
+
? Last extends AnyFn
|
|
21
|
+
? ComposeAsync<Left> extends AnyFn
|
|
22
|
+
? (...args: DropFirst<Parameters<Extract<Last, AnyFn>>>) => Promise<Awaited<ReturnType<Extract<Compose<Left>, AnyFn>>>>
|
|
18
23
|
: never
|
|
19
24
|
: never
|
|
20
25
|
: never;
|
|
@@ -26,16 +31,16 @@ type Pipe<T extends any[]> = Compose<Reverse<T>>;
|
|
|
26
31
|
type PipeAsync<T extends any[]> = ComposeAsync<Reverse<T>>;
|
|
27
32
|
|
|
28
33
|
/** 同步 compose - 支持任意数量函数 */
|
|
29
|
-
export function compose<T extends
|
|
34
|
+
export function compose<T extends [FirstFn, ...NextFn[]]>(...fns: T): T extends [] ? <U>(arg: U) => U : Compose<T>;
|
|
30
35
|
|
|
31
36
|
/** 异步 compose - 支持任意数量函数 */
|
|
32
|
-
export function composeAsync<T extends
|
|
37
|
+
export function composeAsync<T extends [FirstFn, ...NextFn[]]>(...fns: T): T extends [] ? <U>(arg: U) => Promise<U> : ComposeAsync<T>;
|
|
33
38
|
|
|
34
39
|
/** 同步 pipe - 支持任意数量函数 */
|
|
35
|
-
export function pipe<T extends
|
|
40
|
+
export function pipe<T extends [...NextFn[], FirstFn]>(...fns: T): T extends [] ? <U>(arg: U) => U : Pipe<T>;
|
|
36
41
|
|
|
37
42
|
/** 异步 pipe - 支持任意数量函数 */
|
|
38
|
-
export function pipeAsync<T extends
|
|
43
|
+
export function pipeAsync<T extends [...NextFn[], FirstFn]>(...fns: T): T extends [] ? <U>(arg: U) => Promise<U> : PipeAsync<T>;
|
|
39
44
|
|
|
40
45
|
/**
|
|
41
46
|
* 防抖
|
|
@@ -101,19 +106,14 @@ export interface GetQueryParamsOptions {
|
|
|
101
106
|
* @param options 配置选项
|
|
102
107
|
* @returns 解析结果
|
|
103
108
|
*/
|
|
104
|
-
export declare function getQueryParams(
|
|
105
|
-
str?: string,
|
|
106
|
-
options?: GetQueryParamsOptions
|
|
107
|
-
): Record<string, string | string[]>;
|
|
109
|
+
export declare function getQueryParams(str?: string, options?: GetQueryParamsOptions): Record<string, string | string[]>;
|
|
108
110
|
|
|
109
111
|
/**
|
|
110
112
|
* 从当前页面 URL 的 search 部分解析参数(仅浏览器环境)
|
|
111
113
|
* @param options 同 getQueryParams 的 options
|
|
112
114
|
* @returns 解析结果
|
|
113
115
|
*/
|
|
114
|
-
export declare function getQueryParamsFromSearch(
|
|
115
|
-
options?: GetQueryParamsOptions
|
|
116
|
-
): Record<string, string | string[]>;
|
|
116
|
+
export declare function getQueryParamsFromSearch(options?: GetQueryParamsOptions): Record<string, string | string[]>;
|
|
117
117
|
|
|
118
118
|
/**
|
|
119
119
|
* 从当前页面 URL 的 hash 部分解析参数(仅浏览器环境)
|
|
@@ -126,9 +126,7 @@ export declare function getQueryParamsFromSearch(
|
|
|
126
126
|
* @param options 同 getQueryParams 的 options
|
|
127
127
|
* @returns 解析结果
|
|
128
128
|
*/
|
|
129
|
-
export declare function getQueryParamsFromHash(
|
|
130
|
-
options?: GetQueryParamsOptions
|
|
131
|
-
): Record<string, string | string[]>;
|
|
129
|
+
export declare function getQueryParamsFromHash(options?: GetQueryParamsOptions): Record<string, string | string[]>;
|
|
132
130
|
|
|
133
131
|
/**
|
|
134
132
|
* 生成随机标识串(时间戳与随机数拼接)
|
|
@@ -169,14 +167,10 @@ export function getRandomStr(len?: number, $chars?: string): string;
|
|
|
169
167
|
* @param addQuestionMark 是否添加开头问号 ?
|
|
170
168
|
* @returns query串
|
|
171
169
|
*/
|
|
172
|
-
export declare function toQueryString(
|
|
173
|
-
params: Record<string, any>,
|
|
174
|
-
addQuestionMark?: boolean
|
|
175
|
-
): string;
|
|
170
|
+
export declare function toQueryString(params: Record<string, any>, addQuestionMark?: boolean): string;
|
|
176
171
|
|
|
177
172
|
/**
|
|
178
173
|
* 获取页面onLoad参数(自动解析小程序扫码scene)
|
|
179
174
|
* @param { Record<string,any> } e onLoad参数
|
|
180
175
|
*/
|
|
181
176
|
export function getOnLoadParams<T extends Record<string, any>>(e: T & { scene?: string }): T & { fromCode?: boolean } & Record<string, any>;
|
|
182
|
-
|
package/lib/magic/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 s=require("../_rollupPluginBabelHelpers-fa19b5b2.js");function U(){for(var t=arguments.length,a=new Array(t),o=0;o<t;o++)a[o]=arguments[o];if(a.some(function(r){return typeof r!="function"}))throw new TypeError("compose 参数必须是函数");return a.length===0?function(){for(var r=arguments.length,e=new Array(r),n=0;n<r;n++)e[n]=arguments[n];return e.length===1?e[0]:e}:a.reduce(function(r,e){return function(){for(var n=this,u=arguments.length,i=new Array(u),c=0;c<u;c++)i[c]=arguments[c];return e.call.apply(e,[this,function(){for(var f=arguments.length,g=new Array(f),l=0;l<f;l++)g[l]=arguments[l];return r.apply(n,g.concat(i))}].concat(i))}},function(r){return r})}function M(){for(var t=arguments.length,a=new Array(t),o=0;o<t;o++)a[o]=arguments[o];if(a.some(function(r){return typeof r!="function"}))throw new TypeError("composeAsync 参数必须是函数");return a.length===0?s._asyncToGenerator(s._regenerator().m(function r(){var e,n,u,i=arguments;return s._regenerator().w(function(c){for(;;)switch(c.n){case 0:for(e=i.length,n=new Array(e),u=0;u<e;u++)n[u]=i[u];return c.a(2,n.length===1?n[0]:n)}},r)})):a.reduce(function(r,e){return s._asyncToGenerator(s._regenerator().m(function n(){var u=this,i,c,f,g=arguments;return s._regenerator().w(function(l){for(;;)switch(l.n){case 0:for(i=g.length,c=new Array(i),f=0;f<i;f++)c[f]=g[f];return l.a(2,e.call.apply(e,[this,s._asyncToGenerator(s._regenerator().m(function y(){var p,h,m,d=arguments;return s._regenerator().w(function(v){for(;;)switch(v.n){case 0:for(p=d.length,h=new Array(p),m=0;m<p;m++)h[m]=d[m];return v.a(2,r.apply(u,h.concat(c)))}},y)}))].concat(c)))}},n,this)}))},function(){var r=s._asyncToGenerator(s._regenerator().m(function e(n){return s._regenerator().w(function(u){for(;;)switch(u.n){case 0:return u.a(2,n)}},e)}));return function(e){return r.apply(this,arguments)}}())}function O(){for(var t=arguments.length,a=new Array(t),o=0;o<t;o++)a[o]=arguments[o];if(a.some(function(r){return typeof r!="function"}))throw new TypeError("pipe 参数必须是函数");return a.length===0?function(){for(var r=arguments.length,e=new Array(r),n=0;n<r;n++)e[n]=arguments[n];return e.length===1?e[0]:e}:a.reverse().reduce(function(r,e){return function(){for(var n=this,u=arguments.length,i=new Array(u),c=0;c<u;c++)i[c]=arguments[c];return e.call.apply(e,[this,function(){for(var f=arguments.length,g=new Array(f),l=0;l<f;l++)g[l]=arguments[l];return r.apply(n,g.concat(i))}].concat(i))}},function(r){return r})}function Q(){for(var t=arguments.length,a=new Array(t),o=0;o<t;o++)a[o]=arguments[o];if(a.some(function(r){return typeof r!="function"}))throw new TypeError("pipeAsync 参数必须是函数");return a.length===0?s._asyncToGenerator(s._regenerator().m(function r(){var e,n,u,i=arguments;return s._regenerator().w(function(c){for(;;)switch(c.n){case 0:for(e=i.length,n=new Array(e),u=0;u<e;u++)n[u]=i[u];return c.a(2,n.length===1?n[0]:n)}},r)})):a.reverse().reduce(function(r,e){return s._asyncToGenerator(s._regenerator().m(function n(){var u=this,i,c,f,g=arguments;return s._regenerator().w(function(l){for(;;)switch(l.n){case 0:for(i=g.length,c=new Array(i),f=0;f<i;f++)c[f]=g[f];return l.a(2,e.call.apply(e,[this,s._asyncToGenerator(s._regenerator().m(function y(){var p,h,m,d=arguments;return s._regenerator().w(function(v){for(;;)switch(v.n){case 0:for(p=d.length,h=new Array(p),m=0;m<p;m++)h[m]=d[m];return v.a(2,r.apply(u,h.concat(c)))}},y)}))].concat(c)))}},n,this)}))},function(){var r=s._asyncToGenerator(s._regenerator().m(function e(n){return s._regenerator().w(function(u){for(;;)switch(u.n){case 0:return u.a(2,n)}},e)}));return function(e){return r.apply(this,arguments)}}())}function j(t,a){var o=null;r.stop=function(){return o&&clearTimeout(o)};function r(){for(var e=this,n=arguments.length,u=new Array(n),i=0;i<n;i++)u[i]=arguments[i];r.stop(),o=setTimeout(function(){t.apply(e,u)},a)}return r}function G(t,a){var o=null,r=[];e.stop=function(){if(o){var n;(n=r.shift())===null||n===void 0||n("cancel"),clearTimeout(o)}};function e(){for(var n=this,u=arguments.length,i=new Array(u),c=0;c<u;c++)i[c]=arguments[c];return new Promise(function(f,g){e.stop(),r.push(g),o=setTimeout(function(){f(t.apply(n,i))},a)})}return e}function E(t){var a=arguments.length>1&&arguments[1]!==void 0?arguments[1]:500,o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:!0,r;return o?function(){if(!r){r=!0;for(var e=arguments.length,n=new Array(e),u=0;u<e;u++)n[u]=arguments[u];typeof t=="function"&&t.apply(this,n),setTimeout(function(){r=!1},a)}}:function(){for(var e=this,n=arguments.length,u=new Array(n),i=0;i<n;i++)u[i]=arguments[i];r||(r=!0,setTimeout(function(){r=!1,typeof t=="function"&&t.apply(e,u)},a))}}function F(t){return encodeURIComponent(JSON.stringify(t||{}))}function H(t){return JSON.parse(decodeURIComponent(t||"{}"))}function S(){var t=arguments.length>0&&arguments[0]!==void 0?arguments[0]:"",a=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{},o=a.silent,r=o===void 0?!1:o,e=a.decodePlus,n=e===void 0?!0:e,u=a.strict,i=u===void 0?!1:u,c={};if(typeof t!="string")return c;function f(b,R){var w=b;n&&(w=w.replace(/\+/g," "));try{return decodeURIComponent(w)}catch(I){return r||console.warn("Failed to decode ".concat(R?"key":"value",": ").concat(w),I),i?null:(R&&(w=w.replace(/%[0-9a-f]{2}/gi,"")),w)}}var g=t.split("#")[0],l=g.indexOf("?"),y=l>=0?g.slice(l+1):g;if(!y)return c;for(var p=y.split("&"),h=0;h<p.length;h++){var m=p[h];if(m){var d=m.indexOf("="),v=void 0,C=void 0;d===-1?(v=m,C=""):(v=m.slice(0,d),C=m.slice(d+1));var A=f(v,!0);if(A!==null){var P=f(C,!1);if(P!==null&&A){var T=c[A];T===void 0?c[A]=P:Array.isArray(T)?T.push(P):c[A]=[T,P]}}}}return c}function L(t){return typeof window=="undefined"?{}:S(window.location.search,t)}function _(t){if(typeof window=="undefined")return{};var a=window.location.hash,o=a.indexOf("?");if(o===-1)return{};var r=a.substring(o);return S(r,t)}function J(){var t=arguments.length>0&&arguments[0]!==void 0?arguments[0]:0,a=arguments.length>1&&arguments[1]!==void 0?arguments[1]:"-";return[Date.now().toString(),Math.random().toString(16).slice(2,10),Math.random().toString(16).slice(7),t.toString()].join(a)}function N(t){return t.replace(/([^-])([^-]*)/g,function(a,o,r){return o.toLocaleUpperCase()+r.toLocaleLowerCase()}).replace(/-/g,"")}function q(t,a){t=t.split("."),a=a.split(".");for(var o=Math.max(t.length,a.length);t.length<o;)t.push("0");for(;a.length<o;)a.push("0");for(var r=0;r<o;r++){var e=parseInt(t[r]),n=parseInt(a[r]);if(e>n)return 1;if(e<n)return-1}return 0}function B(){return"#"+Math.floor(Math.random()*16777215).toString(16).padEnd(6,"0")}function D(){var t=arguments.length>0&&arguments[0]!==void 0?arguments[0]:8,a=arguments.length>1?arguments[1]:void 0;t=t||32,a||(a="ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678");for(var o=a.length,r="",e=0;e<t;e++)r+=a.charAt(Math.floor(Math.random()*o));return r}function V(t){var a=arguments.length>1&&arguments[1]!==void 0?arguments[1]:!1;if(!t||s._typeof(t)!=="object")return"";var o=[];Object.keys(t).forEach(function(e){var n=t[e];if(n!=null){var u=encodeURIComponent(e);if(Array.isArray(n)){n.forEach(function(c){var f=encodeURIComponent(c);o.push("".concat(u,"=").concat(f))});return}var i=n;s._typeof(i)==="object"&&i!==null&&(i=JSON.stringify(i)),o.push("".concat(u,"=").concat(encodeURIComponent(i)))}});var r=o.join("&");return a&&r?"?".concat(r):r}function $(t){var a=t.scene;return a&&(t=s._objectSpread2(s._objectSpread2({fromCode:!0},t),S(a))),t}exports.compareVersion=q;exports.compose=U;exports.composeAsync=M;exports.debounce=j;exports.debouncePromise=G;exports.decodeURIParams=H;exports.encodeURIParams=F;exports.getOnLoadParams=$;exports.getPascalCase=N;exports.getQueryParams=S;exports.getQueryParamsFromHash=_;exports.getQueryParamsFromSearch=L;exports.getRandom=J;exports.getRandomStr=D;exports.pipe=O;exports.pipeAsync=Q;exports.randomColor=B;exports.throttle=E;exports.toQueryString=V;
|
package/lib/pageStack/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* 禁止使用页面对象引用做相等判断,请使用route/fullPath字符串对比
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
export interface PageWrap extends
|
|
6
|
+
export interface PageWrap extends Page.PageInstance<AnyObject, {}> {
|
|
7
7
|
/** 不带开头斜杠 pages/xxx/xxx?a=1,用于路由匹配对比;基于onLoad options */
|
|
8
8
|
fullPath: string | null;
|
|
9
9
|
/** 带开头斜杠 /pages/xxx/xxx?a=1,可直接用于uni.navigateTo url;基于onLoad options */
|