@kevisual/query 0.0.39 → 0.0.41

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,432 @@
1
+ // src/adapter.ts
2
+ var isTextForContentType = (contentType) => {
3
+ if (!contentType)
4
+ return false;
5
+ const textTypes = ["text/", "xml", "html", "javascript", "css", "csv", "plain", "x-www-form-urlencoded", "md"];
6
+ return textTypes.some((type) => contentType.includes(type));
7
+ };
8
+ var adapter = async (opts = {}, overloadOpts) => {
9
+ const controller = new AbortController;
10
+ const signal = controller.signal;
11
+ const isPostFile = opts.isPostFile || false;
12
+ let responseType = opts.responseType || "json";
13
+ if (opts.isBlob) {
14
+ responseType = "blob";
15
+ } else if (opts.isText) {
16
+ responseType = "text";
17
+ }
18
+ const timeout = opts.timeout || 60000 * 3;
19
+ const timer = setTimeout(() => {
20
+ controller.abort();
21
+ }, timeout);
22
+ let method = overloadOpts?.method || opts?.method || "POST";
23
+ let headers = { ...opts?.headers, ...overloadOpts?.headers };
24
+ let origin = "";
25
+ let url;
26
+ if (opts?.url?.startsWith("http")) {
27
+ url = new URL(opts.url);
28
+ } else {
29
+ origin = globalThis?.location?.origin || "http://localhost:51515";
30
+ url = new URL(opts?.url || "", origin);
31
+ }
32
+ const isGet = method === "GET";
33
+ const oldSearchParams = url.searchParams;
34
+ if (isGet) {
35
+ let searchParams = new URLSearchParams({ ...Object.fromEntries(oldSearchParams), ...opts?.params, ...opts?.body });
36
+ url.search = searchParams.toString();
37
+ } else {
38
+ const params = {
39
+ ...Object.fromEntries(oldSearchParams),
40
+ ...opts.params
41
+ };
42
+ const searchParams = new URLSearchParams(params);
43
+ if (typeof opts.body === "object" && opts.body !== null) {
44
+ let body2 = opts.body || {};
45
+ if (!params.path && body2?.path) {
46
+ searchParams.set("path", body2.path);
47
+ if (body2?.key) {
48
+ searchParams.set("key", body2.key);
49
+ }
50
+ }
51
+ }
52
+ url.search = searchParams.toString();
53
+ }
54
+ let body = undefined;
55
+ if (isGet) {
56
+ body = undefined;
57
+ } else if (isPostFile) {
58
+ body = opts.body;
59
+ } else {
60
+ if (opts.body && typeof opts.body === "object" && !(opts.body instanceof FormData)) {
61
+ headers = {
62
+ "Content-Type": "application/json",
63
+ ...headers
64
+ };
65
+ body = JSON.stringify(opts.body);
66
+ }
67
+ }
68
+ return fetch(url, {
69
+ method: method.toUpperCase(),
70
+ signal,
71
+ body,
72
+ ...overloadOpts,
73
+ headers
74
+ }).then(async (response) => {
75
+ const contentType = response.headers.get("Content-Type");
76
+ if (responseType === "blob") {
77
+ return await response.blob();
78
+ }
79
+ const isText = responseType === "text";
80
+ const isJson = contentType && contentType.includes("application/json");
81
+ if (isJson && !isText) {
82
+ return await response.json();
83
+ } else if (isTextForContentType(contentType)) {
84
+ return {
85
+ code: response.status,
86
+ status: response.status,
87
+ data: await response.text()
88
+ };
89
+ } else {
90
+ return response;
91
+ }
92
+ }).catch((err) => {
93
+ if (err.name === "AbortError") {
94
+ return {
95
+ code: 408,
96
+ message: "请求超时"
97
+ };
98
+ }
99
+ return {
100
+ code: 500,
101
+ message: err.message || "网络错误"
102
+ };
103
+ }).finally(() => {
104
+ clearTimeout(timer);
105
+ });
106
+ };
107
+
108
+ // src/query.ts
109
+ var wrapperError = ({ code, message }) => {
110
+ const result = {
111
+ code: code || 500,
112
+ success: false,
113
+ message: message || "api request error",
114
+ showError: (fn) => {},
115
+ noMsg: true
116
+ };
117
+ return result;
118
+ };
119
+
120
+ class Query {
121
+ adapter;
122
+ url;
123
+ beforeRequest;
124
+ afterResponse;
125
+ headers;
126
+ timeout;
127
+ stop;
128
+ qws;
129
+ isClient = false;
130
+ constructor(opts) {
131
+ this.adapter = opts?.adapter || adapter;
132
+ const defaultURL = opts?.isClient ? "/client/router" : "/api/router";
133
+ this.url = opts?.url || defaultURL;
134
+ this.headers = opts?.headers || {
135
+ "Content-Type": "application/json"
136
+ };
137
+ this.timeout = opts?.timeout || 60000 * 3;
138
+ if (opts?.beforeRequest) {
139
+ this.beforeRequest = opts.beforeRequest;
140
+ } else {
141
+ this.beforeRequest = async (opts2) => {
142
+ const token = globalThis?.localStorage?.getItem("token");
143
+ if (token) {
144
+ opts2.headers = {
145
+ ...opts2.headers,
146
+ Authorization: `Bearer ${token}`
147
+ };
148
+ }
149
+ return opts2;
150
+ };
151
+ }
152
+ }
153
+ setQueryWs(qws) {
154
+ this.qws = qws;
155
+ }
156
+ setStop(stop) {
157
+ this.stop = stop;
158
+ }
159
+ async get(params, options) {
160
+ return this.post(params, options);
161
+ }
162
+ async post(body, options) {
163
+ const url = options?.url || this.url;
164
+ console.log("query post", url, body, options);
165
+ const { headers, adapter: adapter2, beforeRequest, afterResponse, timeout, ...rest } = options || {};
166
+ const _headers = { ...this.headers, ...headers };
167
+ const _adapter = adapter2 || this.adapter;
168
+ const _beforeRequest = beforeRequest || this.beforeRequest;
169
+ const _afterResponse = afterResponse || this.afterResponse;
170
+ const _timeout = timeout || this.timeout;
171
+ const req = {
172
+ url,
173
+ headers: _headers,
174
+ body,
175
+ timeout: _timeout,
176
+ ...rest
177
+ };
178
+ try {
179
+ if (_beforeRequest) {
180
+ const res = await _beforeRequest(req);
181
+ if (res === false) {
182
+ return wrapperError({
183
+ code: 500,
184
+ message: "request is cancel",
185
+ req
186
+ });
187
+ }
188
+ }
189
+ } catch (e) {
190
+ console.error("request beforeFn error", e, req);
191
+ return wrapperError({
192
+ code: 500,
193
+ message: "api request beforeFn error",
194
+ req
195
+ });
196
+ }
197
+ if (this.stop && !options?.noStop) {
198
+ const that = this;
199
+ await new Promise((resolve) => {
200
+ let timer = 0;
201
+ const detect = setInterval(() => {
202
+ if (!that.stop) {
203
+ clearInterval(detect);
204
+ resolve(true);
205
+ }
206
+ timer++;
207
+ if (timer > 30) {
208
+ console.error("request stop: timeout", req.url, timer);
209
+ }
210
+ }, 1000);
211
+ });
212
+ }
213
+ return _adapter(req).then(async (res) => {
214
+ try {
215
+ if (_afterResponse) {
216
+ return await _afterResponse(res, {
217
+ req,
218
+ res,
219
+ fetch: adapter2
220
+ });
221
+ }
222
+ return res;
223
+ } catch (e) {
224
+ console.error("request afterFn error", e, req);
225
+ return wrapperError({
226
+ code: 500,
227
+ message: "api request afterFn error",
228
+ req
229
+ });
230
+ }
231
+ });
232
+ }
233
+ before(fn) {
234
+ this.beforeRequest = fn;
235
+ }
236
+ after(fn) {
237
+ this.afterResponse = fn;
238
+ }
239
+ async fetchText(urlOrOptions, options) {
240
+ let _options = { ...options };
241
+ if (typeof urlOrOptions === "string" && !_options.url) {
242
+ _options.url = urlOrOptions;
243
+ }
244
+ if (typeof urlOrOptions === "object") {
245
+ _options = { ...urlOrOptions, ..._options };
246
+ }
247
+ const res = await adapter({
248
+ method: "GET",
249
+ ..._options,
250
+ headers: {
251
+ ...this.headers,
252
+ ..._options?.headers || {}
253
+ }
254
+ });
255
+ if (res && !res.code) {
256
+ return {
257
+ code: 200,
258
+ data: res
259
+ };
260
+ }
261
+ return res;
262
+ }
263
+ }
264
+
265
+ // src/create-query/index.ts
266
+ var createQueryByRoutes = (list) => {
267
+ const obj = {};
268
+ for (const route of list) {
269
+ if (!obj[route.path]) {
270
+ obj[route.path] = {};
271
+ }
272
+ obj[route.path][route.key] = route;
273
+ }
274
+ const code = `
275
+ import { createQueryApi } from '@kevisual/query/api';
276
+ const api = ${generateApiCode(obj)} as const;
277
+ const queryApi = createQueryApi({ api });
278
+ export { queryApi };
279
+ `;
280
+ return code;
281
+ };
282
+ function generateApiCode(obj) {
283
+ let code = `{
284
+ `;
285
+ const paths = Object.keys(obj);
286
+ for (let i = 0;i < paths.length; i++) {
287
+ const path = paths[i];
288
+ const methods = obj[path];
289
+ code += ` "${path}": {
290
+ `;
291
+ const keys = Object.keys(methods);
292
+ for (let j = 0;j < keys.length; j++) {
293
+ const key = keys[j];
294
+ const route = methods[key];
295
+ if (route?.id) {
296
+ if (route.id.startsWith("rand-")) {
297
+ delete route.id;
298
+ }
299
+ }
300
+ const description = route?.metadata?.summary || route?.description || "";
301
+ const args = route?.metadata?.args || {};
302
+ if (description || Object.keys(args).length > 0) {
303
+ code += ` /**
304
+ `;
305
+ if (description) {
306
+ const escapedDescription = description.replace(/\\/g, "\\\\").replace(/\*/g, "\\*").replace(/\n/g, `
307
+ * `);
308
+ code += ` * ${escapedDescription}
309
+ `;
310
+ }
311
+ if (Object.keys(args).length > 0) {
312
+ if (description) {
313
+ code += ` *
314
+ `;
315
+ }
316
+ code += ` * @param data - Request parameters
317
+ `;
318
+ for (const [argName, schema] of Object.entries(args)) {
319
+ const argSchema = schema;
320
+ const argType = argSchema.type || "unknown";
321
+ const argDesc = argSchema.description || "";
322
+ let typeInfo = argType;
323
+ if (argType === "string" && argSchema.enum) {
324
+ typeInfo = argSchema.enum.map((v) => `"${v}"`).join(" | ");
325
+ } else if (argType === "number" || argType === "integer") {
326
+ const constraints = [];
327
+ if (argSchema.minimum !== undefined)
328
+ constraints.push(`min: ${argSchema.minimum}`);
329
+ if (argSchema.maximum !== undefined)
330
+ constraints.push(`max: ${argSchema.maximum}`);
331
+ if (argSchema.exclusiveMinimum !== undefined)
332
+ constraints.push(`> ${argSchema.exclusiveMinimum}`);
333
+ if (argSchema.exclusiveMaximum !== undefined)
334
+ constraints.push(`< ${argSchema.exclusiveMaximum}`);
335
+ if (constraints.length > 0)
336
+ typeInfo += ` (${constraints.join(", ")})`;
337
+ } else if (argType === "string") {
338
+ const constraints = [];
339
+ if (argSchema.minLength !== undefined)
340
+ constraints.push(`minLength: ${argSchema.minLength}`);
341
+ if (argSchema.maxLength !== undefined)
342
+ constraints.push(`maxLength: ${argSchema.maxLength}`);
343
+ if (argSchema.format)
344
+ constraints.push(`format: ${argSchema.format}`);
345
+ if (constraints.length > 0)
346
+ typeInfo += ` (${constraints.join(", ")})`;
347
+ }
348
+ const escapedArgDesc = argDesc.replace(/\\/g, "\\\\").replace(/\*/g, "\\*").replace(/\n/g, " ");
349
+ code += ` * @param data.${argName} - {${typeInfo}}${escapedArgDesc ? " " + escapedArgDesc : ""}
350
+ `;
351
+ }
352
+ }
353
+ code += ` */
354
+ `;
355
+ }
356
+ code += ` "${key}": ${JSON.stringify(route, null, 2).split(`
357
+ `).map((line, idx) => idx === 0 ? line : " " + line).join(`
358
+ `)}`;
359
+ if (j < keys.length - 1) {
360
+ code += ",";
361
+ }
362
+ code += `
363
+ `;
364
+ }
365
+ code += ` }`;
366
+ if (i < paths.length - 1) {
367
+ code += ",";
368
+ }
369
+ code += `
370
+ `;
371
+ }
372
+ code += "}";
373
+ return code;
374
+ }
375
+
376
+ // node_modules/.pnpm/es-toolkit@1.44.0/node_modules/es-toolkit/dist/object/pick.mjs
377
+ function pick(obj, keys) {
378
+ const result = {};
379
+ for (let i = 0;i < keys.length; i++) {
380
+ const key = keys[i];
381
+ if (Object.hasOwn(obj, key)) {
382
+ result[key] = obj[key];
383
+ }
384
+ }
385
+ return result;
386
+ }
387
+ // src/query-api.ts
388
+ class QueryApi {
389
+ query;
390
+ constructor(opts) {
391
+ this.query = opts?.query ?? new Query;
392
+ if (opts?.api) {
393
+ this.createApi(opts.api);
394
+ }
395
+ }
396
+ post(pos, data, opts) {
397
+ const _pos = pick(pos, ["path", "key", "id"]);
398
+ return this.query.post({
399
+ ..._pos,
400
+ payload: data
401
+ }, opts);
402
+ }
403
+ createApi(api) {
404
+ const that = this;
405
+ const apiEntries = Object.entries(api);
406
+ const keepPaths = ["createApi", "query", "post"];
407
+ for (const [path, methods] of apiEntries) {
408
+ if (keepPaths.includes(path))
409
+ continue;
410
+ if (!that[path]) {
411
+ that[path] = {};
412
+ }
413
+ for (const [key, pos] of Object.entries(methods)) {
414
+ that[path][key] = (data, opts) => {
415
+ const _pos = pick(pos, ["path", "key", "id"]);
416
+ return that.query.post({
417
+ ..._pos,
418
+ payload: data
419
+ }, opts);
420
+ };
421
+ }
422
+ }
423
+ }
424
+ }
425
+ function createQueryApi(opts) {
426
+ return new QueryApi(opts);
427
+ }
428
+ export {
429
+ createQueryByRoutes,
430
+ createQueryApi,
431
+ QueryApi
432
+ };
@@ -180,6 +180,9 @@ declare class Query {
180
180
  */
181
181
  stop?: boolean;
182
182
  qws: QueryWs;
183
+ /**
184
+ * 默认是 /client/router或者 默认是 /api/router
185
+ */
183
186
  isClient: boolean;
184
187
  constructor(opts?: QueryOptions);
185
188
  setQueryWs(qws: QueryWs): void;
@@ -236,13 +239,6 @@ declare class BaseQuery<T extends Query = Query, R extends {
236
239
  post<R = any, P = any>(data: P, options?: DataOpts): Promise<Result<R>>;
237
240
  get<R = any, P = any>(data: P, options?: DataOpts): Promise<Result<R>>;
238
241
  }
239
- /**
240
- * @deprecated
241
- * 前端调用后端QueryRouter, 默认路径 /client/router
242
- */
243
- declare class ClientQuery extends Query {
244
- constructor(opts?: QueryOpts$1);
245
- }
246
242
 
247
243
  type QueryOpts = {
248
244
  url?: string;
@@ -269,5 +265,5 @@ declare class QueryClient extends Query {
269
265
  removeToken(): void;
270
266
  }
271
267
 
272
- export { BaseQuery, ClientQuery, Query, QueryClient, QueryWs, adapter, wrapperError };
268
+ export { BaseQuery, Query, QueryClient, QueryWs, adapter, wrapperError };
273
269
  export type { Data, DataOpts, QueryOptions, QueryOpts, QueryWsOpts, Result };