@kevisual/query 0.0.1 → 0.0.2-alpha.0

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 CHANGED
@@ -2,6 +2,7 @@ type AdapterOpts = {
2
2
  url: string;
3
3
  headers?: Record<string, string>;
4
4
  body?: Record<string, any>;
5
+ timeout?: number;
5
6
  };
6
7
  export declare const adapter: (opts: AdapterOpts) => Promise<any>;
7
8
  export {};
package/dist/index.d.ts CHANGED
@@ -4,20 +4,48 @@ 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;
12
14
  };
15
+ type Data = {
16
+ path?: string;
17
+ key?: string;
18
+ payload?: Record<string, any>;
19
+ [key: string]: any;
20
+ };
21
+ type Result<S = any> = {
22
+ code: number;
23
+ data?: S;
24
+ message?: string;
25
+ success: boolean;
26
+ };
27
+ type DataOpts = Partial<QueryOpts> & {
28
+ beforeRequest?: Fn;
29
+ afterResponse?: (result: Result) => Promise<any>;
30
+ };
31
+ /**
32
+ * const query = new Query();
33
+ * const res = await query.post({
34
+ * path: 'demo',
35
+ * key: '1',
36
+ * });
37
+ */
13
38
  export declare class Query {
14
39
  adapter: typeof adapter;
15
40
  url: string;
16
41
  beforeRequest?: Fn;
42
+ afterResponse?: (result: Result) => Promise<any>;
17
43
  headers?: Record<string, string>;
44
+ timeout?: number;
18
45
  constructor(opts: QueryOpts);
19
- get(params: Record<string, any>): Promise<any>;
20
- post(body: Record<string, any>): Promise<any>;
46
+ get<T, S>(params: Record<string, any> & Data & T, options?: DataOpts): Promise<Result<S>>;
47
+ post<T, S>(body: Record<string, any> & Data & T, options?: DataOpts): Promise<Result<S>>;
21
48
  before(fn: Fn): void;
49
+ after(fn: (result: Result) => Promise<any>): void;
22
50
  }
23
51
  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,38 +33,66 @@ 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
 
42
+ /**
43
+ * const query = new Query();
44
+ * const res = await query.post({
45
+ * path: 'demo',
46
+ * key: '1',
47
+ * });
48
+ */
32
49
  class Query {
33
50
  adapter;
34
51
  url;
35
52
  beforeRequest;
53
+ afterResponse;
36
54
  headers;
55
+ timeout;
37
56
  constructor(opts) {
38
57
  this.adapter = opts.adapter || adapter;
39
58
  this.url = opts.url || '/api/router';
40
59
  this.headers = opts.headers || {
41
60
  'Content-Type': 'application/json',
42
61
  };
62
+ this.timeout = opts.timeout || 60000; // 默认超时时间为 60s
43
63
  }
44
- async get(params) {
45
- return this.post(params);
64
+ async get(params, options) {
65
+ return this.post(params, options);
46
66
  }
47
- async post(body) {
67
+ async post(body, options) {
68
+ const url = options?.url || this.url;
69
+ const headers = { ...this.headers, ...options?.headers };
70
+ const adapter = options?.adapter || this.adapter;
71
+ const beforeRequest = options?.beforeRequest || this.beforeRequest;
72
+ const timeout = options?.timeout || this.timeout;
48
73
  const req = {
49
- url: this.url,
50
- headers: this.headers,
74
+ url: url,
75
+ headers: headers,
51
76
  body,
77
+ timeout,
52
78
  };
53
- if (this.beforeRequest) {
54
- await this.beforeRequest(req);
79
+ if (beforeRequest) {
80
+ await beforeRequest(req);
55
81
  }
56
- return this.adapter(req);
82
+ return adapter(req).then(async (res) => {
83
+ res.success = res.code === 200;
84
+ if (options?.afterResponse) {
85
+ return await options.afterResponse(res);
86
+ }
87
+ return res;
88
+ });
57
89
  }
58
90
  before(fn) {
59
91
  this.beforeRequest = fn;
60
92
  }
93
+ after(fn) {
94
+ this.afterResponse = fn;
95
+ }
61
96
  }
62
97
 
63
98
  export { Query, adapter };
@@ -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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kevisual/query",
3
- "version": "0.0.1",
3
+ "version": "0.0.2-alpha.0",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
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
+ ```