fix; fix add serialize

This commit is contained in:
xion 2025-03-03 23:20:18 +08:00
parent ba7e00bd7a
commit 5911f29c8f
2 changed files with 20 additions and 3 deletions

View File

@ -1,7 +1,7 @@
{
"$schema": "https://json.schemastore.org/package",
"name": "@kevisual/router",
"version": "0.0.8-alpha.3",
"version": "0.0.8",
"description": "",
"main": "dist/index.js",
"module": "dist/index.js",

View File

@ -38,6 +38,8 @@ export type RouteContext<T = { code?: number }, S = any> = {
/** 请求 route的返回结果不包函ctx */
queryRoute?: (message: { path: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) => Promise<any>;
index?: number;
/** 是否需要序列化 */
needSerialize?: boolean;
throw?: (code?: number | string, message?: string, tips?: string) => void;
} & T;
@ -72,6 +74,10 @@ export type RouteOpts = {
*/
idUsePath?: boolean;
isDebug?: boolean;
/**
*
*/
needSerialize?: boolean;
};
export type DefineRouteOpts = Omit<RouteOpts, 'idUsePath' | 'verify' | 'verifyKey' | 'nextRoute'>;
const pickValue = ['path', 'key', 'id', 'description', 'type', 'validator', 'middleware'] as const;
@ -104,11 +110,16 @@ export class Route<U = { [key: string]: any }> {
* debug
*/
isDebug?: boolean;
/**
*
*/
needSerialize?: boolean;
constructor(path: string, key: string = '', opts?: RouteOpts) {
path = path.trim();
key = key.trim();
this.path = path;
this.key = key;
this.needSerialize = opts?.needSerialize ?? true;
if (opts) {
this.id = opts.id || nanoid();
if (!opts.id && opts.idUsePath) {
@ -483,8 +494,14 @@ export class QueryRouter {
ctx.nextQuery = {};
return await this.runRoute(path, key, ctx);
}
// clear body
ctx.body = JSON.parse(JSON.stringify(ctx.body || ''));
try {
if (route.needSerialize) {
// clear body
ctx.body = JSON.parse(JSON.stringify(ctx.body || ''));
}
} catch (e) {
console.log('serialize error', e);
}
if (!ctx.code) ctx.code = 200;
return ctx;
} else {