fix: add json needSerialize

This commit is contained in:
xion 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",
"name": "@kevisual/router",
"version": "0.0.8",
"version": "0.0.9",
"description": "",
"main": "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 server = opts?.server || new Server(opts?.serverOptions || {});
server.setHandle(router.getHandle(router, opts?.routerHandle, opts?.routerContext));
router.setContext(opts?.routerContext);
router.setContext({ needSerialize: true, ...opts?.routerContext });
this.router = router;
this.server = server;
if (opts?.io) {

View File

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