@kevisual/query 0.0.46 → 0.0.48

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.
@@ -11,14 +11,6 @@ type AdapterOpts = {
11
11
  body?: Record<string, any> | FormData;
12
12
  timeout?: number;
13
13
  method?: Method;
14
- /**
15
- * @deprecated use responseType
16
- */
17
- isBlob?: boolean;
18
- /**
19
- * @deprecated use responseType
20
- */
21
- isText?: boolean;
22
14
  /**
23
15
  * 响应类型,
24
16
  * */
@@ -33,10 +25,6 @@ declare const isTextForContentType: (contentType: string | null) => boolean;
33
25
  * @returns
34
26
  */
35
27
  declare const adapter: (opts?: AdapterOpts, overloadOpts?: RequestInit) => Promise<any>;
36
- /**
37
- * adapter
38
- */
39
- declare const queryFetch: (opts?: AdapterOpts, overloadOpts?: RequestInit) => Promise<any>;
40
28
 
41
- export { adapter, isTextForContentType, methods, queryFetch };
29
+ export { adapter, isTextForContentType, methods };
42
30
  export type { AdapterOpts, Method };
@@ -3,19 +3,13 @@ var methods = ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"];
3
3
  var isTextForContentType = (contentType) => {
4
4
  if (!contentType)
5
5
  return false;
6
- const textTypes = ["text/", "xml", "html", "javascript", "css", "csv", "plain", "x-www-form-urlencoded", "md"];
6
+ const textTypes = ["text/", "xml", "html", "javascript", "css", "csv", "plain", "x-www-form-urlencoded", "md", "json"];
7
7
  return textTypes.some((type) => contentType.includes(type));
8
8
  };
9
9
  var adapter = async (opts = {}, overloadOpts) => {
10
10
  const controller = new AbortController;
11
11
  const signal = controller.signal;
12
12
  const isPostFile = opts.isPostFile || false;
13
- let responseType = opts.responseType || "json";
14
- if (opts.isBlob) {
15
- responseType = "blob";
16
- } else if (opts.isText) {
17
- responseType = "text";
18
- }
19
13
  const timeout = opts.timeout || 60000 * 3;
20
14
  const timer = setTimeout(() => {
21
15
  controller.abort();
@@ -74,21 +68,31 @@ var adapter = async (opts = {}, overloadOpts) => {
74
68
  headers
75
69
  }).then(async (response) => {
76
70
  const contentType = response.headers.get("Content-Type");
77
- if (responseType === "blob") {
78
- return await response.blob();
79
- }
80
- const isText = responseType === "text";
81
71
  const isJson = contentType && contentType.includes("application/json");
82
- if (isJson && !isText) {
83
- return await response.json();
72
+ const isSuccess = response.ok;
73
+ if (isJson) {
74
+ const json = await response.json();
75
+ if (json?.code) {
76
+ return json;
77
+ }
78
+ return {
79
+ code: isSuccess ? 200 : response.status,
80
+ status: response.status,
81
+ data: json
82
+ };
84
83
  } else if (isTextForContentType(contentType)) {
85
84
  return {
86
- code: response.status,
85
+ code: isSuccess ? 200 : response.status,
87
86
  status: response.status,
88
87
  data: await response.text()
89
88
  };
90
89
  } else {
91
- return response;
90
+ return {
91
+ code: isSuccess ? 200 : response.status,
92
+ status: response.status,
93
+ data: "非文本非JSON响应, 请手动处理response。",
94
+ response
95
+ };
92
96
  }
93
97
  }).catch((err) => {
94
98
  if (err.name === "AbortError") {
@@ -105,9 +109,7 @@ var adapter = async (opts = {}, overloadOpts) => {
105
109
  clearTimeout(timer);
106
110
  });
107
111
  };
108
- var queryFetch = adapter;
109
112
  export {
110
- queryFetch,
111
113
  methods,
112
114
  isTextForContentType,
113
115
  adapter
@@ -14,14 +14,6 @@ type AdapterOpts = {
14
14
  body?: Record<string, any> | FormData;
15
15
  timeout?: number;
16
16
  method?: Method;
17
- /**
18
- * @deprecated use responseType
19
- */
20
- isBlob?: boolean;
21
- /**
22
- * @deprecated use responseType
23
- */
24
- isText?: boolean;
25
17
  /**
26
18
  * 响应类型,
27
19
  * */
@@ -119,6 +111,8 @@ type QueryOptions = {
119
111
  headers?: Record<string, string>;
120
112
  timeout?: number;
121
113
  isClient?: boolean;
114
+ tokenName?: string;
115
+ storage?: Storage;
122
116
  beforeRequest?: Fn;
123
117
  };
124
118
  type Data = {
@@ -171,10 +165,9 @@ declare class Query {
171
165
  */
172
166
  stop?: boolean;
173
167
  qws: QueryWs;
174
- /**
175
- * 默认是 /client/router或者 默认是 /api/router
176
- */
177
- isClient: boolean;
168
+ tokenName: string;
169
+ storage: Storage;
170
+ token: string;
178
171
  constructor(opts?: QueryOptions);
179
172
  setQueryWs(qws: QueryWs): void;
180
173
  /**
@@ -298,20 +291,25 @@ type InferType<T> = T extends z.ZodType<infer U> ? U : T extends {
298
291
  } ? InferFromJSONSchema<T> : T extends {
299
292
  properties: infer P;
300
293
  } ? InferFromJSONSchema<T> : T;
294
+ type IsOptional<T> = T extends {
295
+ optional: true;
296
+ } ? true : false;
301
297
  type ExtractArgsFromMetadata<T> = T extends {
302
298
  metadata?: {
303
299
  args?: infer A;
304
300
  };
305
- } ? A extends Record<string, any> ? {
306
- [K in keyof A]: InferType<A[K]>;
307
- } : never : never;
301
+ } ? A extends Record<string, any> ? ({
302
+ [K in keyof A as IsOptional<A[K]> extends true ? never : K]: InferType<A[K]>;
303
+ } & {
304
+ [K in keyof A as IsOptional<A[K]> extends true ? K : never]?: InferType<A[K]>;
305
+ }) : never : never;
308
306
  type ApiMethods<P extends {
309
307
  [path: string]: {
310
308
  [key: string]: Pos;
311
309
  };
312
310
  }> = {
313
311
  [Path in keyof P]: {
314
- [Key in keyof P[Path]]: (data?: Partial<ExtractArgsFromMetadata<P[Path][Key]>>, opts?: DataOpts) => ReturnType<Query['post']>;
312
+ [Key in keyof P[Path]]: (data?: ExtractArgsFromMetadata<P[Path][Key]>, opts?: DataOpts) => ReturnType<Query['post']>;
315
313
  };
316
314
  };
317
315
  type QueryApiOpts<P extends {
@@ -329,7 +327,7 @@ declare class QueryApi<P extends {
329
327
  } = {}> {
330
328
  query: Query;
331
329
  constructor(opts?: QueryApiOpts<P>);
332
- post<T extends Pos>(pos: T, data?: Partial<ExtractArgsFromMetadata<T>>, opts?: DataOpts): Promise<{
330
+ post<T extends Pos>(pos: T, data?: ExtractArgsFromMetadata<T>, opts?: DataOpts): Promise<{
333
331
  code: number;
334
332
  data?: any;
335
333
  message?: string;