From 28558d92f00e774d56bff4a0f1d77f605c349724 Mon Sep 17 00:00:00 2001 From: abearxiong Date: Thu, 12 Sep 2024 22:41:37 +0800 Subject: [PATCH] feat: add success and afterResponse --- dist/index.d.ts | 21 +++++++++++++++++++-- dist/index.js | 19 ++++++++++++++++++- package.json | 2 +- src/index.ts | 31 ++++++++++++++++++++++++++++--- 4 files changed, 66 insertions(+), 7 deletions(-) diff --git a/dist/index.d.ts b/dist/index.d.ts index 8ec5f14..2b05277 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -15,20 +15,37 @@ type QueryOpts = { 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; + 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 index 0780350..17e692e 100644 --- a/dist/index.js +++ b/dist/index.js @@ -39,10 +39,18 @@ const adapter = async (opts) => { }); }; +/** + * const query = new Query(); + * const res = await query.post({ + * path: 'demo', + * key: '1', + * }); + */ class Query { adapter; url; beforeRequest; + afterResponse; headers; timeout; constructor(opts) { @@ -71,11 +79,20 @@ class Query { if (beforeRequest) { await beforeRequest(req); } - return adapter(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/package.json b/package.json index 9e0b11b..0e53295 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kevisual/query", - "version": "0.0.2", + "version": "0.0.2-alpha.0", "main": "dist/index.js", "module": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/index.ts b/src/index.ts index 0ecba12..62d308e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,16 +17,32 @@ type QueryOpts = { 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 class Query { adapter: typeof adapter; url: string; beforeRequest?: Fn; + afterResponse?: (result: Result)=>Promise; headers?: Record; timeout?: number; constructor(opts: QueryOpts) { @@ -37,10 +53,10 @@ export class Query { }; this.timeout = opts.timeout || 60000; // 默认超时时间为 60s } - async get(params: Record & Data & T, options?: DataOpts) { + async get(params: Record & Data & T, options?: DataOpts): Promise> { return this.post(params, options); } - async post(body: Record & Data & T, options?: DataOpts) { + async post(body: Record & Data & T, options?: DataOpts): Promise> { const url = options?.url || this.url; const headers = { ...this.headers, ...options?.headers }; const adapter = options?.adapter || this.adapter; @@ -55,11 +71,20 @@ export class Query { if (beforeRequest) { await beforeRequest(req); } - return adapter(req); + return adapter(req).then(async (res) => { + res.success = res.code === 200; + if (options?.afterResponse) { + return await options.afterResponse(res); + } + return res; + }); } before(fn: Fn) { this.beforeRequest = fn; } + after(fn: (result: Result)=>Promise) { + this.afterResponse = fn; + } } export { adapter };