feat: add success and afterResponse
This commit is contained in:
parent
360f6ba180
commit
28558d92f0
21
dist/index.d.ts
vendored
21
dist/index.d.ts
vendored
@ -15,20 +15,37 @@ type QueryOpts = {
|
||||
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>(params: Record<string, any> & Data & T, options?: DataOpts): Promise<any>;
|
||||
post<T>(body: 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, 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 };
|
||||
|
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 {
|
||||
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 };
|
||||
|
@ -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",
|
||||
|
31
src/index.ts
31
src/index.ts
@ -17,16 +17,32 @@ type QueryOpts = {
|
||||
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 class Query {
|
||||
adapter: typeof adapter;
|
||||
url: string;
|
||||
beforeRequest?: Fn;
|
||||
afterResponse?: (result: Result)=>Promise<any>;
|
||||
headers?: Record<string, string>;
|
||||
timeout?: number;
|
||||
constructor(opts: QueryOpts) {
|
||||
@ -37,10 +53,10 @@ export class Query {
|
||||
};
|
||||
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);
|
||||
}
|
||||
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 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<any>) {
|
||||
this.afterResponse = fn;
|
||||
}
|
||||
}
|
||||
|
||||
export { adapter };
|
||||
|
Loading…
x
Reference in New Issue
Block a user