@kevisual/router 0.0.16 → 0.0.18

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.js CHANGED
@@ -116,7 +116,7 @@ try {
116
116
  }
117
117
  */
118
118
 
119
- var util;
119
+ var util$1;
120
120
  (function (util) {
121
121
  util.assertEqual = (val) => val;
122
122
  function assertIs(_arg) { }
@@ -178,7 +178,7 @@ var util;
178
178
  }
179
179
  return value;
180
180
  };
181
- })(util || (util = {}));
181
+ })(util$1 || (util$1 = {}));
182
182
  var objectUtil;
183
183
  (function (objectUtil) {
184
184
  objectUtil.mergeShapes = (first, second) => {
@@ -188,7 +188,7 @@ var objectUtil;
188
188
  };
189
189
  };
190
190
  })(objectUtil || (objectUtil = {}));
191
- const ZodParsedType = util.arrayToEnum([
191
+ const ZodParsedType = util$1.arrayToEnum([
192
192
  "string",
193
193
  "nan",
194
194
  "number",
@@ -255,7 +255,7 @@ const getParsedType = (data) => {
255
255
  }
256
256
  };
257
257
 
258
- const ZodIssueCode = util.arrayToEnum([
258
+ const ZodIssueCode = util$1.arrayToEnum([
259
259
  "invalid_type",
260
260
  "invalid_literal",
261
261
  "custom",
@@ -359,7 +359,7 @@ class ZodError extends Error {
359
359
  return this.message;
360
360
  }
361
361
  get message() {
362
- return JSON.stringify(this.issues, util.jsonStringifyReplacer, 2);
362
+ return JSON.stringify(this.issues, util$1.jsonStringifyReplacer, 2);
363
363
  }
364
364
  get isEmpty() {
365
365
  return this.issues.length === 0;
@@ -399,19 +399,19 @@ const errorMap = (issue, _ctx) => {
399
399
  }
400
400
  break;
401
401
  case ZodIssueCode.invalid_literal:
402
- message = `Invalid literal value, expected ${JSON.stringify(issue.expected, util.jsonStringifyReplacer)}`;
402
+ message = `Invalid literal value, expected ${JSON.stringify(issue.expected, util$1.jsonStringifyReplacer)}`;
403
403
  break;
404
404
  case ZodIssueCode.unrecognized_keys:
405
- message = `Unrecognized key(s) in object: ${util.joinValues(issue.keys, ", ")}`;
405
+ message = `Unrecognized key(s) in object: ${util$1.joinValues(issue.keys, ", ")}`;
406
406
  break;
407
407
  case ZodIssueCode.invalid_union:
408
408
  message = `Invalid input`;
409
409
  break;
410
410
  case ZodIssueCode.invalid_union_discriminator:
411
- message = `Invalid discriminator value. Expected ${util.joinValues(issue.options)}`;
411
+ message = `Invalid discriminator value. Expected ${util$1.joinValues(issue.options)}`;
412
412
  break;
413
413
  case ZodIssueCode.invalid_enum_value:
414
- message = `Invalid enum value. Expected ${util.joinValues(issue.options)}, received '${issue.received}'`;
414
+ message = `Invalid enum value. Expected ${util$1.joinValues(issue.options)}, received '${issue.received}'`;
415
415
  break;
416
416
  case ZodIssueCode.invalid_arguments:
417
417
  message = `Invalid function arguments`;
@@ -437,7 +437,7 @@ const errorMap = (issue, _ctx) => {
437
437
  message = `Invalid input: must end with "${issue.validation.endsWith}"`;
438
438
  }
439
439
  else {
440
- util.assertNever(issue.validation);
440
+ util$1.assertNever(issue.validation);
441
441
  }
442
442
  }
443
443
  else if (issue.validation !== "regex") {
@@ -507,7 +507,7 @@ const errorMap = (issue, _ctx) => {
507
507
  break;
508
508
  default:
509
509
  message = _ctx.defaultError;
510
- util.assertNever(issue);
510
+ util$1.assertNever(issue);
511
511
  }
512
512
  return { message };
513
513
  };
@@ -1466,7 +1466,7 @@ class ZodString extends ZodType {
1466
1466
  }
1467
1467
  }
1468
1468
  else {
1469
- util.assertNever(check);
1469
+ util$1.assertNever(check);
1470
1470
  }
1471
1471
  }
1472
1472
  return { status: status.value, value: input.data };
@@ -1753,7 +1753,7 @@ class ZodNumber extends ZodType {
1753
1753
  const status = new ParseStatus();
1754
1754
  for (const check of this._def.checks) {
1755
1755
  if (check.kind === "int") {
1756
- if (!util.isInteger(input.data)) {
1756
+ if (!util$1.isInteger(input.data)) {
1757
1757
  ctx = this._getOrReturnCtx(input, ctx);
1758
1758
  addIssueToContext(ctx, {
1759
1759
  code: ZodIssueCode.invalid_type,
@@ -1820,7 +1820,7 @@ class ZodNumber extends ZodType {
1820
1820
  }
1821
1821
  }
1822
1822
  else {
1823
- util.assertNever(check);
1823
+ util$1.assertNever(check);
1824
1824
  }
1825
1825
  }
1826
1826
  return { status: status.value, value: input.data };
@@ -1943,7 +1943,7 @@ class ZodNumber extends ZodType {
1943
1943
  }
1944
1944
  get isInt() {
1945
1945
  return !!this._def.checks.find((ch) => ch.kind === "int" ||
1946
- (ch.kind === "multipleOf" && util.isInteger(ch.value)));
1946
+ (ch.kind === "multipleOf" && util$1.isInteger(ch.value)));
1947
1947
  }
1948
1948
  get isFinite() {
1949
1949
  let max = null, min = null;
@@ -2039,7 +2039,7 @@ class ZodBigInt extends ZodType {
2039
2039
  }
2040
2040
  }
2041
2041
  else {
2042
- util.assertNever(check);
2042
+ util$1.assertNever(check);
2043
2043
  }
2044
2044
  }
2045
2045
  return { status: status.value, value: input.data };
@@ -2233,7 +2233,7 @@ class ZodDate extends ZodType {
2233
2233
  }
2234
2234
  }
2235
2235
  else {
2236
- util.assertNever(check);
2236
+ util$1.assertNever(check);
2237
2237
  }
2238
2238
  }
2239
2239
  return {
@@ -2606,7 +2606,7 @@ class ZodObject extends ZodType {
2606
2606
  if (this._cached !== null)
2607
2607
  return this._cached;
2608
2608
  const shape = this._def.shape();
2609
- const keys = util.objectKeys(shape);
2609
+ const keys = util$1.objectKeys(shape);
2610
2610
  return (this._cached = { shape, keys });
2611
2611
  }
2612
2612
  _parse(input) {
@@ -2848,7 +2848,7 @@ class ZodObject extends ZodType {
2848
2848
  }
2849
2849
  pick(mask) {
2850
2850
  const shape = {};
2851
- util.objectKeys(mask).forEach((key) => {
2851
+ util$1.objectKeys(mask).forEach((key) => {
2852
2852
  if (mask[key] && this.shape[key]) {
2853
2853
  shape[key] = this.shape[key];
2854
2854
  }
@@ -2860,7 +2860,7 @@ class ZodObject extends ZodType {
2860
2860
  }
2861
2861
  omit(mask) {
2862
2862
  const shape = {};
2863
- util.objectKeys(this.shape).forEach((key) => {
2863
+ util$1.objectKeys(this.shape).forEach((key) => {
2864
2864
  if (!mask[key]) {
2865
2865
  shape[key] = this.shape[key];
2866
2866
  }
@@ -2878,7 +2878,7 @@ class ZodObject extends ZodType {
2878
2878
  }
2879
2879
  partial(mask) {
2880
2880
  const newShape = {};
2881
- util.objectKeys(this.shape).forEach((key) => {
2881
+ util$1.objectKeys(this.shape).forEach((key) => {
2882
2882
  const fieldSchema = this.shape[key];
2883
2883
  if (mask && !mask[key]) {
2884
2884
  newShape[key] = fieldSchema;
@@ -2894,7 +2894,7 @@ class ZodObject extends ZodType {
2894
2894
  }
2895
2895
  required(mask) {
2896
2896
  const newShape = {};
2897
- util.objectKeys(this.shape).forEach((key) => {
2897
+ util$1.objectKeys(this.shape).forEach((key) => {
2898
2898
  if (mask && !mask[key]) {
2899
2899
  newShape[key] = this.shape[key];
2900
2900
  }
@@ -2913,7 +2913,7 @@ class ZodObject extends ZodType {
2913
2913
  });
2914
2914
  }
2915
2915
  keyof() {
2916
- return createZodEnum(util.objectKeys(this.shape));
2916
+ return createZodEnum(util$1.objectKeys(this.shape));
2917
2917
  }
2918
2918
  }
2919
2919
  ZodObject.create = (shape, params) => {
@@ -3061,7 +3061,7 @@ const getDiscriminator = (type) => {
3061
3061
  }
3062
3062
  else if (type instanceof ZodNativeEnum) {
3063
3063
  // eslint-disable-next-line ban/ban
3064
- return util.objectValues(type.enum);
3064
+ return util$1.objectValues(type.enum);
3065
3065
  }
3066
3066
  else if (type instanceof ZodDefault) {
3067
3067
  return getDiscriminator(type._def.innerType);
@@ -3177,8 +3177,8 @@ function mergeValues(a, b) {
3177
3177
  return { valid: true, data: a };
3178
3178
  }
3179
3179
  else if (aType === ZodParsedType.object && bType === ZodParsedType.object) {
3180
- const bKeys = util.objectKeys(b);
3181
- const sharedKeys = util
3180
+ const bKeys = util$1.objectKeys(b);
3181
+ const sharedKeys = util$1
3182
3182
  .objectKeys(a)
3183
3183
  .filter((key) => bKeys.indexOf(key) !== -1);
3184
3184
  const newObj = { ...a, ...b };
@@ -3733,7 +3733,7 @@ class ZodEnum extends ZodType {
3733
3733
  const ctx = this._getOrReturnCtx(input);
3734
3734
  const expectedValues = this._def.values;
3735
3735
  addIssueToContext(ctx, {
3736
- expected: util.joinValues(expectedValues),
3736
+ expected: util$1.joinValues(expectedValues),
3737
3737
  received: ctx.parsedType,
3738
3738
  code: ZodIssueCode.invalid_type,
3739
3739
  });
@@ -3799,23 +3799,23 @@ class ZodNativeEnum extends ZodType {
3799
3799
  _ZodNativeEnum_cache.set(this, void 0);
3800
3800
  }
3801
3801
  _parse(input) {
3802
- const nativeEnumValues = util.getValidEnumValues(this._def.values);
3802
+ const nativeEnumValues = util$1.getValidEnumValues(this._def.values);
3803
3803
  const ctx = this._getOrReturnCtx(input);
3804
3804
  if (ctx.parsedType !== ZodParsedType.string &&
3805
3805
  ctx.parsedType !== ZodParsedType.number) {
3806
- const expectedValues = util.objectValues(nativeEnumValues);
3806
+ const expectedValues = util$1.objectValues(nativeEnumValues);
3807
3807
  addIssueToContext(ctx, {
3808
- expected: util.joinValues(expectedValues),
3808
+ expected: util$1.joinValues(expectedValues),
3809
3809
  received: ctx.parsedType,
3810
3810
  code: ZodIssueCode.invalid_type,
3811
3811
  });
3812
3812
  return INVALID;
3813
3813
  }
3814
3814
  if (!__classPrivateFieldGet(this, _ZodNativeEnum_cache)) {
3815
- __classPrivateFieldSet(this, _ZodNativeEnum_cache, new Set(util.getValidEnumValues(this._def.values)));
3815
+ __classPrivateFieldSet(this, _ZodNativeEnum_cache, new Set(util$1.getValidEnumValues(this._def.values)));
3816
3816
  }
3817
3817
  if (!__classPrivateFieldGet(this, _ZodNativeEnum_cache).has(input.data)) {
3818
- const expectedValues = util.objectValues(nativeEnumValues);
3818
+ const expectedValues = util$1.objectValues(nativeEnumValues);
3819
3819
  addIssueToContext(ctx, {
3820
3820
  received: ctx.data,
3821
3821
  code: ZodIssueCode.invalid_enum_value,
@@ -3998,7 +3998,7 @@ class ZodEffects extends ZodType {
3998
3998
  });
3999
3999
  }
4000
4000
  }
4001
- util.assertNever(effect);
4001
+ util$1.assertNever(effect);
4002
4002
  }
4003
4003
  }
4004
4004
  ZodEffects.create = (schema, effect, params) => {
@@ -4422,7 +4422,7 @@ var z = /*#__PURE__*/Object.freeze({
4422
4422
  isDirty: isDirty,
4423
4423
  isValid: isValid,
4424
4424
  isAsync: isAsync,
4425
- get util () { return util; },
4425
+ get util () { return util$1; },
4426
4426
  get objectUtil () { return objectUtil; },
4427
4427
  ZodParsedType: ZodParsedType,
4428
4428
  getParsedType: getParsedType,
@@ -11938,6 +11938,97 @@ class WsServer extends WsServerBase {
11938
11938
  }
11939
11939
  }
11940
11940
 
11941
+ function define(value) {
11942
+ return value;
11943
+ }
11944
+ class Chain {
11945
+ object;
11946
+ app;
11947
+ constructor(object, opts) {
11948
+ this.object = object;
11949
+ this.app = opts?.app;
11950
+ }
11951
+ get key() {
11952
+ return this.object.key;
11953
+ }
11954
+ get path() {
11955
+ return this.object.path;
11956
+ }
11957
+ setDescription(desc) {
11958
+ this.object.description = desc;
11959
+ return this;
11960
+ }
11961
+ setMeta(metadata) {
11962
+ this.object.metadata = metadata;
11963
+ return this;
11964
+ }
11965
+ setPath(path) {
11966
+ this.object.path = path;
11967
+ return this;
11968
+ }
11969
+ setMiddleware(middleware) {
11970
+ this.object.middleware = middleware;
11971
+ return this;
11972
+ }
11973
+ setKey(key) {
11974
+ this.object.key = key;
11975
+ return this;
11976
+ }
11977
+ setId(key) {
11978
+ this.object.id = key;
11979
+ return this;
11980
+ }
11981
+ setRun(run) {
11982
+ this.object.run = run;
11983
+ return this;
11984
+ }
11985
+ define(run) {
11986
+ this.object.run = run;
11987
+ return this;
11988
+ }
11989
+ createRoute() {
11990
+ this.app.route(this.object).addTo(this.app);
11991
+ return this;
11992
+ }
11993
+ }
11994
+ const util = {
11995
+ getChain: (obj, opts) => {
11996
+ return new Chain(obj, opts);
11997
+ },
11998
+ };
11999
+ class QueryUtil {
12000
+ routeObject;
12001
+ app;
12002
+ constructor(object, opts) {
12003
+ this.routeObject = object;
12004
+ this.app = opts?.app;
12005
+ }
12006
+ static createFormObj(object, opts) {
12007
+ return new QueryUtil(object, opts);
12008
+ }
12009
+ static create(value, opts) {
12010
+ const obj = value;
12011
+ return new QueryUtil(obj, opts);
12012
+ }
12013
+ get(key) {
12014
+ return this.routeObject[key];
12015
+ }
12016
+ chain(key, opts) {
12017
+ const obj = this.routeObject[key];
12018
+ let newOpts = { app: this.app, ...opts };
12019
+ return new Chain(obj, newOpts);
12020
+ }
12021
+ queryChain(key) {
12022
+ const value = this.routeObject[key];
12023
+ return (queryData) => {
12024
+ return {
12025
+ ...value,
12026
+ ...queryData,
12027
+ };
12028
+ };
12029
+ }
12030
+ }
12031
+
11941
12032
  /**
11942
12033
  * 封装了 Router 和 Server 的 App 模块,处理http的请求和响应,内置了 Cookie 和 Token 和 res 的处理
11943
12034
  */
@@ -12010,4 +12101,4 @@ class App {
12010
12101
  }
12011
12102
  }
12012
12103
 
12013
- export { App, Connect, CustomError, QueryConnect, QueryRouter, QueryRouterServer, Route, ZodType as Schema, Server, createSchema, handleServer };
12104
+ export { App, Connect, CustomError, QueryConnect, QueryRouter, QueryRouterServer, QueryUtil, Route, ZodType as Schema, Server, createSchema, define, handleServer, util };
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.16",
4
+ "version": "0.0.18",
5
5
  "description": "",
6
6
  "type": "module",
7
7
  "main": "./dist/router.js",
package/src/browser.ts CHANGED
@@ -9,3 +9,5 @@ export type { Run } from './route.ts';
9
9
  export { CustomError } from './result/error.ts';
10
10
 
11
11
  export * from './server/parse-body.ts';
12
+
13
+ export * from './router-define.ts';
package/src/index.ts CHANGED
@@ -14,3 +14,5 @@ export { CustomError } from './result/error.ts';
14
14
  export { Rule, Schema, createSchema } from './validator/index.ts';
15
15
 
16
16
  export { App } from './app.ts';
17
+
18
+ export * from './router-define.ts';
package/src/route.ts CHANGED
@@ -57,8 +57,8 @@ export type RouteContext<T = { code?: number }, S = any> = {
57
57
  /** 是否需要序列化 */
58
58
  needSerialize?: boolean;
59
59
  } & T;
60
-
61
- export type Run<T = any> = (ctx: RouteContext<T>) => Promise<typeof ctx | null | void>;
60
+ export type SimpleObject = Record<string, any>;
61
+ export type Run<T extends SimpleObject = {}> = (ctx: RouteContext<T>) => Promise<typeof ctx | null | void>;
62
62
 
63
63
  export type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
64
64
  export type RouteMiddleware =
@@ -1,5 +1,4 @@
1
- // import type { QueryRouterServer, Route, RouteOpts, Run } from '@kevisual/router';
2
- import type { QueryRouterServer, RouteOpts, Run, RouteMiddleware } from './index.ts';
1
+ import type { QueryRouterServer, RouteOpts, Run, RouteMiddleware } from '@kevisual/router';
3
2
 
4
3
  // export type RouteObject<T extends readonly string[]> = {
5
4
  // [K in T[number]]: RouteOpts;
@@ -8,7 +7,7 @@ export type { RouteOpts };
8
7
  export type RouteObject = {
9
8
  [key: string]: RouteOpts;
10
9
  };
11
-
10
+ type SimpleObject = Record<string, any>;
12
11
  export function define<T extends Record<string, RouteOpts>>(
13
12
  value: T,
14
13
  ): {
@@ -58,11 +57,11 @@ class Chain {
58
57
  this.object.id = key;
59
58
  return this;
60
59
  }
61
- setRun(run: Run) {
60
+ setRun<U extends SimpleObject = {}>(run: Run<U>) {
62
61
  this.object.run = run;
63
62
  return this;
64
63
  }
65
- define(run: Run) {
64
+ define<U extends SimpleObject = {}>(run: Run<U>) {
66
65
  this.object.run = run;
67
66
  return this;
68
67
  }
@@ -99,8 +98,8 @@ export class QueryUtil<T extends RouteObject = RouteObject> {
99
98
  let newOpts = { app: this.app, ...opts };
100
99
  return new Chain(obj, newOpts);
101
100
  }
102
- queryChain<K extends keyof T>(key: K) {
103
- const value = this.routeObject[key] as RouteOpts;
101
+ queryChain<K extends keyof T>(key: K): (queryData?: Record<string, any>) => RouteOpts {
102
+ const value = this.routeObject[key];
104
103
  return (queryData?: Record<string, any>) => {
105
104
  return {
106
105
  ...value,
@@ -8,3 +8,7 @@ const v = QueryUtil.create({
8
8
  });
9
9
  const app = new App();
10
10
  app.route(v.get('a'));
11
+
12
+ v.chain('a').define<{ f: () => {} }>(async (ctx) => {
13
+ // ctx.f = 'sdf';
14
+ });