utils-uniapp 1.1.20 → 1.1.21

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.
Files changed (2) hide show
  1. package/lib/http/index.d.ts +26 -11
  2. package/package.json +1 -1
@@ -44,8 +44,13 @@ type RequestFailPayload = {
44
44
  res: UniApp.GeneralCallbackResult;
45
45
  };
46
46
 
47
- /** 请求配置 */
48
- type RequestConfig = Partial<UniApp.RequestOptions | UniApp.UploadFileOption | UniApp.DownloadFileOption>;
47
+ /** 标准化后的请求配置 */
48
+ type NormalizeReqConfig = Partial<UniApp.RequestOptions | UniApp.UploadFileOption | UniApp.DownloadFileOption> & {
49
+ /** 库自定义:请求基础地址,非uni.request原生参数 */
50
+ baseURL: string;
51
+ /** 用于取消请求的token */
52
+ cancelToken: string
53
+ };
49
54
 
50
55
  /**
51
56
  * 缓存适配器抽象接口,对接外部缓存实现
@@ -96,7 +101,7 @@ export interface CacheConfigInput {
96
101
  */
97
102
  adapter?: CacheAdapter | null | ((ctx: {
98
103
  url: string;
99
- config: RequestConfig;
104
+ config: NormalizeReqConfig;
100
105
  options: RequestOptions;
101
106
  cacheConfig: CacheConfig;
102
107
  }) => CacheAdapter | null);
@@ -122,9 +127,9 @@ interface CacheConfig {
122
127
  * @returns 处理后的请求参数
123
128
  */
124
129
  type ReqInterceptor = (
125
- invokeArgs: RequestConfig,
130
+ invokeArgs: NormalizeReqConfig,
126
131
  options: RequestOptions
127
- ) => Promise<RequestConfig> | RequestConfig;
132
+ ) => Promise<NormalizeReqConfig> | NormalizeReqConfig;
128
133
 
129
134
  /**
130
135
  * 响应拦截器【标准化后】
@@ -138,7 +143,7 @@ type ReqInterceptor = (
138
143
  type ResInterceptor = (
139
144
  res: unknown,
140
145
  payload: {
141
- config: RequestConfig;
146
+ config: NormalizeReqConfig;
142
147
  options: RequestOptions;
143
148
  reloadFn: (() => Promise<unknown>) | null;
144
149
  }
@@ -156,7 +161,7 @@ type ResInterceptor = (
156
161
  type ErrInterceptor = (
157
162
  err: RequestFailPayload | Error,
158
163
  payload: {
159
- config: RequestConfig;
164
+ config: NormalizeReqConfig;
160
165
  options: RequestOptions;
161
166
  reloadFn: (() => Promise<unknown>) | null;
162
167
  }
@@ -255,6 +260,16 @@ export interface UserRequestOpts {
255
260
  [key: string]: any;
256
261
  }
257
262
 
263
+ /**
264
+ * 运行时 headers 上下文对象
265
+ * 用于 headers 工厂函数回调,提供请求运行时信息,支持业务自定义扩展字段
266
+ * @template T - 业务原始配置泛型,会合并到上下文对象
267
+ * @property {string} url - 请求地址
268
+ */
269
+ export type RuntimeHeadersCtx<T = {}> = {
270
+ url: string;
271
+ } & T
272
+
258
273
  /**
259
274
  * 请求config:uni.request配置,排除内部强制覆盖字段,允许业务扩展
260
275
  * @note baseURL 为库自定义字段,非uni原生API字段
@@ -263,7 +278,7 @@ export type HttpRequestConfig = Partial<Omit<UniApp.RequestOptions, 'url' | 'hea
263
278
  /** 库自定义:请求基础地址,非uni.request原生参数 */
264
279
  baseURL?: string;
265
280
  /** 库自定义:headers支持对象或者返回对象的工厂函数 */
266
- headers?: Record<string, string> | ((ctx: RequestConfig & { headers?: Record<string, string> }) => Record<string, string>);
281
+ headers?: Record<string, string> | ((ctx: RuntimeHeadersCtx<HttpRequestConfig>) => Record<string, string>);
267
282
  [key: string]: any;
268
283
  };
269
284
 
@@ -275,7 +290,7 @@ export type UploadConfig = Partial<Omit<UniApp.UploadFileOption, 'url' | 'header
275
290
  /** 库自定义:请求基础地址 */
276
291
  baseURL?: string;
277
292
  /** 库自定义:headers支持对象或者返回对象的工厂函数 */
278
- headers?: Record<string, string> | ((ctx: RequestConfig & { headers?: Record<string, string> }) => Record<string, string>);
293
+ headers?: Record<string, string> | ((ctx: RuntimeHeadersCtx<UploadConfig>) => Record<string, string>);
279
294
  [key: string]: any;
280
295
  };
281
296
 
@@ -287,7 +302,7 @@ export type DownloadConfig = Partial<Omit<UniApp.DownloadFileOption, 'url' | 'he
287
302
  /** 库自定义:请求基础地址 */
288
303
  baseURL?: string;
289
304
  /** 库自定义:headers支持对象或者返回对象的工厂函数 */
290
- headers?: Record<string, string> | ((ctx: RequestConfig & { headers?: Record<string, string> }) => Record<string, string>);
305
+ headers?: Record<string, string> | ((ctx: RuntimeHeadersCtx<DownloadConfig>) => Record<string, string>);
291
306
  [key: string]: any;
292
307
  };
293
308
 
@@ -337,7 +352,7 @@ export declare const CancelToken: {
337
352
  */
338
353
  export declare function promisifyRequest(
339
354
  type: 'request' | 'uploadFile' | 'downloadFile',
340
- opts: RequestConfig
355
+ opts: NormalizeReqConfig
341
356
  ): Promise<UniAnyResult>;
342
357
 
343
358
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utils-uniapp",
3
- "version": "1.1.20",
3
+ "version": "1.1.21",
4
4
  "description": "aubyn's utils for uniapp",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",