fix: remove dist for query

This commit is contained in:
xion 2024-09-28 16:50:59 +08:00
parent 28558d92f0
commit 632d164087
6 changed files with 10 additions and 224 deletions

8
dist/adapter.d.ts vendored
View File

@ -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
View File

@ -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
View File

@ -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 };

View File

@ -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
View File

@ -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 };

View File

@ -20,16 +20,16 @@ type Data = {
payload?: Record<string, any>; payload?: Record<string, any>;
[key: string]: any; [key: string]: any;
}; };
type Result<S=any> = { type Result<S = any> = {
code: number; code: number;
data?: S; data?: S;
message?: string; message?: string;
success: boolean; success: boolean;
} };
// 额外功能 // 额外功能
type DataOpts = Partial<QueryOpts> & { type DataOpts = Partial<QueryOpts> & {
beforeRequest?: Fn; beforeRequest?: Fn;
afterResponse?: (result: Result)=>Promise<any>; afterResponse?: (result: Result) => Promise<any>;
}; };
/** /**
* const query = new Query(); * const query = new Query();
@ -42,16 +42,16 @@ export class Query {
adapter: typeof adapter; adapter: typeof adapter;
url: string; url: string;
beforeRequest?: Fn; beforeRequest?: Fn;
afterResponse?: (result: Result)=>Promise<any>; afterResponse?: (result: Result) => Promise<any>;
headers?: Record<string, string>; headers?: Record<string, string>;
timeout?: number; timeout?: number;
constructor(opts: QueryOpts) { constructor(opts?: QueryOpts) {
this.adapter = opts.adapter || adapter; this.adapter = opts?.adapter || adapter;
this.url = opts.url || '/api/router'; this.url = opts?.url || '/api/router';
this.headers = opts.headers || { this.headers = opts?.headers || {
'Content-Type': 'application/json', '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>> { async get<T, S>(params: Record<string, any> & Data & T, options?: DataOpts): Promise<Result<S>> {
return this.post(params, options); return this.post(params, options);
@ -82,7 +82,7 @@ export class Query {
before(fn: Fn) { before(fn: Fn) {
this.beforeRequest = fn; this.beforeRequest = fn;
} }
after(fn: (result: Result)=>Promise<any>) { after(fn: (result: Result) => Promise<any>) {
this.afterResponse = fn; this.afterResponse = fn;
} }
} }