@kevisual/query 0.0.7-alpha.3 → 0.0.8

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.
@@ -0,0 +1,19 @@
1
+ type AdapterOpts = {
2
+ url: string;
3
+ headers?: Record<string, string>;
4
+ body?: Record<string, any>;
5
+ timeout?: number;
6
+ };
7
+ /**
8
+ *
9
+ * @param opts
10
+ * @param overloadOpts 覆盖fetch的默认配置
11
+ * @returns
12
+ */
13
+ declare const adapter: (opts: AdapterOpts, overloadOpts?: RequestInit) => Promise<any>;
14
+ /**
15
+ * adapter
16
+ */
17
+ declare const queryFetch: (opts: AdapterOpts, overloadOpts?: RequestInit) => Promise<any>;
18
+
19
+ export { adapter, queryFetch };
@@ -0,0 +1,53 @@
1
+ /**
2
+ *
3
+ * @param opts
4
+ * @param overloadOpts 覆盖fetch的默认配置
5
+ * @returns
6
+ */
7
+ const adapter = async (opts, overloadOpts) => {
8
+ const controller = new AbortController();
9
+ const signal = controller.signal;
10
+ const timeout = opts.timeout || 60000 * 3; // 默认超时时间为 60s * 3
11
+ const timer = setTimeout(() => {
12
+ controller.abort();
13
+ }, timeout);
14
+ return fetch(opts.url, {
15
+ method: 'POST',
16
+ headers: {
17
+ 'Content-Type': 'application/json',
18
+ ...opts.headers,
19
+ },
20
+ body: JSON.stringify(opts.body),
21
+ signal,
22
+ ...overloadOpts,
23
+ })
24
+ .then((response) => {
25
+ // 获取 Content-Type 头部信息
26
+ const contentType = response.headers.get('Content-Type');
27
+ // 判断返回的数据类型
28
+ if (contentType && contentType.includes('application/json')) {
29
+ return response.json(); // 解析为 JSON
30
+ }
31
+ else {
32
+ return response.text(); // 解析为文本
33
+ }
34
+ })
35
+ .catch((err) => {
36
+ if (err.name === 'AbortError') {
37
+ console.log('Request timed out and was aborted');
38
+ }
39
+ console.error(err);
40
+ return {
41
+ code: 500,
42
+ };
43
+ })
44
+ .finally(() => {
45
+ clearTimeout(timer);
46
+ });
47
+ };
48
+ /**
49
+ * adapter
50
+ */
51
+ const queryFetch = adapter;
52
+
53
+ export { adapter, queryFetch };
@@ -6,7 +6,13 @@ type AdapterOpts = {
6
6
  body?: Record<string, any>;
7
7
  timeout?: number;
8
8
  };
9
- declare const adapter: (opts: AdapterOpts) => Promise<any>;
9
+ /**
10
+ *
11
+ * @param opts
12
+ * @param overloadOpts 覆盖fetch的默认配置
13
+ * @returns
14
+ */
15
+ declare const adapter: (opts: AdapterOpts, overloadOpts?: RequestInit) => Promise<any>;
10
16
 
11
17
  type QueryWsStore = {
12
18
  connected: boolean;
@@ -120,6 +126,5 @@ declare class QueryClient<U = any, V = any> extends Query<U, V> {
120
126
  saveToken(token: string): void;
121
127
  removeToken(): void;
122
128
  }
123
- declare const client: QueryClient<any, any>;
124
129
 
125
- export { Query, QueryClient, type QueryOpts, QueryWs, type QueryWsOpts, adapter, client };
130
+ export { Query, QueryClient, type QueryOpts, QueryWs, type QueryWsOpts, adapter };
@@ -1,4 +1,10 @@
1
- const adapter = async (opts) => {
1
+ /**
2
+ *
3
+ * @param opts
4
+ * @param overloadOpts 覆盖fetch的默认配置
5
+ * @returns
6
+ */
7
+ const adapter = async (opts, overloadOpts) => {
2
8
  const controller = new AbortController();
3
9
  const signal = controller.signal;
4
10
  const timeout = opts.timeout || 60000 * 3; // 默认超时时间为 60s * 3
@@ -13,6 +19,7 @@ const adapter = async (opts) => {
13
19
  },
14
20
  body: JSON.stringify(opts.body),
15
21
  signal,
22
+ ...overloadOpts,
16
23
  })
17
24
  .then((response) => {
18
25
  // 获取 Content-Type 头部信息
@@ -56,15 +63,7 @@ const createStoreImpl = (createState) => {
56
63
  listeners.add(listener);
57
64
  return () => listeners.delete(listener);
58
65
  };
59
- const destroy = () => {
60
- if ((import.meta.env ? import.meta.env.MODE : void 0) !== "production") {
61
- console.warn(
62
- "[DEPRECATED] The `destroy` method will be unsupported in a future version. Instead use unsubscribe function returned by subscribe. Everything will be garbage-collected if store is garbage-collected."
63
- );
64
- }
65
- listeners.clear();
66
- };
67
- const api = { setState, getState, getInitialState, subscribe, destroy };
66
+ const api = { setState, getState, getInitialState, subscribe };
68
67
  const initialState = state = createState(setState, getState, api);
69
68
  return api;
70
69
  };
@@ -262,15 +261,35 @@ class Query {
262
261
  body,
263
262
  timeout,
264
263
  };
265
- if (beforeRequest) {
266
- await beforeRequest(req);
264
+ try {
265
+ if (beforeRequest) {
266
+ await beforeRequest(req);
267
+ }
268
+ }
269
+ catch (e) {
270
+ console.error(e);
271
+ return {
272
+ code: 500,
273
+ success: false,
274
+ message: 'api request beforeFn error',
275
+ };
267
276
  }
268
277
  return adapter(req).then(async (res) => {
269
- res.success = res.code === 200;
270
- if (afterResponse) {
271
- return await afterResponse(res);
278
+ try {
279
+ res.success = res.code === 200;
280
+ if (afterResponse) {
281
+ return await afterResponse(res);
282
+ }
283
+ return res;
284
+ }
285
+ catch (e) {
286
+ console.error(e);
287
+ return {
288
+ code: 500,
289
+ success: false,
290
+ message: 'api request afterFn error',
291
+ };
272
292
  }
273
- return res;
274
293
  });
275
294
  }
276
295
  before(fn) {
@@ -288,6 +307,7 @@ class QueryClient extends Query {
288
307
  tokenName;
289
308
  storage;
290
309
  token;
310
+ // 默认不使用ws
291
311
  qws;
292
312
  constructor(opts) {
293
313
  super(opts);
@@ -320,6 +340,7 @@ class QueryClient extends Query {
320
340
  this.storage.removeItem(this.tokenName);
321
341
  }
322
342
  }
323
- const client = new QueryClient();
343
+ // 移除默认生成的实例
344
+ // export const client = new QueryClient();
324
345
 
325
- export { Query, QueryClient, QueryWs, adapter, client };
346
+ export { Query, QueryClient, QueryWs, adapter };
package/dist/query-ws.js CHANGED
@@ -15,15 +15,7 @@ const createStoreImpl = (createState) => {
15
15
  listeners.add(listener);
16
16
  return () => listeners.delete(listener);
17
17
  };
18
- const destroy = () => {
19
- if ((import.meta.env ? import.meta.env.MODE : void 0) !== "production") {
20
- console.warn(
21
- "[DEPRECATED] The `destroy` method will be unsupported in a future version. Instead use unsubscribe function returned by subscribe. Everything will be garbage-collected if store is garbage-collected."
22
- );
23
- }
24
- listeners.clear();
25
- };
26
- const api = { setState, getState, getInitialState, subscribe, destroy };
18
+ const api = { setState, getState, getInitialState, subscribe };
27
19
  const initialState = state = createState(setState, getState, api);
28
20
  return api;
29
21
  };
package/dist/query.d.ts CHANGED
@@ -4,7 +4,13 @@ type AdapterOpts = {
4
4
  body?: Record<string, any>;
5
5
  timeout?: number;
6
6
  };
7
- declare const adapter: (opts: AdapterOpts) => Promise<any>;
7
+ /**
8
+ *
9
+ * @param opts
10
+ * @param overloadOpts 覆盖fetch的默认配置
11
+ * @returns
12
+ */
13
+ declare const adapter: (opts: AdapterOpts, overloadOpts?: RequestInit) => Promise<any>;
8
14
 
9
15
  type Fn = (opts: {
10
16
  url?: string;
package/dist/query.js CHANGED
@@ -1,4 +1,10 @@
1
- const adapter = async (opts) => {
1
+ /**
2
+ *
3
+ * @param opts
4
+ * @param overloadOpts 覆盖fetch的默认配置
5
+ * @returns
6
+ */
7
+ const adapter = async (opts, overloadOpts) => {
2
8
  const controller = new AbortController();
3
9
  const signal = controller.signal;
4
10
  const timeout = opts.timeout || 60000 * 3; // 默认超时时间为 60s * 3
@@ -13,6 +19,7 @@ const adapter = async (opts) => {
13
19
  },
14
20
  body: JSON.stringify(opts.body),
15
21
  signal,
22
+ ...overloadOpts,
16
23
  })
17
24
  .then((response) => {
18
25
  // 获取 Content-Type 头部信息
@@ -79,15 +86,35 @@ class Query {
79
86
  body,
80
87
  timeout,
81
88
  };
82
- if (beforeRequest) {
83
- await beforeRequest(req);
89
+ try {
90
+ if (beforeRequest) {
91
+ await beforeRequest(req);
92
+ }
93
+ }
94
+ catch (e) {
95
+ console.error(e);
96
+ return {
97
+ code: 500,
98
+ success: false,
99
+ message: 'api request beforeFn error',
100
+ };
84
101
  }
85
102
  return adapter(req).then(async (res) => {
86
- res.success = res.code === 200;
87
- if (afterResponse) {
88
- return await afterResponse(res);
103
+ try {
104
+ res.success = res.code === 200;
105
+ if (afterResponse) {
106
+ return await afterResponse(res);
107
+ }
108
+ return res;
109
+ }
110
+ catch (e) {
111
+ console.error(e);
112
+ return {
113
+ code: 500,
114
+ success: false,
115
+ message: 'api request afterFn error',
116
+ };
89
117
  }
90
- return res;
91
118
  });
92
119
  }
93
120
  before(fn) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kevisual/query",
3
- "version": "0.0.7-alpha.3",
3
+ "version": "0.0.8",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -22,16 +22,16 @@
22
22
  "license": "ISC",
23
23
  "description": "",
24
24
  "devDependencies": {
25
- "@rollup/plugin-node-resolve": "^15.2.3",
26
- "@rollup/plugin-typescript": "^11.1.6",
27
- "rollup": "^4.21.2",
25
+ "@rollup/plugin-node-resolve": "^16.0.0",
26
+ "@rollup/plugin-typescript": "^12.1.2",
27
+ "rollup": "^4.34.9",
28
28
  "ts-node": "^10.9.2",
29
- "tslib": "^2.7.0",
30
- "typescript": "^5.5.4",
31
- "zustand": "^4.5.5",
29
+ "tslib": "^2.8.1",
30
+ "typescript": "^5.8.2",
31
+ "zustand": "^5.0.3",
32
32
  "rollup-plugin-dts": "^6.1.1"
33
33
  },
34
- "packageManager": "yarn@1.22.19+sha1.4ba7fc5c6e704fce2066ecbfb0b0d8976fe62447",
34
+ "packageManager": "yarn@1.22.22",
35
35
  "publishConfig": {
36
36
  "access": "public"
37
37
  },