fix: remove dist for query
This commit is contained in:
parent
28558d92f0
commit
632d164087
8
dist/adapter.d.ts
vendored
8
dist/adapter.d.ts
vendored
@ -1,8 +0,0 @@
|
||||
type AdapterOpts = {
|
||||
url: string;
|
||||
headers?: Record<string, string>;
|
||||
body?: Record<string, any>;
|
||||
timeout?: number;
|
||||
};
|
||||
export declare const adapter: (opts: AdapterOpts) => Promise<any>;
|
||||
export {};
|
51
dist/index.d.ts
vendored
51
dist/index.d.ts
vendored
@ -1,51 +0,0 @@
|
||||
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;
|
||||
payload?: Record<string, any>;
|
||||
[key: string]: any;
|
||||
};
|
||||
type Result<S = any> = {
|
||||
code: number;
|
||||
data?: S;
|
||||
message?: string;
|
||||
success: boolean;
|
||||
};
|
||||
type DataOpts = Partial<QueryOpts> & {
|
||||
beforeRequest?: Fn;
|
||||
afterResponse?: (result: Result) => Promise<any>;
|
||||
};
|
||||
/**
|
||||
* const query = new Query();
|
||||
* const res = await query.post({
|
||||
* path: 'demo',
|
||||
* key: '1',
|
||||
* });
|
||||
*/
|
||||
export declare class Query {
|
||||
adapter: typeof adapter;
|
||||
url: string;
|
||||
beforeRequest?: Fn;
|
||||
afterResponse?: (result: Result) => Promise<any>;
|
||||
headers?: Record<string, string>;
|
||||
timeout?: number;
|
||||
constructor(opts: QueryOpts);
|
||||
get<T, S>(params: Record<string, any> & Data & T, options?: DataOpts): Promise<Result<S>>;
|
||||
post<T, S>(body: Record<string, any> & Data & T, options?: DataOpts): Promise<Result<S>>;
|
||||
before(fn: Fn): void;
|
||||
after(fn: (result: Result) => Promise<any>): void;
|
||||
}
|
||||
export { adapter };
|
98
dist/index.js
vendored
98
dist/index.js
vendored
@ -1,98 +0,0 @@
|
||||
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);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* const query = new Query();
|
||||
* const res = await query.post({
|
||||
* path: 'demo',
|
||||
* key: '1',
|
||||
* });
|
||||
*/
|
||||
class Query {
|
||||
adapter;
|
||||
url;
|
||||
beforeRequest;
|
||||
afterResponse;
|
||||
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).then(async (res) => {
|
||||
res.success = res.code === 200;
|
||||
if (options?.afterResponse) {
|
||||
return await options.afterResponse(res);
|
||||
}
|
||||
return res;
|
||||
});
|
||||
}
|
||||
before(fn) {
|
||||
this.beforeRequest = fn;
|
||||
}
|
||||
after(fn) {
|
||||
this.afterResponse = fn;
|
||||
}
|
||||
}
|
||||
|
||||
export { Query, adapter };
|
8
dist/node-adapter.d.ts
vendored
8
dist/node-adapter.d.ts
vendored
@ -1,8 +0,0 @@
|
||||
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
49
dist/node-adapter.js
vendored
@ -1,49 +0,0 @@
|
||||
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 };
|
12
src/index.ts
12
src/index.ts
@ -25,7 +25,7 @@ type Result<S=any> = {
|
||||
data?: S;
|
||||
message?: string;
|
||||
success: boolean;
|
||||
}
|
||||
};
|
||||
// 额外功能
|
||||
type DataOpts = Partial<QueryOpts> & {
|
||||
beforeRequest?: Fn;
|
||||
@ -45,13 +45,13 @@ export class Query {
|
||||
afterResponse?: (result: Result) => Promise<any>;
|
||||
headers?: Record<string, string>;
|
||||
timeout?: number;
|
||||
constructor(opts: QueryOpts) {
|
||||
this.adapter = opts.adapter || adapter;
|
||||
this.url = opts.url || '/api/router';
|
||||
this.headers = opts.headers || {
|
||||
constructor(opts?: QueryOpts) {
|
||||
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
|
||||
this.timeout = opts?.timeout || 60000; // 默认超时时间为 60s
|
||||
}
|
||||
async get<T, S>(params: Record<string, any> & Data & T, options?: DataOpts): Promise<Result<S>> {
|
||||
return this.post(params, options);
|
||||
|
Loading…
x
Reference in New Issue
Block a user