feat: update new options

This commit is contained in:
熊潇 2025-06-07 19:07:28 +08:00
parent 14dec5f5c5
commit ca154ba883
3 changed files with 16 additions and 20 deletions

View File

@ -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",

View File

@ -8,7 +8,8 @@ export type AdapterOpts = {
body?: Record<string, any> | 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 {

View File

@ -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<Record<string, any> | false>;
export type QueryOpts = {
url?: string;
headers?: Record<string, string>;
body?: Record<string, any> | 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;