fix: add json needSerialize

This commit is contained in:
熊潇 2025-03-06 22:45:11 +08:00
parent 5911f29c8f
commit e8f7f61e09
3 changed files with 10 additions and 22 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", "version": "0.0.9",
"description": "", "description": "",
"main": "dist/index.js", "main": "dist/index.js",
"module": "dist/index.js", "module": "dist/index.js",

View File

@ -27,7 +27,7 @@ export class App<T = {}, U = AppReqRes> {
const router = opts?.router || new QueryRouter(); const router = opts?.router || new QueryRouter();
const server = opts?.server || new Server(opts?.serverOptions || {}); const server = opts?.server || new Server(opts?.serverOptions || {});
server.setHandle(router.getHandle(router, opts?.routerHandle, opts?.routerContext)); server.setHandle(router.getHandle(router, opts?.routerHandle, opts?.routerContext));
router.setContext(opts?.routerContext); router.setContext({ needSerialize: true, ...opts?.routerContext });
this.router = router; this.router = router;
this.server = server; this.server = server;
if (opts?.io) { if (opts?.io) {

View File

@ -38,9 +38,9 @@ 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;
throw?: (code?: number | string, message?: string, tips?: string) => void;
/** 是否需要序列化 */ /** 是否需要序列化 */
needSerialize?: boolean; needSerialize?: boolean;
throw?: (code?: number | string, message?: string, tips?: string) => void;
} & T; } & T;
export type Run<T = any> = (ctx: RouteContext<T>) => Promise<typeof ctx | null | void>; export type Run<T = any> = (ctx: RouteContext<T>) => Promise<typeof ctx | null | void>;
@ -74,10 +74,6 @@ 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;
@ -110,16 +106,11 @@ 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) {
@ -494,14 +485,6 @@ export class QueryRouter {
ctx.nextQuery = {}; ctx.nextQuery = {};
return await this.runRoute(path, key, ctx); return await this.runRoute(path, key, ctx);
} }
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; if (!ctx.code) ctx.code = 200;
return ctx; return ctx;
} else { } else {
@ -534,7 +517,12 @@ export class QueryRouter {
ctx.call = this.call.bind(this); ctx.call = this.call.bind(this);
ctx.queryRoute = this.queryRoute.bind(this); ctx.queryRoute = this.queryRoute.bind(this);
ctx.index = 0; ctx.index = 0;
return await this.runRoute(path, key, ctx); const res = await this.runRoute(path, key, ctx);
const serialize = ctx.needSerialize ?? true; // 是否需要序列化
if (serialize) {
res.body = JSON.parse(JSON.stringify(res.body || ''));
}
return res;
} }
/** /**
* context的请求返回的内容 * context的请求返回的内容
@ -637,7 +625,7 @@ export class QueryRouterServer extends QueryRouter {
constructor(opts?: QueryRouterServerOpts) { constructor(opts?: QueryRouterServerOpts) {
super(); super();
this.handle = this.getHandle(this, opts?.handleFn, opts?.context); this.handle = this.getHandle(this, opts?.handleFn, opts?.context);
this.setContext(opts?.context); this.setContext({ needSerialize: false, ...opts?.context });
} }
setHandle(wrapperFn?: HandleFn, ctx?: RouteContext) { setHandle(wrapperFn?: HandleFn, ctx?: RouteContext) {
this.handle = this.getHandle(this, wrapperFn, ctx); this.handle = this.getHandle(this, wrapperFn, ctx);