tools_dj 1.0.93 → 1.0.94
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.md +61 -1
- package/lib/request.d.ts +10 -50
- package/lib/types.d.ts +38 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -53,7 +53,7 @@ const http = new Request({
|
|
|
53
53
|
is([1, 2, 3]); // "Array"
|
|
54
54
|
is({}); // "Object"
|
|
55
55
|
is("hello"); // "String"
|
|
56
|
-
```
|
|
56
|
+
```[README.md](README.md)
|
|
57
57
|
|
|
58
58
|
#### `is_empty(obj: any): boolean`
|
|
59
59
|
判断值是否为空,支持所有数据类型
|
|
@@ -223,6 +223,13 @@ numberFixed(3.14159, 2); // 3.14
|
|
|
223
223
|
numberRepair('5', 3, '0'); // "005"
|
|
224
224
|
```
|
|
225
225
|
|
|
226
|
+
#### `Number.prototype.repair(n: number): string`
|
|
227
|
+
数字原型方法补位
|
|
228
|
+
```typescript
|
|
229
|
+
(5).repair(3); // "005"
|
|
230
|
+
(42).repair(4); // "0042"
|
|
231
|
+
```
|
|
232
|
+
|
|
226
233
|
#### `randomNumber(min: number, max: number): number`
|
|
227
234
|
生成随机数
|
|
228
235
|
```typescript
|
|
@@ -465,6 +472,59 @@ const merged = mergeObj([
|
|
|
465
472
|
time_change('2024-03-24 10:30'); // 1711252200000
|
|
466
473
|
```
|
|
467
474
|
|
|
475
|
+
### 剪贴板操作
|
|
476
|
+
|
|
477
|
+
#### `copyText(text: string): Promise<boolean>`
|
|
478
|
+
复制文字到剪贴板
|
|
479
|
+
```typescript
|
|
480
|
+
import { copyText } from "tools_dj/clipboard";
|
|
481
|
+
|
|
482
|
+
// 复制文本
|
|
483
|
+
const success = await copyText('要复制的文本');
|
|
484
|
+
console.log(success); // true 或 false
|
|
485
|
+
```
|
|
486
|
+
|
|
487
|
+
#### `isClipboardSupported(): boolean`
|
|
488
|
+
检查浏览器是否支持剪贴板 API
|
|
489
|
+
```typescript
|
|
490
|
+
import { isClipboardSupported } from "tools_dj/clipboard";
|
|
491
|
+
|
|
492
|
+
if (isClipboardSupported()) {
|
|
493
|
+
console.log('支持剪贴板 API');
|
|
494
|
+
}
|
|
495
|
+
```
|
|
496
|
+
|
|
497
|
+
#### `readText(): Promise<string>`
|
|
498
|
+
从剪贴板读取文本
|
|
499
|
+
```typescript
|
|
500
|
+
import { readText } from "tools_dj/clipboard";
|
|
501
|
+
|
|
502
|
+
// 读取剪贴板内容
|
|
503
|
+
const text = await readText();
|
|
504
|
+
console.log(text);
|
|
505
|
+
```
|
|
506
|
+
|
|
507
|
+
### 数据获取
|
|
508
|
+
|
|
509
|
+
#### `getDeepData(data: object, path: string, defaultValue: any): any`
|
|
510
|
+
获取对象的深层属性值,支持点分路径
|
|
511
|
+
```typescript
|
|
512
|
+
import { getDeepData } from "tools_dj/data";
|
|
513
|
+
|
|
514
|
+
const obj = {
|
|
515
|
+
user: {
|
|
516
|
+
name: 'Tom',
|
|
517
|
+
address: {
|
|
518
|
+
city: 'Beijing'
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
};
|
|
522
|
+
|
|
523
|
+
getDeepData(obj, 'user.name'); // 'Tom'
|
|
524
|
+
getDeepData(obj, 'user.address.city'); // 'Beijing'
|
|
525
|
+
getDeepData(obj, 'user.age', 18); // 18 (未找到时返回默认值)
|
|
526
|
+
```
|
|
527
|
+
|
|
468
528
|
## 类模块
|
|
469
529
|
|
|
470
530
|
### 权限管理 (auth)
|
package/lib/request.d.ts
CHANGED
|
@@ -1,52 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*/
|
|
4
|
-
export interface conf {
|
|
5
|
-
/** 请求的基础URL前缀 */
|
|
6
|
-
baseUrl?: string;
|
|
7
|
-
/** 请求超时时间,单位毫秒,默认6000ms */
|
|
8
|
-
timeout?: number;
|
|
9
|
-
/** 请求成功后的回调函数,可以处理响应数据 */
|
|
10
|
-
callback?: (response: Response, conf: conf) => Promise<any> | any;
|
|
11
|
-
/** 请求失败时的错误处理回调函数 */
|
|
12
|
-
errBack?: (error: Error) => void;
|
|
13
|
-
/** 获取AbortController实例的回调,用于手动中断请求 */
|
|
14
|
-
abController?: (controller: AbortController) => void;
|
|
15
|
-
/** 请求发送前的预处理回调,可以修改请求选项 */
|
|
16
|
-
beforeBack?: (options: any) => void;
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Fetch请求客户端类
|
|
20
|
-
* @description 基于fetch API的HTTP请求封装,支持超时、拦截器、错误处理等功能
|
|
21
|
-
* 支持的fetch配置项:
|
|
22
|
-
* - method: HTTP请求方法 (GET, POST, PUT, DELETE等)
|
|
23
|
-
* - mode: 跨域策略 ("cors", "same-origin", "no-cors")
|
|
24
|
-
* - cache: 缓存策略 ("no-cache", "default", "reload", "force-cache")
|
|
25
|
-
* - credentials: 凭证策略 ("same-origin", "include", "omit")
|
|
26
|
-
* - headers: 请求头,如 {"Content-Type": "application/json"}
|
|
27
|
-
* - redirect: 重定向处理 ("follow", "manual", "error")
|
|
28
|
-
* - referrerPolicy: 引用策略 ("no-referrer", "origin", "strict-origin")
|
|
29
|
-
* - body: 请求体,通常使用 JSON.stringify(data) 转换对象
|
|
30
|
-
* @author DJ
|
|
31
|
-
* @since 2025/3/31
|
|
32
|
-
*/
|
|
1
|
+
import { RequestConf, RequestOptions } from "./types";
|
|
2
|
+
/** Fetch请求客户端类 */
|
|
33
3
|
export default class {
|
|
34
|
-
options:
|
|
35
|
-
conf:
|
|
36
|
-
/**
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
* @param conf.callback 成功回调
|
|
41
|
-
* @param conf.errBack 失败回调
|
|
42
|
-
* @param conf.abController 配置中断控制
|
|
43
|
-
* @param conf.timeout 超时
|
|
44
|
-
* @param conf.baseUrl 前缀
|
|
45
|
-
* @param conf.beforeBack (arg:newOptions) 请求前回调 可以设置数据
|
|
46
|
-
*/
|
|
47
|
-
constructor(option?: any, conf?: conf);
|
|
48
|
-
/** 2025/3/28 15:55 User: DJ
|
|
49
|
-
* content: 创建服务
|
|
50
|
-
*/
|
|
51
|
-
serve(url?: string, options?: any, conf?: conf): Promise<any>;
|
|
4
|
+
options: RequestOptions;
|
|
5
|
+
conf: RequestConf;
|
|
6
|
+
/** 请求处理 */
|
|
7
|
+
constructor(option?: RequestOptions, conf?: RequestConf);
|
|
8
|
+
/** 创建服务 */
|
|
9
|
+
serve(url?: string, options?: RequestOptions, conf?: RequestConf): Promise<any>;
|
|
52
10
|
}
|
|
11
|
+
/** @deprecated 使用 RequestConf 代替 */
|
|
12
|
+
export type conf = RequestConf;
|
package/lib/types.d.ts
CHANGED
|
@@ -7,3 +7,41 @@ export interface formatSizeOptions {
|
|
|
7
7
|
/** 转换进制,默认为1024 */
|
|
8
8
|
perUnit?: number;
|
|
9
9
|
}
|
|
10
|
+
/** HTTP请求方法类型 */
|
|
11
|
+
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS' | 'CONNECT' | 'TRACE';
|
|
12
|
+
/** 跨域策略类型 */
|
|
13
|
+
export type RequestMode = 'cors' | 'same-origin' | 'no-cors';
|
|
14
|
+
/** 缓存策略类型 */
|
|
15
|
+
export type RequestCache = 'no-cache' | 'default' | 'reload' | 'force-cache' | 'only-if-cached';
|
|
16
|
+
/** 凭证策略类型 */
|
|
17
|
+
export type RequestCredentials = 'same-origin' | 'include' | 'omit';
|
|
18
|
+
/** 重定向处理类型 */
|
|
19
|
+
export type RequestRedirect = 'follow' | 'manual' | 'error';
|
|
20
|
+
/** 引用策略类型 */
|
|
21
|
+
export type ReferrerPolicy = 'no-referrer' | 'no-referrer-when-downgrade' | 'origin' | 'origin-when-cross-origin' | 'same-origin' | 'strict-origin' | 'strict-origin-when-cross-origin' | 'unsafe-url';
|
|
22
|
+
/** 请求头类型 */
|
|
23
|
+
export type RequestHeaders = Record<string, string> | Headers;
|
|
24
|
+
/** 请求选项接口 */
|
|
25
|
+
export interface RequestOptions {
|
|
26
|
+
method?: HttpMethod;
|
|
27
|
+
mode?: RequestMode;
|
|
28
|
+
cache?: RequestCache;
|
|
29
|
+
credentials?: RequestCredentials;
|
|
30
|
+
headers?: RequestHeaders;
|
|
31
|
+
redirect?: RequestRedirect;
|
|
32
|
+
referrerPolicy?: ReferrerPolicy;
|
|
33
|
+
body?: BodyInit | null;
|
|
34
|
+
referrer?: string;
|
|
35
|
+
signal?: AbortSignal;
|
|
36
|
+
window?: null;
|
|
37
|
+
[key: string]: any;
|
|
38
|
+
}
|
|
39
|
+
/** 请求配置接口 */
|
|
40
|
+
export interface RequestConf {
|
|
41
|
+
baseUrl?: string;
|
|
42
|
+
timeout?: number;
|
|
43
|
+
callback?: (response: Response, conf: RequestConf) => Promise<any> | any;
|
|
44
|
+
errBack?: (error: Error) => void;
|
|
45
|
+
abController?: (controller: AbortController) => void;
|
|
46
|
+
beforeBack?: (options: RequestOptions) => void;
|
|
47
|
+
}
|