@kevisual/router 0.0.8-alpha.1 → 0.0.8-alpha.3

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.
@@ -70,6 +70,11 @@ type RouteContext<T = {
70
70
  path: string;
71
71
  key?: string;
72
72
  payload?: any;
73
+ [key: string]: any;
74
+ } | {
75
+ id: string;
76
+ apyload?: any;
77
+ [key: string]: any;
73
78
  }, ctx?: RouteContext & {
74
79
  [key: string]: any;
75
80
  }) => Promise<any>;
@@ -264,7 +269,8 @@ declare class QueryRouter {
264
269
  * @returns
265
270
  */
266
271
  call(message: {
267
- path: string;
272
+ id?: string;
273
+ path?: string;
268
274
  key?: string;
269
275
  payload?: any;
270
276
  }, ctx?: RouteContext & {
@@ -4513,9 +4513,12 @@ const schemaFormRule = (rule) => {
4513
4513
  };
4514
4514
  const createSchema = (rule) => {
4515
4515
  try {
4516
- rule.required = rule.required || false;
4516
+ rule.required = rule.required ?? false;
4517
4517
  if (!rule.required) {
4518
- return schemaFormRule(rule).nullable();
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
  }
@@ -5673,15 +5676,20 @@ class Route {
5673
5676
  this.isDebug = opts?.isDebug ?? false;
5674
5677
  }
5675
5678
  createSchema() {
5676
- const validator = this.validator;
5677
- const keys = Object.keys(validator || {});
5678
- const schemaList = keys.map((key) => {
5679
- return { [key]: createSchema(validator[key]) };
5680
- });
5681
- const schema = schemaList.reduce((prev, current) => {
5682
- return { ...prev, ...current };
5683
- }, {});
5684
- this.schema = schema;
5679
+ try {
5680
+ const validator = this.validator;
5681
+ const keys = Object.keys(validator || {});
5682
+ const schemaList = keys.map((key) => {
5683
+ return { [key]: createSchema(validator[key]) };
5684
+ });
5685
+ const schema = schemaList.reduce((prev, current) => {
5686
+ return { ...prev, ...current };
5687
+ }, {});
5688
+ this.schema = schema;
5689
+ }
5690
+ catch (e) {
5691
+ console.error('createSchema error:', e);
5692
+ }
5685
5693
  }
5686
5694
  /**
5687
5695
  * set validator and create schema
@@ -6065,7 +6073,24 @@ class QueryRouter {
6065
6073
  * @returns
6066
6074
  */
6067
6075
  async call(message, ctx) {
6068
- return await this.parse(message, { ...this.context, ...ctx });
6076
+ let path = message.path;
6077
+ message.key;
6078
+ if (message.id) {
6079
+ const route = this.routes.find((r) => r.id === message.id);
6080
+ if (route) {
6081
+ path = route.path;
6082
+ route.key;
6083
+ }
6084
+ else {
6085
+ return { code: 404, body: null, message: 'Not found route' };
6086
+ }
6087
+ }
6088
+ else if (path) {
6089
+ return await this.parse({ ...message, path }, { ...this.context, ...ctx });
6090
+ }
6091
+ else {
6092
+ return { code: 404, body: null, message: 'Not found path' };
6093
+ }
6069
6094
  }
6070
6095
  /**
6071
6096
  * 请求 result 的数据
package/dist/router.d.ts CHANGED
@@ -75,6 +75,11 @@ type RouteContext<T = {
75
75
  path: string;
76
76
  key?: string;
77
77
  payload?: any;
78
+ [key: string]: any;
79
+ } | {
80
+ id: string;
81
+ apyload?: any;
82
+ [key: string]: any;
78
83
  }, ctx?: RouteContext & {
79
84
  [key: string]: any;
80
85
  }) => Promise<any>;
@@ -269,7 +274,8 @@ declare class QueryRouter {
269
274
  * @returns
270
275
  */
271
276
  call(message: {
272
- path: string;
277
+ id?: string;
278
+ path?: string;
273
279
  key?: string;
274
280
  payload?: any;
275
281
  }, ctx?: RouteContext & {
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 || false;
4535
+ rule.required = rule.required ?? false;
4536
4536
  if (!rule.required) {
4537
- return schemaFormRule(rule).nullable();
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
  }
@@ -5692,15 +5695,20 @@ class Route {
5692
5695
  this.isDebug = opts?.isDebug ?? false;
5693
5696
  }
5694
5697
  createSchema() {
5695
- const validator = this.validator;
5696
- const keys = Object.keys(validator || {});
5697
- const schemaList = keys.map((key) => {
5698
- return { [key]: createSchema(validator[key]) };
5699
- });
5700
- const schema = schemaList.reduce((prev, current) => {
5701
- return { ...prev, ...current };
5702
- }, {});
5703
- this.schema = schema;
5698
+ try {
5699
+ const validator = this.validator;
5700
+ const keys = Object.keys(validator || {});
5701
+ const schemaList = keys.map((key) => {
5702
+ return { [key]: createSchema(validator[key]) };
5703
+ });
5704
+ const schema = schemaList.reduce((prev, current) => {
5705
+ return { ...prev, ...current };
5706
+ }, {});
5707
+ this.schema = schema;
5708
+ }
5709
+ catch (e) {
5710
+ console.error('createSchema error:', e);
5711
+ }
5704
5712
  }
5705
5713
  /**
5706
5714
  * set validator and create schema
@@ -6084,7 +6092,24 @@ class QueryRouter {
6084
6092
  * @returns
6085
6093
  */
6086
6094
  async call(message, ctx) {
6087
- return await this.parse(message, { ...this.context, ...ctx });
6095
+ let path = message.path;
6096
+ message.key;
6097
+ if (message.id) {
6098
+ const route = this.routes.find((r) => r.id === message.id);
6099
+ if (route) {
6100
+ path = route.path;
6101
+ route.key;
6102
+ }
6103
+ else {
6104
+ return { code: 404, body: null, message: 'Not found route' };
6105
+ }
6106
+ }
6107
+ else if (path) {
6108
+ return await this.parse({ ...message, path }, { ...this.context, ...ctx });
6109
+ }
6110
+ else {
6111
+ return { code: 404, body: null, message: 'Not found path' };
6112
+ }
6088
6113
  }
6089
6114
  /**
6090
6115
  * 请求 result 的数据
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.1",
4
+ "version": "0.0.8-alpha.3",
5
5
  "description": "",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.js",