From 632d164087ac1a36bd3584d05e3527a97b6a5a39 Mon Sep 17 00:00:00 2001 From: xion Date: Sat, 28 Sep 2024 16:50:59 +0800 Subject: [PATCH] fix: remove dist for query --- dist/adapter.d.ts | 8 ---- dist/index.d.ts | 51 ---------------------- dist/index.js | 98 ------------------------------------------ dist/node-adapter.d.ts | 8 ---- dist/node-adapter.js | 49 --------------------- src/index.ts | 20 ++++----- 6 files changed, 10 insertions(+), 224 deletions(-) delete mode 100644 dist/adapter.d.ts delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/node-adapter.d.ts delete mode 100644 dist/node-adapter.js diff --git a/dist/adapter.d.ts b/dist/adapter.d.ts deleted file mode 100644 index 7b6d896..0000000 --- a/dist/adapter.d.ts +++ /dev/null @@ -1,8 +0,0 @@ -type AdapterOpts = { - url: string; - headers?: Record; - body?: Record; - timeout?: number; -}; -export declare const adapter: (opts: AdapterOpts) => Promise; -export {}; diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index 2b05277..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,51 +0,0 @@ -import { adapter } from './adapter.ts'; -type Fn = (opts: { - url?: string; - headers?: Record; - body?: Record; - [key: string]: any; - timeout?: number; -}) => Promise>; -type QueryOpts = { - url?: string; - adapter?: typeof adapter; - headers?: Record; - timeout?: number; -}; -type Data = { - path?: string; - key?: string; - payload?: Record; - [key: string]: any; -}; -type Result = { - code: number; - data?: S; - message?: string; - success: boolean; -}; -type DataOpts = Partial & { - beforeRequest?: Fn; - afterResponse?: (result: Result) => Promise; -}; -/** - * 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; - headers?: Record; - timeout?: number; - constructor(opts: QueryOpts); - get(params: Record & Data & T, options?: DataOpts): Promise>; - post(body: Record & Data & T, options?: DataOpts): Promise>; - before(fn: Fn): void; - after(fn: (result: Result) => Promise): void; -} -export { adapter }; diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index 17e692e..0000000 --- a/dist/index.js +++ /dev/null @@ -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 }; diff --git a/dist/node-adapter.d.ts b/dist/node-adapter.d.ts deleted file mode 100644 index c83d0fe..0000000 --- a/dist/node-adapter.d.ts +++ /dev/null @@ -1,8 +0,0 @@ -type AdapterOpts = { - url: string; - headers?: Record; - body?: Record; -}; -export declare const nodeAdapter: (opts: AdapterOpts) => Promise; -export declare const adapter: (opts: AdapterOpts) => Promise; -export {}; diff --git a/dist/node-adapter.js b/dist/node-adapter.js deleted file mode 100644 index b329b52..0000000 --- a/dist/node-adapter.js +++ /dev/null @@ -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 }; diff --git a/src/index.ts b/src/index.ts index 62d308e..5d3f216 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,16 +20,16 @@ type Data = { payload?: Record; [key: string]: any; }; -type Result = { +type Result = { code: number; data?: S; message?: string; success: boolean; -} +}; // 额外功能 type DataOpts = Partial & { beforeRequest?: Fn; - afterResponse?: (result: Result)=>Promise; + afterResponse?: (result: Result) => Promise; }; /** * const query = new Query(); @@ -42,16 +42,16 @@ export class Query { adapter: typeof adapter; url: string; beforeRequest?: Fn; - afterResponse?: (result: Result)=>Promise; + afterResponse?: (result: Result) => Promise; headers?: Record; 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(params: Record & Data & T, options?: DataOpts): Promise> { return this.post(params, options); @@ -82,7 +82,7 @@ export class Query { before(fn: Fn) { this.beforeRequest = fn; } - after(fn: (result: Result)=>Promise) { + after(fn: (result: Result) => Promise) { this.afterResponse = fn; } }