@kevisual/query 0.0.12 → 0.0.14

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.
@@ -1,8 +1,11 @@
1
+ declare const methods: readonly ["GET", "POST"];
2
+ type Method = (typeof methods)[number];
1
3
  type AdapterOpts = {
2
4
  url: string;
3
5
  headers?: Record<string, string>;
4
6
  body?: Record<string, any>;
5
7
  timeout?: number;
8
+ method?: Method;
6
9
  };
7
10
  /**
8
11
  *
@@ -16,4 +19,5 @@ declare const adapter: (opts: AdapterOpts, overloadOpts?: RequestInit) => Promis
16
19
  */
17
20
  declare const queryFetch: (opts: AdapterOpts, overloadOpts?: RequestInit) => Promise<any>;
18
21
 
19
- export { adapter, queryFetch };
22
+ export { adapter, methods, queryFetch };
23
+ export type { AdapterOpts, Method };
@@ -1,3 +1,4 @@
1
+ const methods = ['GET', 'POST'];
1
2
  /**
2
3
  *
3
4
  * @param opts
@@ -11,15 +12,21 @@ const adapter = async (opts, overloadOpts) => {
11
12
  const timer = setTimeout(() => {
12
13
  controller.abort();
13
14
  }, timeout);
14
- return fetch(opts.url, {
15
- method: 'POST',
15
+ let method = overloadOpts?.method || opts.method || 'POST';
16
+ let url = new URL(opts.url, window.location.origin);
17
+ const isGet = method === 'GET';
18
+ if (isGet) {
19
+ url.search = new URLSearchParams(opts.body).toString();
20
+ }
21
+ return fetch(url, {
22
+ method: method,
16
23
  headers: {
17
24
  'Content-Type': 'application/json',
18
25
  ...opts.headers,
19
26
  },
20
- body: JSON.stringify(opts.body),
21
27
  signal,
22
28
  ...overloadOpts,
29
+ body: isGet ? undefined : JSON.stringify(opts.body),
23
30
  })
24
31
  .then((response) => {
25
32
  // 获取 Content-Type 头部信息
@@ -50,4 +57,4 @@ const adapter = async (opts, overloadOpts) => {
50
57
  */
51
58
  const queryFetch = adapter;
52
59
 
53
- export { adapter, queryFetch };
60
+ export { adapter, methods, queryFetch };
@@ -1,10 +1,13 @@
1
1
  import { StoreApi } from 'zustand/vanilla';
2
2
 
3
+ declare const methods: readonly ["GET", "POST"];
4
+ type Method = (typeof methods)[number];
3
5
  type AdapterOpts = {
4
6
  url: string;
5
7
  headers?: Record<string, string>;
6
8
  body?: Record<string, any>;
7
9
  timeout?: number;
10
+ method?: Method;
8
11
  };
9
12
  /**
10
13
  *
@@ -67,6 +70,8 @@ type QueryOpts$1 = {
67
70
  adapter?: typeof adapter;
68
71
  headers?: Record<string, string>;
69
72
  timeout?: number;
73
+ method?: Method;
74
+ [key: string]: any;
70
75
  };
71
76
  type Data = {
72
77
  path?: string;
@@ -93,11 +98,11 @@ type Result<S = any> = {
93
98
  };
94
99
  type DataOpts = Partial<QueryOpts$1> & {
95
100
  beforeRequest?: Fn;
96
- afterResponse?: <S, U = S>(result: Result<S>, ctx?: {
101
+ afterResponse?: <S = any>(result: Result<S>, ctx?: {
97
102
  req?: any;
98
103
  res?: any;
99
104
  fetch?: any;
100
- }) => Promise<U>;
105
+ }) => Promise<S>;
101
106
  };
102
107
  /**
103
108
  * const query = new Query();
@@ -111,11 +116,19 @@ type DataOpts = Partial<QueryOpts$1> & {
111
116
  declare class Query {
112
117
  adapter: typeof adapter;
113
118
  url: string;
114
- beforeRequest?: Fn;
119
+ beforeRequest?: DataOpts['beforeRequest'];
115
120
  afterResponse?: DataOpts['afterResponse'];
116
121
  headers?: Record<string, string>;
117
122
  timeout?: number;
123
+ /**
124
+ * 需要突然停止请求,比如401的时候
125
+ */
126
+ stop?: boolean;
118
127
  constructor(opts?: QueryOpts$1);
128
+ /**
129
+ * 突然停止请求
130
+ */
131
+ setStop(stop: boolean): void;
119
132
  /**
120
133
  * 发送 get 请求,转到 post 请求
121
134
  * T是请求类型自定义
@@ -138,12 +151,12 @@ declare class Query {
138
151
  * 请求前处理,设置请求前处理函数
139
152
  * @param fn 处理函数
140
153
  */
141
- before(fn: Fn): void;
154
+ before(fn: DataOpts['beforeRequest']): void;
142
155
  /**
143
156
  * 请求后处理,设置请求后处理函数
144
157
  * @param fn 处理函数
145
158
  */
146
- after(fn: (result: Result, req?: any) => Promise<any>): void;
159
+ after(fn: DataOpts['afterResponse']): void;
147
160
  }
148
161
 
149
162
  type QueryOpts = {
@@ -11,15 +11,21 @@ const adapter = async (opts, overloadOpts) => {
11
11
  const timer = setTimeout(() => {
12
12
  controller.abort();
13
13
  }, timeout);
14
- return fetch(opts.url, {
15
- method: 'POST',
14
+ let method = overloadOpts?.method || opts.method || 'POST';
15
+ let url = new URL(opts.url, window.location.origin);
16
+ const isGet = method === 'GET';
17
+ if (isGet) {
18
+ url.search = new URLSearchParams(opts.body).toString();
19
+ }
20
+ return fetch(url, {
21
+ method: method,
16
22
  headers: {
17
23
  'Content-Type': 'application/json',
18
24
  ...opts.headers,
19
25
  },
20
- body: JSON.stringify(opts.body),
21
26
  signal,
22
27
  ...overloadOpts,
28
+ body: isGet ? undefined : JSON.stringify(opts.body),
23
29
  })
24
30
  .then((response) => {
25
31
  // 获取 Content-Type 头部信息
@@ -255,6 +261,10 @@ class Query {
255
261
  afterResponse;
256
262
  headers;
257
263
  timeout;
264
+ /**
265
+ * 需要突然停止请求,比如401的时候
266
+ */
267
+ stop;
258
268
  constructor(opts) {
259
269
  this.adapter = opts?.adapter || adapter;
260
270
  this.url = opts?.url || '/api/router';
@@ -263,6 +273,12 @@ class Query {
263
273
  };
264
274
  this.timeout = opts?.timeout || 60000 * 3; // 默认超时时间为 60s * 3
265
275
  }
276
+ /**
277
+ * 突然停止请求
278
+ */
279
+ setStop(stop) {
280
+ this.stop = stop;
281
+ }
266
282
  /**
267
283
  * 发送 get 请求,转到 post 请求
268
284
  * T是请求类型自定义
@@ -284,20 +300,22 @@ class Query {
284
300
  */
285
301
  async post(body, options) {
286
302
  const url = options?.url || this.url;
287
- const headers = { ...this.headers, ...options?.headers };
288
- const adapter = options?.adapter || this.adapter;
289
- const beforeRequest = options?.beforeRequest || this.beforeRequest;
290
- const afterResponse = options?.afterResponse || this.afterResponse;
291
- const timeout = options?.timeout || this.timeout;
303
+ const { headers, adapter, beforeRequest, afterResponse, timeout, ...rest } = options || {};
304
+ const _headers = { ...this.headers, ...headers };
305
+ const _adapter = adapter || this.adapter;
306
+ const _beforeRequest = beforeRequest || this.beforeRequest;
307
+ const _afterResponse = afterResponse || this.afterResponse;
308
+ const _timeout = timeout || this.timeout;
292
309
  const req = {
293
310
  url: url,
294
- headers: headers,
311
+ headers: _headers,
295
312
  body,
296
- timeout,
313
+ timeout: _timeout,
314
+ ...rest,
297
315
  };
298
316
  try {
299
- if (beforeRequest) {
300
- await beforeRequest(req);
317
+ if (_beforeRequest) {
318
+ await _beforeRequest(req);
301
319
  }
302
320
  }
303
321
  catch (e) {
@@ -309,11 +327,27 @@ class Query {
309
327
  showError: () => { },
310
328
  };
311
329
  }
312
- return adapter(req).then(async (res) => {
330
+ if (this.stop) {
331
+ const that = this;
332
+ await new Promise((resolve) => {
333
+ let timer = 0;
334
+ const detect = setInterval(() => {
335
+ if (!that.stop) {
336
+ clearInterval(detect);
337
+ resolve(true);
338
+ }
339
+ timer++;
340
+ if (timer > 30) {
341
+ console.error('request stop: timeout', req.url, timer);
342
+ }
343
+ }, 1000);
344
+ });
345
+ }
346
+ return _adapter(req).then(async (res) => {
313
347
  try {
314
348
  setBaseResponse(res);
315
- if (afterResponse) {
316
- return await afterResponse(res, {
349
+ if (_afterResponse) {
350
+ return await _afterResponse(res, {
317
351
  req,
318
352
  res,
319
353
  fetch: adapter,
package/dist/query.d.ts CHANGED
@@ -1,8 +1,11 @@
1
+ declare const methods: readonly ["GET", "POST"];
2
+ type Method = (typeof methods)[number];
1
3
  type AdapterOpts = {
2
4
  url: string;
3
5
  headers?: Record<string, string>;
4
6
  body?: Record<string, any>;
5
7
  timeout?: number;
8
+ method?: Method;
6
9
  };
7
10
  /**
8
11
  *
@@ -29,6 +32,8 @@ type QueryOpts = {
29
32
  adapter?: typeof adapter;
30
33
  headers?: Record<string, string>;
31
34
  timeout?: number;
35
+ method?: Method;
36
+ [key: string]: any;
32
37
  };
33
38
  type Data = {
34
39
  path?: string;
@@ -55,11 +60,11 @@ type Result<S = any> = {
55
60
  };
56
61
  type DataOpts = Partial<QueryOpts> & {
57
62
  beforeRequest?: Fn;
58
- afterResponse?: <S, U = S>(result: Result<S>, ctx?: {
63
+ afterResponse?: <S = any>(result: Result<S>, ctx?: {
59
64
  req?: any;
60
65
  res?: any;
61
66
  fetch?: any;
62
- }) => Promise<U>;
67
+ }) => Promise<S>;
63
68
  };
64
69
  /**
65
70
  * 设置基础响应, 设置 success 和 showError,
@@ -80,11 +85,19 @@ declare const setBaseResponse: (res: Result) => void;
80
85
  declare class Query {
81
86
  adapter: typeof adapter;
82
87
  url: string;
83
- beforeRequest?: Fn;
88
+ beforeRequest?: DataOpts['beforeRequest'];
84
89
  afterResponse?: DataOpts['afterResponse'];
85
90
  headers?: Record<string, string>;
86
91
  timeout?: number;
92
+ /**
93
+ * 需要突然停止请求,比如401的时候
94
+ */
95
+ stop?: boolean;
87
96
  constructor(opts?: QueryOpts);
97
+ /**
98
+ * 突然停止请求
99
+ */
100
+ setStop(stop: boolean): void;
88
101
  /**
89
102
  * 发送 get 请求,转到 post 请求
90
103
  * T是请求类型自定义
@@ -107,12 +120,12 @@ declare class Query {
107
120
  * 请求前处理,设置请求前处理函数
108
121
  * @param fn 处理函数
109
122
  */
110
- before(fn: Fn): void;
123
+ before(fn: DataOpts['beforeRequest']): void;
111
124
  /**
112
125
  * 请求后处理,设置请求后处理函数
113
126
  * @param fn 处理函数
114
127
  */
115
- after(fn: (result: Result, req?: any) => Promise<any>): void;
128
+ after(fn: DataOpts['afterResponse']): void;
116
129
  }
117
130
 
118
131
  export { Query, adapter, setBaseResponse };
package/dist/query.js CHANGED
@@ -11,15 +11,21 @@ const adapter = async (opts, overloadOpts) => {
11
11
  const timer = setTimeout(() => {
12
12
  controller.abort();
13
13
  }, timeout);
14
- return fetch(opts.url, {
15
- method: 'POST',
14
+ let method = overloadOpts?.method || opts.method || 'POST';
15
+ let url = new URL(opts.url, window.location.origin);
16
+ const isGet = method === 'GET';
17
+ if (isGet) {
18
+ url.search = new URLSearchParams(opts.body).toString();
19
+ }
20
+ return fetch(url, {
21
+ method: method,
16
22
  headers: {
17
23
  'Content-Type': 'application/json',
18
24
  ...opts.headers,
19
25
  },
20
- body: JSON.stringify(opts.body),
21
26
  signal,
22
27
  ...overloadOpts,
28
+ body: isGet ? undefined : JSON.stringify(opts.body),
23
29
  })
24
30
  .then((response) => {
25
31
  // 获取 Content-Type 头部信息
@@ -80,6 +86,10 @@ class Query {
80
86
  afterResponse;
81
87
  headers;
82
88
  timeout;
89
+ /**
90
+ * 需要突然停止请求,比如401的时候
91
+ */
92
+ stop;
83
93
  constructor(opts) {
84
94
  this.adapter = opts?.adapter || adapter;
85
95
  this.url = opts?.url || '/api/router';
@@ -88,6 +98,12 @@ class Query {
88
98
  };
89
99
  this.timeout = opts?.timeout || 60000 * 3; // 默认超时时间为 60s * 3
90
100
  }
101
+ /**
102
+ * 突然停止请求
103
+ */
104
+ setStop(stop) {
105
+ this.stop = stop;
106
+ }
91
107
  /**
92
108
  * 发送 get 请求,转到 post 请求
93
109
  * T是请求类型自定义
@@ -109,20 +125,22 @@ class Query {
109
125
  */
110
126
  async post(body, options) {
111
127
  const url = options?.url || this.url;
112
- const headers = { ...this.headers, ...options?.headers };
113
- const adapter = options?.adapter || this.adapter;
114
- const beforeRequest = options?.beforeRequest || this.beforeRequest;
115
- const afterResponse = options?.afterResponse || this.afterResponse;
116
- const timeout = options?.timeout || this.timeout;
128
+ const { headers, adapter, beforeRequest, afterResponse, timeout, ...rest } = options || {};
129
+ const _headers = { ...this.headers, ...headers };
130
+ const _adapter = adapter || this.adapter;
131
+ const _beforeRequest = beforeRequest || this.beforeRequest;
132
+ const _afterResponse = afterResponse || this.afterResponse;
133
+ const _timeout = timeout || this.timeout;
117
134
  const req = {
118
135
  url: url,
119
- headers: headers,
136
+ headers: _headers,
120
137
  body,
121
- timeout,
138
+ timeout: _timeout,
139
+ ...rest,
122
140
  };
123
141
  try {
124
- if (beforeRequest) {
125
- await beforeRequest(req);
142
+ if (_beforeRequest) {
143
+ await _beforeRequest(req);
126
144
  }
127
145
  }
128
146
  catch (e) {
@@ -134,11 +152,27 @@ class Query {
134
152
  showError: () => { },
135
153
  };
136
154
  }
137
- return adapter(req).then(async (res) => {
155
+ if (this.stop) {
156
+ const that = this;
157
+ await new Promise((resolve) => {
158
+ let timer = 0;
159
+ const detect = setInterval(() => {
160
+ if (!that.stop) {
161
+ clearInterval(detect);
162
+ resolve(true);
163
+ }
164
+ timer++;
165
+ if (timer > 30) {
166
+ console.error('request stop: timeout', req.url, timer);
167
+ }
168
+ }, 1000);
169
+ });
170
+ }
171
+ return _adapter(req).then(async (res) => {
138
172
  try {
139
173
  setBaseResponse(res);
140
- if (afterResponse) {
141
- return await afterResponse(res, {
174
+ if (_afterResponse) {
175
+ return await _afterResponse(res, {
142
176
  req,
143
177
  res,
144
178
  fetch: adapter,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kevisual/query",
3
- "version": "0.0.12",
3
+ "version": "0.0.14",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",