utils-uniapp 1.1.22 → 1.1.25

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.
@@ -40,11 +40,15 @@ 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;
@@ -102,6 +106,10 @@ export interface CacheConfigInput {
102
106
  adapter?: CacheAdapter | null | ((ctx: { url: string; config: NormalizeReqConfig; options: RequestOptions; cacheConfig: CacheConfig }) => CacheAdapter | null);
103
107
  }
104
108
 
109
+ /**
110
+ * 重载请求函数类型
111
+ * @template T
112
+ */
105
113
  type ReloadFn = <T>(...newArgvs: Parameters<RequestInstance>) => RequestPromise<T>;
106
114
 
107
115
  /**
@@ -110,25 +118,34 @@ type ReloadFn = <T>(...newArgvs: Parameters<RequestInstance>) => RequestPromise<
110
118
  * @internal
111
119
  */
112
120
  interface CacheConfig {
121
+ /** 是否启用缓存 */
113
122
  readonly enable: boolean;
123
+ /** 最终生成的缓存key */
114
124
  readonly key: string;
125
+ /** 缓存key前缀 */
115
126
  readonly prefix: string;
127
+ /** 缓存有效期,单位秒 */
116
128
  readonly expire: number;
129
+ /** 缓存适配器实例 */
117
130
  readonly adapter: CacheAdapter | null;
118
131
  }
119
132
 
133
+ /** 拦截器返回类型 */
120
134
  type InterceptorReturn<T> = Promise<T> | T;
121
135
  /**
122
136
  * 请求前置拦截器【标准化后】
137
+ * @description 若拦截器返回 null / undefined,会自动使用原始 invokeArgs;有返回值则使用拦截器输出,必须保持 NormalizeReqConfig 结构
123
138
  * @param ctx 上下文
124
139
  * @param ctx.invokeArgs 组装完成即将传给uni的请求参数(原invokeArgs)
125
140
  * @param ctx.options normalizeOpts合并后的完整配置
126
141
  * @param next 执行下一个拦截器,返回处理后的请求参数
127
- * @returns 处理后的请求参数
142
+ * @returns 处理后的请求参数;返回 null/undefined 将自动兜底为原始 invokeArgs
128
143
  */
129
144
  type ReqInterceptor = (
130
145
  ctx: {
146
+ /** 组装完成即将传给uni的请求参数 */
131
147
  invokeArgs: NormalizeReqConfig;
148
+ /** normalizeOpts合并后的完整配置 */
132
149
  options: RequestOptions;
133
150
  },
134
151
  next: () => InterceptorReturn<NormalizeReqConfig>
@@ -136,40 +153,51 @@ type ReqInterceptor = (
136
153
 
137
154
  /**
138
155
  * 响应拦截器【标准化后】
156
+ * @description 若拦截器返回 null / undefined,会自动使用原始 res;有返回值则使用拦截器输出作为最终业务数据
139
157
  * @param ctx 上下文
140
- * @param ctx res uni原始响应对象,实际类型为 UniAnyResult
158
+ * @param ctx.res uni原始响应对象,实际类型为 UniAnyResult
141
159
  * @param ctx.invokeArgs 组装完成即将传给uni的请求参数
142
160
  * @param ctx.options normalizeOpts合并后的完整配置
143
161
  * @param ctx.reload 重发当前请求函数
144
162
  * @param next 执行下一个拦截器,返回处理后业务数据
145
- * @returns 处理后业务数据
163
+ * @returns 处理后业务数据;返回 null/undefined 将自动兜底为原始 res
146
164
  */
147
165
  type ResInterceptor = (
148
166
  ctx: {
167
+ /** uni原始响应对象 */
149
168
  res: UniAnyResult;
169
+ /** 组装完成即将传给uni的请求参数 */
150
170
  invokeArgs: NormalizeReqConfig;
171
+ /** normalizeOpts合并后的完整配置 */
151
172
  options: RequestOptions;
173
+ /** 重发当前请求函数 */
152
174
  reload: ReloadFn;
153
175
  },
154
176
  next: () => InterceptorReturn<any>
155
177
  ) => InterceptorReturn<any>;
156
178
 
179
+ /** 错误类型:接口失败载荷 | JS异常 */
157
180
  type Err = RequestFailPayload | Error;
158
181
  /**
159
182
  * 错误拦截器【标准化后】
183
+ * @description 若拦截器返回 null / undefined,会自动抛出原始 err;有返回值则使用拦截器输出作为拒绝原因
160
184
  * @param ctx 上下文
161
185
  * @param ctx.err 捕获到的异常对象,可能为接口回调失败载荷 RequestFailPayload 或 JS运行异常 Error
162
186
  * @param ctx.invokeArgs 组装完成即将传给uni的请求参数
163
187
  * @param ctx.options normalizeOpts合并后的完整配置
164
- * @param ctx.reloadFn 重发当前请求函数
188
+ * @param ctx.reload 重发当前请求函数
165
189
  * @param next 执行下一个拦截器,返回错误载荷
166
- * @returns 返回非undefined代表错误已接管;返回undefined会继续抛出异常
190
+ * @returns 错误载荷;返回 null/undefined 将自动兜底为原始 err
167
191
  */
168
192
  type ErrInterceptor = (
169
193
  ctx: {
194
+ /** 捕获到的异常对象 */
170
195
  err: Err;
196
+ /** 组装完成即将传给uni的请求参数 */
171
197
  invokeArgs: NormalizeReqConfig;
198
+ /** normalizeOpts合并后的完整配置 */
172
199
  options: RequestOptions;
200
+ /** 重发当前请求函数 */
173
201
  reload: ReloadFn;
174
202
  },
175
203
  next: () => InterceptorReturn<Err>
@@ -179,7 +207,13 @@ type ErrInterceptor = (
179
207
  * 用户传入拦截器:支持单个函数 / 函数数组,交由 normalizeInterceptor + composeAsync合并
180
208
  */
181
209
  export type UserReqInterceptor = ReqInterceptor | ReqInterceptor[];
210
+ /**
211
+ * 用户传入响应拦截器:支持单个函数 / 函数数组,交由 normalizeInterceptor + composeAsync合并
212
+ */
182
213
  export type UserResInterceptor = ResInterceptor | ResInterceptor[];
214
+ /**
215
+ * 用户传入错误拦截器:支持单个函数 / 函数数组,交由 normalizeInterceptor + composeAsync合并
216
+ */
183
217
  export type UserErrInterceptor = ErrInterceptor | ErrInterceptor[];
184
218
 
185
219
  /**
@@ -251,15 +285,40 @@ export interface UserRequestOpts {
251
285
  */
252
286
  loading?: boolean;
253
287
  /**
254
- * 请求前置拦截器,支持单个函数或函数数组
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
255
296
  */
256
297
  reqInterceptor?: UserReqInterceptor;
257
298
  /**
258
- * 响应拦截器,支持单个函数或函数数组
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
259
309
  */
260
310
  resInterceptor?: UserResInterceptor;
261
311
  /**
262
- * 错误拦截器,支持单个函数或函数数组
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
263
322
  */
264
323
  errInterceptor?: UserErrInterceptor;
265
324
  /**
@@ -275,6 +334,7 @@ export interface UserRequestOpts {
275
334
  * @property url - 请求完整地址
276
335
  */
277
336
  export type RuntimeHeadersCtx<T = {}> = {
337
+ /** 请求完整地址 */
278
338
  url: string;
279
339
  } & T;
280
340
 
@@ -287,6 +347,7 @@ export type HttpRequestConfig = Partial<Omit<UniApp.RequestOptions, 'url' | 'hea
287
347
  baseURL?: string;
288
348
  /** 库自定义:headers支持对象或者返回对象的工厂函数 */
289
349
  headers?: Record<string, string> | ((ctx: RuntimeHeadersCtx<HttpRequestConfig>) => Record<string, string>);
350
+ /** 自定义扩展字段 */
290
351
  [key: string]: any;
291
352
  };
292
353
 
@@ -299,6 +360,7 @@ export type UploadConfig = Partial<Omit<UniApp.UploadFileOption, 'url' | 'header
299
360
  baseURL?: string;
300
361
  /** 库自定义:headers支持对象或者返回对象的工厂函数 */
301
362
  headers?: Record<string, string> | ((ctx: RuntimeHeadersCtx<UploadConfig>) => Record<string, string>);
363
+ /** 自定义扩展字段 */
302
364
  [key: string]: any;
303
365
  };
304
366
 
@@ -311,6 +373,7 @@ export type DownloadConfig = Partial<Omit<UniApp.DownloadFileOption, 'url' | 'he
311
373
  baseURL?: string;
312
374
  /** 库自定义:headers支持对象或者返回对象的工厂函数 */
313
375
  headers?: Record<string, string> | ((ctx: RuntimeHeadersCtx<DownloadConfig>) => Record<string, string>);
376
+ /** 自定义扩展字段 */
314
377
  [key: string]: any;
315
378
  };
316
379
 
@@ -386,7 +449,9 @@ export declare function endLoading(): void;
386
449
  * 请求返回Promise扩展类型,携带取消能力
387
450
  */
388
451
  export interface RequestPromise<T> extends Promise<T> {
452
+ /** 取消当前请求 */
389
453
  cancel(): void;
454
+ /** 当前请求的取消令牌 */
390
455
  token: string;
391
456
  }
392
457
 
@@ -446,9 +511,11 @@ export declare function createUploadFile(
446
511
  ): <T = any>(
447
512
  url: string,
448
513
  params: {
514
+ /** 文件本地路径 */
449
515
  filePath: string;
450
516
  /** keyName 映射到 uni.uploadFile 的 name 字段(后端接收的表单字段名) */
451
517
  keyName?: string;
518
+ /** 额外表单参数 */
452
519
  [key: string]: any;
453
520
  },
454
521
  config?: UploadConfig,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utils-uniapp",
3
- "version": "1.1.22",
3
+ "version": "1.1.25",
4
4
  "description": "aubyn's utils for uniapp",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",