@kevisual/query 0.0.1 → 0.0.2
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/dist/adapter.d.ts +1 -0
- package/dist/index.d.ts +13 -2
- package/dist/index.js +26 -8
- package/dist/node-adapter.js +0 -1
- package/package.json +1 -1
- package/readme.md +34 -0
package/dist/adapter.d.ts
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -4,20 +4,31 @@ type Fn = (opts: {
|
|
|
4
4
|
headers?: Record<string, string>;
|
|
5
5
|
body?: Record<string, any>;
|
|
6
6
|
[key: string]: any;
|
|
7
|
+
timeout?: number;
|
|
7
8
|
}) => Promise<Record<string, any>>;
|
|
8
9
|
type QueryOpts = {
|
|
9
10
|
url?: string;
|
|
10
11
|
adapter?: typeof adapter;
|
|
11
12
|
headers?: Record<string, string>;
|
|
13
|
+
timeout?: number;
|
|
14
|
+
};
|
|
15
|
+
type Data = {
|
|
16
|
+
path?: string;
|
|
17
|
+
key?: string;
|
|
18
|
+
[key: string]: any;
|
|
19
|
+
};
|
|
20
|
+
type DataOpts = Partial<QueryOpts> & {
|
|
21
|
+
beforeRequest?: Fn;
|
|
12
22
|
};
|
|
13
23
|
export declare class Query {
|
|
14
24
|
adapter: typeof adapter;
|
|
15
25
|
url: string;
|
|
16
26
|
beforeRequest?: Fn;
|
|
17
27
|
headers?: Record<string, string>;
|
|
28
|
+
timeout?: number;
|
|
18
29
|
constructor(opts: QueryOpts);
|
|
19
|
-
get(params: Record<string, any>): Promise<any>;
|
|
20
|
-
post(body: Record<string, any>): Promise<any>;
|
|
30
|
+
get<T>(params: Record<string, any> & Data & T, options?: DataOpts): Promise<any>;
|
|
31
|
+
post<T>(body: Record<string, any> & Data & T, options?: DataOpts): Promise<any>;
|
|
21
32
|
before(fn: Fn): void;
|
|
22
33
|
}
|
|
23
34
|
export { adapter };
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
const adapter = async (opts) => {
|
|
2
|
+
const controller = new AbortController();
|
|
3
|
+
const signal = controller.signal;
|
|
4
|
+
const timeout = opts.timeout || 60000; // 默认超时时间为 60s
|
|
5
|
+
const timer = setTimeout(() => {
|
|
6
|
+
controller.abort();
|
|
7
|
+
}, timeout);
|
|
2
8
|
return fetch(opts.url, {
|
|
3
9
|
method: 'POST',
|
|
4
10
|
headers: {
|
|
@@ -6,6 +12,7 @@ const adapter = async (opts) => {
|
|
|
6
12
|
...opts.headers,
|
|
7
13
|
},
|
|
8
14
|
body: JSON.stringify(opts.body),
|
|
15
|
+
signal,
|
|
9
16
|
})
|
|
10
17
|
.then((response) => {
|
|
11
18
|
// 获取 Content-Type 头部信息
|
|
@@ -26,6 +33,9 @@ const adapter = async (opts) => {
|
|
|
26
33
|
return {
|
|
27
34
|
code: 500,
|
|
28
35
|
};
|
|
36
|
+
})
|
|
37
|
+
.finally(() => {
|
|
38
|
+
clearTimeout(timer);
|
|
29
39
|
});
|
|
30
40
|
};
|
|
31
41
|
|
|
@@ -34,26 +44,34 @@ class Query {
|
|
|
34
44
|
url;
|
|
35
45
|
beforeRequest;
|
|
36
46
|
headers;
|
|
47
|
+
timeout;
|
|
37
48
|
constructor(opts) {
|
|
38
49
|
this.adapter = opts.adapter || adapter;
|
|
39
50
|
this.url = opts.url || '/api/router';
|
|
40
51
|
this.headers = opts.headers || {
|
|
41
52
|
'Content-Type': 'application/json',
|
|
42
53
|
};
|
|
54
|
+
this.timeout = opts.timeout || 60000; // 默认超时时间为 60s
|
|
43
55
|
}
|
|
44
|
-
async get(params) {
|
|
45
|
-
return this.post(params);
|
|
56
|
+
async get(params, options) {
|
|
57
|
+
return this.post(params, options);
|
|
46
58
|
}
|
|
47
|
-
async post(body) {
|
|
59
|
+
async post(body, options) {
|
|
60
|
+
const url = options?.url || this.url;
|
|
61
|
+
const headers = { ...this.headers, ...options?.headers };
|
|
62
|
+
const adapter = options?.adapter || this.adapter;
|
|
63
|
+
const beforeRequest = options?.beforeRequest || this.beforeRequest;
|
|
64
|
+
const timeout = options?.timeout || this.timeout;
|
|
48
65
|
const req = {
|
|
49
|
-
url:
|
|
50
|
-
headers:
|
|
66
|
+
url: url,
|
|
67
|
+
headers: headers,
|
|
51
68
|
body,
|
|
69
|
+
timeout,
|
|
52
70
|
};
|
|
53
|
-
if (
|
|
54
|
-
await
|
|
71
|
+
if (beforeRequest) {
|
|
72
|
+
await beforeRequest(req);
|
|
55
73
|
}
|
|
56
|
-
return
|
|
74
|
+
return adapter(req);
|
|
57
75
|
}
|
|
58
76
|
before(fn) {
|
|
59
77
|
this.beforeRequest = fn;
|
package/dist/node-adapter.js
CHANGED
|
@@ -5,7 +5,6 @@ const nodeAdapter = async (opts) => {
|
|
|
5
5
|
const postData = JSON.stringify(opts.body || '');
|
|
6
6
|
const _url = new URL(opts.url);
|
|
7
7
|
const { hostname, port, pathname } = _url;
|
|
8
|
-
console.log('hostname:', hostname, port, pathname);
|
|
9
8
|
const options = {
|
|
10
9
|
hostname: hostname,
|
|
11
10
|
port: port,
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -1,2 +1,36 @@
|
|
|
1
1
|
# query
|
|
2
2
|
|
|
3
|
+
对应的 fetch 内容的一部分功能的封装。
|
|
4
|
+
|
|
5
|
+
主要目的:请求路径默认`/api/router`,使用`post`,`post`的数据分流使用`path`和`key`.
|
|
6
|
+
|
|
7
|
+
## query
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
const query = new Query();
|
|
12
|
+
const res = await query.post({
|
|
13
|
+
path: 'demo',
|
|
14
|
+
key: '1',
|
|
15
|
+
});
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
### 参数
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
type QueryOpts = {
|
|
22
|
+
url?: string;
|
|
23
|
+
adapter?: typeof adapter;
|
|
24
|
+
headers?: Record<string, string>;
|
|
25
|
+
timeout?: number;
|
|
26
|
+
};
|
|
27
|
+
type Data = {
|
|
28
|
+
path?: string;
|
|
29
|
+
key?: string;
|
|
30
|
+
[key: string]: any;
|
|
31
|
+
};
|
|
32
|
+
// 额外功能
|
|
33
|
+
type DataOpts = Partial<QueryOpts> & {
|
|
34
|
+
beforeRequest?: Fn;
|
|
35
|
+
};
|
|
36
|
+
```
|