feat: 优化query,添加别名queryFetch

This commit is contained in:
xion 2025-03-02 09:11:59 +08:00
parent 0b575d44c0
commit 3a8396279b
3 changed files with 30 additions and 2 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@kevisual/query", "name": "@kevisual/query",
"version": "0.0.7", "version": "0.0.8",
"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",

View File

@ -65,4 +65,20 @@ export default [
}, },
plugins: [dts()], plugins: [dts()],
}, },
{
input: 'src/adapter.ts',
output: {
file: 'dist/query-adapter.js',
format: 'es',
},
plugins: [resolve(), typescript()],
},
{
input: 'src/adapter.ts', // TypeScript 入口文件
output: {
file: 'dist/query-adapter.d.ts', // 输出文件
format: 'es', // 输出格式设置为 ES 模块
},
plugins: [dts()],
},
]; ];

View File

@ -4,7 +4,14 @@ type AdapterOpts = {
body?: Record<string, any>; body?: Record<string, any>;
timeout?: number; timeout?: number;
}; };
export const adapter = async (opts: AdapterOpts) => {
/**
*
* @param opts
* @param overloadOpts fetch的默认配置
* @returns
*/
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 timeout = opts.timeout || 60000 * 3; // 默认超时时间为 60s * 3 const timeout = opts.timeout || 60000 * 3; // 默认超时时间为 60s * 3
@ -20,6 +27,7 @@ export const adapter = async (opts: AdapterOpts) => {
}, },
body: JSON.stringify(opts.body), body: JSON.stringify(opts.body),
signal, signal,
...overloadOpts,
}) })
.then((response) => { .then((response) => {
// 获取 Content-Type 头部信息 // 获取 Content-Type 头部信息
@ -44,3 +52,7 @@ export const adapter = async (opts: AdapterOpts) => {
clearTimeout(timer); clearTimeout(timer);
}); });
}; };
/**
* adapter
*/
export const queryFetch = adapter;