fix: json for query

This commit is contained in:
2025-12-10 13:23:37 +08:00
parent 9bbc245dcc
commit 37de79fba6
3 changed files with 11 additions and 19 deletions

1
.npmrc
View File

@@ -1,3 +1,2 @@
//npm.xiongxiao.me/:_authToken=${ME_NPM_TOKEN} //npm.xiongxiao.me/:_authToken=${ME_NPM_TOKEN}
@abearxiong:registry=https://npm.pkg.github.com
//registry.npmjs.org/:_authToken=${NPM_TOKEN} //registry.npmjs.org/:_authToken=${NPM_TOKEN}

View File

@@ -1,6 +1,6 @@
{ {
"name": "@kevisual/query", "name": "@kevisual/query",
"version": "0.0.31", "version": "0.0.32",
"main": "dist/query-browser.js", "main": "dist/query-browser.js",
"private": false, "private": false,
"type": "module", "type": "module",

View File

@@ -23,23 +23,11 @@ export type Data = {
payload?: Record<string, any>; payload?: Record<string, any>;
[key: string]: any; [key: string]: any;
}; };
export type Result<S = any> = { export type Result<S = any, U = {}> = {
code: number; code: number;
data?: S; data?: S;
message?: string; message?: string;
success: boolean; } & U;
/**
* 是否不返回 message
*/
noMsg?: boolean;
/**
* 显示错误, 当 handle的信息被处理的时候如果不是success同时自己设置了noMsg那么就不显示错误信息了因为被处理。
*
* 日常: fetch().then(res=>if(res.sucess){message.error('error')})的时候比如401被处理过了就不再提示401错误了。
*
*/
showError: (fn?: () => void) => void;
};
// 额外功能 // 额外功能
export type DataOpts = Partial<QueryOpts> & { export type DataOpts = Partial<QueryOpts> & {
beforeRequest?: Fn; beforeRequest?: Fn;
@@ -55,7 +43,7 @@ export type DataOpts = Partial<QueryOpts> & {
* showError 是 如果 success 为 false 且 noMsg 为 false, 则调用 showError * showError 是 如果 success 为 false 且 noMsg 为 false, 则调用 showError
* @param res 响应 * @param res 响应
*/ */
export const setBaseResponse = (res: Partial<Result>) => { export const setBaseResponse = (res: Partial<Result & { success?: boolean; showError?: (fn?: () => void) => void; noMsg?: boolean }>) => {
res.success = res.code === 200; res.success = res.code === 200;
/** /**
* 显示错误 * 显示错误
@@ -199,7 +187,6 @@ export class Query {
} }
return _adapter(req).then(async (res) => { return _adapter(req).then(async (res) => {
try { try {
setBaseResponse(res);
if (_afterResponse) { if (_afterResponse) {
return await _afterResponse(res, { return await _afterResponse(res, {
req, req,
@@ -250,7 +237,13 @@ export class Query {
...(_options?.headers || {}), ...(_options?.headers || {}),
}, },
}); });
return setBaseResponse(res); if (res && !res.code) {
return {
code: 200,
data: res,
}
}
return res;
} }
} }