fix; fix add serialize
This commit is contained in:
parent
ba7e00bd7a
commit
5911f29c8f
@ -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",
|
||||
|
21
src/route.ts
21
src/route.ts
@ -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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user