utils-lib-js 1.0.7 → 1.0.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.
- package/README.en.md +302 -1
- package/README.md +302 -1
- package/dist/common/request.d.ts +8 -8
- package/dist/common/request.js +0 -21
- package/dist/common/types.d.ts +1 -1
- package/dist/esm/request.d.ts +8 -8
- package/dist/esm/request.js +0 -21
- package/dist/esm/types.d.ts +1 -1
- package/package.json +1 -1
package/README.en.md
CHANGED
|
@@ -26,4 +26,305 @@ JavaScript工具函数,封装的一些常用的js函数
|
|
|
26
26
|
|
|
27
27
|
#### 功能
|
|
28
28
|
接口:
|
|
29
|
-
|
|
29
|
+
|
|
30
|
+
export type IKey = string | symbol | number
|
|
31
|
+
// 对象类型
|
|
32
|
+
export interface IObject<T> {
|
|
33
|
+
[key: IKey]: T | IObject<any>
|
|
34
|
+
}
|
|
35
|
+
export interface IPromise extends IObject<any> {
|
|
36
|
+
promise: Promise<void>
|
|
37
|
+
resolve: (res: any) => unknown
|
|
38
|
+
reject: (err: any) => unknown
|
|
39
|
+
}
|
|
40
|
+
export type IInstance<T> = {
|
|
41
|
+
_instance: Function
|
|
42
|
+
} & T
|
|
43
|
+
export type IDemoteArray<T> = Array<IDemoteArray<T> | T>
|
|
44
|
+
|
|
45
|
+
// base
|
|
46
|
+
/**产生区间随机数
|
|
47
|
+
* @param {number} min 最小区间
|
|
48
|
+
* @param {number} max 最大区间
|
|
49
|
+
* @param {boolean} bool 包含最大值
|
|
50
|
+
* @return {number} 随机数
|
|
51
|
+
**/
|
|
52
|
+
export type IRandomNum = (min: number, max: number, bool?: boolean) => number
|
|
53
|
+
|
|
54
|
+
/**获取url的参数
|
|
55
|
+
* @param {string} url 待截取的地址
|
|
56
|
+
* @return {object} 参数对象
|
|
57
|
+
**/
|
|
58
|
+
export type IUrlSplit = (url: string) => IObject<any>
|
|
59
|
+
|
|
60
|
+
/**添加url的参数
|
|
61
|
+
* @param {string} url 待添加参数的地址
|
|
62
|
+
* @param {object} query 待添加的参数
|
|
63
|
+
* @return {string} 添加参数后的url
|
|
64
|
+
**/
|
|
65
|
+
export type IUrlJoin = (url: string, query: object) => string
|
|
66
|
+
|
|
67
|
+
/**获取数据类型
|
|
68
|
+
* @param {any} data 待检测数据
|
|
69
|
+
* @return {string} 数据类型
|
|
70
|
+
**/
|
|
71
|
+
export type IGetType<T> = (data: any) => typeof data | T[keyof T] | "null";
|
|
72
|
+
|
|
73
|
+
/**批量判断数据类型
|
|
74
|
+
* @param {any} data 待检测数据
|
|
75
|
+
* @param {any} whiteList 数据类型名单
|
|
76
|
+
* @return {boolean} 是否在白名单中
|
|
77
|
+
**/
|
|
78
|
+
export type IGetTypeByList = (data: any, whiteList: string[]) => boolean;
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
// object
|
|
82
|
+
/**lodash中的 _.get(),获取对象某级属性
|
|
83
|
+
* @param {IObject} object 目标对象
|
|
84
|
+
* @param {string} key 对象层级
|
|
85
|
+
* @param {any} defaultValue 未取得时默认值
|
|
86
|
+
* @return {IObject[IKey]} 对象某个属性
|
|
87
|
+
**/
|
|
88
|
+
export type IGetValue = <T, U = IObject<T> | IObject<T>[IKey]>(object: U, key: string, defaultValue?: any) => U
|
|
89
|
+
|
|
90
|
+
/**lodash中的 _.set(),赋值对象某级属性
|
|
91
|
+
* @param {IObject} object 目标对象
|
|
92
|
+
* @param {string} key 对象层级
|
|
93
|
+
* @param {any} value 需要赋的值
|
|
94
|
+
* @return {IObject} 目标对象
|
|
95
|
+
**/
|
|
96
|
+
export type ISetValue = <T>(object: IObject<T>, key: string, value?: any) => IObject<T>
|
|
97
|
+
|
|
98
|
+
/**对象混入
|
|
99
|
+
* @param {IObject} target 目标对象
|
|
100
|
+
* @param {string} source 需要混入的对象集合
|
|
101
|
+
* @param {boolean} overwrite 是否重写覆盖原有属性
|
|
102
|
+
* @return {IObject} 目标对象
|
|
103
|
+
**/
|
|
104
|
+
export type IMixIn = <U extends IObject<any>>(target?: U, source?: IObject<any>, overwrite?: boolean) => U
|
|
105
|
+
|
|
106
|
+
/**枚举值反向映射
|
|
107
|
+
* @param {IObject<string>} target 目标对象
|
|
108
|
+
* @return {IObject<string>} 目标对象
|
|
109
|
+
**/
|
|
110
|
+
export type IEnumInversion = (target: IObject<string>) => IObject<string>
|
|
111
|
+
|
|
112
|
+
/**对象复制
|
|
113
|
+
* @param {IObject<any>} target 目标对象
|
|
114
|
+
* @return {IObject} 目标对象
|
|
115
|
+
**/
|
|
116
|
+
export type ICloneDeep = (target?: any) => any
|
|
117
|
+
|
|
118
|
+
/**生成 对象 类型的初始值
|
|
119
|
+
* @param {string} type 数据类型
|
|
120
|
+
* @param {any} __init 初始值
|
|
121
|
+
* @return {any} 目标对象
|
|
122
|
+
**/
|
|
123
|
+
export type ICreateObjectVariable = (type: string, source?: any) => any
|
|
124
|
+
|
|
125
|
+
/**Object.create 根据源对象产出新对象
|
|
126
|
+
* @param {Function|Object} source 源对象
|
|
127
|
+
* @return {Function|Object} 对象产物
|
|
128
|
+
**/
|
|
129
|
+
export type ICreateObject = <T, U extends T>(source: T) => U
|
|
130
|
+
|
|
131
|
+
/**类的继承
|
|
132
|
+
* @param {Function} source 源对象
|
|
133
|
+
* @return {Function} 继承产物
|
|
134
|
+
**/
|
|
135
|
+
export type IInherit = <T extends Function>(source: T, target?: Function) => Function
|
|
136
|
+
|
|
137
|
+
/**生成类的实例单例
|
|
138
|
+
* @param {Function} classProto 类
|
|
139
|
+
* @param {Boolean} overwrite 是否覆盖已有单例
|
|
140
|
+
* @param {any[]} params 构造函数的参数
|
|
141
|
+
* @return {IObject} 实例化的单例
|
|
142
|
+
**/
|
|
143
|
+
export type IGetInstance = (classProto: IInstance<FunctionConstructor>, overwrite?: boolean, ...params: any[]) => Function
|
|
144
|
+
|
|
145
|
+
/**通过装饰器将属性混入类中
|
|
146
|
+
* @param {IObject<any>} params 混入的属性
|
|
147
|
+
* @return {ClassDecorator} 装饰器钩子函数
|
|
148
|
+
**/
|
|
149
|
+
export type IClassDecorator = (params: IObject<any>) => <TFunction extends Function>(target: TFunction) => void
|
|
150
|
+
|
|
151
|
+
/**JSON.parse封装
|
|
152
|
+
* @param {string} target 字符串
|
|
153
|
+
* @return {IObject<any>} 对象
|
|
154
|
+
**/
|
|
155
|
+
export type IStringToJson = (target: string) => IObject<any>
|
|
156
|
+
|
|
157
|
+
/**JSON.stringify封装
|
|
158
|
+
* @param {IObject<any>} target 对象
|
|
159
|
+
* @return {string} 字符串
|
|
160
|
+
**/
|
|
161
|
+
export type IJsonToString = (target: IObject<any>) => string
|
|
162
|
+
|
|
163
|
+
// function
|
|
164
|
+
/**节流(throttle):高频事件触发,但在 n 秒内只会执行一次
|
|
165
|
+
* @param {Function} fn 节流处理的函数
|
|
166
|
+
* @param {number} time 执行间隔/毫秒
|
|
167
|
+
* @return {Function} 处理后的函数
|
|
168
|
+
**/
|
|
169
|
+
export type IThrottle = (fn: Function, time: number) => (...args: any[]) => void
|
|
170
|
+
|
|
171
|
+
/**防抖(debounce):触发高频事件后 n 秒内函数只会执行一次
|
|
172
|
+
* @param {Function} fn 防抖处理的函数
|
|
173
|
+
* @param {number} time 允许运行函数间隔/毫秒
|
|
174
|
+
* @return {Function} 处理后的函数
|
|
175
|
+
**/
|
|
176
|
+
export type IDebounce = (fn: Function, time: number) => (...args: any[]) => void
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Promise扁平化,避免Promise嵌套
|
|
180
|
+
* @returns {Promise,resolve,reject}
|
|
181
|
+
*/
|
|
182
|
+
export type IDefer = () => IPromise
|
|
183
|
+
|
|
184
|
+
/**await与try catch 捕获异常处理方法
|
|
185
|
+
* @param {Promise<any>} defer 延迟函数
|
|
186
|
+
* @returns {Promise<any>} [error, result]
|
|
187
|
+
*/
|
|
188
|
+
export type ICatchAwait<T extends Promise<any>> = (defer: T) => T
|
|
189
|
+
|
|
190
|
+
// array
|
|
191
|
+
/**数组乱序
|
|
192
|
+
* @param {Array<any>} arr 目标数组
|
|
193
|
+
* @returns {Array<any>} 乱序后的数组
|
|
194
|
+
*/
|
|
195
|
+
export type IArrayRandom<T extends any[]> = (arr: T) => T
|
|
196
|
+
|
|
197
|
+
/**数组数组去重
|
|
198
|
+
* @param {Array<any>} arr 目标数组
|
|
199
|
+
* @returns {Array<any>} 去重后的数组
|
|
200
|
+
*/
|
|
201
|
+
export type IArrayUniq<T extends any[]> = (arr: T) => T
|
|
202
|
+
|
|
203
|
+
/**数组扁平化
|
|
204
|
+
* @param {Array<any>} arr 目标数组
|
|
205
|
+
* @returns {Array<any>} 扁平化的数组
|
|
206
|
+
*/
|
|
207
|
+
|
|
208
|
+
export type IArrayDemote<T extends IDemoteArray<any>> = (arr: T, result?: T) => T
|
|
209
|
+
|
|
210
|
+
// element
|
|
211
|
+
/**IElementParams
|
|
212
|
+
* @param {string} ele 标签类型
|
|
213
|
+
* @param {CSSStyleDeclaration} style 样式
|
|
214
|
+
* @param {Attr} attr 属性
|
|
215
|
+
* @param {object} parent 父元素
|
|
216
|
+
*/
|
|
217
|
+
interface IElementParams<T> {
|
|
218
|
+
ele: T | string
|
|
219
|
+
style: CSSStyleDeclaration
|
|
220
|
+
attr: Attr
|
|
221
|
+
parent: T
|
|
222
|
+
}
|
|
223
|
+
/**新增标签,设置属性及样式
|
|
224
|
+
* @param {IElementParams} params 配置
|
|
225
|
+
* @return {ElementObject} 生成的标签
|
|
226
|
+
*/
|
|
227
|
+
export type ICreateElement<T = HTMLElement> = (params: IElementParams<T>) => T
|
|
228
|
+
|
|
229
|
+
// event
|
|
230
|
+
/**浏览器事件
|
|
231
|
+
* @param {Document} ele 标签
|
|
232
|
+
* @param {string} type 事件类型
|
|
233
|
+
* @param {(e: Event) => void} handler 事件回调
|
|
234
|
+
* @return {void}
|
|
235
|
+
*/
|
|
236
|
+
export type IAddHandler = <T extends Document>(ele: T, type: string, handler: (e: Event) => void) => void
|
|
237
|
+
|
|
238
|
+
/**取消事件冒泡
|
|
239
|
+
* @param {Event} e 浏览器事件对象
|
|
240
|
+
* @return {void}
|
|
241
|
+
*/
|
|
242
|
+
export type IStopBubble = (e: Event) => void
|
|
243
|
+
|
|
244
|
+
/**取消默认事件
|
|
245
|
+
* @param {Event} e 浏览器事件对象
|
|
246
|
+
* @return {void}
|
|
247
|
+
*/
|
|
248
|
+
export type IStopDefault = (e: Event) => void
|
|
249
|
+
|
|
250
|
+
/**取消浏览器事件
|
|
251
|
+
* @param {Document} ele 标签
|
|
252
|
+
* @param {string} type 事件类型
|
|
253
|
+
* @param {(e: Event) => void} handler 事件回调
|
|
254
|
+
* @return {void}
|
|
255
|
+
*/
|
|
256
|
+
export type IRemoveHandler = <T extends Document>(ele: T, type: string, handler: (e: Event) => void) => void
|
|
257
|
+
|
|
258
|
+
/**取消默认事件
|
|
259
|
+
* @param {Event} e 浏览器事件对象
|
|
260
|
+
* @return {void}
|
|
261
|
+
*/
|
|
262
|
+
export type IDispatchEvent = <T extends Document>(ele: T, data: any) => void
|
|
263
|
+
|
|
264
|
+
// request
|
|
265
|
+
|
|
266
|
+
export type IRequestParams<T> = T | IObject<any> | null
|
|
267
|
+
// 请求路径
|
|
268
|
+
export type IUrl = string
|
|
269
|
+
// 环境判断
|
|
270
|
+
export type IEnv = 'Window' | 'Node'
|
|
271
|
+
// fetch返回取值方式
|
|
272
|
+
export type IDataType = "text" | "json" | "blob" | "formData" | "arrayBuffer"
|
|
273
|
+
// 请求方式
|
|
274
|
+
export type IRequestMethods = "GET" | "POST" | "DELETE" | "PUT" | "OPTION"
|
|
275
|
+
// body结构
|
|
276
|
+
export type IRequestBody = IRequestParams<BodyInit>
|
|
277
|
+
// heads结构
|
|
278
|
+
export type IRequestHeaders = IRequestParams<HeadersInit>
|
|
279
|
+
// 请求基础函数
|
|
280
|
+
export type IRequestBaseFn = (url: IUrl, opts: IRequestOptions) => Promise<any>
|
|
281
|
+
// 请求函数体
|
|
282
|
+
export type IRequestFn = (url?: IUrl, query?: IObject<any>, body?: IRequestBody, opts?: IRequestOptions) => Promise<any>
|
|
283
|
+
// 请求参数
|
|
284
|
+
export type IRequestOptions = {
|
|
285
|
+
method?: IRequestMethods
|
|
286
|
+
query?: IRequestParams<IObject<any>>
|
|
287
|
+
body?: IRequestBody
|
|
288
|
+
headers?: IRequestHeaders
|
|
289
|
+
controller?: AbortController
|
|
290
|
+
timeout?: number
|
|
291
|
+
timer?: number | unknown | null
|
|
292
|
+
[key: string]: any
|
|
293
|
+
}
|
|
294
|
+
// 拦截器
|
|
295
|
+
export type IInterceptors = {
|
|
296
|
+
use(type: "request" | "response" | "error", fn: Function): void
|
|
297
|
+
get reqFn(): Function
|
|
298
|
+
get resFn(): Function
|
|
299
|
+
get errFn(): Function
|
|
300
|
+
}
|
|
301
|
+
// 公共函数
|
|
302
|
+
export type IRequestBase = {
|
|
303
|
+
readonly origin: string
|
|
304
|
+
chackUrl: (url: IUrl) => boolean
|
|
305
|
+
envDesc: () => IEnv
|
|
306
|
+
errorFn: <Err = any, R = Function>(reject: R) => (err: Err) => R
|
|
307
|
+
clearTimer: (opts: IRequestOptions) => void
|
|
308
|
+
initAbort: <T = IRequestOptions>(opts: T) => T
|
|
309
|
+
requestType: () => IRequestBaseFn
|
|
310
|
+
fixOrigin: (fixStr: string) => string
|
|
311
|
+
fetch: IRequestBaseFn
|
|
312
|
+
http: IRequestBaseFn
|
|
313
|
+
getDataByType: (type: IDataType, response: Response) => Promise<any>
|
|
314
|
+
}
|
|
315
|
+
// 初始化参数
|
|
316
|
+
export type IRequestInit = {
|
|
317
|
+
initDefaultParams: (url: IUrl, opts: IRequestOptions) => any
|
|
318
|
+
initFetchParams: (url: IUrl, opts: IRequestOptions) => any
|
|
319
|
+
initHttpParams: (url: IUrl, opts: IRequestOptions) => any
|
|
320
|
+
}
|
|
321
|
+
// 请求主体类
|
|
322
|
+
export type IRequest = {
|
|
323
|
+
GET: IRequestFn
|
|
324
|
+
POST: IRequestFn
|
|
325
|
+
DELETE: IRequestFn
|
|
326
|
+
PUT: IRequestFn
|
|
327
|
+
OPTIONS: IRequestFn
|
|
328
|
+
HEAD: IRequestFn
|
|
329
|
+
PATCH: IRequestFn
|
|
330
|
+
} & IRequestBase
|
package/README.md
CHANGED
|
@@ -26,4 +26,305 @@ JavaScript工具函数,封装的一些常用的js函数
|
|
|
26
26
|
|
|
27
27
|
#### 功能
|
|
28
28
|
接口:
|
|
29
|
-
|
|
29
|
+
|
|
30
|
+
export type IKey = string | symbol | number
|
|
31
|
+
// 对象类型
|
|
32
|
+
export interface IObject<T> {
|
|
33
|
+
[key: IKey]: T | IObject<any>
|
|
34
|
+
}
|
|
35
|
+
export interface IPromise extends IObject<any> {
|
|
36
|
+
promise: Promise<void>
|
|
37
|
+
resolve: (res: any) => unknown
|
|
38
|
+
reject: (err: any) => unknown
|
|
39
|
+
}
|
|
40
|
+
export type IInstance<T> = {
|
|
41
|
+
_instance: Function
|
|
42
|
+
} & T
|
|
43
|
+
export type IDemoteArray<T> = Array<IDemoteArray<T> | T>
|
|
44
|
+
|
|
45
|
+
// base
|
|
46
|
+
/**产生区间随机数
|
|
47
|
+
* @param {number} min 最小区间
|
|
48
|
+
* @param {number} max 最大区间
|
|
49
|
+
* @param {boolean} bool 包含最大值
|
|
50
|
+
* @return {number} 随机数
|
|
51
|
+
**/
|
|
52
|
+
export type IRandomNum = (min: number, max: number, bool?: boolean) => number
|
|
53
|
+
|
|
54
|
+
/**获取url的参数
|
|
55
|
+
* @param {string} url 待截取的地址
|
|
56
|
+
* @return {object} 参数对象
|
|
57
|
+
**/
|
|
58
|
+
export type IUrlSplit = (url: string) => IObject<any>
|
|
59
|
+
|
|
60
|
+
/**添加url的参数
|
|
61
|
+
* @param {string} url 待添加参数的地址
|
|
62
|
+
* @param {object} query 待添加的参数
|
|
63
|
+
* @return {string} 添加参数后的url
|
|
64
|
+
**/
|
|
65
|
+
export type IUrlJoin = (url: string, query: object) => string
|
|
66
|
+
|
|
67
|
+
/**获取数据类型
|
|
68
|
+
* @param {any} data 待检测数据
|
|
69
|
+
* @return {string} 数据类型
|
|
70
|
+
**/
|
|
71
|
+
export type IGetType<T> = (data: any) => typeof data | T[keyof T] | "null";
|
|
72
|
+
|
|
73
|
+
/**批量判断数据类型
|
|
74
|
+
* @param {any} data 待检测数据
|
|
75
|
+
* @param {any} whiteList 数据类型名单
|
|
76
|
+
* @return {boolean} 是否在白名单中
|
|
77
|
+
**/
|
|
78
|
+
export type IGetTypeByList = (data: any, whiteList: string[]) => boolean;
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
// object
|
|
82
|
+
/**lodash中的 _.get(),获取对象某级属性
|
|
83
|
+
* @param {IObject} object 目标对象
|
|
84
|
+
* @param {string} key 对象层级
|
|
85
|
+
* @param {any} defaultValue 未取得时默认值
|
|
86
|
+
* @return {IObject[IKey]} 对象某个属性
|
|
87
|
+
**/
|
|
88
|
+
export type IGetValue = <T, U = IObject<T> | IObject<T>[IKey]>(object: U, key: string, defaultValue?: any) => U
|
|
89
|
+
|
|
90
|
+
/**lodash中的 _.set(),赋值对象某级属性
|
|
91
|
+
* @param {IObject} object 目标对象
|
|
92
|
+
* @param {string} key 对象层级
|
|
93
|
+
* @param {any} value 需要赋的值
|
|
94
|
+
* @return {IObject} 目标对象
|
|
95
|
+
**/
|
|
96
|
+
export type ISetValue = <T>(object: IObject<T>, key: string, value?: any) => IObject<T>
|
|
97
|
+
|
|
98
|
+
/**对象混入
|
|
99
|
+
* @param {IObject} target 目标对象
|
|
100
|
+
* @param {string} source 需要混入的对象集合
|
|
101
|
+
* @param {boolean} overwrite 是否重写覆盖原有属性
|
|
102
|
+
* @return {IObject} 目标对象
|
|
103
|
+
**/
|
|
104
|
+
export type IMixIn = <U extends IObject<any>>(target?: U, source?: IObject<any>, overwrite?: boolean) => U
|
|
105
|
+
|
|
106
|
+
/**枚举值反向映射
|
|
107
|
+
* @param {IObject<string>} target 目标对象
|
|
108
|
+
* @return {IObject<string>} 目标对象
|
|
109
|
+
**/
|
|
110
|
+
export type IEnumInversion = (target: IObject<string>) => IObject<string>
|
|
111
|
+
|
|
112
|
+
/**对象复制
|
|
113
|
+
* @param {IObject<any>} target 目标对象
|
|
114
|
+
* @return {IObject} 目标对象
|
|
115
|
+
**/
|
|
116
|
+
export type ICloneDeep = (target?: any) => any
|
|
117
|
+
|
|
118
|
+
/**生成 对象 类型的初始值
|
|
119
|
+
* @param {string} type 数据类型
|
|
120
|
+
* @param {any} __init 初始值
|
|
121
|
+
* @return {any} 目标对象
|
|
122
|
+
**/
|
|
123
|
+
export type ICreateObjectVariable = (type: string, source?: any) => any
|
|
124
|
+
|
|
125
|
+
/**Object.create 根据源对象产出新对象
|
|
126
|
+
* @param {Function|Object} source 源对象
|
|
127
|
+
* @return {Function|Object} 对象产物
|
|
128
|
+
**/
|
|
129
|
+
export type ICreateObject = <T, U extends T>(source: T) => U
|
|
130
|
+
|
|
131
|
+
/**类的继承
|
|
132
|
+
* @param {Function} source 源对象
|
|
133
|
+
* @return {Function} 继承产物
|
|
134
|
+
**/
|
|
135
|
+
export type IInherit = <T extends Function>(source: T, target?: Function) => Function
|
|
136
|
+
|
|
137
|
+
/**生成类的实例单例
|
|
138
|
+
* @param {Function} classProto 类
|
|
139
|
+
* @param {Boolean} overwrite 是否覆盖已有单例
|
|
140
|
+
* @param {any[]} params 构造函数的参数
|
|
141
|
+
* @return {IObject} 实例化的单例
|
|
142
|
+
**/
|
|
143
|
+
export type IGetInstance = (classProto: IInstance<FunctionConstructor>, overwrite?: boolean, ...params: any[]) => Function
|
|
144
|
+
|
|
145
|
+
/**通过装饰器将属性混入类中
|
|
146
|
+
* @param {IObject<any>} params 混入的属性
|
|
147
|
+
* @return {ClassDecorator} 装饰器钩子函数
|
|
148
|
+
**/
|
|
149
|
+
export type IClassDecorator = (params: IObject<any>) => <TFunction extends Function>(target: TFunction) => void
|
|
150
|
+
|
|
151
|
+
/**JSON.parse封装
|
|
152
|
+
* @param {string} target 字符串
|
|
153
|
+
* @return {IObject<any>} 对象
|
|
154
|
+
**/
|
|
155
|
+
export type IStringToJson = (target: string) => IObject<any>
|
|
156
|
+
|
|
157
|
+
/**JSON.stringify封装
|
|
158
|
+
* @param {IObject<any>} target 对象
|
|
159
|
+
* @return {string} 字符串
|
|
160
|
+
**/
|
|
161
|
+
export type IJsonToString = (target: IObject<any>) => string
|
|
162
|
+
|
|
163
|
+
// function
|
|
164
|
+
/**节流(throttle):高频事件触发,但在 n 秒内只会执行一次
|
|
165
|
+
* @param {Function} fn 节流处理的函数
|
|
166
|
+
* @param {number} time 执行间隔/毫秒
|
|
167
|
+
* @return {Function} 处理后的函数
|
|
168
|
+
**/
|
|
169
|
+
export type IThrottle = (fn: Function, time: number) => (...args: any[]) => void
|
|
170
|
+
|
|
171
|
+
/**防抖(debounce):触发高频事件后 n 秒内函数只会执行一次
|
|
172
|
+
* @param {Function} fn 防抖处理的函数
|
|
173
|
+
* @param {number} time 允许运行函数间隔/毫秒
|
|
174
|
+
* @return {Function} 处理后的函数
|
|
175
|
+
**/
|
|
176
|
+
export type IDebounce = (fn: Function, time: number) => (...args: any[]) => void
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Promise扁平化,避免Promise嵌套
|
|
180
|
+
* @returns {Promise,resolve,reject}
|
|
181
|
+
*/
|
|
182
|
+
export type IDefer = () => IPromise
|
|
183
|
+
|
|
184
|
+
/**await与try catch 捕获异常处理方法
|
|
185
|
+
* @param {Promise<any>} defer 延迟函数
|
|
186
|
+
* @returns {Promise<any>} [error, result]
|
|
187
|
+
*/
|
|
188
|
+
export type ICatchAwait<T extends Promise<any>> = (defer: T) => T
|
|
189
|
+
|
|
190
|
+
// array
|
|
191
|
+
/**数组乱序
|
|
192
|
+
* @param {Array<any>} arr 目标数组
|
|
193
|
+
* @returns {Array<any>} 乱序后的数组
|
|
194
|
+
*/
|
|
195
|
+
export type IArrayRandom<T extends any[]> = (arr: T) => T
|
|
196
|
+
|
|
197
|
+
/**数组数组去重
|
|
198
|
+
* @param {Array<any>} arr 目标数组
|
|
199
|
+
* @returns {Array<any>} 去重后的数组
|
|
200
|
+
*/
|
|
201
|
+
export type IArrayUniq<T extends any[]> = (arr: T) => T
|
|
202
|
+
|
|
203
|
+
/**数组扁平化
|
|
204
|
+
* @param {Array<any>} arr 目标数组
|
|
205
|
+
* @returns {Array<any>} 扁平化的数组
|
|
206
|
+
*/
|
|
207
|
+
|
|
208
|
+
export type IArrayDemote<T extends IDemoteArray<any>> = (arr: T, result?: T) => T
|
|
209
|
+
|
|
210
|
+
// element
|
|
211
|
+
/**IElementParams
|
|
212
|
+
* @param {string} ele 标签类型
|
|
213
|
+
* @param {CSSStyleDeclaration} style 样式
|
|
214
|
+
* @param {Attr} attr 属性
|
|
215
|
+
* @param {object} parent 父元素
|
|
216
|
+
*/
|
|
217
|
+
interface IElementParams<T> {
|
|
218
|
+
ele: T | string
|
|
219
|
+
style: CSSStyleDeclaration
|
|
220
|
+
attr: Attr
|
|
221
|
+
parent: T
|
|
222
|
+
}
|
|
223
|
+
/**新增标签,设置属性及样式
|
|
224
|
+
* @param {IElementParams} params 配置
|
|
225
|
+
* @return {ElementObject} 生成的标签
|
|
226
|
+
*/
|
|
227
|
+
export type ICreateElement<T = HTMLElement> = (params: IElementParams<T>) => T
|
|
228
|
+
|
|
229
|
+
// event
|
|
230
|
+
/**浏览器事件
|
|
231
|
+
* @param {Document} ele 标签
|
|
232
|
+
* @param {string} type 事件类型
|
|
233
|
+
* @param {(e: Event) => void} handler 事件回调
|
|
234
|
+
* @return {void}
|
|
235
|
+
*/
|
|
236
|
+
export type IAddHandler = <T extends Document>(ele: T, type: string, handler: (e: Event) => void) => void
|
|
237
|
+
|
|
238
|
+
/**取消事件冒泡
|
|
239
|
+
* @param {Event} e 浏览器事件对象
|
|
240
|
+
* @return {void}
|
|
241
|
+
*/
|
|
242
|
+
export type IStopBubble = (e: Event) => void
|
|
243
|
+
|
|
244
|
+
/**取消默认事件
|
|
245
|
+
* @param {Event} e 浏览器事件对象
|
|
246
|
+
* @return {void}
|
|
247
|
+
*/
|
|
248
|
+
export type IStopDefault = (e: Event) => void
|
|
249
|
+
|
|
250
|
+
/**取消浏览器事件
|
|
251
|
+
* @param {Document} ele 标签
|
|
252
|
+
* @param {string} type 事件类型
|
|
253
|
+
* @param {(e: Event) => void} handler 事件回调
|
|
254
|
+
* @return {void}
|
|
255
|
+
*/
|
|
256
|
+
export type IRemoveHandler = <T extends Document>(ele: T, type: string, handler: (e: Event) => void) => void
|
|
257
|
+
|
|
258
|
+
/**取消默认事件
|
|
259
|
+
* @param {Event} e 浏览器事件对象
|
|
260
|
+
* @return {void}
|
|
261
|
+
*/
|
|
262
|
+
export type IDispatchEvent = <T extends Document>(ele: T, data: any) => void
|
|
263
|
+
|
|
264
|
+
// request
|
|
265
|
+
|
|
266
|
+
export type IRequestParams<T> = T | IObject<any> | null
|
|
267
|
+
// 请求路径
|
|
268
|
+
export type IUrl = string
|
|
269
|
+
// 环境判断
|
|
270
|
+
export type IEnv = 'Window' | 'Node'
|
|
271
|
+
// fetch返回取值方式
|
|
272
|
+
export type IDataType = "text" | "json" | "blob" | "formData" | "arrayBuffer"
|
|
273
|
+
// 请求方式
|
|
274
|
+
export type IRequestMethods = "GET" | "POST" | "DELETE" | "PUT" | "OPTION"
|
|
275
|
+
// body结构
|
|
276
|
+
export type IRequestBody = IRequestParams<BodyInit>
|
|
277
|
+
// heads结构
|
|
278
|
+
export type IRequestHeaders = IRequestParams<HeadersInit>
|
|
279
|
+
// 请求基础函数
|
|
280
|
+
export type IRequestBaseFn = (url: IUrl, opts: IRequestOptions) => Promise<any>
|
|
281
|
+
// 请求函数体
|
|
282
|
+
export type IRequestFn = (url?: IUrl, query?: IObject<any>, body?: IRequestBody, opts?: IRequestOptions) => Promise<any>
|
|
283
|
+
// 请求参数
|
|
284
|
+
export type IRequestOptions = {
|
|
285
|
+
method?: IRequestMethods
|
|
286
|
+
query?: IRequestParams<IObject<any>>
|
|
287
|
+
body?: IRequestBody
|
|
288
|
+
headers?: IRequestHeaders
|
|
289
|
+
controller?: AbortController
|
|
290
|
+
timeout?: number
|
|
291
|
+
timer?: number | unknown | null
|
|
292
|
+
[key: string]: any
|
|
293
|
+
}
|
|
294
|
+
// 拦截器
|
|
295
|
+
export type IInterceptors = {
|
|
296
|
+
use(type: "request" | "response" | "error", fn: Function): void
|
|
297
|
+
get reqFn(): Function
|
|
298
|
+
get resFn(): Function
|
|
299
|
+
get errFn(): Function
|
|
300
|
+
}
|
|
301
|
+
// 公共函数
|
|
302
|
+
export type IRequestBase = {
|
|
303
|
+
readonly origin: string
|
|
304
|
+
chackUrl: (url: IUrl) => boolean
|
|
305
|
+
envDesc: () => IEnv
|
|
306
|
+
errorFn: <Err = any, R = Function>(reject: R) => (err: Err) => R
|
|
307
|
+
clearTimer: (opts: IRequestOptions) => void
|
|
308
|
+
initAbort: <T = IRequestOptions>(opts: T) => T
|
|
309
|
+
requestType: () => IRequestBaseFn
|
|
310
|
+
fixOrigin: (fixStr: string) => string
|
|
311
|
+
fetch: IRequestBaseFn
|
|
312
|
+
http: IRequestBaseFn
|
|
313
|
+
getDataByType: (type: IDataType, response: Response) => Promise<any>
|
|
314
|
+
}
|
|
315
|
+
// 初始化参数
|
|
316
|
+
export type IRequestInit = {
|
|
317
|
+
initDefaultParams: (url: IUrl, opts: IRequestOptions) => any
|
|
318
|
+
initFetchParams: (url: IUrl, opts: IRequestOptions) => any
|
|
319
|
+
initHttpParams: (url: IUrl, opts: IRequestOptions) => any
|
|
320
|
+
}
|
|
321
|
+
// 请求主体类
|
|
322
|
+
export type IRequest = {
|
|
323
|
+
GET: IRequestFn
|
|
324
|
+
POST: IRequestFn
|
|
325
|
+
DELETE: IRequestFn
|
|
326
|
+
PUT: IRequestFn
|
|
327
|
+
OPTIONS: IRequestFn
|
|
328
|
+
HEAD: IRequestFn
|
|
329
|
+
PATCH: IRequestFn
|
|
330
|
+
} & IRequestBase
|
package/dist/common/request.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IRequest, IRequestBase, IRequestInit, IInterceptors } from "./index.js";
|
|
1
|
+
import { IRequest, IRequestBase, IRequestInit, IInterceptors, IUrl, IObject, IRequestBody, IRequestOptions } from "./index.js";
|
|
2
2
|
declare class Interceptors implements IInterceptors {
|
|
3
3
|
private requestSuccess;
|
|
4
4
|
private responseSuccess;
|
|
@@ -54,12 +54,12 @@ export declare class Request extends RequestInit implements IRequest {
|
|
|
54
54
|
constructor(origin: any);
|
|
55
55
|
fetch: (_url: any, _opts: any) => Promise<void>;
|
|
56
56
|
http: (_url: any, _opts: any) => Promise<void>;
|
|
57
|
-
GET: (url
|
|
58
|
-
POST: (url
|
|
59
|
-
PUT: (url
|
|
60
|
-
DELETE: (url
|
|
61
|
-
OPTIONS: (url
|
|
62
|
-
HEAD: (url
|
|
63
|
-
PATCH: (url
|
|
57
|
+
GET: (url?: IUrl, query?: IObject<any>, _?: IRequestBody | void, opts?: IRequestOptions) => any;
|
|
58
|
+
POST: (url?: IUrl, query?: IObject<any>, body?: IRequestBody, opts?: IRequestOptions) => any;
|
|
59
|
+
PUT: (url?: IUrl, query?: IObject<any>, body?: IRequestBody, opts?: IRequestOptions) => any;
|
|
60
|
+
DELETE: (url?: IUrl, query?: IObject<any>, body?: IRequestBody, opts?: IRequestOptions) => any;
|
|
61
|
+
OPTIONS: (url?: IUrl, query?: IObject<any>, body?: IRequestBody, opts?: IRequestOptions) => any;
|
|
62
|
+
HEAD: (url?: IUrl, query?: IObject<any>, body?: IRequestBody, opts?: IRequestOptions) => any;
|
|
63
|
+
PATCH: (url?: IUrl, query?: IObject<any>, body?: IRequestBody, opts?: IRequestOptions) => any;
|
|
64
64
|
}
|
|
65
65
|
export {};
|
package/dist/common/request.js
CHANGED
|
@@ -232,45 +232,24 @@ var Request = function (_super) {
|
|
|
232
232
|
return promise;
|
|
233
233
|
};
|
|
234
234
|
_this.GET = function (url, query, _, opts) {
|
|
235
|
-
if (query === void 0) {
|
|
236
|
-
query = {};
|
|
237
|
-
}
|
|
238
235
|
return _this.request(url, __assign({ query: query, method: "GET" }, opts));
|
|
239
236
|
};
|
|
240
237
|
_this.POST = function (url, query, body, opts) {
|
|
241
|
-
if (query === void 0) {
|
|
242
|
-
query = {};
|
|
243
|
-
}
|
|
244
238
|
return _this.request(url, __assign({ query: query, method: "POST", body: body }, opts));
|
|
245
239
|
};
|
|
246
240
|
_this.PUT = function (url, query, body, opts) {
|
|
247
|
-
if (query === void 0) {
|
|
248
|
-
query = {};
|
|
249
|
-
}
|
|
250
241
|
return _this.request(url, __assign({ query: query, method: "PUT", body: body }, opts));
|
|
251
242
|
};
|
|
252
243
|
_this.DELETE = function (url, query, body, opts) {
|
|
253
|
-
if (query === void 0) {
|
|
254
|
-
query = {};
|
|
255
|
-
}
|
|
256
244
|
return _this.request(url, __assign({ query: query, method: "DELETE", body: body }, opts));
|
|
257
245
|
};
|
|
258
246
|
_this.OPTIONS = function (url, query, body, opts) {
|
|
259
|
-
if (query === void 0) {
|
|
260
|
-
query = {};
|
|
261
|
-
}
|
|
262
247
|
return _this.request(url, __assign({ query: query, method: "OPTIONS", body: body }, opts));
|
|
263
248
|
};
|
|
264
249
|
_this.HEAD = function (url, query, body, opts) {
|
|
265
|
-
if (query === void 0) {
|
|
266
|
-
query = {};
|
|
267
|
-
}
|
|
268
250
|
return _this.request(url, __assign({ query: query, method: "HEAD", body: body }, opts));
|
|
269
251
|
};
|
|
270
252
|
_this.PATCH = function (url, query, body, opts) {
|
|
271
|
-
if (query === void 0) {
|
|
272
|
-
query = {};
|
|
273
|
-
}
|
|
274
253
|
return _this.request(url, __assign({ query: query, method: "PATCH", body: body }, opts));
|
|
275
254
|
};
|
|
276
255
|
_this.request = _this.requestType();
|
package/dist/common/types.d.ts
CHANGED
|
@@ -55,7 +55,7 @@ export declare type IRequestMethods = "GET" | "POST" | "DELETE" | "PUT" | "OPTIO
|
|
|
55
55
|
export declare type IRequestBody = IRequestParams<BodyInit>;
|
|
56
56
|
export declare type IRequestHeaders = IRequestParams<HeadersInit>;
|
|
57
57
|
export declare type IRequestBaseFn = (url: IUrl, opts: IRequestOptions) => Promise<any>;
|
|
58
|
-
export declare type IRequestFn = (url
|
|
58
|
+
export declare type IRequestFn = (url?: IUrl, query?: IObject<any>, body?: IRequestBody, opts?: IRequestOptions) => Promise<any>;
|
|
59
59
|
export declare type IRequestOptions = {
|
|
60
60
|
method?: IRequestMethods;
|
|
61
61
|
query?: IRequestParams<IObject<any>>;
|
package/dist/esm/request.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IRequest, IRequestBase, IRequestInit, IInterceptors } from "./index.js";
|
|
1
|
+
import { IRequest, IRequestBase, IRequestInit, IInterceptors, IUrl, IObject, IRequestBody, IRequestOptions } from "./index.js";
|
|
2
2
|
declare class Interceptors implements IInterceptors {
|
|
3
3
|
private requestSuccess;
|
|
4
4
|
private responseSuccess;
|
|
@@ -54,12 +54,12 @@ export declare class Request extends RequestInit implements IRequest {
|
|
|
54
54
|
constructor(origin: any);
|
|
55
55
|
fetch: (_url: any, _opts: any) => Promise<void>;
|
|
56
56
|
http: (_url: any, _opts: any) => Promise<void>;
|
|
57
|
-
GET: (url
|
|
58
|
-
POST: (url
|
|
59
|
-
PUT: (url
|
|
60
|
-
DELETE: (url
|
|
61
|
-
OPTIONS: (url
|
|
62
|
-
HEAD: (url
|
|
63
|
-
PATCH: (url
|
|
57
|
+
GET: (url?: IUrl, query?: IObject<any>, _?: IRequestBody | void, opts?: IRequestOptions) => any;
|
|
58
|
+
POST: (url?: IUrl, query?: IObject<any>, body?: IRequestBody, opts?: IRequestOptions) => any;
|
|
59
|
+
PUT: (url?: IUrl, query?: IObject<any>, body?: IRequestBody, opts?: IRequestOptions) => any;
|
|
60
|
+
DELETE: (url?: IUrl, query?: IObject<any>, body?: IRequestBody, opts?: IRequestOptions) => any;
|
|
61
|
+
OPTIONS: (url?: IUrl, query?: IObject<any>, body?: IRequestBody, opts?: IRequestOptions) => any;
|
|
62
|
+
HEAD: (url?: IUrl, query?: IObject<any>, body?: IRequestBody, opts?: IRequestOptions) => any;
|
|
63
|
+
PATCH: (url?: IUrl, query?: IObject<any>, body?: IRequestBody, opts?: IRequestOptions) => any;
|
|
64
64
|
}
|
|
65
65
|
export {};
|
package/dist/esm/request.js
CHANGED
|
@@ -228,45 +228,24 @@ var Request = function (_super) {
|
|
|
228
228
|
return promise;
|
|
229
229
|
};
|
|
230
230
|
_this.GET = function (url, query, _, opts) {
|
|
231
|
-
if (query === void 0) {
|
|
232
|
-
query = {};
|
|
233
|
-
}
|
|
234
231
|
return _this.request(url, __assign({ query: query, method: "GET" }, opts));
|
|
235
232
|
};
|
|
236
233
|
_this.POST = function (url, query, body, opts) {
|
|
237
|
-
if (query === void 0) {
|
|
238
|
-
query = {};
|
|
239
|
-
}
|
|
240
234
|
return _this.request(url, __assign({ query: query, method: "POST", body: body }, opts));
|
|
241
235
|
};
|
|
242
236
|
_this.PUT = function (url, query, body, opts) {
|
|
243
|
-
if (query === void 0) {
|
|
244
|
-
query = {};
|
|
245
|
-
}
|
|
246
237
|
return _this.request(url, __assign({ query: query, method: "PUT", body: body }, opts));
|
|
247
238
|
};
|
|
248
239
|
_this.DELETE = function (url, query, body, opts) {
|
|
249
|
-
if (query === void 0) {
|
|
250
|
-
query = {};
|
|
251
|
-
}
|
|
252
240
|
return _this.request(url, __assign({ query: query, method: "DELETE", body: body }, opts));
|
|
253
241
|
};
|
|
254
242
|
_this.OPTIONS = function (url, query, body, opts) {
|
|
255
|
-
if (query === void 0) {
|
|
256
|
-
query = {};
|
|
257
|
-
}
|
|
258
243
|
return _this.request(url, __assign({ query: query, method: "OPTIONS", body: body }, opts));
|
|
259
244
|
};
|
|
260
245
|
_this.HEAD = function (url, query, body, opts) {
|
|
261
|
-
if (query === void 0) {
|
|
262
|
-
query = {};
|
|
263
|
-
}
|
|
264
246
|
return _this.request(url, __assign({ query: query, method: "HEAD", body: body }, opts));
|
|
265
247
|
};
|
|
266
248
|
_this.PATCH = function (url, query, body, opts) {
|
|
267
|
-
if (query === void 0) {
|
|
268
|
-
query = {};
|
|
269
|
-
}
|
|
270
249
|
return _this.request(url, __assign({ query: query, method: "PATCH", body: body }, opts));
|
|
271
250
|
};
|
|
272
251
|
_this.request = _this.requestType();
|
package/dist/esm/types.d.ts
CHANGED
|
@@ -55,7 +55,7 @@ export declare type IRequestMethods = "GET" | "POST" | "DELETE" | "PUT" | "OPTIO
|
|
|
55
55
|
export declare type IRequestBody = IRequestParams<BodyInit>;
|
|
56
56
|
export declare type IRequestHeaders = IRequestParams<HeadersInit>;
|
|
57
57
|
export declare type IRequestBaseFn = (url: IUrl, opts: IRequestOptions) => Promise<any>;
|
|
58
|
-
export declare type IRequestFn = (url
|
|
58
|
+
export declare type IRequestFn = (url?: IUrl, query?: IObject<any>, body?: IRequestBody, opts?: IRequestOptions) => Promise<any>;
|
|
59
59
|
export declare type IRequestOptions = {
|
|
60
60
|
method?: IRequestMethods;
|
|
61
61
|
query?: IRequestParams<IObject<any>>;
|