@kevisual/router 0.0.15 → 0.0.17

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.
@@ -2,6 +2,7 @@ import { Schema } from 'zod';
2
2
  export { Schema } from 'zod';
3
3
  import * as querystring from 'querystring';
4
4
  import { IncomingMessage } from 'node:http';
5
+ import { RouteOpts as RouteOpts$1, QueryRouterServer as QueryRouterServer$1, RouteMiddleware as RouteMiddleware$1, Run as Run$1 } from '@kevisual/router';
5
6
 
6
7
  type BaseRule = {
7
8
  value?: any;
@@ -110,6 +111,11 @@ type RouteContext<T = {
110
111
  } & T;
111
112
  type Run<T = any> = (ctx: RouteContext<T>) => Promise<typeof ctx | null | void>;
112
113
  type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
114
+ type RouteMiddleware = {
115
+ path: string;
116
+ key?: string;
117
+ id?: string;
118
+ } | string;
113
119
  type RouteOpts = {
114
120
  path?: string;
115
121
  key?: string;
@@ -120,7 +126,7 @@ type RouteOpts = {
120
126
  metadata?: {
121
127
  [key: string]: any;
122
128
  };
123
- middleware?: Route[] | string[];
129
+ middleware?: RouteMiddleware[];
124
130
  type?: 'route' | 'middleware';
125
131
  /**
126
132
  * validator: {
@@ -171,7 +177,7 @@ declare class Route<U = {
171
177
  metadata?: {
172
178
  [key: string]: any;
173
179
  };
174
- middleware?: (Route | string)[];
180
+ middleware?: RouteMiddleware[];
175
181
  type?: string;
176
182
  private _validator?;
177
183
  schema?: {
@@ -431,5 +437,47 @@ declare const parseSearchValue: (value?: string, opts?: {
431
437
  decode?: boolean;
432
438
  }) => any;
433
439
 
434
- export { CustomError, QueryRouter, QueryRouterServer, Route, createSchema, parseBody, parseSearch, parseSearchValue };
435
- export type { RouteContext, RouteOpts, Rule, Run };
440
+ type RouteObject = {
441
+ [key: string]: RouteOpts$1;
442
+ };
443
+ declare function define<T extends Record<string, RouteOpts$1>>(value: T): {
444
+ [K in keyof T]: T[K] & RouteOpts$1;
445
+ };
446
+ type RouteArray = RouteOpts$1[];
447
+ type ChainOptions = {
448
+ app: QueryRouterServer$1;
449
+ };
450
+ declare class Chain {
451
+ object: RouteOpts$1;
452
+ app?: QueryRouterServer$1;
453
+ constructor(object: RouteOpts$1, opts?: ChainOptions);
454
+ get key(): string;
455
+ get path(): string;
456
+ setDescription(desc: string): this;
457
+ setMeta(metadata: {
458
+ [key: string]: any;
459
+ }): this;
460
+ setPath(path: string): this;
461
+ setMiddleware(middleware: RouteMiddleware$1[]): this;
462
+ setKey(key: string): this;
463
+ setId(key: string): this;
464
+ setRun(run: Run$1): this;
465
+ define(run: Run$1): this;
466
+ createRoute(): this;
467
+ }
468
+ declare const util: {
469
+ getChain: (obj: RouteOpts$1, opts?: ChainOptions) => Chain;
470
+ };
471
+ declare class QueryUtil<T extends RouteObject = RouteObject> {
472
+ routeObject: T;
473
+ app: QueryRouterServer$1;
474
+ constructor(object: T, opts?: ChainOptions);
475
+ static createFormObj<U extends RouteObject>(object: U, opts?: ChainOptions): QueryUtil<U>;
476
+ static create<U extends Record<string, RouteOpts$1>>(value: U, opts?: ChainOptions): QueryUtil<U>;
477
+ get<K extends keyof T>(key: K): RouteOpts$1;
478
+ chain<K extends keyof T>(key: K, opts?: ChainOptions): Chain;
479
+ queryChain<K extends keyof T>(key: K): (queryData?: Record<string, any>) => RouteOpts$1;
480
+ }
481
+
482
+ export { CustomError, QueryRouter, QueryRouterServer, QueryUtil, Route, createSchema, define, parseBody, parseSearch, parseSearchValue, util };
483
+ export type { RouteArray, RouteContext, RouteObject, RouteOpts, Rule, Run };
@@ -94,7 +94,7 @@ try {
94
94
  }
95
95
  */
96
96
 
97
- var util;
97
+ var util$1;
98
98
  (function (util) {
99
99
  util.assertEqual = (val) => val;
100
100
  function assertIs(_arg) { }
@@ -156,7 +156,7 @@ var util;
156
156
  }
157
157
  return value;
158
158
  };
159
- })(util || (util = {}));
159
+ })(util$1 || (util$1 = {}));
160
160
  var objectUtil;
161
161
  (function (objectUtil) {
162
162
  objectUtil.mergeShapes = (first, second) => {
@@ -166,7 +166,7 @@ var objectUtil;
166
166
  };
167
167
  };
168
168
  })(objectUtil || (objectUtil = {}));
169
- const ZodParsedType = util.arrayToEnum([
169
+ const ZodParsedType = util$1.arrayToEnum([
170
170
  "string",
171
171
  "nan",
172
172
  "number",
@@ -233,7 +233,7 @@ const getParsedType = (data) => {
233
233
  }
234
234
  };
235
235
 
236
- const ZodIssueCode = util.arrayToEnum([
236
+ const ZodIssueCode = util$1.arrayToEnum([
237
237
  "invalid_type",
238
238
  "invalid_literal",
239
239
  "custom",
@@ -337,7 +337,7 @@ class ZodError extends Error {
337
337
  return this.message;
338
338
  }
339
339
  get message() {
340
- return JSON.stringify(this.issues, util.jsonStringifyReplacer, 2);
340
+ return JSON.stringify(this.issues, util$1.jsonStringifyReplacer, 2);
341
341
  }
342
342
  get isEmpty() {
343
343
  return this.issues.length === 0;
@@ -377,19 +377,19 @@ const errorMap = (issue, _ctx) => {
377
377
  }
378
378
  break;
379
379
  case ZodIssueCode.invalid_literal:
380
- message = `Invalid literal value, expected ${JSON.stringify(issue.expected, util.jsonStringifyReplacer)}`;
380
+ message = `Invalid literal value, expected ${JSON.stringify(issue.expected, util$1.jsonStringifyReplacer)}`;
381
381
  break;
382
382
  case ZodIssueCode.unrecognized_keys:
383
- message = `Unrecognized key(s) in object: ${util.joinValues(issue.keys, ", ")}`;
383
+ message = `Unrecognized key(s) in object: ${util$1.joinValues(issue.keys, ", ")}`;
384
384
  break;
385
385
  case ZodIssueCode.invalid_union:
386
386
  message = `Invalid input`;
387
387
  break;
388
388
  case ZodIssueCode.invalid_union_discriminator:
389
- message = `Invalid discriminator value. Expected ${util.joinValues(issue.options)}`;
389
+ message = `Invalid discriminator value. Expected ${util$1.joinValues(issue.options)}`;
390
390
  break;
391
391
  case ZodIssueCode.invalid_enum_value:
392
- message = `Invalid enum value. Expected ${util.joinValues(issue.options)}, received '${issue.received}'`;
392
+ message = `Invalid enum value. Expected ${util$1.joinValues(issue.options)}, received '${issue.received}'`;
393
393
  break;
394
394
  case ZodIssueCode.invalid_arguments:
395
395
  message = `Invalid function arguments`;
@@ -415,7 +415,7 @@ const errorMap = (issue, _ctx) => {
415
415
  message = `Invalid input: must end with "${issue.validation.endsWith}"`;
416
416
  }
417
417
  else {
418
- util.assertNever(issue.validation);
418
+ util$1.assertNever(issue.validation);
419
419
  }
420
420
  }
421
421
  else if (issue.validation !== "regex") {
@@ -485,7 +485,7 @@ const errorMap = (issue, _ctx) => {
485
485
  break;
486
486
  default:
487
487
  message = _ctx.defaultError;
488
- util.assertNever(issue);
488
+ util$1.assertNever(issue);
489
489
  }
490
490
  return { message };
491
491
  };
@@ -1444,7 +1444,7 @@ class ZodString extends ZodType {
1444
1444
  }
1445
1445
  }
1446
1446
  else {
1447
- util.assertNever(check);
1447
+ util$1.assertNever(check);
1448
1448
  }
1449
1449
  }
1450
1450
  return { status: status.value, value: input.data };
@@ -1731,7 +1731,7 @@ class ZodNumber extends ZodType {
1731
1731
  const status = new ParseStatus();
1732
1732
  for (const check of this._def.checks) {
1733
1733
  if (check.kind === "int") {
1734
- if (!util.isInteger(input.data)) {
1734
+ if (!util$1.isInteger(input.data)) {
1735
1735
  ctx = this._getOrReturnCtx(input, ctx);
1736
1736
  addIssueToContext(ctx, {
1737
1737
  code: ZodIssueCode.invalid_type,
@@ -1798,7 +1798,7 @@ class ZodNumber extends ZodType {
1798
1798
  }
1799
1799
  }
1800
1800
  else {
1801
- util.assertNever(check);
1801
+ util$1.assertNever(check);
1802
1802
  }
1803
1803
  }
1804
1804
  return { status: status.value, value: input.data };
@@ -1921,7 +1921,7 @@ class ZodNumber extends ZodType {
1921
1921
  }
1922
1922
  get isInt() {
1923
1923
  return !!this._def.checks.find((ch) => ch.kind === "int" ||
1924
- (ch.kind === "multipleOf" && util.isInteger(ch.value)));
1924
+ (ch.kind === "multipleOf" && util$1.isInteger(ch.value)));
1925
1925
  }
1926
1926
  get isFinite() {
1927
1927
  let max = null, min = null;
@@ -2017,7 +2017,7 @@ class ZodBigInt extends ZodType {
2017
2017
  }
2018
2018
  }
2019
2019
  else {
2020
- util.assertNever(check);
2020
+ util$1.assertNever(check);
2021
2021
  }
2022
2022
  }
2023
2023
  return { status: status.value, value: input.data };
@@ -2211,7 +2211,7 @@ class ZodDate extends ZodType {
2211
2211
  }
2212
2212
  }
2213
2213
  else {
2214
- util.assertNever(check);
2214
+ util$1.assertNever(check);
2215
2215
  }
2216
2216
  }
2217
2217
  return {
@@ -2584,7 +2584,7 @@ class ZodObject extends ZodType {
2584
2584
  if (this._cached !== null)
2585
2585
  return this._cached;
2586
2586
  const shape = this._def.shape();
2587
- const keys = util.objectKeys(shape);
2587
+ const keys = util$1.objectKeys(shape);
2588
2588
  return (this._cached = { shape, keys });
2589
2589
  }
2590
2590
  _parse(input) {
@@ -2826,7 +2826,7 @@ class ZodObject extends ZodType {
2826
2826
  }
2827
2827
  pick(mask) {
2828
2828
  const shape = {};
2829
- util.objectKeys(mask).forEach((key) => {
2829
+ util$1.objectKeys(mask).forEach((key) => {
2830
2830
  if (mask[key] && this.shape[key]) {
2831
2831
  shape[key] = this.shape[key];
2832
2832
  }
@@ -2838,7 +2838,7 @@ class ZodObject extends ZodType {
2838
2838
  }
2839
2839
  omit(mask) {
2840
2840
  const shape = {};
2841
- util.objectKeys(this.shape).forEach((key) => {
2841
+ util$1.objectKeys(this.shape).forEach((key) => {
2842
2842
  if (!mask[key]) {
2843
2843
  shape[key] = this.shape[key];
2844
2844
  }
@@ -2856,7 +2856,7 @@ class ZodObject extends ZodType {
2856
2856
  }
2857
2857
  partial(mask) {
2858
2858
  const newShape = {};
2859
- util.objectKeys(this.shape).forEach((key) => {
2859
+ util$1.objectKeys(this.shape).forEach((key) => {
2860
2860
  const fieldSchema = this.shape[key];
2861
2861
  if (mask && !mask[key]) {
2862
2862
  newShape[key] = fieldSchema;
@@ -2872,7 +2872,7 @@ class ZodObject extends ZodType {
2872
2872
  }
2873
2873
  required(mask) {
2874
2874
  const newShape = {};
2875
- util.objectKeys(this.shape).forEach((key) => {
2875
+ util$1.objectKeys(this.shape).forEach((key) => {
2876
2876
  if (mask && !mask[key]) {
2877
2877
  newShape[key] = this.shape[key];
2878
2878
  }
@@ -2891,7 +2891,7 @@ class ZodObject extends ZodType {
2891
2891
  });
2892
2892
  }
2893
2893
  keyof() {
2894
- return createZodEnum(util.objectKeys(this.shape));
2894
+ return createZodEnum(util$1.objectKeys(this.shape));
2895
2895
  }
2896
2896
  }
2897
2897
  ZodObject.create = (shape, params) => {
@@ -3039,7 +3039,7 @@ const getDiscriminator = (type) => {
3039
3039
  }
3040
3040
  else if (type instanceof ZodNativeEnum) {
3041
3041
  // eslint-disable-next-line ban/ban
3042
- return util.objectValues(type.enum);
3042
+ return util$1.objectValues(type.enum);
3043
3043
  }
3044
3044
  else if (type instanceof ZodDefault) {
3045
3045
  return getDiscriminator(type._def.innerType);
@@ -3155,8 +3155,8 @@ function mergeValues(a, b) {
3155
3155
  return { valid: true, data: a };
3156
3156
  }
3157
3157
  else if (aType === ZodParsedType.object && bType === ZodParsedType.object) {
3158
- const bKeys = util.objectKeys(b);
3159
- const sharedKeys = util
3158
+ const bKeys = util$1.objectKeys(b);
3159
+ const sharedKeys = util$1
3160
3160
  .objectKeys(a)
3161
3161
  .filter((key) => bKeys.indexOf(key) !== -1);
3162
3162
  const newObj = { ...a, ...b };
@@ -3711,7 +3711,7 @@ class ZodEnum extends ZodType {
3711
3711
  const ctx = this._getOrReturnCtx(input);
3712
3712
  const expectedValues = this._def.values;
3713
3713
  addIssueToContext(ctx, {
3714
- expected: util.joinValues(expectedValues),
3714
+ expected: util$1.joinValues(expectedValues),
3715
3715
  received: ctx.parsedType,
3716
3716
  code: ZodIssueCode.invalid_type,
3717
3717
  });
@@ -3777,23 +3777,23 @@ class ZodNativeEnum extends ZodType {
3777
3777
  _ZodNativeEnum_cache.set(this, void 0);
3778
3778
  }
3779
3779
  _parse(input) {
3780
- const nativeEnumValues = util.getValidEnumValues(this._def.values);
3780
+ const nativeEnumValues = util$1.getValidEnumValues(this._def.values);
3781
3781
  const ctx = this._getOrReturnCtx(input);
3782
3782
  if (ctx.parsedType !== ZodParsedType.string &&
3783
3783
  ctx.parsedType !== ZodParsedType.number) {
3784
- const expectedValues = util.objectValues(nativeEnumValues);
3784
+ const expectedValues = util$1.objectValues(nativeEnumValues);
3785
3785
  addIssueToContext(ctx, {
3786
- expected: util.joinValues(expectedValues),
3786
+ expected: util$1.joinValues(expectedValues),
3787
3787
  received: ctx.parsedType,
3788
3788
  code: ZodIssueCode.invalid_type,
3789
3789
  });
3790
3790
  return INVALID;
3791
3791
  }
3792
3792
  if (!__classPrivateFieldGet(this, _ZodNativeEnum_cache)) {
3793
- __classPrivateFieldSet(this, _ZodNativeEnum_cache, new Set(util.getValidEnumValues(this._def.values)));
3793
+ __classPrivateFieldSet(this, _ZodNativeEnum_cache, new Set(util$1.getValidEnumValues(this._def.values)));
3794
3794
  }
3795
3795
  if (!__classPrivateFieldGet(this, _ZodNativeEnum_cache).has(input.data)) {
3796
- const expectedValues = util.objectValues(nativeEnumValues);
3796
+ const expectedValues = util$1.objectValues(nativeEnumValues);
3797
3797
  addIssueToContext(ctx, {
3798
3798
  received: ctx.data,
3799
3799
  code: ZodIssueCode.invalid_enum_value,
@@ -3976,7 +3976,7 @@ class ZodEffects extends ZodType {
3976
3976
  });
3977
3977
  }
3978
3978
  }
3979
- util.assertNever(effect);
3979
+ util$1.assertNever(effect);
3980
3980
  }
3981
3981
  }
3982
3982
  ZodEffects.create = (schema, effect, params) => {
@@ -4400,7 +4400,7 @@ var z = /*#__PURE__*/Object.freeze({
4400
4400
  isDirty: isDirty,
4401
4401
  isValid: isValid,
4402
4402
  isAsync: isAsync,
4403
- get util () { return util; },
4403
+ get util () { return util$1; },
4404
4404
  get objectUtil () { return objectUtil; },
4405
4405
  ZodParsedType: ZodParsedType,
4406
4406
  getParsedType: getParsedType,
@@ -5911,7 +5911,15 @@ class QueryRouter {
5911
5911
  route = this.routes.find((r) => r.id === item);
5912
5912
  }
5913
5913
  else {
5914
- route = this.routes.find((r) => r.path === item.path && r.key === item.key);
5914
+ route = this.routes.find((r) => {
5915
+ if (item.id) {
5916
+ return r.id === item.id;
5917
+ }
5918
+ else {
5919
+ // key 可以是空,所以可以不严格验证
5920
+ return r.path === item.path && r.key == item.key;
5921
+ }
5922
+ });
5915
5923
  }
5916
5924
  if (!route) {
5917
5925
  if (isString) {
@@ -6291,4 +6299,95 @@ const parseSearchValue = (value, opts) => {
6291
6299
  }
6292
6300
  };
6293
6301
 
6294
- export { CustomError, QueryRouter, QueryRouterServer, Route, ZodType as Schema, createSchema, parseBody, parseSearch, parseSearchValue };
6302
+ function define(value) {
6303
+ return value;
6304
+ }
6305
+ class Chain {
6306
+ object;
6307
+ app;
6308
+ constructor(object, opts) {
6309
+ this.object = object;
6310
+ this.app = opts?.app;
6311
+ }
6312
+ get key() {
6313
+ return this.object.key;
6314
+ }
6315
+ get path() {
6316
+ return this.object.path;
6317
+ }
6318
+ setDescription(desc) {
6319
+ this.object.description = desc;
6320
+ return this;
6321
+ }
6322
+ setMeta(metadata) {
6323
+ this.object.metadata = metadata;
6324
+ return this;
6325
+ }
6326
+ setPath(path) {
6327
+ this.object.path = path;
6328
+ return this;
6329
+ }
6330
+ setMiddleware(middleware) {
6331
+ this.object.middleware = middleware;
6332
+ return this;
6333
+ }
6334
+ setKey(key) {
6335
+ this.object.key = key;
6336
+ return this;
6337
+ }
6338
+ setId(key) {
6339
+ this.object.id = key;
6340
+ return this;
6341
+ }
6342
+ setRun(run) {
6343
+ this.object.run = run;
6344
+ return this;
6345
+ }
6346
+ define(run) {
6347
+ this.object.run = run;
6348
+ return this;
6349
+ }
6350
+ createRoute() {
6351
+ this.app.route(this.object).addTo(this.app);
6352
+ return this;
6353
+ }
6354
+ }
6355
+ const util = {
6356
+ getChain: (obj, opts) => {
6357
+ return new Chain(obj, opts);
6358
+ },
6359
+ };
6360
+ class QueryUtil {
6361
+ routeObject;
6362
+ app;
6363
+ constructor(object, opts) {
6364
+ this.routeObject = object;
6365
+ this.app = opts?.app;
6366
+ }
6367
+ static createFormObj(object, opts) {
6368
+ return new QueryUtil(object, opts);
6369
+ }
6370
+ static create(value, opts) {
6371
+ const obj = value;
6372
+ return new QueryUtil(obj, opts);
6373
+ }
6374
+ get(key) {
6375
+ return this.routeObject[key];
6376
+ }
6377
+ chain(key, opts) {
6378
+ const obj = this.routeObject[key];
6379
+ let newOpts = { app: this.app, ...opts };
6380
+ return new Chain(obj, newOpts);
6381
+ }
6382
+ queryChain(key) {
6383
+ const value = this.routeObject[key];
6384
+ return (queryData) => {
6385
+ return {
6386
+ ...value,
6387
+ ...queryData,
6388
+ };
6389
+ };
6390
+ }
6391
+ }
6392
+
6393
+ export { CustomError, QueryRouter, QueryRouterServer, QueryUtil, Route, ZodType as Schema, createSchema, define, parseBody, parseSearch, parseSearchValue, util };