utils-lib-js 1.0.9 → 1.0.13

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 CHANGED
@@ -26,4 +26,305 @@ JavaScript工具函数,封装的一些常用的js函数
26
26
 
27
27
  #### 功能
28
28
  接口:
29
- src\types.ts
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,373 @@ JavaScript工具函数,封装的一些常用的js函数
26
26
 
27
27
  #### 功能
28
28
  接口:
29
- src\types.ts
29
+
30
+ export type IKey = string | symbol | number
31
+
32
+ ##### 对象类型
33
+
34
+ export interface IObject<T> {
35
+ [key: IKey]: T | IObject<any>
36
+ }
37
+
38
+ export interface IPromise extends IObject<any> {
39
+ promise: Promise<void>
40
+ resolve: (res: any) => unknown
41
+ reject: (err: any) => unknown
42
+ }
43
+
44
+ export type IInstance<T> = {
45
+ _instance: Function
46
+ } & T
47
+
48
+ export type IDemoteArray<T> = Array<IDemoteArray<T> | T>
49
+
50
+ ##### base
51
+
52
+ /**产生区间随机数
53
+ * @param {number} min 最小区间
54
+ * @param {number} max 最大区间
55
+ * @param {boolean} bool 包含最大值
56
+ * @return {number} 随机数
57
+ **/
58
+
59
+ export type IRandomNum = (min: number, max: number, bool?: boolean) => number
60
+
61
+ /**获取url的参数
62
+ * @param {string} url 待截取的地址
63
+ * @return {object} 参数对象
64
+ **/
65
+
66
+ export type IUrlSplit = (url: string) => IObject<any>
67
+
68
+ /**添加url的参数
69
+ * @param {string} url 待添加参数的地址
70
+ * @param {object} query 待添加的参数
71
+ * @return {string} 添加参数后的url
72
+ **/
73
+
74
+ export type IUrlJoin = (url: string, query: object) => string
75
+
76
+ /**获取数据类型
77
+ * @param {any} data 待检测数据
78
+ * @return {string} 数据类型
79
+ **/
80
+
81
+ export type IGetType<T> = (data: any) => typeof data | T[keyof T] | "null";
82
+
83
+ /**批量判断数据类型
84
+ * @param {any} data 待检测数据
85
+ * @param {any} whiteList 数据类型名单
86
+ * @return {boolean} 是否在白名单中
87
+ **/
88
+
89
+ export type IGetTypeByList = (data: any, whiteList: string[]) => boolean;
90
+
91
+
92
+ ##### object
93
+
94
+ /**lodash中的 _.get(),获取对象某级属性
95
+ * @param {IObject} object 目标对象
96
+ * @param {string} key 对象层级
97
+ * @param {any} defaultValue 未取得时默认值
98
+ * @return {IObject[IKey]} 对象某个属性
99
+ **/
100
+
101
+ export type IGetValue = <T, U = IObject<T> | IObject<T>[IKey]>(object: U, key: string, defaultValue?: any) => U
102
+
103
+ /**lodash中的 _.set(),赋值对象某级属性
104
+ * @param {IObject} object 目标对象
105
+ * @param {string} key 对象层级
106
+ * @param {any} value 需要赋的值
107
+ * @return {IObject} 目标对象
108
+ **/
109
+
110
+ export type ISetValue = <T>(object: IObject<T>, key: string, value?: any) => IObject<T>
111
+
112
+ /**对象混入
113
+ * @param {IObject} target 目标对象
114
+ * @param {string} source 需要混入的对象集合
115
+ * @param {boolean} overwrite 是否重写覆盖原有属性
116
+ * @return {IObject} 目标对象
117
+ **/
118
+
119
+ export type IMixIn = <U extends IObject<any>>(target?: U, source?: IObject<any>, overwrite?: boolean) => U
120
+
121
+ /**枚举值反向映射
122
+ * @param {IObject<string>} target 目标对象
123
+ * @return {IObject<string>} 目标对象
124
+ **/
125
+
126
+ export type IEnumInversion = (target: IObject<string>) => IObject<string>
127
+
128
+ /**对象复制
129
+ * @param {IObject<any>} target 目标对象
130
+ * @return {IObject} 目标对象
131
+ **/
132
+
133
+ export type ICloneDeep = (target?: any) => any
134
+
135
+ /**生成 对象 类型的初始值
136
+ * @param {string} type 数据类型
137
+ * @param {any} __init 初始值
138
+ * @return {any} 目标对象
139
+ **/
140
+
141
+ export type ICreateObjectVariable = (type: string, source?: any) => any
142
+
143
+ /**Object.create 根据源对象产出新对象
144
+ * @param {Function|Object} source 源对象
145
+ * @return {Function|Object} 对象产物
146
+ **/
147
+
148
+ export type ICreateObject = <T, U extends T>(source: T) => U
149
+
150
+ /**类的继承
151
+ * @param {Function} source 源对象
152
+ * @return {Function} 继承产物
153
+ **/
154
+
155
+ export type IInherit = <T extends Function>(source: T, target?: Function) => Function
156
+
157
+ /**生成类的实例单例
158
+ * @param {Function} classProto 类
159
+ * @param {Boolean} overwrite 是否覆盖已有单例
160
+ * @param {any[]} params 构造函数的参数
161
+ * @return {IObject} 实例化的单例
162
+ **/
163
+
164
+ export type IGetInstance = (classProto: IInstance<FunctionConstructor>, overwrite?: boolean, ...params: any[]) => Function
165
+
166
+ /**通过装饰器将属性混入类中
167
+ * @param {IObject<any>} params 混入的属性
168
+ * @return {ClassDecorator} 装饰器钩子函数
169
+ **/
170
+
171
+ export type IClassDecorator = (params: IObject<any>) => <TFunction extends Function>(target: TFunction) => void
172
+
173
+ /**JSON.parse封装
174
+ * @param {string} target 字符串
175
+ * @return {IObject<any>} 对象
176
+ **/
177
+
178
+ export type IStringToJson = (target: string) => IObject<any>
179
+
180
+ /**JSON.stringify封装
181
+ * @param {IObject<any>} target 对象
182
+ * @return {string} 字符串
183
+ **/
184
+
185
+ export type IJsonToString = (target: IObject<any>) => string
186
+
187
+ ##### function
188
+
189
+ /**节流(throttle):高频事件触发,但在 n 秒内只会执行一次
190
+ * @param {Function} fn 节流处理的函数
191
+ * @param {number} time 执行间隔/毫秒
192
+ * @return {Function} 处理后的函数
193
+ **/
194
+
195
+ export type IThrottle = (fn: Function, time: number) => (...args: any[]) => void
196
+
197
+ /**防抖(debounce):触发高频事件后 n 秒内函数只会执行一次
198
+ * @param {Function} fn 防抖处理的函数
199
+ * @param {number} time 允许运行函数间隔/毫秒
200
+ * @return {Function} 处理后的函数
201
+ **/
202
+
203
+ export type IDebounce = (fn: Function, time: number) => (...args: any[]) => void
204
+
205
+ /**
206
+ * Promise扁平化,避免Promise嵌套
207
+ * @returns {Promise,resolve,reject}
208
+ */
209
+
210
+ export type IDefer = () => IPromise
211
+
212
+ /**await与try catch 捕获异常处理方法
213
+ * @param {Promise<any>} defer 延迟函数
214
+ * @returns {Promise<any>} [error, result]
215
+ */
216
+
217
+ export type ICatchAwait<T extends Promise<any>> = (defer: T) => T
218
+
219
+ ##### array
220
+
221
+ /**数组乱序
222
+ * @param {Array<any>} arr 目标数组
223
+ * @returns {Array<any>} 乱序后的数组
224
+ */
225
+
226
+ export type IArrayRandom<T extends any[]> = (arr: T) => T
227
+
228
+ /**数组数组去重
229
+ * @param {Array<any>} arr 目标数组
230
+ * @returns {Array<any>} 去重后的数组
231
+ */
232
+
233
+ export type IArrayUniq<T extends any[]> = (arr: T) => T
234
+
235
+ /**数组扁平化
236
+ * @param {Array<any>} arr 目标数组
237
+ * @returns {Array<any>} 扁平化的数组
238
+ */
239
+
240
+ export type IArrayDemote<T extends IDemoteArray<any>> = (arr: T, result?: T) => T
241
+
242
+ ##### element
243
+
244
+ /**IElementParams
245
+ * @param {string} ele 标签类型
246
+ * @param {CSSStyleDeclaration} style 样式
247
+ * @param {Attr} attr 属性
248
+ * @param {object} parent 父元素
249
+ */
250
+
251
+ interface IElementParams<T> {
252
+ ele: T | string
253
+ style: CSSStyleDeclaration
254
+ attr: Attr
255
+ parent: T
256
+ }
257
+
258
+ /**新增标签,设置属性及样式
259
+ * @param {IElementParams} params 配置
260
+ * @return {ElementObject} 生成的标签
261
+ */
262
+
263
+ export type ICreateElement<T = HTMLElement> = (params: IElementParams<T>) => T
264
+
265
+ ##### event
266
+
267
+ /**浏览器事件
268
+ * @param {Document} ele 标签
269
+ * @param {string} type 事件类型
270
+ * @param {(e: Event) => void} handler 事件回调
271
+ * @return {void}
272
+ */
273
+
274
+ export type IAddHandler = <T extends Document>(ele: T, type: string, handler: (e: Event) => void) => void
275
+
276
+ /**取消事件冒泡
277
+ * @param {Event} e 浏览器事件对象
278
+ * @return {void}
279
+ */
280
+
281
+ export type IStopBubble = (e: Event) => void
282
+
283
+ /**取消默认事件
284
+ * @param {Event} e 浏览器事件对象
285
+ * @return {void}
286
+ */
287
+
288
+ export type IStopDefault = (e: Event) => void
289
+
290
+ /**取消浏览器事件
291
+ * @param {Document} ele 标签
292
+ * @param {string} type 事件类型
293
+ * @param {(e: Event) => void} handler 事件回调
294
+ * @return {void}
295
+ */
296
+
297
+ export type IRemoveHandler = <T extends Document>(ele: T, type: string, handler: (e: Event) => void) => void
298
+
299
+ /**取消默认事件
300
+ * @param {Event} e 浏览器事件对象
301
+ * @return {void}
302
+ */
303
+
304
+ export type IDispatchEvent = <T extends Document>(ele: T, data: any) => void
305
+
306
+ ##### request
307
+
308
+ export type IRequestParams<T> = T | IObject<any> | null
309
+
310
+ // 请求路径
311
+
312
+ export type IUrl = string
313
+
314
+ // 环境判断
315
+
316
+ export type IEnv = 'Window' | 'Node'
317
+
318
+ // fetch返回取值方式
319
+
320
+ export type IDataType = "text" | "json" | "blob" | "formData" | "arrayBuffer"
321
+
322
+ // 请求方式
323
+
324
+ export type IRequestMethods = "GET" | "POST" | "DELETE" | "PUT" | "OPTION"
325
+
326
+ // body结构
327
+
328
+ export type IRequestBody = IRequestParams<BodyInit>
329
+
330
+ // heads结构
331
+
332
+ export type IRequestHeaders = IRequestParams<HeadersInit>
333
+
334
+ // 请求基础函数
335
+
336
+ export type IRequestBaseFn = (url: IUrl, opts: IRequestOptions) => Promise<any>
337
+
338
+ // 请求函数体
339
+
340
+ export type IRequestFn = (url?: IUrl, query?: IObject<any>, body?: IRequestBody, opts?: IRequestOptions) => Promise<any>
341
+
342
+ // 请求参数
343
+
344
+ export type IRequestOptions = {
345
+ method?: IRequestMethods
346
+ query?: IRequestParams<IObject<any>>
347
+ body?: IRequestBody
348
+ headers?: IRequestHeaders
349
+ controller?: AbortController
350
+ timeout?: number
351
+ timer?: number | unknown | null
352
+ [key: string]: any
353
+ }
354
+
355
+ // 拦截器
356
+
357
+ export type IInterceptors = {
358
+ use(type: "request" | "response" | "error", fn: Function): void
359
+ get reqFn(): Function
360
+ get resFn(): Function
361
+ get errFn(): Function
362
+ }
363
+
364
+ // 公共函数
365
+
366
+ export type IRequestBase = {
367
+ readonly origin: string
368
+ chackUrl: (url: IUrl) => boolean
369
+ envDesc: () => IEnv
370
+ errorFn: <Err = any, R = Function>(reject: R) => (err: Err) => R
371
+ clearTimer: (opts: IRequestOptions) => void
372
+ initAbort: <T = IRequestOptions>(opts: T) => T
373
+ requestType: () => IRequestBaseFn
374
+ fixOrigin: (fixStr: string) => string
375
+ fetch: IRequestBaseFn
376
+ http: IRequestBaseFn
377
+ getDataByType: (type: IDataType, response: Response) => Promise<any>
378
+ }
379
+
380
+ // 初始化参数
381
+
382
+ export type IRequestInit = {
383
+ initDefaultParams: (url: IUrl, opts: IRequestOptions) => any
384
+ initFetchParams: (url: IUrl, opts: IRequestOptions) => any
385
+ initHttpParams: (url: IUrl, opts: IRequestOptions) => any
386
+ }
387
+
388
+ // 请求主体类
389
+
390
+ export type IRequest = {
391
+ GET: IRequestFn
392
+ POST: IRequestFn
393
+ DELETE: IRequestFn
394
+ PUT: IRequestFn
395
+ OPTIONS: IRequestFn
396
+ HEAD: IRequestFn
397
+ PATCH: IRequestFn
398
+ } & IRequestBase
@@ -39,8 +39,8 @@ var __rest = this && this.__rest || function (s, e) {
39
39
  Object.defineProperty(exports, "__esModule", { value: true });
40
40
  exports.Request = void 0;
41
41
  var index_js_1 = require("./index.js");
42
- var node_http_1 = require("node:http");
43
- var node_url_1 = require("node:url");
42
+ var http_1 = require("http");
43
+ var url_1 = require("url");
44
44
  var Interceptors = function () {
45
45
  function Interceptors() {}
46
46
  Interceptors.prototype.use = function (type, fn) {
@@ -169,7 +169,7 @@ var RequestInit = function (_super) {
169
169
  _this.initHttpParams = function (url, opts) {
170
170
  var _a, _b;
171
171
  var params = _this.initAbort(_this.initDefaultParams(url, opts));
172
- var options = (0, node_url_1.parse)(params.url, true);
172
+ var options = (0, url_1.parse)(params.url, true);
173
173
  return (_b = (_a = _this.reqFn) === null || _a === void 0 ? void 0 : _a.call(_this, __assign(__assign({}, params), options))) !== null && _b !== void 0 ? _b : params;
174
174
  };
175
175
  return _this;
@@ -211,7 +211,7 @@ var Request = function (_super) {
211
211
  reject = _a.reject;
212
212
  var params = _this.initHttpParams(_url, _opts);
213
213
  var signal = params.signal;
214
- var req = (0, node_http_1.request)(params, function (response) {
214
+ var req = (0, http_1.request)(params, function (response) {
215
215
  if (response.statusCode >= 200 && response.statusCode < 300) {
216
216
  var data_1 = "";
217
217
  response.setEncoding('utf8');
@@ -51,7 +51,7 @@ export declare type IRequestParams<T> = T | IObject<any> | null;
51
51
  export declare type IUrl = string;
52
52
  export declare type IEnv = 'Window' | 'Node';
53
53
  export declare type IDataType = "text" | "json" | "blob" | "formData" | "arrayBuffer";
54
- export declare type IRequestMethods = "GET" | "POST" | "DELETE" | "PUT" | "OPTION";
54
+ export declare type IRequestMethods = "GET" | "POST" | "DELETE" | "PUT" | "OPTION" | "HEAD" | "PATCH";
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>;
@@ -35,8 +35,8 @@ var __rest = this && this.__rest || function (s, e) {
35
35
  return t;
36
36
  };
37
37
  import { urlJoin, defer, jsonToString } from "./index.js";
38
- import { request } from "node:http";
39
- import { parse } from "node:url";
38
+ import { request } from "http";
39
+ import { parse } from "url";
40
40
  var Interceptors = function () {
41
41
  function Interceptors() {}
42
42
  Interceptors.prototype.use = function (type, fn) {
@@ -51,7 +51,7 @@ export declare type IRequestParams<T> = T | IObject<any> | null;
51
51
  export declare type IUrl = string;
52
52
  export declare type IEnv = 'Window' | 'Node';
53
53
  export declare type IDataType = "text" | "json" | "blob" | "formData" | "arrayBuffer";
54
- export declare type IRequestMethods = "GET" | "POST" | "DELETE" | "PUT" | "OPTION";
54
+ export declare type IRequestMethods = "GET" | "POST" | "DELETE" | "PUT" | "OPTION" | "HEAD" | "PATCH";
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>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utils-lib-js",
3
- "version": "1.0.9",
3
+ "version": "1.0.13",
4
4
  "description": "JavaScript工具函数,封装的一些常用的js函数",
5
5
  "main": "./dist/common/index.js",
6
6
  "types": "./dist/common/index.d.ts",
@@ -33,11 +33,8 @@
33
33
  ],
34
34
  "author": "",
35
35
  "license": "ISC",
36
- "dependencies": {
37
- "event-message-center": "^1.0.16"
38
- },
39
36
  "devDependencies": {
40
- "@types/node": "^18.7.1",
37
+ "@types/node": "^18.7.15",
41
38
  "babel-cli": "^6.26.0"
42
39
  }
43
40
  }
package/pnpm-lock.yaml CHANGED
@@ -1,17 +1,19 @@
1
1
  lockfileVersion: 5.4
2
2
 
3
3
  specifiers:
4
+ '@types/node': ^18.7.15
4
5
  babel-cli: ^6.26.0
5
- event-message-center: ^1.0.16
6
-
7
- dependencies:
8
- event-message-center: 1.0.16
9
6
 
10
7
  devDependencies:
8
+ '@types/node': 18.7.15
11
9
  babel-cli: 6.26.0
12
10
 
13
11
  packages:
14
12
 
13
+ /@types/node/18.7.15:
14
+ resolution: {integrity: sha512-XnjpaI8Bgc3eBag2Aw4t2Uj/49lLBSStHWfqKvIuXD7FIrZyMLWp8KuAFHAqxMZYTF9l08N1ctUn9YNybZJVmQ==}
15
+ dev: true
16
+
15
17
  /ansi-regex/2.1.1:
16
18
  resolution: {integrity: sha512-TIGnTpdo+E3+pCyAluZvtED5p5wCqLdezCyhPZzKPcxvFplEt4i+W7OONCKgeZFT3+y5NZZfOOS/Bdcanm1MYA==}
17
19
  engines: {node: '>=0.10.0'}
@@ -389,7 +391,7 @@ packages:
389
391
 
390
392
  /core-js/2.6.12:
391
393
  resolution: {integrity: sha512-Kb2wC0fvsWfQrgk8HU5lW6U/Lcs8+9aaYcy4ZFc6DDlo4nZ7n70dEgE5rtR0oG6ufKDUnrwfWL1mXR5ljDatrQ==}
392
- deprecated: core-js@<3.4 is no longer maintained and not recommended for usage due to the number of issues. Because of the V8 engine whims, feature detection in old core-js versions could cause a slowdown up to 100x even if nothing is polyfilled. Please, upgrade your dependencies to the actual version of core-js.
394
+ deprecated: core-js@<3.23.3 is no longer maintained and not recommended for usage due to the number of issues. Because of the V8 engine whims, feature detection in old core-js versions could cause a slowdown up to 100x even if nothing is polyfilled. Some versions have web compatibility issues. Please, upgrade your dependencies to the actual version of core-js.
393
395
  requiresBuild: true
394
396
  dev: true
395
397
 
@@ -452,10 +454,6 @@ packages:
452
454
  engines: {node: '>=0.10.0'}
453
455
  dev: true
454
456
 
455
- /event-message-center/1.0.16:
456
- resolution: {integrity: sha512-K4wmY3Do7/xXIME6fYIZx0vHSr7Drd3mWsTSYc/XzBhl1EZSx45LngldH+4r7gIY3nfR5TqWRwYbghsMc8kLqg==}
457
- dev: false
458
-
459
457
  /expand-brackets/0.1.5:
460
458
  resolution: {integrity: sha512-hxx03P2dJxss6ceIeri9cmYOT4SRs3Zk3afZwWpOsRqLqprhTR8u++SlC+sFGsQr7WGFPdMF7Gjc1njDLDK6UA==}
461
459
  engines: {node: '>=0.10.0'}