update: stop

This commit is contained in:
xion 2025-03-21 20:00:09 +08:00
parent a488c6ec48
commit dcab87c77a
2 changed files with 29 additions and 2 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@kevisual/query", "name": "@kevisual/query",
"version": "0.0.12", "version": "0.0.13",
"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",

View File

@ -44,7 +44,7 @@ export type Result<S = any> = {
// 额外功能 // 额外功能
export type DataOpts = Partial<QueryOpts> & { export type DataOpts = Partial<QueryOpts> & {
beforeRequest?: Fn; beforeRequest?: Fn;
afterResponse?: <S, U = S>(result: Result<S>, ctx?: { req?: any; res?: any; fetch?: any }) => Promise<U>; afterResponse?: <S = any>(result: Result<S>, ctx?: { req?: any; res?: any; fetch?: any }) => Promise<S>;
}; };
/** /**
* , success showError, * , success showError,
@ -80,6 +80,11 @@ export class Query {
afterResponse?: DataOpts['afterResponse']; afterResponse?: DataOpts['afterResponse'];
headers?: Record<string, string>; headers?: Record<string, string>;
timeout?: number; timeout?: number;
/**
* 401
*/
stop?: boolean;
constructor(opts?: QueryOpts) { constructor(opts?: QueryOpts) {
this.adapter = opts?.adapter || adapter; this.adapter = opts?.adapter || adapter;
this.url = opts?.url || '/api/router'; this.url = opts?.url || '/api/router';
@ -88,6 +93,12 @@ export class Query {
}; };
this.timeout = opts?.timeout || 60000 * 3; // 默认超时时间为 60s * 3 this.timeout = opts?.timeout || 60000 * 3; // 默认超时时间为 60s * 3
} }
/**
*
*/
setStop(stop: boolean) {
this.stop = stop;
}
/** /**
* get post * get post
* T是请求类型自定义 * T是请求类型自定义
@ -133,6 +144,22 @@ export class Query {
showError: () => {}, showError: () => {},
}; };
} }
if (this.stop) {
const that = this;
await new Promise((resolve) => {
let timer = 0;
const detect = setInterval(() => {
if (!that.stop) {
clearInterval(detect);
resolve(true);
}
timer++;
if (timer > 30) {
console.error('request stop: timeout', req.url, timer);
}
}, 1000);
});
}
return adapter(req).then(async (res) => { return adapter(req).then(async (res) => {
try { try {
setBaseResponse(res); setBaseResponse(res);