@kevisual/router 0.0.6 → 0.0.7

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.
@@ -93,6 +93,9 @@ type RouteOpts = {
93
93
  run?: Run;
94
94
  nextRoute?: NextRoute;
95
95
  description?: string;
96
+ metadata?: {
97
+ [key: string]: any;
98
+ };
96
99
  middleware?: Route[] | string[];
97
100
  type?: 'route' | 'middleware';
98
101
  /**
@@ -124,13 +127,22 @@ type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
124
127
  declare class Route<U = {
125
128
  [key: string]: any;
126
129
  }> {
130
+ /**
131
+ * 一级路径
132
+ */
127
133
  path?: string;
134
+ /**
135
+ * 二级路径
136
+ */
128
137
  key?: string;
129
138
  id?: string;
130
139
  share?: boolean;
131
140
  run?: Run;
132
141
  nextRoute?: NextRoute;
133
142
  description?: string;
143
+ metadata?: {
144
+ [key: string]: any;
145
+ };
134
146
  middleware?: (Route | string)[];
135
147
  type?: string;
136
148
  private _validator?;
@@ -138,7 +150,13 @@ declare class Route<U = {
138
150
  [key: string]: Schema<any>;
139
151
  };
140
152
  data?: any;
153
+ /**
154
+ * 是否需要验证
155
+ */
141
156
  isVerify?: boolean;
157
+ /**
158
+ * 是否开启debug,开启后会打印错误信息
159
+ */
142
160
  isDebug?: boolean;
143
161
  constructor(path: string, key?: string, opts?: RouteOpts);
144
162
  private createSchema;
@@ -1,6 +1,7 @@
1
1
  const urlAlphabet =
2
2
  'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict';
3
3
 
4
+ /* @ts-self-types="./index.d.ts" */
4
5
  let nanoid = (size = 21) => {
5
6
  let id = '';
6
7
  let bytes = crypto.getRandomValues(new Uint8Array((size |= 0)));
@@ -241,6 +242,9 @@ const quotelessJson = (obj) => {
241
242
  return json.replace(/"([^"]+)":/g, "$1:");
242
243
  };
243
244
  class ZodError extends Error {
245
+ get errors() {
246
+ return this.issues;
247
+ }
244
248
  constructor(issues) {
245
249
  super();
246
250
  this.issues = [];
@@ -261,9 +265,6 @@ class ZodError extends Error {
261
265
  this.name = "ZodError";
262
266
  this.issues = issues;
263
267
  }
264
- get errors() {
265
- return this.issues;
266
- }
267
268
  format(_mapper) {
268
269
  const mapper = _mapper ||
269
270
  function (issue) {
@@ -519,9 +520,9 @@ function addIssueToContext(ctx, issueData) {
519
520
  data: ctx.data,
520
521
  path: ctx.path,
521
522
  errorMaps: [
522
- ctx.common.contextualErrorMap,
523
- ctx.schemaErrorMap,
524
- overrideMap,
523
+ ctx.common.contextualErrorMap, // contextual error map is first priority
524
+ ctx.schemaErrorMap, // then schema-bound map if available
525
+ overrideMap, // then global override map
525
526
  overrideMap === errorMap ? undefined : errorMap, // then global default map
526
527
  ].filter((x) => !!x),
527
528
  });
@@ -608,12 +609,12 @@ PERFORMANCE OF THIS SOFTWARE.
608
609
  ***************************************************************************** */
609
610
 
610
611
  function __classPrivateFieldGet(receiver, state, kind, f) {
611
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
612
+ if (typeof state === "function" ? receiver !== state || true : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
612
613
  return state.get(receiver);
613
614
  }
614
615
 
615
616
  function __classPrivateFieldSet(receiver, state, value, kind, f) {
616
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
617
+ if (typeof state === "function" ? receiver !== state || true : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
617
618
  return (state.set(receiver, value)), value;
618
619
  }
619
620
 
@@ -694,35 +695,6 @@ function processCreateParams(params) {
694
695
  return { errorMap: customMap, description };
695
696
  }
696
697
  class ZodType {
697
- constructor(def) {
698
- /** Alias of safeParseAsync */
699
- this.spa = this.safeParseAsync;
700
- this._def = def;
701
- this.parse = this.parse.bind(this);
702
- this.safeParse = this.safeParse.bind(this);
703
- this.parseAsync = this.parseAsync.bind(this);
704
- this.safeParseAsync = this.safeParseAsync.bind(this);
705
- this.spa = this.spa.bind(this);
706
- this.refine = this.refine.bind(this);
707
- this.refinement = this.refinement.bind(this);
708
- this.superRefine = this.superRefine.bind(this);
709
- this.optional = this.optional.bind(this);
710
- this.nullable = this.nullable.bind(this);
711
- this.nullish = this.nullish.bind(this);
712
- this.array = this.array.bind(this);
713
- this.promise = this.promise.bind(this);
714
- this.or = this.or.bind(this);
715
- this.and = this.and.bind(this);
716
- this.transform = this.transform.bind(this);
717
- this.brand = this.brand.bind(this);
718
- this.default = this.default.bind(this);
719
- this.catch = this.catch.bind(this);
720
- this.describe = this.describe.bind(this);
721
- this.pipe = this.pipe.bind(this);
722
- this.readonly = this.readonly.bind(this);
723
- this.isNullable = this.isNullable.bind(this);
724
- this.isOptional = this.isOptional.bind(this);
725
- }
726
698
  get description() {
727
699
  return this._def.description;
728
700
  }
@@ -786,6 +758,48 @@ class ZodType {
786
758
  const result = this._parseSync({ data, path: ctx.path, parent: ctx });
787
759
  return handleResult(ctx, result);
788
760
  }
761
+ "~validate"(data) {
762
+ var _a, _b;
763
+ const ctx = {
764
+ common: {
765
+ issues: [],
766
+ async: !!this["~standard"].async,
767
+ },
768
+ path: [],
769
+ schemaErrorMap: this._def.errorMap,
770
+ parent: null,
771
+ data,
772
+ parsedType: getParsedType(data),
773
+ };
774
+ if (!this["~standard"].async) {
775
+ try {
776
+ const result = this._parseSync({ data, path: [], parent: ctx });
777
+ return isValid(result)
778
+ ? {
779
+ value: result.value,
780
+ }
781
+ : {
782
+ issues: ctx.common.issues,
783
+ };
784
+ }
785
+ catch (err) {
786
+ if ((_b = (_a = err === null || err === void 0 ? void 0 : err.message) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === null || _b === void 0 ? void 0 : _b.includes("encountered")) {
787
+ this["~standard"].async = true;
788
+ }
789
+ ctx.common = {
790
+ issues: [],
791
+ async: true,
792
+ };
793
+ }
794
+ }
795
+ return this._parseAsync({ data, path: [], parent: ctx }).then((result) => isValid(result)
796
+ ? {
797
+ value: result.value,
798
+ }
799
+ : {
800
+ issues: ctx.common.issues,
801
+ });
802
+ }
789
803
  async parseAsync(data, params) {
790
804
  const result = await this.safeParseAsync(data, params);
791
805
  if (result.success)
@@ -872,6 +886,40 @@ class ZodType {
872
886
  superRefine(refinement) {
873
887
  return this._refinement(refinement);
874
888
  }
889
+ constructor(def) {
890
+ /** Alias of safeParseAsync */
891
+ this.spa = this.safeParseAsync;
892
+ this._def = def;
893
+ this.parse = this.parse.bind(this);
894
+ this.safeParse = this.safeParse.bind(this);
895
+ this.parseAsync = this.parseAsync.bind(this);
896
+ this.safeParseAsync = this.safeParseAsync.bind(this);
897
+ this.spa = this.spa.bind(this);
898
+ this.refine = this.refine.bind(this);
899
+ this.refinement = this.refinement.bind(this);
900
+ this.superRefine = this.superRefine.bind(this);
901
+ this.optional = this.optional.bind(this);
902
+ this.nullable = this.nullable.bind(this);
903
+ this.nullish = this.nullish.bind(this);
904
+ this.array = this.array.bind(this);
905
+ this.promise = this.promise.bind(this);
906
+ this.or = this.or.bind(this);
907
+ this.and = this.and.bind(this);
908
+ this.transform = this.transform.bind(this);
909
+ this.brand = this.brand.bind(this);
910
+ this.default = this.default.bind(this);
911
+ this.catch = this.catch.bind(this);
912
+ this.describe = this.describe.bind(this);
913
+ this.pipe = this.pipe.bind(this);
914
+ this.readonly = this.readonly.bind(this);
915
+ this.isNullable = this.isNullable.bind(this);
916
+ this.isOptional = this.isOptional.bind(this);
917
+ this["~standard"] = {
918
+ version: 1,
919
+ vendor: "zod",
920
+ validate: (data) => this["~validate"](data),
921
+ };
922
+ }
875
923
  optional() {
876
924
  return ZodOptional.create(this, this._def);
877
925
  }
@@ -882,7 +930,7 @@ class ZodType {
882
930
  return this.nullable().optional();
883
931
  }
884
932
  array() {
885
- return ZodArray.create(this, this._def);
933
+ return ZodArray.create(this);
886
934
  }
887
935
  promise() {
888
936
  return ZodPromise.create(this, this._def);
@@ -948,11 +996,12 @@ class ZodType {
948
996
  }
949
997
  const cuidRegex = /^c[^\s-]{8,}$/i;
950
998
  const cuid2Regex = /^[0-9a-z]+$/;
951
- const ulidRegex = /^[0-9A-HJKMNP-TV-Z]{26}$/;
999
+ const ulidRegex = /^[0-9A-HJKMNP-TV-Z]{26}$/i;
952
1000
  // const uuidRegex =
953
1001
  // /^([a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}|00000000-0000-0000-0000-000000000000)$/i;
954
1002
  const uuidRegex = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/i;
955
1003
  const nanoidRegex = /^[a-z0-9_-]{21}$/i;
1004
+ const jwtRegex = /^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$/;
956
1005
  const durationRegex = /^[-+]?P(?!$)(?:(?:[-+]?\d+Y)|(?:[-+]?\d+[.,]\d+Y$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:(?:[-+]?\d+W)|(?:[-+]?\d+[.,]\d+W$))?(?:(?:[-+]?\d+D)|(?:[-+]?\d+[.,]\d+D$))?(?:T(?=[\d+-])(?:(?:[-+]?\d+H)|(?:[-+]?\d+[.,]\d+H$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:[-+]?\d+(?:[.,]\d+)?S)?)??$/;
957
1006
  // from https://stackoverflow.com/a/46181/1550155
958
1007
  // old version: too slow, didn't support unicode
@@ -974,9 +1023,15 @@ const _emojiRegex = `^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$`;
974
1023
  let emojiRegex;
975
1024
  // faster, simpler, safer
976
1025
  const ipv4Regex = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;
977
- const ipv6Regex = /^(([a-f0-9]{1,4}:){7}|::([a-f0-9]{1,4}:){0,6}|([a-f0-9]{1,4}:){1}:([a-f0-9]{1,4}:){0,5}|([a-f0-9]{1,4}:){2}:([a-f0-9]{1,4}:){0,4}|([a-f0-9]{1,4}:){3}:([a-f0-9]{1,4}:){0,3}|([a-f0-9]{1,4}:){4}:([a-f0-9]{1,4}:){0,2}|([a-f0-9]{1,4}:){5}:([a-f0-9]{1,4}:){0,1})([a-f0-9]{1,4}|(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2})))$/;
1026
+ const ipv4CidrRegex = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\/(3[0-2]|[12]?[0-9])$/;
1027
+ // const ipv6Regex =
1028
+ // /^(([a-f0-9]{1,4}:){7}|::([a-f0-9]{1,4}:){0,6}|([a-f0-9]{1,4}:){1}:([a-f0-9]{1,4}:){0,5}|([a-f0-9]{1,4}:){2}:([a-f0-9]{1,4}:){0,4}|([a-f0-9]{1,4}:){3}:([a-f0-9]{1,4}:){0,3}|([a-f0-9]{1,4}:){4}:([a-f0-9]{1,4}:){0,2}|([a-f0-9]{1,4}:){5}:([a-f0-9]{1,4}:){0,1})([a-f0-9]{1,4}|(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2})))$/;
1029
+ const ipv6Regex = /^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/;
1030
+ const ipv6CidrRegex = /^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$/;
978
1031
  // https://stackoverflow.com/questions/7860392/determine-if-string-is-in-base64-using-javascript
979
1032
  const base64Regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
1033
+ // https://base64.guru/standards/base64url
1034
+ const base64urlRegex = /^([0-9a-zA-Z-_]{4})*(([0-9a-zA-Z-_]{2}(==)?)|([0-9a-zA-Z-_]{3}(=)?))?$/;
980
1035
  // simple
981
1036
  // const dateRegexSource = `\\d{4}-\\d{2}-\\d{2}`;
982
1037
  // no leap year validation
@@ -1017,6 +1072,38 @@ function isValidIP(ip, version) {
1017
1072
  }
1018
1073
  return false;
1019
1074
  }
1075
+ function isValidJWT(jwt, alg) {
1076
+ if (!jwtRegex.test(jwt))
1077
+ return false;
1078
+ try {
1079
+ const [header] = jwt.split(".");
1080
+ // Convert base64url to base64
1081
+ const base64 = header
1082
+ .replace(/-/g, "+")
1083
+ .replace(/_/g, "/")
1084
+ .padEnd(header.length + ((4 - (header.length % 4)) % 4), "=");
1085
+ const decoded = JSON.parse(atob(base64));
1086
+ if (typeof decoded !== "object" || decoded === null)
1087
+ return false;
1088
+ if (!decoded.typ || !decoded.alg)
1089
+ return false;
1090
+ if (alg && decoded.alg !== alg)
1091
+ return false;
1092
+ return true;
1093
+ }
1094
+ catch (_a) {
1095
+ return false;
1096
+ }
1097
+ }
1098
+ function isValidCidr(ip, version) {
1099
+ if ((version === "v4" || !version) && ipv4CidrRegex.test(ip)) {
1100
+ return true;
1101
+ }
1102
+ if ((version === "v6" || !version) && ipv6CidrRegex.test(ip)) {
1103
+ return true;
1104
+ }
1105
+ return false;
1106
+ }
1020
1107
  class ZodString extends ZodType {
1021
1108
  _parse(input) {
1022
1109
  if (this._def.coerce) {
@@ -1298,6 +1385,28 @@ class ZodString extends ZodType {
1298
1385
  status.dirty();
1299
1386
  }
1300
1387
  }
1388
+ else if (check.kind === "jwt") {
1389
+ if (!isValidJWT(input.data, check.alg)) {
1390
+ ctx = this._getOrReturnCtx(input, ctx);
1391
+ addIssueToContext(ctx, {
1392
+ validation: "jwt",
1393
+ code: ZodIssueCode.invalid_string,
1394
+ message: check.message,
1395
+ });
1396
+ status.dirty();
1397
+ }
1398
+ }
1399
+ else if (check.kind === "cidr") {
1400
+ if (!isValidCidr(input.data, check.version)) {
1401
+ ctx = this._getOrReturnCtx(input, ctx);
1402
+ addIssueToContext(ctx, {
1403
+ validation: "cidr",
1404
+ code: ZodIssueCode.invalid_string,
1405
+ message: check.message,
1406
+ });
1407
+ status.dirty();
1408
+ }
1409
+ }
1301
1410
  else if (check.kind === "base64") {
1302
1411
  if (!base64Regex.test(input.data)) {
1303
1412
  ctx = this._getOrReturnCtx(input, ctx);
@@ -1309,6 +1418,17 @@ class ZodString extends ZodType {
1309
1418
  status.dirty();
1310
1419
  }
1311
1420
  }
1421
+ else if (check.kind === "base64url") {
1422
+ if (!base64urlRegex.test(input.data)) {
1423
+ ctx = this._getOrReturnCtx(input, ctx);
1424
+ addIssueToContext(ctx, {
1425
+ validation: "base64url",
1426
+ code: ZodIssueCode.invalid_string,
1427
+ message: check.message,
1428
+ });
1429
+ status.dirty();
1430
+ }
1431
+ }
1312
1432
  else {
1313
1433
  util.assertNever(check);
1314
1434
  }
@@ -1355,9 +1475,22 @@ class ZodString extends ZodType {
1355
1475
  base64(message) {
1356
1476
  return this._addCheck({ kind: "base64", ...errorUtil.errToObj(message) });
1357
1477
  }
1478
+ base64url(message) {
1479
+ // base64url encoding is a modification of base64 that can safely be used in URLs and filenames
1480
+ return this._addCheck({
1481
+ kind: "base64url",
1482
+ ...errorUtil.errToObj(message),
1483
+ });
1484
+ }
1485
+ jwt(options) {
1486
+ return this._addCheck({ kind: "jwt", ...errorUtil.errToObj(options) });
1487
+ }
1358
1488
  ip(options) {
1359
1489
  return this._addCheck({ kind: "ip", ...errorUtil.errToObj(options) });
1360
1490
  }
1491
+ cidr(options) {
1492
+ return this._addCheck({ kind: "cidr", ...errorUtil.errToObj(options) });
1493
+ }
1361
1494
  datetime(options) {
1362
1495
  var _a, _b;
1363
1496
  if (typeof options === "string") {
@@ -1448,8 +1581,7 @@ class ZodString extends ZodType {
1448
1581
  });
1449
1582
  }
1450
1583
  /**
1451
- * @deprecated Use z.string().min(1) instead.
1452
- * @see {@link ZodString.min}
1584
+ * Equivalent to `.min(1)`
1453
1585
  */
1454
1586
  nonempty(message) {
1455
1587
  return this.min(1, errorUtil.errToObj(message));
@@ -1511,9 +1643,16 @@ class ZodString extends ZodType {
1511
1643
  get isIP() {
1512
1644
  return !!this._def.checks.find((ch) => ch.kind === "ip");
1513
1645
  }
1646
+ get isCIDR() {
1647
+ return !!this._def.checks.find((ch) => ch.kind === "cidr");
1648
+ }
1514
1649
  get isBase64() {
1515
1650
  return !!this._def.checks.find((ch) => ch.kind === "base64");
1516
1651
  }
1652
+ get isBase64url() {
1653
+ // base64url encoding is a modification of base64 that can safely be used in URLs and filenames
1654
+ return !!this._def.checks.find((ch) => ch.kind === "base64url");
1655
+ }
1517
1656
  get minLength() {
1518
1657
  let min = null;
1519
1658
  for (const ch of this._def.checks) {
@@ -1806,17 +1945,16 @@ class ZodBigInt extends ZodType {
1806
1945
  }
1807
1946
  _parse(input) {
1808
1947
  if (this._def.coerce) {
1809
- input.data = BigInt(input.data);
1948
+ try {
1949
+ input.data = BigInt(input.data);
1950
+ }
1951
+ catch (_a) {
1952
+ return this._getInvalidInput(input);
1953
+ }
1810
1954
  }
1811
1955
  const parsedType = this._getType(input);
1812
1956
  if (parsedType !== ZodParsedType.bigint) {
1813
- const ctx = this._getOrReturnCtx(input);
1814
- addIssueToContext(ctx, {
1815
- code: ZodIssueCode.invalid_type,
1816
- expected: ZodParsedType.bigint,
1817
- received: ctx.parsedType,
1818
- });
1819
- return INVALID;
1957
+ return this._getInvalidInput(input);
1820
1958
  }
1821
1959
  let ctx = undefined;
1822
1960
  const status = new ParseStatus();
@@ -1870,6 +2008,15 @@ class ZodBigInt extends ZodType {
1870
2008
  }
1871
2009
  return { status: status.value, value: input.data };
1872
2010
  }
2011
+ _getInvalidInput(input) {
2012
+ const ctx = this._getOrReturnCtx(input);
2013
+ addIssueToContext(ctx, {
2014
+ code: ZodIssueCode.invalid_type,
2015
+ expected: ZodParsedType.bigint,
2016
+ received: ctx.parsedType,
2017
+ });
2018
+ return INVALID;
2019
+ }
1873
2020
  gte(value, message) {
1874
2021
  return this.setLimit("min", value, true, errorUtil.toString(message));
1875
2022
  }
@@ -4076,7 +4223,23 @@ ZodReadonly.create = (type, params) => {
4076
4223
  ...processCreateParams(params),
4077
4224
  });
4078
4225
  };
4079
- function custom(check, params = {},
4226
+ ////////////////////////////////////////
4227
+ ////////////////////////////////////////
4228
+ ////////// //////////
4229
+ ////////// z.custom //////////
4230
+ ////////// //////////
4231
+ ////////////////////////////////////////
4232
+ ////////////////////////////////////////
4233
+ function cleanParams(params, data) {
4234
+ const p = typeof params === "function"
4235
+ ? params(data)
4236
+ : typeof params === "string"
4237
+ ? { message: params }
4238
+ : params;
4239
+ const p2 = typeof p === "string" ? { message: p } : p;
4240
+ return p2;
4241
+ }
4242
+ function custom(check, _params = {},
4080
4243
  /**
4081
4244
  * @deprecated
4082
4245
  *
@@ -4091,16 +4254,23 @@ fatal) {
4091
4254
  if (check)
4092
4255
  return ZodAny.create().superRefine((data, ctx) => {
4093
4256
  var _a, _b;
4094
- if (!check(data)) {
4095
- const p = typeof params === "function"
4096
- ? params(data)
4097
- : typeof params === "string"
4098
- ? { message: params }
4099
- : params;
4100
- const _fatal = (_b = (_a = p.fatal) !== null && _a !== void 0 ? _a : fatal) !== null && _b !== void 0 ? _b : true;
4101
- const p2 = typeof p === "string" ? { message: p } : p;
4102
- ctx.addIssue({ code: "custom", ...p2, fatal: _fatal });
4257
+ const r = check(data);
4258
+ if (r instanceof Promise) {
4259
+ return r.then((r) => {
4260
+ var _a, _b;
4261
+ if (!r) {
4262
+ const params = cleanParams(_params, data);
4263
+ const _fatal = (_b = (_a = params.fatal) !== null && _a !== void 0 ? _a : fatal) !== null && _b !== void 0 ? _b : true;
4264
+ ctx.addIssue({ code: "custom", ...params, fatal: _fatal });
4265
+ }
4266
+ });
4267
+ }
4268
+ if (!r) {
4269
+ const params = cleanParams(_params, data);
4270
+ const _fatal = (_b = (_a = params.fatal) !== null && _a !== void 0 ? _a : fatal) !== null && _b !== void 0 ? _b : true;
4271
+ ctx.addIssue({ code: "custom", ...params, fatal: _fatal });
4103
4272
  }
4273
+ return;
4104
4274
  });
4105
4275
  return ZodAny.create();
4106
4276
  }
@@ -4564,9 +4734,6 @@ function arrayMap(array, iteratee) {
4564
4734
  */
4565
4735
  var isArray = Array.isArray;
4566
4736
 
4567
- /** Used as references for various `Number` constants. */
4568
- var INFINITY$1 = 1 / 0;
4569
-
4570
4737
  /** Used to convert symbols to primitives and strings. */
4571
4738
  var symbolProto = Symbol$1 ? Symbol$1.prototype : undefined,
4572
4739
  symbolToString = symbolProto ? symbolProto.toString : undefined;
@@ -4592,7 +4759,7 @@ function baseToString(value) {
4592
4759
  return symbolToString ? symbolToString.call(value) : '';
4593
4760
  }
4594
4761
  var result = (value + '');
4595
- return (result == '0' && (1 / value) == -INFINITY$1) ? '-0' : result;
4762
+ return (result == '0' && (1 / value) == -Infinity) ? '-0' : result;
4596
4763
  }
4597
4764
 
4598
4765
  /**
@@ -5382,9 +5549,6 @@ function castPath(value, object) {
5382
5549
  return isKey(value, object) ? [value] : stringToPath(toString(value));
5383
5550
  }
5384
5551
 
5385
- /** Used as references for various `Number` constants. */
5386
- var INFINITY = 1 / 0;
5387
-
5388
5552
  /**
5389
5553
  * Converts `value` to a string key if it's not a string or symbol.
5390
5554
  *
@@ -5397,7 +5561,7 @@ function toKey(value) {
5397
5561
  return value;
5398
5562
  }
5399
5563
  var result = (value + '');
5400
- return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result;
5564
+ return (result == '0' && (1 / value) == -Infinity) ? '-0' : result;
5401
5565
  }
5402
5566
 
5403
5567
  /**
@@ -5452,19 +5616,32 @@ function get(object, path, defaultValue) {
5452
5616
 
5453
5617
  const pickValue = ['path', 'key', 'id', 'description', 'type', 'validator', 'middleware'];
5454
5618
  class Route {
5619
+ /**
5620
+ * 一级路径
5621
+ */
5455
5622
  path;
5623
+ /**
5624
+ * 二级路径
5625
+ */
5456
5626
  key;
5457
5627
  id;
5458
5628
  share = false;
5459
5629
  run;
5460
5630
  nextRoute; // route to run after this route
5461
5631
  description;
5632
+ metadata;
5462
5633
  middleware; // middleware
5463
5634
  type = 'route';
5464
5635
  _validator;
5465
5636
  schema;
5466
5637
  data;
5638
+ /**
5639
+ * 是否需要验证
5640
+ */
5467
5641
  isVerify;
5642
+ /**
5643
+ * 是否开启debug,开启后会打印错误信息
5644
+ */
5468
5645
  isDebug;
5469
5646
  constructor(path, key = '', opts) {
5470
5647
  path = path.trim();
@@ -5479,6 +5656,7 @@ class Route {
5479
5656
  this.run = opts.run;
5480
5657
  this.nextRoute = opts.nextRoute;
5481
5658
  this.description = opts.description;
5659
+ this.metadata = opts.metadata;
5482
5660
  this.type = opts.type || 'route';
5483
5661
  this.validator = opts.validator;
5484
5662
  this.middleware = opts.middleware || [];
@@ -5593,7 +5771,7 @@ class Route {
5593
5771
  // 全覆盖,所以opts需要准确,不能由idUsePath 需要check的变量
5594
5772
  const setOpts = (opts) => {
5595
5773
  const keys = Object.keys(opts);
5596
- const checkList = ['path', 'key', 'run', 'nextRoute', 'description', 'middleware', 'type', 'validator', 'isVerify', 'isDebug'];
5774
+ const checkList = ['path', 'key', 'run', 'nextRoute', 'description', 'metadata', 'middleware', 'type', 'validator', 'isVerify', 'isDebug'];
5597
5775
  for (let item of keys) {
5598
5776
  if (!checkList.includes(item)) {
5599
5777
  continue;
@@ -5691,30 +5869,41 @@ class QueryRouter {
5691
5869
  // run middleware
5692
5870
  if (route && route.middleware && route.middleware.length > 0) {
5693
5871
  const errorMiddleware = [];
5694
- // TODO: 向上递归执行动作, 暂时不考虑
5695
- const routeMiddleware = route.middleware.map((m) => {
5696
- let route;
5697
- const isString = typeof m === 'string';
5698
- if (typeof m === 'string') {
5699
- route = this.routes.find((r) => r.id === m);
5700
- }
5701
- else {
5702
- route = this.routes.find((r) => r.path === m.path && r.key === m.key);
5703
- }
5704
- if (!route) {
5872
+ const getMiddleware = (m) => {
5873
+ if (!m.middleware || m.middleware.length === 0)
5874
+ return [];
5875
+ const routeMiddleware = [];
5876
+ for (let i = 0; i < m.middleware.length; i++) {
5877
+ const item = m.middleware[i];
5878
+ let route;
5879
+ const isString = typeof item === 'string';
5705
5880
  if (isString) {
5706
- errorMiddleware.push({
5707
- id: m,
5708
- });
5881
+ route = this.routes.find((r) => r.id === item);
5709
5882
  }
5710
- else
5711
- errorMiddleware.push({
5712
- path: m?.path,
5713
- key: m?.key,
5714
- });
5883
+ else {
5884
+ route = this.routes.find((r) => r.path === item.path && r.key === item.key);
5885
+ }
5886
+ if (!route) {
5887
+ if (isString) {
5888
+ errorMiddleware.push({
5889
+ id: item,
5890
+ });
5891
+ }
5892
+ else
5893
+ errorMiddleware.push({
5894
+ path: m?.path,
5895
+ key: m?.key,
5896
+ });
5897
+ }
5898
+ const routeMiddlewarePrevious = getMiddleware(route);
5899
+ if (routeMiddlewarePrevious.length > 0) {
5900
+ routeMiddleware.push(...routeMiddlewarePrevious);
5901
+ }
5902
+ routeMiddleware.push(route);
5715
5903
  }
5716
- return route;
5717
- });
5904
+ return routeMiddleware;
5905
+ };
5906
+ const routeMiddleware = getMiddleware(route);
5718
5907
  if (errorMiddleware.length > 0) {
5719
5908
  console.error('middleware not found');
5720
5909
  ctx.body = errorMiddleware;