@kevisual/query 0.0.24 → 0.0.26

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.
@@ -3,10 +3,11 @@ type Method = (typeof methods)[number];
3
3
  type AdapterOpts = {
4
4
  url?: string;
5
5
  headers?: Record<string, string>;
6
- body?: Record<string, any>;
6
+ body?: Record<string, any> | FormData;
7
7
  timeout?: number;
8
8
  method?: Method;
9
9
  isBlob?: boolean;
10
+ isPostFile?: boolean;
10
11
  };
11
12
  declare const isTextForContentType: (contentType: string | null) => boolean;
12
13
  /**
@@ -15,6 +15,7 @@ const adapter = async (opts, overloadOpts) => {
15
15
  const controller = new AbortController();
16
16
  const signal = controller.signal;
17
17
  const isBlob = opts.isBlob || false; // 是否返回 Blob 对象
18
+ const isPostFile = opts.isPostFile || false; // 是否为文件上传
18
19
  const timeout = opts.timeout || 60000 * 3; // 默认超时时间为 60s * 3
19
20
  const timer = setTimeout(() => {
20
21
  controller.abort();
@@ -33,6 +34,16 @@ const adapter = async (opts, overloadOpts) => {
33
34
  if (isGet) {
34
35
  url.search = new URLSearchParams(opts.body).toString();
35
36
  }
37
+ let body = undefined;
38
+ if (isGet) {
39
+ body = undefined;
40
+ }
41
+ else if (isPostFile) {
42
+ body = opts.body; // 如果是文件上传,直接使用 FormData
43
+ }
44
+ else {
45
+ body = JSON.stringify(opts.body); // 否则将对象转换为 JSON 字符串
46
+ }
36
47
  return fetch(url, {
37
48
  method: method.toUpperCase(),
38
49
  headers: {
@@ -41,24 +52,24 @@ const adapter = async (opts, overloadOpts) => {
41
52
  },
42
53
  signal,
43
54
  ...overloadOpts,
44
- body: isGet ? undefined : JSON.stringify(opts.body),
55
+ body: body,
45
56
  })
46
- .then((response) => {
57
+ .then(async (response) => {
47
58
  // 获取 Content-Type 头部信息
48
59
  const contentType = response.headers.get('Content-Type');
49
60
  if (isBlob) {
50
- return response.blob(); // 直接返回 Blob 对象
61
+ return await response.blob(); // 直接返回 Blob 对象
51
62
  }
52
63
  const isJson = contentType && contentType.includes('application/json');
53
64
  // 判断返回的数据类型
54
65
  if (isJson) {
55
- return response.json(); // 解析为 JSON
66
+ return await response.json(); // 解析为 JSON
56
67
  }
57
68
  else if (isTextForContentType(contentType)) {
58
69
  return {
59
70
  code: 200,
60
71
  status: response.status,
61
- data: response.text(), // 直接返回文本内容
72
+ data: await response.text(), // 直接返回文本内容
62
73
  };
63
74
  }
64
75
  else {
@@ -19,8 +19,12 @@ declare class QueryAI {
19
19
  model?: string;
20
20
  constructor(opts?: QueryOpts);
21
21
  init(opts: ClientOptions): void;
22
- query(prompt: string, opts?: RequestOptions): Promise<any>;
23
- queryAsync(prompt: string, opts?: RequestOptions): Promise<any>;
22
+ query(prompt: string, opts?: RequestOptions): Promise<OpenAI.Chat.Completions.ChatCompletion & {
23
+ _request_id?: string | null;
24
+ }>;
25
+ queryAsync(prompt: string, opts?: RequestOptions): Promise<OpenAI.Chat.Completions.ChatCompletion & {
26
+ _request_id?: string | null;
27
+ }>;
24
28
  }
25
29
 
26
30
  export { QueryAI };