feat: 修改query,添加noMsg的确认

This commit is contained in:
xion 2025-03-20 12:33:36 +08:00
parent 164c06c6a7
commit d0ce0b2c36
3 changed files with 63 additions and 12 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@kevisual/query", "name": "@kevisual/query",
"version": "0.0.9-alpha.2", "version": "0.0.9",
"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",
@ -22,10 +22,10 @@
"license": "ISC", "license": "ISC",
"description": "", "description": "",
"devDependencies": { "devDependencies": {
"@rollup/plugin-node-resolve": "^16.0.0", "@rollup/plugin-node-resolve": "^16.0.1",
"@rollup/plugin-typescript": "^12.1.2", "@rollup/plugin-typescript": "^12.1.2",
"rollup": "^4.34.9", "rollup": "^4.36.0",
"rollup-plugin-dts": "^6.1.1", "rollup-plugin-dts": "^6.2.0",
"ts-node": "^10.9.2", "ts-node": "^10.9.2",
"tslib": "^2.8.1", "tslib": "^2.8.1",
"typescript": "^5.8.2", "typescript": "^5.8.2",
@ -58,6 +58,6 @@
} }
}, },
"dependencies": { "dependencies": {
"openai": "^4.86.1" "openai": "^4.88.0"
} }
} }

View File

@ -14,7 +14,7 @@ type QueryOpts = {
/** /**
* QueryRouter * QueryRouter
*/ */
export class QueryClient<U = any, V = any> extends Query<U, V> { export class QueryClient<R = any> extends Query<R> {
tokenName: string; tokenName: string;
storage: Storage; storage: Storage;
token: string; token: string;

View File

@ -1,4 +1,9 @@
import { adapter } from './adapter.ts'; import { adapter } from './adapter.ts';
/**
*
* @param opts
* @returns
*/
type Fn = (opts: { type Fn = (opts: {
url?: string; url?: string;
headers?: Record<string, string>; headers?: Record<string, string>;
@ -24,11 +29,22 @@ type Result<S = any> = {
data?: S; data?: S;
message?: string; message?: string;
success: boolean; success: boolean;
/**
* message
*/
noMsg?: boolean;
/**
* , handle的信息被处理的时候successnoMsg
*
* 日常: fetch().then(res=>if(res.sucess){message.error('error')})401401
*
*/
showError: (fn?: () => void) => void;
}; };
// 额外功能 // 额外功能
type DataOpts = Partial<QueryOpts> & { type DataOpts = Partial<QueryOpts> & {
beforeRequest?: Fn; beforeRequest?: Fn;
afterResponse?: (result: Result) => Promise<any>; afterResponse?: <S, U = S>(result: Result<S>) => Promise<U>;
}; };
/** /**
* const query = new Query(); * const query = new Query();
@ -39,7 +55,7 @@ type DataOpts = Partial<QueryOpts> & {
* *
* U是参数 V是返回值 * U是参数 V是返回值
*/ */
export class Query<U = any, V = any> { export class Query<R = any> {
adapter: typeof adapter; adapter: typeof adapter;
url: string; url: string;
beforeRequest?: Fn; beforeRequest?: Fn;
@ -54,10 +70,26 @@ export class Query<U = any, V = any> {
}; };
this.timeout = opts?.timeout || 60000 * 3; // 默认超时时间为 60s * 3 this.timeout = opts?.timeout || 60000 * 3; // 默认超时时间为 60s * 3
} }
async get<T = any, S = any>(params: Record<string, any> & Data & U & T, options?: DataOpts): Promise<Result<V & S>> { /**
* get post
* T是请求类型自定义
* S是返回类型自定义
* @param params
* @param options
* @returns
*/
async get<T = any, S = any>(params: Record<string, any> & Data & T, options?: DataOpts): Promise<Result<R & S>> {
return this.post(params, options); return this.post(params, options);
} }
async post<T = any, S = any>(body: Record<string, any> & Data & T, options?: DataOpts): Promise<Result<S>> { /**
* post
* T是请求类型自定义
* S是返回类型自定义
* @param body
* @param options
* @returns
*/
async post<T = any, S = any>(body: Record<string, any> & Data & T, options?: DataOpts): Promise<Result<R & 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;
@ -75,11 +107,12 @@ export class Query<U = any, V = any> {
await beforeRequest(req); await beforeRequest(req);
} }
} catch (e) { } catch (e) {
console.error(e); console.error('request beforeFn error', e, req);
return { return {
code: 500, code: 500,
success: false, success: false,
message: 'api request beforeFn error', message: 'api request beforeFn error',
showError: () => {},
}; };
} }
return adapter(req).then(async (res) => { return adapter(req).then(async (res) => {
@ -88,20 +121,38 @@ export class Query<U = any, V = any> {
if (afterResponse) { if (afterResponse) {
return await afterResponse(res); return await afterResponse(res);
} }
/**
*
* @param fn
*/
res.showError = (fn?: () => void) => {
if (!res.success && !res.noResult) {
fn?.();
}
};
return res; return res;
} catch (e) { } catch (e) {
console.error(e); console.error('request error', e, req);
return { return {
code: 500, code: 500,
success: false, success: false,
message: 'api request afterFn error', message: 'api request afterFn error',
showError: () => {},
}; };
} }
}); });
} }
/**
*
* @param fn
*/
before(fn: Fn) { before(fn: Fn) {
this.beforeRequest = fn; this.beforeRequest = fn;
} }
/**
*
* @param fn
*/
after(fn: (result: Result) => Promise<any>) { after(fn: (result: Result) => Promise<any>) {
this.afterResponse = fn; this.afterResponse = fn;
} }