feat: update new options
This commit is contained in:
parent
14dec5f5c5
commit
ca154ba883
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@kevisual/query",
|
"name": "@kevisual/query",
|
||||||
"version": "0.0.26",
|
"version": "0.0.28",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"module": "dist/index.js",
|
"module": "dist/index.js",
|
||||||
"types": "dist/index.d.ts",
|
"types": "dist/index.d.ts",
|
||||||
|
@ -8,7 +8,8 @@ export type AdapterOpts = {
|
|||||||
body?: Record<string, any> | FormData; // body 可以是对象、字符串或 FormData
|
body?: Record<string, any> | FormData; // body 可以是对象、字符串或 FormData
|
||||||
timeout?: number;
|
timeout?: number;
|
||||||
method?: Method;
|
method?: Method;
|
||||||
isBlob?: boolean; // 是否返回 Blob 对象
|
isBlob?: boolean; // 是否返回 Blob 对象, 第一优先
|
||||||
|
isText?: boolean; // 是否返回文本内容, 第二优先
|
||||||
isPostFile?: boolean; // 是否为文件上传
|
isPostFile?: boolean; // 是否为文件上传
|
||||||
};
|
};
|
||||||
export const isTextForContentType = (contentType: string | null) => {
|
export const isTextForContentType = (contentType: string | null) => {
|
||||||
@ -22,16 +23,18 @@ export const isTextForContentType = (contentType: string | null) => {
|
|||||||
* @param overloadOpts 覆盖fetch的默认配置
|
* @param overloadOpts 覆盖fetch的默认配置
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export const adapter = async (opts: AdapterOpts, overloadOpts?: RequestInit) => {
|
export const adapter = async (opts: AdapterOpts = {}, overloadOpts?: RequestInit) => {
|
||||||
const controller = new AbortController();
|
const controller = new AbortController();
|
||||||
const signal = controller.signal;
|
const signal = controller.signal;
|
||||||
const isBlob = opts.isBlob || false; // 是否返回 Blob 对象
|
const isBlob = opts.isBlob || false; // 是否返回 Blob 对象
|
||||||
|
const isText = opts.isText || false; // 是否返回文本内容
|
||||||
const isPostFile = opts.isPostFile || false; // 是否为文件上传
|
const isPostFile = opts.isPostFile || false; // 是否为文件上传
|
||||||
const timeout = opts.timeout || 60000 * 3; // 默认超时时间为 60s * 3
|
const timeout = opts.timeout || 60000 * 3; // 默认超时时间为 60s * 3
|
||||||
const timer = setTimeout(() => {
|
const timer = setTimeout(() => {
|
||||||
controller.abort();
|
controller.abort();
|
||||||
}, timeout);
|
}, timeout);
|
||||||
let method = overloadOpts?.method || opts.method || 'POST';
|
let method = overloadOpts?.method || opts?.method || 'POST';
|
||||||
|
let headers = { ...opts?.headers, ...overloadOpts?.headers };
|
||||||
let origin = '';
|
let origin = '';
|
||||||
let url: URL;
|
let url: URL;
|
||||||
if (opts?.url?.startsWith('http')) {
|
if (opts?.url?.startsWith('http')) {
|
||||||
@ -50,17 +53,18 @@ export const adapter = async (opts: AdapterOpts, overloadOpts?: RequestInit) =>
|
|||||||
} else if (isPostFile) {
|
} else if (isPostFile) {
|
||||||
body = opts.body as FormData; // 如果是文件上传,直接使用 FormData
|
body = opts.body as FormData; // 如果是文件上传,直接使用 FormData
|
||||||
} else {
|
} else {
|
||||||
|
headers = {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
...headers,
|
||||||
|
};
|
||||||
body = JSON.stringify(opts.body); // 否则将对象转换为 JSON 字符串
|
body = JSON.stringify(opts.body); // 否则将对象转换为 JSON 字符串
|
||||||
}
|
}
|
||||||
return fetch(url, {
|
return fetch(url, {
|
||||||
method: method.toUpperCase(),
|
method: method.toUpperCase(),
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
...opts.headers,
|
|
||||||
},
|
|
||||||
signal,
|
signal,
|
||||||
...overloadOpts,
|
|
||||||
body: body,
|
body: body,
|
||||||
|
...overloadOpts,
|
||||||
|
headers: headers,
|
||||||
})
|
})
|
||||||
.then(async (response) => {
|
.then(async (response) => {
|
||||||
// 获取 Content-Type 头部信息
|
// 获取 Content-Type 头部信息
|
||||||
@ -70,7 +74,7 @@ export const adapter = async (opts: AdapterOpts, overloadOpts?: RequestInit) =>
|
|||||||
}
|
}
|
||||||
const isJson = contentType && contentType.includes('application/json');
|
const isJson = contentType && contentType.includes('application/json');
|
||||||
// 判断返回的数据类型
|
// 判断返回的数据类型
|
||||||
if (isJson) {
|
if (isJson && !isText) {
|
||||||
return await response.json(); // 解析为 JSON
|
return await response.json(); // 解析为 JSON
|
||||||
} else if (isTextForContentType(contentType)) {
|
} else if (isTextForContentType(contentType)) {
|
||||||
return {
|
return {
|
||||||
|
12
src/query.ts
12
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';
|
import type { QueryWs } from './ws.ts';
|
||||||
/**
|
/**
|
||||||
* 请求前处理函数
|
* 请求前处理函数
|
||||||
@ -14,17 +14,9 @@ export type Fn = (opts: {
|
|||||||
}) => Promise<Record<string, any> | false>;
|
}) => Promise<Record<string, any> | false>;
|
||||||
|
|
||||||
export type QueryOpts = {
|
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;
|
adapter?: typeof adapter;
|
||||||
[key: string]: any;
|
[key: string]: any;
|
||||||
};
|
} & AdapterOpts;
|
||||||
export type Data = {
|
export type Data = {
|
||||||
path?: string;
|
path?: string;
|
||||||
key?: string;
|
key?: string;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user