@kevisual/router 0.0.8-alpha.3 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -87,6 +87,8 @@ type RouteContext<T = {
87
87
  [key: string]: any;
88
88
  }) => Promise<any>;
89
89
  index?: number;
90
+ /** 是否需要序列化 */
91
+ needSerialize?: boolean;
90
92
  throw?: (code?: number | string, message?: string, tips?: string) => void;
91
93
  } & T;
92
94
  type Run<T = any> = (ctx: RouteContext<T>) => Promise<typeof ctx | null | void>;
@@ -125,6 +127,10 @@ type RouteOpts = {
125
127
  */
126
128
  idUsePath?: boolean;
127
129
  isDebug?: boolean;
130
+ /**
131
+ * 是否需要序列化
132
+ */
133
+ needSerialize?: boolean;
128
134
  };
129
135
  type DefineRouteOpts = Omit<RouteOpts, 'idUsePath' | 'verify' | 'verifyKey' | 'nextRoute'>;
130
136
  declare const pickValue: readonly ["path", "key", "id", "description", "type", "validator", "middleware"];
@@ -163,6 +169,10 @@ declare class Route<U = {
163
169
  * 是否开启debug,开启后会打印错误信息
164
170
  */
165
171
  isDebug?: boolean;
172
+ /**
173
+ * 是否需要序列化
174
+ */
175
+ needSerialize?: boolean;
166
176
  constructor(path: string, key?: string, opts?: RouteOpts);
167
177
  private createSchema;
168
178
  /**
@@ -5646,11 +5646,16 @@ class Route {
5646
5646
  * 是否开启debug,开启后会打印错误信息
5647
5647
  */
5648
5648
  isDebug;
5649
+ /**
5650
+ * 是否需要序列化
5651
+ */
5652
+ needSerialize;
5649
5653
  constructor(path, key = '', opts) {
5650
5654
  path = path.trim();
5651
5655
  key = key.trim();
5652
5656
  this.path = path;
5653
5657
  this.key = key;
5658
+ this.needSerialize = opts?.needSerialize ?? true;
5654
5659
  if (opts) {
5655
5660
  this.id = opts.id || nanoid();
5656
5661
  if (!opts.id && opts.idUsePath) {
@@ -6028,8 +6033,15 @@ class QueryRouter {
6028
6033
  ctx.nextQuery = {};
6029
6034
  return await this.runRoute(path, key, ctx);
6030
6035
  }
6031
- // clear body
6032
- ctx.body = JSON.parse(JSON.stringify(ctx.body || ''));
6036
+ try {
6037
+ if (route.needSerialize) {
6038
+ // clear body
6039
+ ctx.body = JSON.parse(JSON.stringify(ctx.body || ''));
6040
+ }
6041
+ }
6042
+ catch (e) {
6043
+ console.log('serialize error', e);
6044
+ }
6033
6045
  if (!ctx.code)
6034
6046
  ctx.code = 200;
6035
6047
  return ctx;
package/dist/router.d.ts CHANGED
@@ -92,6 +92,8 @@ type RouteContext<T = {
92
92
  [key: string]: any;
93
93
  }) => Promise<any>;
94
94
  index?: number;
95
+ /** 是否需要序列化 */
96
+ needSerialize?: boolean;
95
97
  throw?: (code?: number | string, message?: string, tips?: string) => void;
96
98
  } & T;
97
99
  type Run<T = any> = (ctx: RouteContext<T>) => Promise<typeof ctx | null | void>;
@@ -130,6 +132,10 @@ type RouteOpts = {
130
132
  */
131
133
  idUsePath?: boolean;
132
134
  isDebug?: boolean;
135
+ /**
136
+ * 是否需要序列化
137
+ */
138
+ needSerialize?: boolean;
133
139
  };
134
140
  type DefineRouteOpts = Omit<RouteOpts, 'idUsePath' | 'verify' | 'verifyKey' | 'nextRoute'>;
135
141
  declare const pickValue: readonly ["path", "key", "id", "description", "type", "validator", "middleware"];
@@ -168,6 +174,10 @@ declare class Route<U = {
168
174
  * 是否开启debug,开启后会打印错误信息
169
175
  */
170
176
  isDebug?: boolean;
177
+ /**
178
+ * 是否需要序列化
179
+ */
180
+ needSerialize?: boolean;
171
181
  constructor(path: string, key?: string, opts?: RouteOpts);
172
182
  private createSchema;
173
183
  /**
package/dist/router.js CHANGED
@@ -5665,11 +5665,16 @@ class Route {
5665
5665
  * 是否开启debug,开启后会打印错误信息
5666
5666
  */
5667
5667
  isDebug;
5668
+ /**
5669
+ * 是否需要序列化
5670
+ */
5671
+ needSerialize;
5668
5672
  constructor(path, key = '', opts) {
5669
5673
  path = path.trim();
5670
5674
  key = key.trim();
5671
5675
  this.path = path;
5672
5676
  this.key = key;
5677
+ this.needSerialize = opts?.needSerialize ?? true;
5673
5678
  if (opts) {
5674
5679
  this.id = opts.id || nanoid();
5675
5680
  if (!opts.id && opts.idUsePath) {
@@ -6047,8 +6052,15 @@ class QueryRouter {
6047
6052
  ctx.nextQuery = {};
6048
6053
  return await this.runRoute(path, key, ctx);
6049
6054
  }
6050
- // clear body
6051
- ctx.body = JSON.parse(JSON.stringify(ctx.body || ''));
6055
+ try {
6056
+ if (route.needSerialize) {
6057
+ // clear body
6058
+ ctx.body = JSON.parse(JSON.stringify(ctx.body || ''));
6059
+ }
6060
+ }
6061
+ catch (e) {
6062
+ console.log('serialize error', e);
6063
+ }
6052
6064
  if (!ctx.code)
6053
6065
  ctx.code = 200;
6054
6066
  return ctx;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@kevisual/router",
4
- "version": "0.0.8-alpha.3",
4
+ "version": "0.0.8",
5
5
  "description": "",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.js",