@kevisual/query 0.0.25 → 0.0.27

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,11 +16,11 @@ declare const isTextForContentType: (contentType: string | null) => boolean;
15
16
  * @param overloadOpts 覆盖fetch的默认配置
16
17
  * @returns
17
18
  */
18
- declare const adapter: (opts: AdapterOpts, overloadOpts?: RequestInit) => Promise<any>;
19
+ declare const adapter: (opts?: AdapterOpts, overloadOpts?: RequestInit) => Promise<any>;
19
20
  /**
20
21
  * adapter
21
22
  */
22
- declare const queryFetch: (opts: AdapterOpts, overloadOpts?: RequestInit) => Promise<any>;
23
+ declare const queryFetch: (opts?: AdapterOpts, overloadOpts?: RequestInit) => Promise<any>;
23
24
 
24
25
  export { adapter, isTextForContentType, methods, queryFetch };
25
26
  export type { AdapterOpts, Method };
@@ -11,15 +11,17 @@ const isTextForContentType = (contentType) => {
11
11
  * @param overloadOpts 覆盖fetch的默认配置
12
12
  * @returns
13
13
  */
14
- const adapter = async (opts, overloadOpts) => {
14
+ 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();
21
22
  }, timeout);
22
- let method = overloadOpts?.method || opts.method || 'POST';
23
+ let method = overloadOpts?.method || opts?.method || 'POST';
24
+ let headers = { ...opts?.headers, ...overloadOpts?.headers };
23
25
  let origin = '';
24
26
  let url;
25
27
  if (opts?.url?.startsWith('http')) {
@@ -33,15 +35,26 @@ const adapter = async (opts, overloadOpts) => {
33
35
  if (isGet) {
34
36
  url.search = new URLSearchParams(opts.body).toString();
35
37
  }
38
+ let body = undefined;
39
+ if (isGet) {
40
+ body = undefined;
41
+ }
42
+ else if (isPostFile) {
43
+ body = opts.body; // 如果是文件上传,直接使用 FormData
44
+ }
45
+ else {
46
+ headers = {
47
+ 'Content-Type': 'application/json',
48
+ ...headers,
49
+ };
50
+ body = JSON.stringify(opts.body); // 否则将对象转换为 JSON 字符串
51
+ }
36
52
  return fetch(url, {
37
53
  method: method.toUpperCase(),
38
- headers: {
39
- 'Content-Type': 'application/json',
40
- ...opts.headers,
41
- },
42
54
  signal,
55
+ body: body,
43
56
  ...overloadOpts,
44
- body: isGet ? undefined : JSON.stringify(opts.body),
57
+ headers: headers,
45
58
  })
46
59
  .then(async (response) => {
47
60
  // 获取 Content-Type 头部信息
@@ -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 };