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