@kevisual/router 0.0.8-alpha.2 → 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.
- package/dist/router-browser.d.ts +10 -0
- package/dist/router-browser.js +33 -13
- package/dist/router.d.ts +10 -0
- package/dist/router.js +33 -13
- package/package.json +1 -1
package/dist/router-browser.d.ts
CHANGED
|
@@ -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
|
/**
|
package/dist/router-browser.js
CHANGED
|
@@ -4513,9 +4513,12 @@ const schemaFormRule = (rule) => {
|
|
|
4513
4513
|
};
|
|
4514
4514
|
const createSchema = (rule) => {
|
|
4515
4515
|
try {
|
|
4516
|
-
rule.required = rule.required
|
|
4516
|
+
rule.required = rule.required ?? false;
|
|
4517
4517
|
if (!rule.required) {
|
|
4518
|
-
|
|
4518
|
+
// nullable is null
|
|
4519
|
+
// nullish is null or undefined
|
|
4520
|
+
// optional is undefined
|
|
4521
|
+
return schemaFormRule(rule).optional();
|
|
4519
4522
|
}
|
|
4520
4523
|
return schemaFormRule(rule);
|
|
4521
4524
|
}
|
|
@@ -5643,11 +5646,16 @@ class Route {
|
|
|
5643
5646
|
* 是否开启debug,开启后会打印错误信息
|
|
5644
5647
|
*/
|
|
5645
5648
|
isDebug;
|
|
5649
|
+
/**
|
|
5650
|
+
* 是否需要序列化
|
|
5651
|
+
*/
|
|
5652
|
+
needSerialize;
|
|
5646
5653
|
constructor(path, key = '', opts) {
|
|
5647
5654
|
path = path.trim();
|
|
5648
5655
|
key = key.trim();
|
|
5649
5656
|
this.path = path;
|
|
5650
5657
|
this.key = key;
|
|
5658
|
+
this.needSerialize = opts?.needSerialize ?? true;
|
|
5651
5659
|
if (opts) {
|
|
5652
5660
|
this.id = opts.id || nanoid();
|
|
5653
5661
|
if (!opts.id && opts.idUsePath) {
|
|
@@ -5673,15 +5681,20 @@ class Route {
|
|
|
5673
5681
|
this.isDebug = opts?.isDebug ?? false;
|
|
5674
5682
|
}
|
|
5675
5683
|
createSchema() {
|
|
5676
|
-
|
|
5677
|
-
|
|
5678
|
-
|
|
5679
|
-
|
|
5680
|
-
|
|
5681
|
-
|
|
5682
|
-
|
|
5683
|
-
|
|
5684
|
-
|
|
5684
|
+
try {
|
|
5685
|
+
const validator = this.validator;
|
|
5686
|
+
const keys = Object.keys(validator || {});
|
|
5687
|
+
const schemaList = keys.map((key) => {
|
|
5688
|
+
return { [key]: createSchema(validator[key]) };
|
|
5689
|
+
});
|
|
5690
|
+
const schema = schemaList.reduce((prev, current) => {
|
|
5691
|
+
return { ...prev, ...current };
|
|
5692
|
+
}, {});
|
|
5693
|
+
this.schema = schema;
|
|
5694
|
+
}
|
|
5695
|
+
catch (e) {
|
|
5696
|
+
console.error('createSchema error:', e);
|
|
5697
|
+
}
|
|
5685
5698
|
}
|
|
5686
5699
|
/**
|
|
5687
5700
|
* set validator and create schema
|
|
@@ -6020,8 +6033,15 @@ class QueryRouter {
|
|
|
6020
6033
|
ctx.nextQuery = {};
|
|
6021
6034
|
return await this.runRoute(path, key, ctx);
|
|
6022
6035
|
}
|
|
6023
|
-
|
|
6024
|
-
|
|
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
|
+
}
|
|
6025
6045
|
if (!ctx.code)
|
|
6026
6046
|
ctx.code = 200;
|
|
6027
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
|
@@ -4532,9 +4532,12 @@ const schemaFormRule = (rule) => {
|
|
|
4532
4532
|
};
|
|
4533
4533
|
const createSchema = (rule) => {
|
|
4534
4534
|
try {
|
|
4535
|
-
rule.required = rule.required
|
|
4535
|
+
rule.required = rule.required ?? false;
|
|
4536
4536
|
if (!rule.required) {
|
|
4537
|
-
|
|
4537
|
+
// nullable is null
|
|
4538
|
+
// nullish is null or undefined
|
|
4539
|
+
// optional is undefined
|
|
4540
|
+
return schemaFormRule(rule).optional();
|
|
4538
4541
|
}
|
|
4539
4542
|
return schemaFormRule(rule);
|
|
4540
4543
|
}
|
|
@@ -5662,11 +5665,16 @@ class Route {
|
|
|
5662
5665
|
* 是否开启debug,开启后会打印错误信息
|
|
5663
5666
|
*/
|
|
5664
5667
|
isDebug;
|
|
5668
|
+
/**
|
|
5669
|
+
* 是否需要序列化
|
|
5670
|
+
*/
|
|
5671
|
+
needSerialize;
|
|
5665
5672
|
constructor(path, key = '', opts) {
|
|
5666
5673
|
path = path.trim();
|
|
5667
5674
|
key = key.trim();
|
|
5668
5675
|
this.path = path;
|
|
5669
5676
|
this.key = key;
|
|
5677
|
+
this.needSerialize = opts?.needSerialize ?? true;
|
|
5670
5678
|
if (opts) {
|
|
5671
5679
|
this.id = opts.id || nanoid();
|
|
5672
5680
|
if (!opts.id && opts.idUsePath) {
|
|
@@ -5692,15 +5700,20 @@ class Route {
|
|
|
5692
5700
|
this.isDebug = opts?.isDebug ?? false;
|
|
5693
5701
|
}
|
|
5694
5702
|
createSchema() {
|
|
5695
|
-
|
|
5696
|
-
|
|
5697
|
-
|
|
5698
|
-
|
|
5699
|
-
|
|
5700
|
-
|
|
5701
|
-
|
|
5702
|
-
|
|
5703
|
-
|
|
5703
|
+
try {
|
|
5704
|
+
const validator = this.validator;
|
|
5705
|
+
const keys = Object.keys(validator || {});
|
|
5706
|
+
const schemaList = keys.map((key) => {
|
|
5707
|
+
return { [key]: createSchema(validator[key]) };
|
|
5708
|
+
});
|
|
5709
|
+
const schema = schemaList.reduce((prev, current) => {
|
|
5710
|
+
return { ...prev, ...current };
|
|
5711
|
+
}, {});
|
|
5712
|
+
this.schema = schema;
|
|
5713
|
+
}
|
|
5714
|
+
catch (e) {
|
|
5715
|
+
console.error('createSchema error:', e);
|
|
5716
|
+
}
|
|
5704
5717
|
}
|
|
5705
5718
|
/**
|
|
5706
5719
|
* set validator and create schema
|
|
@@ -6039,8 +6052,15 @@ class QueryRouter {
|
|
|
6039
6052
|
ctx.nextQuery = {};
|
|
6040
6053
|
return await this.runRoute(path, key, ctx);
|
|
6041
6054
|
}
|
|
6042
|
-
|
|
6043
|
-
|
|
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
|
+
}
|
|
6044
6064
|
if (!ctx.code)
|
|
6045
6065
|
ctx.code = 200;
|
|
6046
6066
|
return ctx;
|