@kevisual/router 0.0.33 → 0.0.35

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.
@@ -171,7 +171,6 @@ declare class Route<U = {
171
171
  */
172
172
  key?: string;
173
173
  id?: string;
174
- share?: boolean;
175
174
  run?: Run;
176
175
  nextRoute?: NextRoute;
177
176
  description?: string;
@@ -313,6 +312,7 @@ declare class QueryRouter {
313
312
  * 请求 result 的数据
314
313
  * @param message
315
314
  * @param ctx
315
+ * @deprecated use run or call instead
316
316
  * @returns
317
317
  */
318
318
  queryRoute(message: {
@@ -327,6 +327,24 @@ declare class QueryRouter {
327
327
  data: any;
328
328
  message: any;
329
329
  }>;
330
+ /**
331
+ * Router Run获取数据
332
+ * @param message
333
+ * @param ctx
334
+ * @returns
335
+ */
336
+ run(message: {
337
+ id?: string;
338
+ path?: string;
339
+ key?: string;
340
+ payload?: any;
341
+ }, ctx?: RouteContext & {
342
+ [key: string]: any;
343
+ }): Promise<{
344
+ code: any;
345
+ data: any;
346
+ message: any;
347
+ }>;
330
348
  /**
331
349
  * 设置上下文
332
350
  * @description 这里的上下文是为了在handle函数中使用
@@ -418,7 +436,7 @@ declare class QueryRouterServer extends QueryRouter {
418
436
  prompt(description: string): Route<Required<RouteContext>>;
419
437
  prompt(description: Function): Route<Required<RouteContext>>;
420
438
  /**
421
- * 等于queryRoute,但是调用了handle
439
+ * 调用了handle
422
440
  * @param param0
423
441
  * @returns
424
442
  */
@@ -426,9 +444,12 @@ declare class QueryRouterServer extends QueryRouter {
426
444
  path: string;
427
445
  key?: string;
428
446
  payload?: any;
447
+ }, ctx?: RouteContext & {
448
+ [key: string]: any;
429
449
  }): Promise<any>;
430
450
  }
431
- declare const Mini: typeof QueryRouterServer;
451
+ declare class Mini extends QueryRouterServer {
452
+ }
432
453
 
433
454
  /** 自定义错误 */
434
455
  declare class CustomError extends Error {
@@ -97,21 +97,30 @@ try {
97
97
  /** A special constant with type `never` */
98
98
  function $constructor(name, initializer, params) {
99
99
  function init(inst, def) {
100
- var _a;
101
- Object.defineProperty(inst, "_zod", {
102
- value: inst._zod ?? {},
103
- enumerable: false,
104
- });
105
- (_a = inst._zod).traits ?? (_a.traits = new Set());
100
+ if (!inst._zod) {
101
+ Object.defineProperty(inst, "_zod", {
102
+ value: {
103
+ def,
104
+ constr: _,
105
+ traits: new Set(),
106
+ },
107
+ enumerable: false,
108
+ });
109
+ }
110
+ if (inst._zod.traits.has(name)) {
111
+ return;
112
+ }
106
113
  inst._zod.traits.add(name);
107
114
  initializer(inst, def);
108
115
  // support prototype modifications
109
- for (const k in _.prototype) {
110
- if (!(k in inst))
111
- Object.defineProperty(inst, k, { value: _.prototype[k].bind(inst) });
116
+ const proto = _.prototype;
117
+ const keys = Object.keys(proto);
118
+ for (let i = 0; i < keys.length; i++) {
119
+ const k = keys[i];
120
+ if (!(k in inst)) {
121
+ inst[k] = proto[k].bind(inst);
122
+ }
112
123
  }
113
- inst._zod.constr = _;
114
- inst._zod.def = def;
115
124
  }
116
125
  // doesn't work if Parent has a constructor with arguments
117
126
  const Parent = params?.Parent ?? Object;
@@ -246,6 +255,14 @@ function mergeDefs(...defs) {
246
255
  function esc(str) {
247
256
  return JSON.stringify(str);
248
257
  }
258
+ function slugify(input) {
259
+ return input
260
+ .toLowerCase()
261
+ .trim()
262
+ .replace(/[^\w\s-]/g, "")
263
+ .replace(/[\s_-]+/g, "-")
264
+ .replace(/^-+|-+$/g, "");
265
+ }
249
266
  const captureStackTrace = ("captureStackTrace" in Error ? Error.captureStackTrace : (..._args) => { });
250
267
  function isObject$1(data) {
251
268
  return typeof data === "object" && data !== null && !Array.isArray(data);
@@ -271,6 +288,8 @@ function isPlainObject(o) {
271
288
  const ctor = o.constructor;
272
289
  if (ctor === undefined)
273
290
  return true;
291
+ if (typeof ctor !== "function")
292
+ return true;
274
293
  // modified prototype
275
294
  const prot = ctor.prototype;
276
295
  if (isObject$1(prot) === false)
@@ -734,9 +753,6 @@ const cidrv6 = /^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|::|([0-9a-fA-F]{1,4})?:
734
753
  // https://stackoverflow.com/questions/7860392/determine-if-string-is-in-base64-using-javascript
735
754
  const base64 = /^$|^(?:[0-9a-zA-Z+/]{4})*(?:(?:[0-9a-zA-Z+/]{2}==)|(?:[0-9a-zA-Z+/]{3}=))?$/;
736
755
  const base64url = /^[A-Za-z0-9_-]*$/;
737
- // based on https://stackoverflow.com/questions/106179/regular-expression-to-match-dns-hostname-or-ip-address
738
- // export const hostname: RegExp = /^([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+$/;
739
- const hostname = /^(?=.{1,253}\.?$)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[-0-9a-zA-Z]{0,61}[0-9a-zA-Z])?)*\.?$/;
740
756
  // https://blog.stevenlevithan.com/archives/validate-phone-number#r4-3 (regex sans spaces)
741
757
  const e164 = /^\+(?:[0-9]){6,14}[0-9]$/;
742
758
  // const dateSource = `((\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-((0[13578]|1[02])-(0[1-9]|[12]\\d|3[01])|(0[469]|11)-(0[1-9]|[12]\\d|30)|(02)-(0[1-9]|1\\d|2[0-8])))`;
@@ -1227,7 +1243,7 @@ class Doc {
1227
1243
  const version = {
1228
1244
  major: 4,
1229
1245
  minor: 1,
1230
- patch: 12,
1246
+ patch: 13,
1231
1247
  };
1232
1248
 
1233
1249
  const $ZodType = /*@__PURE__*/ $constructor("$ZodType", (inst, def) => {
@@ -1430,7 +1446,7 @@ const $ZodURL = /*@__PURE__*/ $constructor("$ZodURL", (inst, def) => {
1430
1446
  code: "invalid_format",
1431
1447
  format: "url",
1432
1448
  note: "Invalid hostname",
1433
- pattern: hostname.source,
1449
+ pattern: def.hostname.source,
1434
1450
  input: payload.value,
1435
1451
  inst,
1436
1452
  continue: !def.abort,
@@ -1520,18 +1536,12 @@ const $ZodISODuration = /*@__PURE__*/ $constructor("$ZodISODuration", (inst, def
1520
1536
  const $ZodIPv4 = /*@__PURE__*/ $constructor("$ZodIPv4", (inst, def) => {
1521
1537
  def.pattern ?? (def.pattern = ipv4);
1522
1538
  $ZodStringFormat.init(inst, def);
1523
- inst._zod.onattach.push((inst) => {
1524
- const bag = inst._zod.bag;
1525
- bag.format = `ipv4`;
1526
- });
1539
+ inst._zod.bag.format = `ipv4`;
1527
1540
  });
1528
1541
  const $ZodIPv6 = /*@__PURE__*/ $constructor("$ZodIPv6", (inst, def) => {
1529
1542
  def.pattern ?? (def.pattern = ipv6);
1530
1543
  $ZodStringFormat.init(inst, def);
1531
- inst._zod.onattach.push((inst) => {
1532
- const bag = inst._zod.bag;
1533
- bag.format = `ipv6`;
1534
- });
1544
+ inst._zod.bag.format = `ipv6`;
1535
1545
  inst._zod.check = (payload) => {
1536
1546
  try {
1537
1547
  // @ts-ignore
@@ -1601,9 +1611,7 @@ function isValidBase64(data) {
1601
1611
  const $ZodBase64 = /*@__PURE__*/ $constructor("$ZodBase64", (inst, def) => {
1602
1612
  def.pattern ?? (def.pattern = base64);
1603
1613
  $ZodStringFormat.init(inst, def);
1604
- inst._zod.onattach.push((inst) => {
1605
- inst._zod.bag.contentEncoding = "base64";
1606
- });
1614
+ inst._zod.bag.contentEncoding = "base64";
1607
1615
  inst._zod.check = (payload) => {
1608
1616
  if (isValidBase64(payload.value))
1609
1617
  return;
@@ -1627,9 +1635,7 @@ function isValidBase64URL(data) {
1627
1635
  const $ZodBase64URL = /*@__PURE__*/ $constructor("$ZodBase64URL", (inst, def) => {
1628
1636
  def.pattern ?? (def.pattern = base64url);
1629
1637
  $ZodStringFormat.init(inst, def);
1630
- inst._zod.onattach.push((inst) => {
1631
- inst._zod.bag.contentEncoding = "base64url";
1632
- });
1638
+ inst._zod.bag.contentEncoding = "base64url";
1633
1639
  inst._zod.check = (payload) => {
1634
1640
  if (isValidBase64URL(payload.value))
1635
1641
  return;
@@ -1713,9 +1719,9 @@ const $ZodNumber = /*@__PURE__*/ $constructor("$ZodNumber", (inst, def) => {
1713
1719
  return payload;
1714
1720
  };
1715
1721
  });
1716
- const $ZodNumberFormat = /*@__PURE__*/ $constructor("$ZodNumber", (inst, def) => {
1722
+ const $ZodNumberFormat = /*@__PURE__*/ $constructor("$ZodNumberFormat", (inst, def) => {
1717
1723
  $ZodCheckNumberFormat.init(inst, def);
1718
- $ZodNumber.init(inst, def); // no format checksp
1724
+ $ZodNumber.init(inst, def); // no format checks
1719
1725
  });
1720
1726
  const $ZodBoolean = /*@__PURE__*/ $constructor("$ZodBoolean", (inst, def) => {
1721
1727
  $ZodType.init(inst, def);
@@ -1833,7 +1839,7 @@ function handleCatchall(proms, input, payload, ctx, def, inst) {
1833
1839
  const keySet = def.keySet;
1834
1840
  const _catchall = def.catchall._zod;
1835
1841
  const t = _catchall.def.type;
1836
- for (const key of Object.keys(input)) {
1842
+ for (const key in input) {
1837
1843
  if (keySet.has(key))
1838
1844
  continue;
1839
1845
  if (t === "never") {
@@ -2387,8 +2393,8 @@ const $ZodReadonly = /*@__PURE__*/ $constructor("$ZodReadonly", (inst, def) => {
2387
2393
  $ZodType.init(inst, def);
2388
2394
  defineLazy(inst._zod, "propValues", () => def.innerType._zod.propValues);
2389
2395
  defineLazy(inst._zod, "values", () => def.innerType._zod.values);
2390
- defineLazy(inst._zod, "optin", () => def.innerType._zod.optin);
2391
- defineLazy(inst._zod, "optout", () => def.innerType._zod.optout);
2396
+ defineLazy(inst._zod, "optin", () => def.innerType?._zod?.optin);
2397
+ defineLazy(inst._zod, "optout", () => def.innerType?._zod?.optout);
2392
2398
  inst._zod.parse = (payload, ctx) => {
2393
2399
  if (ctx.direction === "backward") {
2394
2400
  return def.innerType._zod.run(payload, ctx);
@@ -2436,6 +2442,7 @@ function handleRefineResult(result, payload, input, inst) {
2436
2442
  }
2437
2443
  }
2438
2444
 
2445
+ var _a;
2439
2446
  class $ZodRegistry {
2440
2447
  constructor() {
2441
2448
  this._map = new WeakMap();
@@ -2485,7 +2492,8 @@ class $ZodRegistry {
2485
2492
  function registry() {
2486
2493
  return new $ZodRegistry();
2487
2494
  }
2488
- const globalRegistry = /*@__PURE__*/ registry();
2495
+ (_a = globalThis).__zod_globalRegistry ?? (_a.__zod_globalRegistry = registry());
2496
+ const globalRegistry = globalThis.__zod_globalRegistry;
2489
2497
 
2490
2498
  function _string(Class, params) {
2491
2499
  return new Class({
@@ -2897,6 +2905,10 @@ function _toLowerCase() {
2897
2905
  function _toUpperCase() {
2898
2906
  return _overwrite((input) => input.toUpperCase());
2899
2907
  }
2908
+ // slugify
2909
+ function _slugify() {
2910
+ return _overwrite((input) => slugify(input));
2911
+ }
2900
2912
  function _array(Class, element, params) {
2901
2913
  return new Class({
2902
2914
  type: "array",
@@ -3140,6 +3152,7 @@ const _ZodString = /*@__PURE__*/ $constructor("_ZodString", (inst, def) => {
3140
3152
  inst.normalize = (...args) => inst.check(_normalize(...args));
3141
3153
  inst.toLowerCase = () => inst.check(_toLowerCase());
3142
3154
  inst.toUpperCase = () => inst.check(_toUpperCase());
3155
+ inst.slugify = () => inst.check(_slugify());
3143
3156
  });
3144
3157
  const ZodString = /*@__PURE__*/ $constructor("ZodString", (inst, def) => {
3145
3158
  $ZodString.init(inst, def);
@@ -4799,7 +4812,6 @@ class Route {
4799
4812
  */
4800
4813
  key;
4801
4814
  id;
4802
- share = false;
4803
4815
  run;
4804
4816
  nextRoute; // route to run after this route
4805
4817
  description;
@@ -5276,9 +5288,7 @@ class QueryRouter {
5276
5288
  ctx.query = { ...ctx.query, ...query, ...payload };
5277
5289
  ctx.state = { ...ctx?.state };
5278
5290
  ctx.throw = this.throw;
5279
- // put queryRouter to ctx
5280
- // TODO: 是否需要queryRouter,函数内部处理router路由执行,这应该是避免去内部去包含的功能过
5281
- ctx.queryRouter = this;
5291
+ ctx.app = this;
5282
5292
  ctx.call = this.call.bind(this);
5283
5293
  ctx.queryRoute = this.queryRoute.bind(this);
5284
5294
  ctx.index = 0;
@@ -5299,7 +5309,11 @@ class QueryRouter {
5299
5309
  async call(message, ctx) {
5300
5310
  let path = message.path;
5301
5311
  let key = message.key;
5302
- if (message.id) {
5312
+ // 优先 path + key
5313
+ if (path) {
5314
+ return await this.parse({ ...message, path, key }, { ...this.context, ...ctx });
5315
+ }
5316
+ else if (message.id) {
5303
5317
  const route = this.routes.find((r) => r.id === message.id);
5304
5318
  if (route) {
5305
5319
  path = route.path;
@@ -5310,9 +5324,6 @@ class QueryRouter {
5310
5324
  }
5311
5325
  return await this.parse({ ...message, path, key }, { ...this.context, ...ctx });
5312
5326
  }
5313
- else if (path) {
5314
- return await this.parse({ ...message, path, key }, { ...this.context, ...ctx });
5315
- }
5316
5327
  else {
5317
5328
  return { code: 404, body: null, message: 'Not found path' };
5318
5329
  }
@@ -5321,10 +5332,25 @@ class QueryRouter {
5321
5332
  * 请求 result 的数据
5322
5333
  * @param message
5323
5334
  * @param ctx
5335
+ * @deprecated use run or call instead
5324
5336
  * @returns
5325
5337
  */
5326
5338
  async queryRoute(message, ctx) {
5327
- const res = await this.parse(message, { ...this.context, ...ctx });
5339
+ const res = await this.call(message, { ...this.context, ...ctx });
5340
+ return {
5341
+ code: res.code,
5342
+ data: res.body,
5343
+ message: res.message,
5344
+ };
5345
+ }
5346
+ /**
5347
+ * Router Run获取数据
5348
+ * @param message
5349
+ * @param ctx
5350
+ * @returns
5351
+ */
5352
+ async run(message, ctx) {
5353
+ const res = await this.call(message, { ...this.context, ...ctx });
5328
5354
  return {
5329
5355
  code: res.code,
5330
5356
  data: res.body,
@@ -5351,18 +5377,7 @@ class QueryRouter {
5351
5377
  return async (msg, handleContext) => {
5352
5378
  try {
5353
5379
  const context = { ...ctx, ...handleContext };
5354
- if (msg.id) {
5355
- const route = router.routes.find((r) => r.id === msg.id);
5356
- if (route) {
5357
- msg.path = route.path;
5358
- msg.key = route.key;
5359
- }
5360
- else {
5361
- return { code: 404, message: 'Not found route' };
5362
- }
5363
- }
5364
- // @ts-ignore
5365
- const res = await router.parse(msg, context);
5380
+ const res = await router.call(msg, context);
5366
5381
  if (wrapperFn) {
5367
5382
  res.data = res.body;
5368
5383
  return wrapperFn(res, context);
@@ -5474,37 +5489,21 @@ class QueryRouterServer extends QueryRouter {
5474
5489
  return new Route('', '', { description });
5475
5490
  }
5476
5491
  /**
5477
- * 等于queryRoute,但是调用了handle
5492
+ * 调用了handle
5478
5493
  * @param param0
5479
5494
  * @returns
5480
5495
  */
5481
- async run({ path, key, payload }) {
5496
+ async run({ path, key, payload }, ctx) {
5482
5497
  const handle = this.handle;
5483
- const resultError = (error, code = 500) => {
5484
- const r = {
5485
- code: code,
5486
- message: error,
5487
- };
5488
- return r;
5489
- };
5490
- try {
5491
- const end = handle({ path, key, ...payload });
5492
- return end;
5493
- }
5494
- catch (e) {
5495
- if (e.code && typeof e.code === 'number') {
5496
- return {
5497
- code: e.code,
5498
- message: e.message,
5499
- };
5500
- }
5501
- else {
5502
- return resultError('Router Server error');
5503
- }
5498
+ if (handle) {
5499
+ const result = await this.call({ path, key, payload }, ctx);
5500
+ return handle(result);
5504
5501
  }
5502
+ return super.run({ path, key, payload }, ctx);
5505
5503
  }
5506
5504
  }
5507
- const Mini = QueryRouterServer;
5505
+ class Mini extends QueryRouterServer {
5506
+ }
5508
5507
 
5509
5508
  const parseBody = async (req) => {
5510
5509
  return new Promise((resolve, reject) => {
@@ -8,8 +8,8 @@ type AltNames = {
8
8
  ip?: string;
9
9
  };
10
10
  declare const createCert: (attrs?: Attributes[], altNames?: AltNames[]) => {
11
- key: string;
12
- cert: string;
11
+ key: any;
12
+ cert: any;
13
13
  };
14
14
 
15
15
  export { createCert };