utils-uniapp 1.1.8 → 1.1.9
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/cache/index.d.ts +15 -3
- package/lib/cache/index.js +1 -1
- package/lib/http/index.d.ts +77 -29
- package/package.json +1 -1
package/lib/cache/index.d.ts
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
type StorageMode = 'session' | 'local';
|
|
7
7
|
|
|
8
|
+
|
|
8
9
|
/**
|
|
9
10
|
* 底层存储适配器接口,抹平uni‑storage与sessionStorage API差异
|
|
10
11
|
*/
|
|
@@ -41,6 +42,7 @@ interface IStorageAdapter {
|
|
|
41
42
|
clear(): void;
|
|
42
43
|
}
|
|
43
44
|
|
|
45
|
+
|
|
44
46
|
/**
|
|
45
47
|
* StorageCache实例构造配置项
|
|
46
48
|
*/
|
|
@@ -51,11 +53,18 @@ interface StorageCacheOptions {
|
|
|
51
53
|
version?: string | number;
|
|
52
54
|
/** 实例隔离前缀,内部自动追加下划线分隔,默认空字符串 */
|
|
53
55
|
prefix?: string;
|
|
56
|
+
/**
|
|
57
|
+
* 实例默认过期秒数;set不传expire时使用该值
|
|
58
|
+
* <=0代表永不过期,默认0
|
|
59
|
+
*/
|
|
60
|
+
defaultExpire?: number;
|
|
54
61
|
}
|
|
55
62
|
|
|
63
|
+
|
|
56
64
|
/**
|
|
57
65
|
* 多端缓存工具类
|
|
58
66
|
* 特性:过期控制、版本校验、实例key隔离前缀;适配uni‑app、H5‑sessionStorage
|
|
67
|
+
* 优先级说明:set传入expire > 实例defaultExpire;仅不传参(undefined)才使用实例默认配置
|
|
59
68
|
* 注意:
|
|
60
69
|
* 1. 业务传入key为业务key,内部自动拼接实例prefix
|
|
61
70
|
* 2. clear仅删除本实例前缀key,无prefix时不执行删除
|
|
@@ -72,7 +81,7 @@ export declare class StorageCache {
|
|
|
72
81
|
* 设置缓存,支持过期
|
|
73
82
|
* @param key 业务缓存key
|
|
74
83
|
* @param data 缓存数据
|
|
75
|
-
* @param expire
|
|
84
|
+
* @param expire 过期时间,单位秒;不传使用实例defaultExpire;<=0永不过期
|
|
76
85
|
* @returns true成功,false异常失败
|
|
77
86
|
*/
|
|
78
87
|
set(key: string, data: any, expire?: number): boolean;
|
|
@@ -113,16 +122,19 @@ export declare class StorageCache {
|
|
|
113
122
|
removeByPrefix(prefix: string): boolean;
|
|
114
123
|
}
|
|
115
124
|
|
|
125
|
+
|
|
116
126
|
/**
|
|
117
|
-
* 全局默认local缓存实例,无实例隔离前缀,直接操作原始key
|
|
127
|
+
* 全局默认local缓存实例,无实例隔离前缀,直接操作原始key,默认永不过期
|
|
118
128
|
* @type {StorageCache}
|
|
119
129
|
*/
|
|
120
130
|
export declare const localCache: StorageCache;
|
|
121
131
|
|
|
132
|
+
|
|
122
133
|
/**
|
|
123
|
-
* 全局默认session缓存实例,无实例隔离前缀;小程序/App自动降级为local
|
|
134
|
+
* 全局默认session缓存实例,无实例隔离前缀;小程序/App自动降级为local,默认永不过期
|
|
124
135
|
* @type {StorageCache}
|
|
125
136
|
*/
|
|
126
137
|
export declare const sessionCache: StorageCache;
|
|
127
138
|
|
|
139
|
+
|
|
128
140
|
export default StorageCache;
|
package/lib/cache/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var p=(i,r,e)=>{if(!r.has(i))throw TypeError("Cannot "+e)};var n=(i,r,e)=>(p(i,r,"read from private field"),e?e.call(i):r.get(i)),l=(i,r,e)=>{if(r.has(i))throw TypeError("Cannot add the same private member more than once");r instanceof WeakSet?r.add(i):r.set(i,e)},y=(i,r,e,s)=>(p(i,r,"write to private field"),s?s.call(i,e):r.set(i,e),e);var h=(i,r,e)=>(p(i,r,"access private method"),e);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});var o,f,c,v,S,x,u,m;const w=class w{constructor(){l(this,u);l(this,o,void 0);l(this,f,void 0);l(this,c,void 0);l(this,v,void 0);var g;let r=arguments.length>0&&arguments[0]!==void 0?arguments[0]:{};const{mode:e="local",version:s="v1",prefix:t="",defaultExpire:a=0}=r;y(this,o,h(g=w,S,x).call(g,e)),y(this,f,String(s)),y(this,c,t?t+"_":""),y(this,v,Number(a)),Object.seal(this)}set(r,e,s){try{const t=s!==void 0?s:n(this,v);let a=Number(t);Number.isFinite(a)||(a=0);const g=a>0?Math.round(a)*1e3:a,I=h(this,u,m).call(this,r);return n(this,o).set(I,{time:Date.now(),expire:g,version:n(this,f),data:e}),!0}catch(t){return console.error(`设置缓存[${r}]失败:`,t),!1}}get(r){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:null;try{const s=h(this,u,m).call(this,r),t=n(this,o).get(s);return!t||typeof t!="object"||Array.isArray(t)||!("time"in t)||!("expire"in t)||!("version"in t)?(t&&n(this,o).remove(s),e):t.version!==n(this,f)?(n(this,o).remove(s),e):t.expire>0&&Date.now()-t.time>=t.expire?(n(this,o).remove(s),e):t.data}catch(s){return console.error(`获取缓存[${r}]失败:`,s),e}}renew(r){try{const e=h(this,u,m).call(this,r),s=n(this,o).get(e);if(!s||typeof s!="object"||Array.isArray(s)||!("time"in s)||!("expire"in s)||!("version"in s))return!1;if(s.version!==n(this,f))return n(this,o).remove(e),!1;if(s.expire>0){if(Date.now()-s.time>=s.expire)return n(this,o).remove(e),!1;s.time=Date.now(),n(this,o).set(e,s)}return!0}catch(e){return console.error(`续期缓存[${r}]失败:`,e),!1}}remove(r){try{const e=h(this,u,m).call(this,r);return n(this,o).remove(e),!0}catch(e){return console.error(`删除缓存[${r}]失败:`,e),!1}}clear(){try{return n(this,o).keys().forEach(e=>{n(this,c)&&e.startsWith(n(this,c))&&n(this,o).remove(e)}),!0}catch(r){return console.error("清空实例缓存失败:",r),!1}}removeByPrefix(r){if(!r)return!1;if(!n(this,c))return console.warn("StorageCache: 当前实例无隔离前缀,禁止调用removeByPrefix,避免误删"),!1;try{const e=h(this,u,m).call(this,r);return n(this,o).keys().forEach(t=>{t.startsWith(e)&&n(this,o).remove(t)}),!0}catch(e){return console.error(`按前缀删除缓存[${r}]失败:`,e),!1}}};o=new WeakMap,f=new WeakMap,c=new WeakMap,v=new WeakMap,S=new WeakSet,x=function(){let r=arguments.length>0&&arguments[0]!==void 0?arguments[0]:"local";const e=typeof window<"u"&&window.sessionStorage,s={set(t,a){uni.setStorageSync(t,a)},get(t){return uni.getStorageSync(t)},remove(t){uni.removeStorageSync(t)},keys(){try{return uni.getStorageInfoSync().keys||[]}catch(t){return console.error("uni getStorageInfoSync error",t),[]}},clear(){uni.clearStorageSync()}};return e&&r==="session"?{set(t,a){try{window.sessionStorage.setItem(t,JSON.stringify(a))}catch(g){console.error("sessionStorage set error",g)}},get(t){const a=window.sessionStorage.getItem(t);if(a===null)return null;try{return JSON.parse(a)}catch{return window.sessionStorage.removeItem(t),null}},remove(t){window.sessionStorage.removeItem(t)},keys(){return Object.keys(window.sessionStorage)},clear(){window.sessionStorage.clear()}}:s},u=new WeakSet,m=function(r){return n(this,c)+r},l(w,S);let d=w;const b=new d,K=new d({mode:"session"});exports.StorageCache=d;exports.default=d;exports.localCache=b;exports.sessionCache=K;
|
package/lib/http/index.d.ts
CHANGED
|
@@ -21,39 +21,86 @@ type UniUploadResult = UniApp.UploadFileSuccessCallbackResult;
|
|
|
21
21
|
type UniDownloadResult = UniApp.DownloadFileSuccessCallbackResult;
|
|
22
22
|
type UniAnyResult = UniRequestResult | UniUploadResult | UniDownloadResult;
|
|
23
23
|
|
|
24
|
+
/**
|
|
25
|
+
* 请求失败载荷
|
|
26
|
+
*/
|
|
24
27
|
export type RequestFailPayload = {
|
|
25
28
|
type: 'fail';
|
|
26
29
|
res: UniApp.GeneralCallbackResult;
|
|
27
30
|
};
|
|
28
31
|
|
|
32
|
+
/**
|
|
33
|
+
* 缓存适配器抽象接口,对接外部缓存实现(如前面StorageCache)
|
|
34
|
+
*/
|
|
29
35
|
export interface CacheAdapter {
|
|
36
|
+
/**
|
|
37
|
+
* 获取缓存
|
|
38
|
+
* @param key 缓存key
|
|
39
|
+
* @returns 缓存值,不存在返回undefined
|
|
40
|
+
*/
|
|
30
41
|
get(key: string): unknown;
|
|
42
|
+
/**
|
|
43
|
+
* 设置缓存
|
|
44
|
+
* @param key 缓存key
|
|
45
|
+
* @param value 缓存存储内容
|
|
46
|
+
* @param expire 过期秒数,<=0永不过期
|
|
47
|
+
*/
|
|
31
48
|
set(key: string, value: unknown, expire: number): void;
|
|
32
49
|
}
|
|
33
50
|
|
|
34
51
|
/**
|
|
35
|
-
*
|
|
52
|
+
* 外部传入缓存配置入参(仅调用方使用,业务传入)
|
|
53
|
+
* @description 开启请求缓存 isCache=true 时,必须传入对应 mode 的适配器 localAdapter / sessionAdapter
|
|
36
54
|
*/
|
|
37
55
|
export interface CacheConfigInput {
|
|
56
|
+
/**
|
|
57
|
+
* 自定义缓存业务key;不填默认由url生成,**不包含请求参数**,同URL不同参数务必手动指定key
|
|
58
|
+
*/
|
|
38
59
|
key?: string;
|
|
60
|
+
/**
|
|
61
|
+
* 缓存key前缀,用于业务隔离,会拼接在业务key前面
|
|
62
|
+
*/
|
|
39
63
|
prefix?: string;
|
|
64
|
+
/**
|
|
65
|
+
* 缓存存储模式
|
|
66
|
+
* - local:持久存储
|
|
67
|
+
* - session:会话存储(H5会话销毁;小程序降级持久)
|
|
68
|
+
*/
|
|
40
69
|
mode?: 'local' | 'session';
|
|
70
|
+
/**
|
|
71
|
+
* 缓存过期时间,单位秒;<=0代表永不过期,默认1小时过期
|
|
72
|
+
*/
|
|
41
73
|
expire?: number;
|
|
74
|
+
/**
|
|
75
|
+
* local模式对应的缓存适配器实例;mode=local且开启缓存时必填
|
|
76
|
+
*/
|
|
42
77
|
localAdapter?: CacheAdapter | null;
|
|
78
|
+
/**
|
|
79
|
+
* session模式对应的缓存适配器实例;mode=session且开启缓存时必填
|
|
80
|
+
*/
|
|
43
81
|
sessionAdapter?: CacheAdapter | null;
|
|
44
82
|
}
|
|
45
83
|
|
|
46
84
|
/**
|
|
47
85
|
* normalizeOpts 之后内部运行时缓存配置对象
|
|
86
|
+
* @description adapter 字段由内部派生生成,外部禁止传入、禁止读取使用
|
|
48
87
|
*/
|
|
49
88
|
export interface CacheConfig {
|
|
50
|
-
/**
|
|
89
|
+
/**
|
|
90
|
+
* 缓存key;默认仅由url生成,**不包含请求参数**,同URL不同参数需手动指定key
|
|
91
|
+
*/
|
|
51
92
|
key: string;
|
|
52
|
-
/**
|
|
93
|
+
/**
|
|
94
|
+
* 缓存key前缀
|
|
95
|
+
*/
|
|
53
96
|
prefix: string;
|
|
54
|
-
/**
|
|
97
|
+
/**
|
|
98
|
+
* 缓存模式
|
|
99
|
+
*/
|
|
55
100
|
mode: 'local' | 'session';
|
|
56
|
-
/**
|
|
101
|
+
/**
|
|
102
|
+
* 过期时间,单位秒;<=0代表永不过期,默认1小时过期
|
|
103
|
+
*/
|
|
57
104
|
expire: number;
|
|
58
105
|
/**
|
|
59
106
|
* ⚠️ adapter 为内部运行时派生字段,**外部禁止传入、禁止访问**;
|
|
@@ -99,23 +146,23 @@ export type UserErrInterceptor = ErrInterceptor | ErrInterceptor[];
|
|
|
99
146
|
* normalizeOpts标准化之后的请求额外配置对象【内部运行时】
|
|
100
147
|
*/
|
|
101
148
|
export interface RequestOptions {
|
|
102
|
-
/**
|
|
149
|
+
/** 是否使用url‑encoded表单编码 */
|
|
103
150
|
qs: boolean;
|
|
104
|
-
/**
|
|
151
|
+
/** 是否展示错误toast提示 */
|
|
105
152
|
showError: boolean;
|
|
106
|
-
/**
|
|
153
|
+
/** 是否开启请求缓存;开启时cacheConfig必须提供对应mode的适配器 */
|
|
107
154
|
isCache: boolean;
|
|
108
155
|
/** 缓存配置;adapter为内部运行时派生字段,外部请勿传入、请勿访问;开启isCache:true,必须传入对应模式的适配器,否则运行时抛出异常 */
|
|
109
156
|
cacheConfig: CacheConfig;
|
|
110
157
|
/** loading提示文字 */
|
|
111
158
|
loadingText: string;
|
|
112
|
-
/**
|
|
159
|
+
/** 是否开启全局loading弹窗 */
|
|
113
160
|
loading: boolean;
|
|
114
|
-
/**
|
|
161
|
+
/** 请求前置拦截器(已合并为单个函数) */
|
|
115
162
|
reqInterceptor: ReqInterceptor;
|
|
116
|
-
/**
|
|
163
|
+
/** 响应拦截器(已合并为单个函数) */
|
|
117
164
|
resInterceptor: ResInterceptor;
|
|
118
|
-
/**
|
|
165
|
+
/** 错误拦截器(已合并为单个函数) */
|
|
119
166
|
errInterceptor: ErrInterceptor;
|
|
120
167
|
/** 允许扩展自定义字段 */
|
|
121
168
|
[key: string]: unknown;
|
|
@@ -127,17 +174,17 @@ export interface RequestOptions {
|
|
|
127
174
|
* @description isCache 设置为 true 时,cacheConfig 需要传入对应 mode 的适配器 localAdapter / sessionAdapter,否则运行时抛出异常
|
|
128
175
|
*/
|
|
129
176
|
export interface UserRequestOpts {
|
|
130
|
-
/**
|
|
177
|
+
/** 是否使用url‑encoded表单编码 */
|
|
131
178
|
qs?: boolean;
|
|
132
|
-
/**
|
|
179
|
+
/** 是否展示错误toast提示 */
|
|
133
180
|
showError?: boolean;
|
|
134
|
-
/**
|
|
181
|
+
/** 是否开启请求缓存;开启时cacheConfig必须提供对应mode的适配器(localAdapter/sessionAdapter) */
|
|
135
182
|
isCache?: boolean;
|
|
136
183
|
/** 缓存配置,用户传入部分字段即可;isCache=true必须传入对应mode适配器 */
|
|
137
184
|
cacheConfig?: Partial<CacheConfigInput>;
|
|
138
185
|
/** loading提示文字 */
|
|
139
186
|
loadingText?: string;
|
|
140
|
-
/**
|
|
187
|
+
/** 是否开启全局loading弹窗 */
|
|
141
188
|
loading?: boolean;
|
|
142
189
|
/** 请求前置拦截器,支持单个函数或函数数组 */
|
|
143
190
|
reqInterceptor?: UserReqInterceptor;
|
|
@@ -154,6 +201,7 @@ export interface UserRequestOpts {
|
|
|
154
201
|
* 注意:当前实现token由请求内部source生成,**不支持外部传入自定义cancelToken**
|
|
155
202
|
*/
|
|
156
203
|
export declare const CancelToken: {
|
|
204
|
+
/** 保存token与uni任务映射 */
|
|
157
205
|
caches: Record<string, UniTask>;
|
|
158
206
|
|
|
159
207
|
/**
|
|
@@ -187,7 +235,6 @@ export declare const CancelToken: {
|
|
|
187
235
|
source(): { token: string; cancel: () => void };
|
|
188
236
|
};
|
|
189
237
|
|
|
190
|
-
|
|
191
238
|
/**
|
|
192
239
|
* 包装uni异步API为Promise
|
|
193
240
|
* @param type uni接口名称 request | uploadFile | downloadFile
|
|
@@ -198,24 +245,24 @@ export declare const CancelToken: {
|
|
|
198
245
|
export declare function promisifyRequest<T = UniAnyResult>(type: 'request' | 'uploadFile' | 'downloadFile', opts: Record<string, unknown>): Promise<T>;
|
|
199
246
|
|
|
200
247
|
/**
|
|
201
|
-
*
|
|
202
|
-
* @param
|
|
248
|
+
* 底层直接展示loading,不做计数
|
|
249
|
+
* @param title loading提示文字
|
|
203
250
|
*/
|
|
204
251
|
export declare function showLoading(title: string): void;
|
|
205
252
|
|
|
206
253
|
/**
|
|
207
|
-
*
|
|
254
|
+
* 底层直接隐藏loading,不做计数
|
|
208
255
|
*/
|
|
209
256
|
export declare function hideLoading(): void;
|
|
210
257
|
|
|
211
258
|
/**
|
|
212
|
-
* 开启loading计数,计数>0时展示loading
|
|
259
|
+
* 开启loading计数,计数>0时展示loading;多次调用会叠加计数
|
|
213
260
|
* @param [title='加载中...'] loading提示文案
|
|
214
261
|
*/
|
|
215
262
|
export declare function startLoading(title?: string): void;
|
|
216
263
|
|
|
217
264
|
/**
|
|
218
|
-
* 关闭loading计数,计数归0
|
|
265
|
+
* 关闭loading计数,计数归0则真正隐藏loading弹窗
|
|
219
266
|
*/
|
|
220
267
|
export declare function endLoading(): void;
|
|
221
268
|
|
|
@@ -224,12 +271,13 @@ export declare function endLoading(): void;
|
|
|
224
271
|
*/
|
|
225
272
|
export interface RequestInstance {
|
|
226
273
|
/**
|
|
274
|
+
* 基础请求调用
|
|
227
275
|
* @param url 请求地址
|
|
228
|
-
* @param method get/post等
|
|
276
|
+
* @param method http方法 get/post/put/delete等
|
|
229
277
|
* @param params 请求参数
|
|
230
|
-
* @param [config]
|
|
231
|
-
* @param [options]
|
|
232
|
-
* @returns
|
|
278
|
+
* @param [config] 当前请求配置,覆盖实例defaultConfig(baseURL、headers等)
|
|
279
|
+
* @param [options] 当前请求扩展选项,覆盖实例defaultOpts(loading、缓存、拦截器)
|
|
280
|
+
* @returns Promise业务响应数据
|
|
233
281
|
*/
|
|
234
282
|
(url: string, method: string, params?: unknown, config?: Record<string, unknown>, options?: UserRequestOpts): Promise<unknown>;
|
|
235
283
|
|
|
@@ -256,7 +304,7 @@ export interface RequestInstance {
|
|
|
256
304
|
* 创建常规http请求实例(get/post)
|
|
257
305
|
* @param defaultConfig 实例默认配置(baseURL、headers等)
|
|
258
306
|
* @param defaultOpts 实例默认扩展选项(loading、缓存、拦截器)
|
|
259
|
-
* @returns
|
|
307
|
+
* @returns RequestInstance 请求实例
|
|
260
308
|
*/
|
|
261
309
|
export declare function createRequest(defaultConfig: Record<string, unknown>, defaultOpts?: UserRequestOpts): RequestInstance;
|
|
262
310
|
|
|
@@ -264,7 +312,7 @@ export declare function createRequest(defaultConfig: Record<string, unknown>, de
|
|
|
264
312
|
* 创建上传文件实例
|
|
265
313
|
* @param defaultConfig 实例默认配置(baseURL、headers等)
|
|
266
314
|
* @param defaultOpts 实例默认扩展选项
|
|
267
|
-
* @returns
|
|
315
|
+
* @returns 上传调用函数
|
|
268
316
|
*/
|
|
269
317
|
export declare function createUploadFile(
|
|
270
318
|
defaultConfig: Record<string, unknown>,
|
|
@@ -275,7 +323,7 @@ export declare function createUploadFile(
|
|
|
275
323
|
* 创建下载文件实例
|
|
276
324
|
* @param defaultConfig 实例默认配置(baseURL、headers等)
|
|
277
325
|
* @param defaultOpts 实例默认扩展选项
|
|
278
|
-
* @returns
|
|
326
|
+
* @returns 下载调用函数
|
|
279
327
|
*/
|
|
280
328
|
export declare function createDownloadFile(
|
|
281
329
|
defaultConfig: Record<string, unknown>,
|