chore: bump @kevisual/router to version 0.0.75, update QueryClient constructor, and enhance token handling in Query class
This commit is contained in:
4
bun.lock
4
bun.lock
@@ -6,7 +6,7 @@
|
|||||||
"name": "@kevisual/query",
|
"name": "@kevisual/query",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@kevisual/code-builder": "^0.0.6",
|
"@kevisual/code-builder": "^0.0.6",
|
||||||
"@kevisual/router": "^0.0.74",
|
"@kevisual/router": "^0.0.75",
|
||||||
"@types/node": "^25.2.3",
|
"@types/node": "^25.2.3",
|
||||||
"es-toolkit": "^1.44.0",
|
"es-toolkit": "^1.44.0",
|
||||||
"typescript": "^5.9.3",
|
"typescript": "^5.9.3",
|
||||||
@@ -18,7 +18,7 @@
|
|||||||
"packages": {
|
"packages": {
|
||||||
"@kevisual/code-builder": ["@kevisual/code-builder@0.0.6", "", { "bin": { "code-builder": "bin/code.js", "builder": "bin/code.js" } }, "sha512-0aqATB31/yw4k4s5/xKnfr4DKbUnx8e3Z3BmKbiXTrc+CqWiWTdlGe9bKI9dZ2Df+xNp6g11W4xM2NICNyyCCw=="],
|
"@kevisual/code-builder": ["@kevisual/code-builder@0.0.6", "", { "bin": { "code-builder": "bin/code.js", "builder": "bin/code.js" } }, "sha512-0aqATB31/yw4k4s5/xKnfr4DKbUnx8e3Z3BmKbiXTrc+CqWiWTdlGe9bKI9dZ2Df+xNp6g11W4xM2NICNyyCCw=="],
|
||||||
|
|
||||||
"@kevisual/router": ["@kevisual/router@0.0.74", "", { "dependencies": { "es-toolkit": "1.44.0" } }, "sha512-J8qDsvrpf317H0Gq9YkeGwI+GS23RC0q/mYbKOia8wF33ylz+pDhBN8T1KmXx90AVBt/tMGNVJRgEhTVdTgpvA=="],
|
"@kevisual/router": ["@kevisual/router@0.0.75", "", { "dependencies": { "es-toolkit": "^1.44.0" } }, "sha512-WBDRKMjNYTP7ymkUUtiQwWYIcqnc+TGo3rFuRze8ovYV2UN5cQxIkIfsDbgWOdV1/v9b57gtiJvJRqWjCBWKRg=="],
|
||||||
|
|
||||||
"@types/node": ["@types/node@25.2.3", "", { "dependencies": { "undici-types": "7.16.0" } }, "sha512-m0jEgYlYz+mDJZ2+F4v8D1AyQb+QzsNqRuI7xg1VQX/KlKS0qT9r1Mo16yo5F/MtifXFgaofIFsdFMox2SxIbQ=="],
|
"@types/node": ["@types/node@25.2.3", "", { "dependencies": { "undici-types": "7.16.0" } }, "sha512-m0jEgYlYz+mDJZ2+F4v8D1AyQb+QzsNqRuI7xg1VQX/KlKS0qT9r1Mo16yo5F/MtifXFgaofIFsdFMox2SxIbQ=="],
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@kevisual/query",
|
"name": "@kevisual/query",
|
||||||
"version": "0.0.46",
|
"version": "0.0.47",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "npm run clean && bun run bun.config.ts",
|
"build": "npm run clean && bun run bun.config.ts",
|
||||||
@@ -19,7 +19,7 @@
|
|||||||
"description": "",
|
"description": "",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@kevisual/code-builder": "^0.0.6",
|
"@kevisual/code-builder": "^0.0.6",
|
||||||
"@kevisual/router": "^0.0.74",
|
"@kevisual/router": "^0.0.75",
|
||||||
"@types/node": "^25.2.3",
|
"@types/node": "^25.2.3",
|
||||||
"typescript": "^5.9.3",
|
"typescript": "^5.9.3",
|
||||||
"es-toolkit": "^1.44.0",
|
"es-toolkit": "^1.44.0",
|
||||||
|
|||||||
@@ -13,14 +13,6 @@ export type AdapterOpts = {
|
|||||||
body?: Record<string, any> | FormData; // body 可以是对象、字符串或 FormData
|
body?: Record<string, any> | FormData; // body 可以是对象、字符串或 FormData
|
||||||
timeout?: number;
|
timeout?: number;
|
||||||
method?: Method;
|
method?: Method;
|
||||||
/**
|
|
||||||
* @deprecated use responseType
|
|
||||||
*/
|
|
||||||
isBlob?: boolean; // 是否返回 Blob 对象, 第一优先
|
|
||||||
/**
|
|
||||||
* @deprecated use responseType
|
|
||||||
*/
|
|
||||||
isText?: boolean; // 是否返回文本内容, 第二优先
|
|
||||||
/**
|
/**
|
||||||
* 响应类型,
|
* 响应类型,
|
||||||
* */
|
* */
|
||||||
@@ -29,7 +21,7 @@ export type AdapterOpts = {
|
|||||||
};
|
};
|
||||||
export const isTextForContentType = (contentType: string | null) => {
|
export const isTextForContentType = (contentType: string | null) => {
|
||||||
if (!contentType) return false;
|
if (!contentType) return false;
|
||||||
const textTypes = ['text/', 'xml', 'html', 'javascript', 'css', 'csv', 'plain', 'x-www-form-urlencoded', 'md'];
|
const textTypes = ['text/', 'xml', 'html', 'javascript', 'css', 'csv', 'plain', 'x-www-form-urlencoded', 'md', 'json'];
|
||||||
return textTypes.some((type) => contentType.includes(type));
|
return textTypes.some((type) => contentType.includes(type));
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
@@ -42,13 +34,6 @@ export const adapter = async (opts: AdapterOpts = {}, overloadOpts?: RequestInit
|
|||||||
const controller = new AbortController();
|
const controller = new AbortController();
|
||||||
const signal = controller.signal;
|
const signal = controller.signal;
|
||||||
const isPostFile = opts.isPostFile || false; // 是否为文件上传
|
const isPostFile = opts.isPostFile || false; // 是否为文件上传
|
||||||
let responseType = opts.responseType || 'json'; // 响应类型
|
|
||||||
if (opts.isBlob) {
|
|
||||||
responseType = 'blob';
|
|
||||||
} else if (opts.isText) {
|
|
||||||
responseType = 'text';
|
|
||||||
}
|
|
||||||
|
|
||||||
const timeout = opts.timeout || 60000 * 3; // 默认超时时间为 60s * 3
|
const timeout = opts.timeout || 60000 * 3; // 默认超时时间为 60s * 3
|
||||||
const timer = setTimeout(() => {
|
const timer = setTimeout(() => {
|
||||||
controller.abort();
|
controller.abort();
|
||||||
@@ -110,22 +95,32 @@ export const adapter = async (opts: AdapterOpts = {}, overloadOpts?: RequestInit
|
|||||||
.then(async (response) => {
|
.then(async (response) => {
|
||||||
// 获取 Content-Type 头部信息
|
// 获取 Content-Type 头部信息
|
||||||
const contentType = response.headers.get('Content-Type');
|
const contentType = response.headers.get('Content-Type');
|
||||||
if (responseType === 'blob') {
|
|
||||||
return await response.blob(); // 直接返回 Blob 对象
|
|
||||||
}
|
|
||||||
const isText = responseType === 'text';
|
|
||||||
const isJson = contentType && contentType.includes('application/json');
|
const isJson = contentType && contentType.includes('application/json');
|
||||||
|
const isSuccess = response.ok;
|
||||||
// 判断返回的数据类型
|
// 判断返回的数据类型
|
||||||
if (isJson && !isText) {
|
if (isJson) {
|
||||||
return await response.json(); // 解析为 JSON
|
const json = await response.json();
|
||||||
|
if (json?.code) {
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
code: isSuccess ? 200 : response.status,
|
||||||
|
status: response.status,
|
||||||
|
data: json,
|
||||||
|
}
|
||||||
} else if (isTextForContentType(contentType)) {
|
} else if (isTextForContentType(contentType)) {
|
||||||
return {
|
return {
|
||||||
code: response.status,
|
code: isSuccess ? 200 : response.status,
|
||||||
status: response.status,
|
status: response.status,
|
||||||
data: await response.text(), // 直接返回文本内容
|
data: await response.text(), // 直接返回文本内容
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
return response;
|
return {
|
||||||
|
code: isSuccess ? 200 : response.status,
|
||||||
|
status: response.status,
|
||||||
|
data: '非文本非JSON响应, 请手动处理response。',
|
||||||
|
response
|
||||||
|
};
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
@@ -143,8 +138,4 @@ export const adapter = async (opts: AdapterOpts = {}, overloadOpts?: RequestInit
|
|||||||
.finally(() => {
|
.finally(() => {
|
||||||
clearTimeout(timer);
|
clearTimeout(timer);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
/**
|
|
||||||
* adapter
|
|
||||||
*/
|
|
||||||
export const queryFetch = adapter;
|
|
||||||
@@ -121,8 +121,11 @@ export class QueryApi<P extends { [path: string]: { [key: string]: Pos } } = {}>
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (const [key, pos] of Object.entries(methods)) {
|
for (const [key, pos] of Object.entries(methods)) {
|
||||||
that[path][key] = (data?: Partial<ExtractArgsFromMetadata<typeof pos>>, opts?: DataOpts) => {
|
that[path][key] = (data?: Partial<ExtractArgsFromMetadata<typeof pos>>, opts: DataOpts = {}) => {
|
||||||
const _pos = pick(pos, ['path', 'key', 'id']);
|
const _pos = pick(pos, ['path', 'key', 'id']);
|
||||||
|
if (pos.metadata?.viewItem?.api?.url && !opts.url) {
|
||||||
|
opts.url = pos.metadata.viewItem.api.url;
|
||||||
|
}
|
||||||
return that.query.post({
|
return that.query.post({
|
||||||
..._pos,
|
..._pos,
|
||||||
payload: data
|
payload: data
|
||||||
|
|||||||
@@ -19,23 +19,8 @@ type QueryOpts = {
|
|||||||
* 前端调用后端QueryRouter, 封装 beforeRequest 和 wss
|
* 前端调用后端QueryRouter, 封装 beforeRequest 和 wss
|
||||||
*/
|
*/
|
||||||
export class QueryClient extends Query {
|
export class QueryClient extends Query {
|
||||||
tokenName: string;
|
constructor(opts?: QueryOptions & { io?: boolean }) {
|
||||||
storage: Storage;
|
|
||||||
token: string;
|
|
||||||
constructor(opts?: QueryOptions & { tokenName?: string; storage?: Storage; io?: boolean }) {
|
|
||||||
super(opts);
|
super(opts);
|
||||||
this.tokenName = opts?.tokenName || 'token';
|
|
||||||
this.storage = opts?.storage || globalThis.localStorage;
|
|
||||||
this.beforeRequest = async (opts) => {
|
|
||||||
const token = this.token || this.getToken();
|
|
||||||
if (token) {
|
|
||||||
opts.headers = {
|
|
||||||
...opts.headers,
|
|
||||||
Authorization: `Bearer ${token}`,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return opts;
|
|
||||||
};
|
|
||||||
if (opts?.io) {
|
if (opts?.io) {
|
||||||
this.createWs();
|
this.createWs();
|
||||||
}
|
}
|
||||||
@@ -43,15 +28,6 @@ export class QueryClient extends Query {
|
|||||||
createWs(opts?: QueryWsOpts) {
|
createWs(opts?: QueryWsOpts) {
|
||||||
this.qws = new QueryWs({ url: this.url, ...opts });
|
this.qws = new QueryWs({ url: this.url, ...opts });
|
||||||
}
|
}
|
||||||
getToken() {
|
|
||||||
return this.storage.getItem(this.tokenName);
|
|
||||||
}
|
|
||||||
saveToken(token: string) {
|
|
||||||
this.storage.setItem(this.tokenName, token);
|
|
||||||
}
|
|
||||||
removeToken() {
|
|
||||||
this.storage.removeItem(this.tokenName);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// 移除默认生成的实例
|
// 移除默认生成的实例
|
||||||
// export const client = new QueryClient();
|
// export const client = new QueryClient();
|
||||||
|
|||||||
65
src/query.ts
65
src/query.ts
@@ -24,6 +24,8 @@ export type QueryOptions = {
|
|||||||
headers?: Record<string, string>;
|
headers?: Record<string, string>;
|
||||||
timeout?: number;
|
timeout?: number;
|
||||||
isClient?: boolean;
|
isClient?: boolean;
|
||||||
|
tokenName?: string;
|
||||||
|
storage?: Storage;
|
||||||
beforeRequest?: Fn;
|
beforeRequest?: Fn;
|
||||||
}
|
}
|
||||||
export type Data = {
|
export type Data = {
|
||||||
@@ -46,34 +48,11 @@ export type DataOpts = Partial<QueryOpts> & {
|
|||||||
*/
|
*/
|
||||||
noStop?: boolean;
|
noStop?: boolean;
|
||||||
};
|
};
|
||||||
/**
|
|
||||||
* 设置基础响应, 设置 success 和 showError,
|
|
||||||
* success 是 code 是否等于 200
|
|
||||||
* showError 是 如果 success 为 false 且 noMsg 为 false, 则调用 showError
|
|
||||||
* @param res 响应
|
|
||||||
*/
|
|
||||||
export const setBaseResponse = (res: Partial<Result & { success?: boolean; showError?: (fn?: () => void) => void; noMsg?: boolean }>) => {
|
|
||||||
res.success = res.code === 200;
|
|
||||||
/**
|
|
||||||
* 显示错误
|
|
||||||
* @param fn 错误处理函数
|
|
||||||
*/
|
|
||||||
res.showError = (fn?: () => void) => {
|
|
||||||
if (!res.success && !res.noMsg) {
|
|
||||||
fn?.();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
return res as Result;
|
|
||||||
};
|
|
||||||
export const wrapperError = ({ code, message }: { code?: number; message?: string }) => {
|
export const wrapperError = ({ code, message }: { code?: number; message?: string }) => {
|
||||||
const result = {
|
const result = {
|
||||||
code: code || 500,
|
code: code || 500,
|
||||||
success: false,
|
message: message || '请求错误'
|
||||||
message: message || 'api request error',
|
|
||||||
showError: (fn?: () => void) => {
|
|
||||||
//
|
|
||||||
},
|
|
||||||
noMsg: true,
|
|
||||||
};
|
};
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
@@ -105,12 +84,13 @@ export class Query {
|
|||||||
stop?: boolean;
|
stop?: boolean;
|
||||||
// 默认不使用ws
|
// 默认不使用ws
|
||||||
qws: QueryWs;
|
qws: QueryWs;
|
||||||
/**
|
tokenName: string;
|
||||||
* 默认是 /client/router或者 默认是 /api/router
|
storage: Storage;
|
||||||
*/
|
token: string;
|
||||||
isClient = false;
|
|
||||||
constructor(opts?: QueryOptions) {
|
constructor(opts?: QueryOptions) {
|
||||||
this.adapter = opts?.adapter || adapter;
|
this.adapter = opts?.adapter || adapter;
|
||||||
|
this.tokenName = opts?.tokenName || 'token';
|
||||||
|
this.storage = opts?.storage || globalThis?.localStorage;
|
||||||
const defaultURL = opts?.isClient ? '/client/router' : '/api/router';
|
const defaultURL = opts?.isClient ? '/client/router' : '/api/router';
|
||||||
this.url = opts?.url || defaultURL;
|
this.url = opts?.url || defaultURL;
|
||||||
this.headers = opts?.headers || {
|
this.headers = opts?.headers || {
|
||||||
@@ -121,7 +101,7 @@ export class Query {
|
|||||||
this.beforeRequest = opts.beforeRequest;
|
this.beforeRequest = opts.beforeRequest;
|
||||||
} else {
|
} else {
|
||||||
this.beforeRequest = async (opts) => {
|
this.beforeRequest = async (opts) => {
|
||||||
const token = globalThis?.localStorage?.getItem('token');
|
const token = this.token || this.storage?.getItem?.(this.tokenName);
|
||||||
if (token) {
|
if (token) {
|
||||||
opts.headers = {
|
opts.headers = {
|
||||||
...opts.headers,
|
...opts.headers,
|
||||||
@@ -162,7 +142,6 @@ export class Query {
|
|||||||
*/
|
*/
|
||||||
async post<R = any, P = any>(body: Data & P, options?: DataOpts): Promise<Result<R>> {
|
async post<R = any, P = any>(body: Data & P, options?: DataOpts): Promise<Result<R>> {
|
||||||
const url = options?.url || this.url;
|
const url = options?.url || this.url;
|
||||||
console.log('query post', url, body, options);
|
|
||||||
const { headers, adapter, beforeRequest, afterResponse, timeout, ...rest } = options || {};
|
const { headers, adapter, beforeRequest, afterResponse, timeout, ...rest } = options || {};
|
||||||
const _headers = { ...this.headers, ...headers };
|
const _headers = { ...this.headers, ...headers };
|
||||||
const _adapter = adapter || this.adapter;
|
const _adapter = adapter || this.adapter;
|
||||||
@@ -182,7 +161,7 @@ export class Query {
|
|||||||
if (res === false) {
|
if (res === false) {
|
||||||
return wrapperError({
|
return wrapperError({
|
||||||
code: 500,
|
code: 500,
|
||||||
message: 'request is cancel',
|
message: '请求取消',
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
req: req,
|
req: req,
|
||||||
});
|
});
|
||||||
@@ -192,14 +171,14 @@ export class Query {
|
|||||||
console.error('request beforeFn error', e, req);
|
console.error('request beforeFn error', e, req);
|
||||||
return wrapperError({
|
return wrapperError({
|
||||||
code: 500,
|
code: 500,
|
||||||
message: 'api request beforeFn error',
|
message: '请求在请求前处理时发生错误',
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
req: req,
|
req: req,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (this.stop && !options?.noStop) {
|
if (this.stop && !options?.noStop) {
|
||||||
const that = this;
|
const that = this;
|
||||||
await new Promise((resolve) => {
|
const res = await new Promise((resolve) => {
|
||||||
let timer = 0;
|
let timer = 0;
|
||||||
const detect = setInterval(() => {
|
const detect = setInterval(() => {
|
||||||
if (!that.stop) {
|
if (!that.stop) {
|
||||||
@@ -207,11 +186,21 @@ export class Query {
|
|||||||
resolve(true);
|
resolve(true);
|
||||||
}
|
}
|
||||||
timer++;
|
timer++;
|
||||||
if (timer > 30) {
|
if (timer > 5) {
|
||||||
console.error('request stop: timeout', req.url, timer);
|
console.error('等待请求失败:', req.url, timer);
|
||||||
|
clearInterval(detect);
|
||||||
|
resolve(false);
|
||||||
}
|
}
|
||||||
}, 1000);
|
}, 1000);
|
||||||
});
|
});
|
||||||
|
if (!res) {
|
||||||
|
return wrapperError({
|
||||||
|
code: 500,
|
||||||
|
message: '请求取消,可能是因为用户未登录或者token过期',
|
||||||
|
// @ts-ignore
|
||||||
|
req: req,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return _adapter(req).then(async (res) => {
|
return _adapter(req).then(async (res) => {
|
||||||
try {
|
try {
|
||||||
@@ -225,10 +214,10 @@ export class Query {
|
|||||||
|
|
||||||
return res;
|
return res;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('request afterFn error', e, req);
|
console.error('请求在响应后处理时发生错误', e, req);
|
||||||
return wrapperError({
|
return wrapperError({
|
||||||
code: 500,
|
code: 500,
|
||||||
message: 'api request afterFn error',
|
message: '请求在响应后处理时发生错误',
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
req: req,
|
req: req,
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user