feat: add success and afterResponse
This commit is contained in:
		
							
								
								
									
										21
									
								
								dist/index.d.ts
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										21
									
								
								dist/index.d.ts
									
									
									
									
										vendored
									
									
								
							| @@ -15,20 +15,37 @@ type QueryOpts = { | |||||||
| type Data = { | type Data = { | ||||||
|     path?: string; |     path?: string; | ||||||
|     key?: string; |     key?: string; | ||||||
|  |     payload?: Record<string, any>; | ||||||
|     [key: string]: any; |     [key: string]: any; | ||||||
| }; | }; | ||||||
|  | type Result<S = any> = { | ||||||
|  |     code: number; | ||||||
|  |     data?: S; | ||||||
|  |     message?: string; | ||||||
|  |     success: boolean; | ||||||
|  | }; | ||||||
| type DataOpts = Partial<QueryOpts> & { | type DataOpts = Partial<QueryOpts> & { | ||||||
|     beforeRequest?: Fn; |     beforeRequest?: Fn; | ||||||
|  |     afterResponse?: (result: Result) => Promise<any>; | ||||||
| }; | }; | ||||||
|  | /** | ||||||
|  |  * const query = new Query(); | ||||||
|  |  * const res = await query.post({ | ||||||
|  |  *   path: 'demo', | ||||||
|  |  *   key: '1', | ||||||
|  |  *  }); | ||||||
|  |  */ | ||||||
| export declare class Query { | export declare class Query { | ||||||
|     adapter: typeof adapter; |     adapter: typeof adapter; | ||||||
|     url: string; |     url: string; | ||||||
|     beforeRequest?: Fn; |     beforeRequest?: Fn; | ||||||
|  |     afterResponse?: (result: Result) => Promise<any>; | ||||||
|     headers?: Record<string, string>; |     headers?: Record<string, string>; | ||||||
|     timeout?: number; |     timeout?: number; | ||||||
|     constructor(opts: QueryOpts); |     constructor(opts: QueryOpts); | ||||||
|     get<T>(params: Record<string, any> & Data & T, options?: DataOpts): Promise<any>; |     get<T, S>(params: Record<string, any> & Data & T, options?: DataOpts): Promise<Result<S>>; | ||||||
|     post<T>(body: Record<string, any> & Data & T, options?: DataOpts): Promise<any>; |     post<T, S>(body: Record<string, any> & Data & T, options?: DataOpts): Promise<Result<S>>; | ||||||
|     before(fn: Fn): void; |     before(fn: Fn): void; | ||||||
|  |     after(fn: (result: Result) => Promise<any>): void; | ||||||
| } | } | ||||||
| export { adapter }; | export { adapter }; | ||||||
|   | |||||||
							
								
								
									
										19
									
								
								dist/index.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										19
									
								
								dist/index.js
									
									
									
									
										vendored
									
									
								
							| @@ -39,10 +39,18 @@ const adapter = async (opts) => { | |||||||
|     }); |     }); | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * const query = new Query(); | ||||||
|  |  * const res = await query.post({ | ||||||
|  |  *   path: 'demo', | ||||||
|  |  *   key: '1', | ||||||
|  |  *  }); | ||||||
|  |  */ | ||||||
| class Query { | class Query { | ||||||
|     adapter; |     adapter; | ||||||
|     url; |     url; | ||||||
|     beforeRequest; |     beforeRequest; | ||||||
|  |     afterResponse; | ||||||
|     headers; |     headers; | ||||||
|     timeout; |     timeout; | ||||||
|     constructor(opts) { |     constructor(opts) { | ||||||
| @@ -71,11 +79,20 @@ class Query { | |||||||
|         if (beforeRequest) { |         if (beforeRequest) { | ||||||
|             await beforeRequest(req); |             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) { |     before(fn) { | ||||||
|         this.beforeRequest = fn; |         this.beforeRequest = fn; | ||||||
|     } |     } | ||||||
|  |     after(fn) { | ||||||
|  |         this.afterResponse = fn; | ||||||
|  |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| export { Query, adapter }; | export { Query, adapter }; | ||||||
|   | |||||||
| @@ -1,6 +1,6 @@ | |||||||
| { | { | ||||||
|   "name": "@kevisual/query", |   "name": "@kevisual/query", | ||||||
|   "version": "0.0.2", |   "version": "0.0.2-alpha.0", | ||||||
|   "main": "dist/index.js", |   "main": "dist/index.js", | ||||||
|   "module": "dist/index.js", |   "module": "dist/index.js", | ||||||
|   "types": "dist/index.d.ts", |   "types": "dist/index.d.ts", | ||||||
|   | |||||||
							
								
								
									
										31
									
								
								src/index.ts
									
									
									
									
									
								
							
							
						
						
									
										31
									
								
								src/index.ts
									
									
									
									
									
								
							| @@ -17,16 +17,32 @@ type QueryOpts = { | |||||||
| type Data = { | type Data = { | ||||||
|   path?: string; |   path?: string; | ||||||
|   key?: string; |   key?: string; | ||||||
|  |   payload?: Record<string, any>; | ||||||
|   [key: string]: any; |   [key: string]: any; | ||||||
| }; | }; | ||||||
|  | type Result<S=any> = { | ||||||
|  |   code: number; | ||||||
|  |   data?: S; | ||||||
|  |   message?: string; | ||||||
|  |   success: boolean; | ||||||
|  | } | ||||||
| // 额外功能 | // 额外功能 | ||||||
| type DataOpts = Partial<QueryOpts> & { | type DataOpts = Partial<QueryOpts> & { | ||||||
|   beforeRequest?: Fn; |   beforeRequest?: Fn; | ||||||
|  |   afterResponse?: (result: Result)=>Promise<any>; | ||||||
| }; | }; | ||||||
|  | /** | ||||||
|  |  * const query = new Query(); | ||||||
|  |  * const res = await query.post({ | ||||||
|  |  *   path: 'demo', | ||||||
|  |  *   key: '1', | ||||||
|  |  *  }); | ||||||
|  |  */ | ||||||
| export class Query { | export class Query { | ||||||
|   adapter: typeof adapter; |   adapter: typeof adapter; | ||||||
|   url: string; |   url: string; | ||||||
|   beforeRequest?: Fn; |   beforeRequest?: Fn; | ||||||
|  |   afterResponse?: (result: Result)=>Promise<any>; | ||||||
|   headers?: Record<string, string>; |   headers?: Record<string, string>; | ||||||
|   timeout?: number; |   timeout?: number; | ||||||
|   constructor(opts: QueryOpts) { |   constructor(opts: QueryOpts) { | ||||||
| @@ -37,10 +53,10 @@ export class Query { | |||||||
|     }; |     }; | ||||||
|     this.timeout = opts.timeout || 60000; // 默认超时时间为 60s |     this.timeout = opts.timeout || 60000; // 默认超时时间为 60s | ||||||
|   } |   } | ||||||
|   async get<T>(params: Record<string, any> & Data & T, options?: DataOpts) { |   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); | ||||||
|   } |   } | ||||||
|   async post<T>(body: Record<string, any> & Data & T, options?: DataOpts) { |   async post<T, S>(body: Record<string, any> & Data & T, options?: DataOpts): Promise<Result<S>> { | ||||||
|     const url = options?.url || this.url; |     const url = options?.url || this.url; | ||||||
|     const headers = { ...this.headers, ...options?.headers }; |     const headers = { ...this.headers, ...options?.headers }; | ||||||
|     const adapter = options?.adapter || this.adapter; |     const adapter = options?.adapter || this.adapter; | ||||||
| @@ -55,11 +71,20 @@ export class Query { | |||||||
|     if (beforeRequest) { |     if (beforeRequest) { | ||||||
|       await beforeRequest(req); |       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) { |   before(fn: Fn) { | ||||||
|     this.beforeRequest = fn; |     this.beforeRequest = fn; | ||||||
|   } |   } | ||||||
|  |   after(fn: (result: Result)=>Promise<any>) { | ||||||
|  |     this.afterResponse = fn; | ||||||
|  |   } | ||||||
| } | } | ||||||
|  |  | ||||||
| export { adapter }; | export { adapter }; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user