feat: 添加query功能

This commit is contained in:
2024-09-08 04:32:41 +08:00
parent 443a41110a
commit 360f6ba180
24 changed files with 6228 additions and 48 deletions

8
dist/adapter.d.ts vendored Normal file
View File

@@ -0,0 +1,8 @@
type AdapterOpts = {
url: string;
headers?: Record<string, string>;
body?: Record<string, any>;
timeout?: number;
};
export declare const adapter: (opts: AdapterOpts) => Promise<any>;
export {};

34
dist/index.d.ts vendored Normal file
View File

@@ -0,0 +1,34 @@
import { adapter } from './adapter.ts';
type Fn = (opts: {
url?: string;
headers?: Record<string, string>;
body?: Record<string, any>;
[key: string]: any;
timeout?: number;
}) => Promise<Record<string, any>>;
type QueryOpts = {
url?: string;
adapter?: typeof adapter;
headers?: Record<string, string>;
timeout?: number;
};
type Data = {
path?: string;
key?: string;
[key: string]: any;
};
type DataOpts = Partial<QueryOpts> & {
beforeRequest?: Fn;
};
export declare class Query {
adapter: typeof adapter;
url: string;
beforeRequest?: Fn;
headers?: Record<string, string>;
timeout?: number;
constructor(opts: QueryOpts);
get<T>(params: Record<string, any> & Data & T, options?: DataOpts): Promise<any>;
post<T>(body: Record<string, any> & Data & T, options?: DataOpts): Promise<any>;
before(fn: Fn): void;
}
export { adapter };

81
dist/index.js vendored Normal file
View File

@@ -0,0 +1,81 @@
const adapter = async (opts) => {
const controller = new AbortController();
const signal = controller.signal;
const timeout = opts.timeout || 60000; // 默认超时时间为 60s
const timer = setTimeout(() => {
controller.abort();
}, timeout);
return fetch(opts.url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...opts.headers,
},
body: JSON.stringify(opts.body),
signal,
})
.then((response) => {
// 获取 Content-Type 头部信息
const contentType = response.headers.get('Content-Type');
// 判断返回的数据类型
if (contentType && contentType.includes('application/json')) {
return response.json(); // 解析为 JSON
}
else {
return response.text(); // 解析为文本
}
})
.catch((err) => {
if (err.name === 'AbortError') {
console.log('Request timed out and was aborted');
}
console.error(err);
return {
code: 500,
};
})
.finally(() => {
clearTimeout(timer);
});
};
class Query {
adapter;
url;
beforeRequest;
headers;
timeout;
constructor(opts) {
this.adapter = opts.adapter || adapter;
this.url = opts.url || '/api/router';
this.headers = opts.headers || {
'Content-Type': 'application/json',
};
this.timeout = opts.timeout || 60000; // 默认超时时间为 60s
}
async get(params, options) {
return this.post(params, options);
}
async post(body, options) {
const url = options?.url || this.url;
const headers = { ...this.headers, ...options?.headers };
const adapter = options?.adapter || this.adapter;
const beforeRequest = options?.beforeRequest || this.beforeRequest;
const timeout = options?.timeout || this.timeout;
const req = {
url: url,
headers: headers,
body,
timeout,
};
if (beforeRequest) {
await beforeRequest(req);
}
return adapter(req);
}
before(fn) {
this.beforeRequest = fn;
}
}
export { Query, adapter };

8
dist/node-adapter.d.ts vendored Normal file
View File

@@ -0,0 +1,8 @@
type AdapterOpts = {
url: string;
headers?: Record<string, string>;
body?: Record<string, any>;
};
export declare const nodeAdapter: (opts: AdapterOpts) => Promise<any>;
export declare const adapter: (opts: AdapterOpts) => Promise<any>;
export {};

49
dist/node-adapter.js vendored Normal file
View File

@@ -0,0 +1,49 @@
import http from 'http';
const nodeAdapter = async (opts) => {
return new Promise((resolve, reject) => {
const postData = JSON.stringify(opts.body || '');
const _url = new URL(opts.url);
const { hostname, port, pathname } = _url;
const options = {
hostname: hostname,
port: port,
path: pathname || '/api/router',
method: 'POST', // Assuming it's a POST request
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData),
...opts.headers,
},
};
const req = http.request(options, (res) => {
let data = '';
// Collect data chunks
res.on('data', (chunk) => {
data += chunk;
});
// Resolve when the response is complete
res.on('end', () => {
try {
const parsedData = JSON.parse(data);
resolve(parsedData);
}
catch (error) {
reject(error);
}
});
});
// Handle request errors
req.on('error', (error) => {
reject(error);
});
// Write the request body and end the request
if (opts.body) {
req.write(postData);
}
req.end();
});
};
const adapter = nodeAdapter;
export { adapter, nodeAdapter };