fix; fix add serialize

This commit is contained in:
熊潇 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", "$schema": "https://json.schemastore.org/package",
"name": "@kevisual/router", "name": "@kevisual/router",
"version": "0.0.8-alpha.3", "version": "0.0.8",
"description": "", "description": "",
"main": "dist/index.js", "main": "dist/index.js",
"module": "dist/index.js", "module": "dist/index.js",

View File

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