From ca154ba883f567b63c32fc5ea7bbceb83031b89c Mon Sep 17 00:00:00 2001 From: xion Date: Sat, 7 Jun 2025 19:07:28 +0800 Subject: [PATCH] feat: update new options --- package.json | 2 +- src/adapter.ts | 22 +++++++++++++--------- src/query.ts | 12 ++---------- 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/package.json b/package.json index 9ee756b..3a2a2ce 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kevisual/query", - "version": "0.0.26", + "version": "0.0.28", "main": "dist/index.js", "module": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/adapter.ts b/src/adapter.ts index 540f7fa..8867de9 100644 --- a/src/adapter.ts +++ b/src/adapter.ts @@ -8,7 +8,8 @@ export type AdapterOpts = { body?: Record | FormData; // body 可以是对象、字符串或 FormData timeout?: number; method?: Method; - isBlob?: boolean; // 是否返回 Blob 对象 + isBlob?: boolean; // 是否返回 Blob 对象, 第一优先 + isText?: boolean; // 是否返回文本内容, 第二优先 isPostFile?: boolean; // 是否为文件上传 }; export const isTextForContentType = (contentType: string | null) => { @@ -22,16 +23,18 @@ export const isTextForContentType = (contentType: string | null) => { * @param overloadOpts 覆盖fetch的默认配置 * @returns */ -export const adapter = async (opts: AdapterOpts, overloadOpts?: RequestInit) => { +export const adapter = async (opts: AdapterOpts = {}, overloadOpts?: RequestInit) => { const controller = new AbortController(); const signal = controller.signal; const isBlob = opts.isBlob || false; // 是否返回 Blob 对象 + const isText = opts.isText || false; // 是否返回文本内容 const isPostFile = opts.isPostFile || false; // 是否为文件上传 const timeout = opts.timeout || 60000 * 3; // 默认超时时间为 60s * 3 const timer = setTimeout(() => { controller.abort(); }, timeout); - let method = overloadOpts?.method || opts.method || 'POST'; + let method = overloadOpts?.method || opts?.method || 'POST'; + let headers = { ...opts?.headers, ...overloadOpts?.headers }; let origin = ''; let url: URL; if (opts?.url?.startsWith('http')) { @@ -50,17 +53,18 @@ export const adapter = async (opts: AdapterOpts, overloadOpts?: RequestInit) => } else if (isPostFile) { body = opts.body as FormData; // 如果是文件上传,直接使用 FormData } else { + headers = { + 'Content-Type': 'application/json', + ...headers, + }; body = JSON.stringify(opts.body); // 否则将对象转换为 JSON 字符串 } return fetch(url, { method: method.toUpperCase(), - headers: { - 'Content-Type': 'application/json', - ...opts.headers, - }, signal, - ...overloadOpts, body: body, + ...overloadOpts, + headers: headers, }) .then(async (response) => { // 获取 Content-Type 头部信息 @@ -70,7 +74,7 @@ export const adapter = async (opts: AdapterOpts, overloadOpts?: RequestInit) => } const isJson = contentType && contentType.includes('application/json'); // 判断返回的数据类型 - if (isJson) { + if (isJson && !isText) { return await response.json(); // 解析为 JSON } else if (isTextForContentType(contentType)) { return { diff --git a/src/query.ts b/src/query.ts index ef8a98b..3a9410f 100644 --- a/src/query.ts +++ b/src/query.ts @@ -1,4 +1,4 @@ -import { adapter, isTextForContentType, Method } from './adapter.ts'; +import { adapter, isTextForContentType, Method, AdapterOpts } from './adapter.ts'; import type { QueryWs } from './ws.ts'; /** * 请求前处理函数 @@ -14,17 +14,9 @@ export type Fn = (opts: { }) => Promise | false>; export type QueryOpts = { - url?: string; - headers?: Record; - body?: Record | FormData; // body 可以是对象、字符串或 FormData - timeout?: number; - method?: Method; - isBlob?: boolean; // 是否返回 Blob 对象 - isPostFile?: boolean; // 是否为文件上传 - adapter?: typeof adapter; [key: string]: any; -}; +} & AdapterOpts; export type Data = { path?: string; key?: string;