@kevisual/router 0.0.39 → 0.0.41

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.
@@ -1,4 +1,4 @@
1
- import * as querystring from 'querystring';
1
+ import * as node_querystring from 'node:querystring';
2
2
  import { Key } from 'path-to-regexp';
3
3
  import { IncomingMessage, ServerResponse, Server } from 'node:http';
4
4
  import { ListenOptions } from 'node:net';
@@ -25,7 +25,7 @@ declare class SimpleRouter {
25
25
  exclude?: string[];
26
26
  });
27
27
  getBody(req: Req): Promise<Record<string, any>>;
28
- getSearch(req: Req): querystring.ParsedUrlQuery;
28
+ getSearch(req: Req): node_querystring.ParsedUrlQuery;
29
29
  parseSearchValue: (value?: string, opts?: {
30
30
  decode?: boolean;
31
31
  }) => any;
package/dist/router.d.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import http, { IncomingMessage, ServerResponse } from 'node:http';
2
2
  import https from 'node:https';
3
3
  import http2 from 'node:http2';
4
- import * as cookie from 'cookie';
5
4
  import { z } from 'zod';
6
5
  import { WebSocketServer, WebSocket } from 'ws';
7
6
  import { IncomingMessage as IncomingMessage$1, ServerResponse as ServerResponse$1 } from 'http';
@@ -86,16 +85,14 @@ type RouteMiddleware = {
86
85
  key?: string;
87
86
  id?: string;
88
87
  } | string;
89
- type RouteOpts<T = {}> = {
88
+ type RouteOpts<U = {}, T = SimpleObject$1> = {
90
89
  path?: string;
91
90
  key?: string;
92
91
  id?: string;
93
- run?: Run<T>;
92
+ run?: Run<U>;
94
93
  nextRoute?: NextRoute;
95
94
  description?: string;
96
- metadata?: {
97
- [key: string]: any;
98
- };
95
+ metadata?: T;
99
96
  middleware?: RouteMiddleware[];
100
97
  type?: 'route' | 'middleware';
101
98
  /**
@@ -113,7 +110,7 @@ declare const pickValue: readonly ["path", "key", "id", "description", "type", "
113
110
  type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
114
111
  declare class Route<U = {
115
112
  [key: string]: any;
116
- }> {
113
+ }, T extends SimpleObject$1 = SimpleObject$1> {
117
114
  /**
118
115
  * 一级路径
119
116
  */
@@ -126,9 +123,7 @@ declare class Route<U = {
126
123
  run?: Run;
127
124
  nextRoute?: NextRoute;
128
125
  description?: string;
129
- metadata?: {
130
- [key: string]: any;
131
- };
126
+ metadata?: T;
132
127
  middleware?: RouteMiddleware[];
133
128
  type?: string;
134
129
  data?: any;
@@ -256,7 +251,7 @@ declare class QueryRouter {
256
251
  * @param ctx
257
252
  */
258
253
  setContext(ctx: RouteContext): void;
259
- getList(): RouteInfo[];
254
+ getList(filter?: (route: Route) => boolean): RouteInfo[];
260
255
  /**
261
256
  * 获取handle函数, 这里会去执行parse函数
262
257
  */
@@ -281,13 +276,13 @@ declare class QueryRouter {
281
276
  }>;
282
277
  exportRoutes(): Route<{
283
278
  [key: string]: any;
284
- }>[];
279
+ }, SimpleObject$1>[];
285
280
  importRoutes(routes: Route[]): void;
286
281
  importRouter(router: QueryRouter): void;
287
282
  throw(code?: number | string, message?: string, tips?: string): void;
288
283
  hasRoute(path: string, key?: string): Route<{
289
284
  [key: string]: any;
290
- }>;
285
+ }, SimpleObject$1>;
291
286
  createRouteList(force?: boolean): void;
292
287
  /**
293
288
  * 等待程序运行, 获取到message的数据,就执行
@@ -387,8 +382,103 @@ declare class QueryConnect {
387
382
  }[];
388
383
  }
389
384
 
385
+ interface StringifyOptions {
386
+ /**
387
+ * Specifies a function that will be used to encode a [cookie-value](https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.1).
388
+ * Since value of a cookie has a limited character set (and must be a simple string), this function can be used to encode
389
+ * a value into a string suited for a cookie's value, and should mirror `decode` when parsing.
390
+ *
391
+ * @default encodeURIComponent
392
+ */
393
+ encode?: (str: string) => string;
394
+ }
395
+ /**
396
+ * Set-Cookie object.
397
+ */
398
+ interface SetCookie {
399
+ /**
400
+ * Specifies the name of the cookie.
401
+ */
402
+ name: string;
403
+ /**
404
+ * Specifies the string to be the value for the cookie.
405
+ */
406
+ value: string | undefined;
407
+ /**
408
+ * Specifies the `number` (in seconds) to be the value for the [`Max-Age` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.2).
409
+ *
410
+ * The [cookie storage model specification](https://tools.ietf.org/html/rfc6265#section-5.3) states that if both `expires` and
411
+ * `maxAge` are set, then `maxAge` takes precedence, but it is possible not all clients by obey this,
412
+ * so if both are set, they should point to the same date and time.
413
+ */
414
+ maxAge?: number;
415
+ /**
416
+ * Specifies the `Date` object to be the value for the [`Expires` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.1).
417
+ * When no expiration is set, clients consider this a "non-persistent cookie" and delete it when the current session is over.
418
+ *
419
+ * The [cookie storage model specification](https://tools.ietf.org/html/rfc6265#section-5.3) states that if both `expires` and
420
+ * `maxAge` are set, then `maxAge` takes precedence, but it is possible not all clients by obey this,
421
+ * so if both are set, they should point to the same date and time.
422
+ */
423
+ expires?: Date;
424
+ /**
425
+ * Specifies the value for the [`Domain` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.3).
426
+ * When no domain is set, clients consider the cookie to apply to the current domain only.
427
+ */
428
+ domain?: string;
429
+ /**
430
+ * Specifies the value for the [`Path` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.4).
431
+ * When no path is set, the path is considered the ["default path"](https://tools.ietf.org/html/rfc6265#section-5.1.4).
432
+ */
433
+ path?: string;
434
+ /**
435
+ * Enables the [`HttpOnly` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.6).
436
+ * When enabled, clients will not allow client-side JavaScript to see the cookie in `document.cookie`.
437
+ */
438
+ httpOnly?: boolean;
439
+ /**
440
+ * Enables the [`Secure` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.5).
441
+ * When enabled, clients will only send the cookie back if the browser has an HTTPS connection.
442
+ */
443
+ secure?: boolean;
444
+ /**
445
+ * Enables the [`Partitioned` `Set-Cookie` attribute](https://tools.ietf.org/html/draft-cutler-httpbis-partitioned-cookies/).
446
+ * When enabled, clients will only send the cookie back when the current domain _and_ top-level domain matches.
447
+ *
448
+ * This is an attribute that has not yet been fully standardized, and may change in the future.
449
+ * This also means clients may ignore this attribute until they understand it. More information
450
+ * about can be found in [the proposal](https://github.com/privacycg/CHIPS).
451
+ */
452
+ partitioned?: boolean;
453
+ /**
454
+ * Specifies the value for the [`Priority` `Set-Cookie` attribute](https://tools.ietf.org/html/draft-west-cookie-priority-00#section-4.1).
455
+ *
456
+ * - `'low'` will set the `Priority` attribute to `Low`.
457
+ * - `'medium'` will set the `Priority` attribute to `Medium`, the default priority when not set.
458
+ * - `'high'` will set the `Priority` attribute to `High`.
459
+ *
460
+ * More information about priority levels can be found in [the specification](https://tools.ietf.org/html/draft-west-cookie-priority-00#section-4.1).
461
+ */
462
+ priority?: "low" | "medium" | "high";
463
+ /**
464
+ * Specifies the value for the [`SameSite` `Set-Cookie` attribute](https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-09#section-5.4.7).
465
+ *
466
+ * - `true` will set the `SameSite` attribute to `Strict` for strict same site enforcement.
467
+ * - `'lax'` will set the `SameSite` attribute to `Lax` for lax same site enforcement.
468
+ * - `'none'` will set the `SameSite` attribute to `None` for an explicit cross-site cookie.
469
+ * - `'strict'` will set the `SameSite` attribute to `Strict` for strict same site enforcement.
470
+ *
471
+ * More information about enforcement levels can be found in [the specification](https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-09#section-5.4.7).
472
+ */
473
+ sameSite?: boolean | "lax" | "strict" | "none";
474
+ }
475
+ /**
476
+ * Backward compatibility serialize options.
477
+ */
478
+ type SerializeOptions = StringifyOptions & Omit<SetCookie, "name" | "value">;
479
+
390
480
  type Listener = (...args: any[]) => void;
391
- type CookieFn = (name: string, value: string, options?: cookie.SerializeOptions, end?: boolean) => void;
481
+ type CookieFn = (name: string, value: string, options?: SerializeOptions, end?: boolean) => void;
392
482
  type HandleCtx = {
393
483
  req: IncomingMessage & {
394
484
  cookies: Record<string, string>;
@@ -441,7 +531,7 @@ declare class Server {
441
531
  listen(path: string, listeningListener?: () => void): void;
442
532
  listen(handle: any, backlog?: number, listeningListener?: () => void): void;
443
533
  listen(handle: any, listeningListener?: () => void): void;
444
- createServer(): http.Server<typeof IncomingMessage, typeof ServerResponse> | https.Server<typeof IncomingMessage, typeof ServerResponse> | http2.Http2SecureServer<typeof IncomingMessage, typeof ServerResponse, typeof http2.Http2ServerRequest, typeof http2.Http2ServerResponse>;
534
+ createServer(): http.Server<typeof IncomingMessage, typeof ServerResponse> | http2.Http2SecureServer<typeof IncomingMessage, typeof ServerResponse, typeof http2.Http2ServerRequest, typeof http2.Http2ServerResponse>;
445
535
  setHandle(handle?: any): void;
446
536
  /**
447
537
  * get callback
@@ -495,7 +585,7 @@ declare class CustomError extends Error {
495
585
  * @param err
496
586
  * @returns
497
587
  */
498
- static isError(err: any): boolean;
588
+ static isError(error: unknown): error is CustomError;
499
589
  parse(e?: CustomError): {
500
590
  code: number;
501
591
  data: any;
@@ -724,7 +814,7 @@ declare class App<U = {}> {
724
814
  }>;
725
815
  exportRoutes(): Route<{
726
816
  [key: string]: any;
727
- }>[];
817
+ }, SimpleObject$1>[];
728
818
  importRoutes(routes: any[]): void;
729
819
  importApp(app: App): void;
730
820
  throw(code?: number | string, message?: string, tips?: string): void;