@rhythmjs/router 0.0.3 → 0.0.4

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,5 +1,5 @@
1
1
  import { t as RhythmHttpContext } from "./context-B0711qPV.js";
2
- import { Middleware } from "@rhythmjs/rhythm/types";
2
+ import { DeriveMiddleware, Middleware } from "@rhythmjs/rhythm/types";
3
3
  //#region src/rhythm-router.d.ts
4
4
  export interface RhythmRouterContext {
5
5
  readonly params: Readonly<Record<string, string>>;
@@ -7,16 +7,22 @@ export interface RhythmRouterContext {
7
7
  export interface RhythmRouterOptions {
8
8
  prefix?: string;
9
9
  }
10
+ type RouteHandler<TContext> = Middleware<TContext & RhythmRouterContext>;
10
11
  export declare class RhythmRouter<TContext extends RhythmHttpContext = RhythmHttpContext> {
11
12
  #private;
12
13
  constructor(options?: RhythmRouterOptions);
13
- use(child: RhythmRouter<any>): this;
14
- use<TExtra extends object = {}>(fn: Middleware<TContext>): RhythmRouter<TContext & TExtra>;
15
- get<TExtra extends object = {}>(path: string, ...handlers: Middleware<TContext & RhythmRouterContext & TExtra>[]): this;
16
- post<TExtra extends object = {}>(path: string, ...handlers: Middleware<TContext & RhythmRouterContext & TExtra>[]): this;
17
- put<TExtra extends object = {}>(path: string, ...handlers: Middleware<TContext & RhythmRouterContext & TExtra>[]): this;
18
- patch<TExtra extends object = {}>(path: string, ...handlers: Middleware<TContext & RhythmRouterContext & TExtra>[]): this;
19
- delete<TExtra extends object = {}>(path: string, ...handlers: Middleware<TContext & RhythmRouterContext & TExtra>[]): this;
20
- routes(): Middleware<TContext>;
14
+ use<TExtra extends object>(fn: DeriveMiddleware<TContext, TExtra>): RhythmRouter<TContext & TExtra>;
15
+ use(fn: Middleware<TContext>): this;
16
+ get<TExtra extends object>(path: string, middleware: DeriveMiddleware<TContext & RhythmRouterContext, TExtra>, ...handlers: RouteHandler<TContext & TExtra>[]): this;
17
+ get(path: string, ...handlers: RouteHandler<TContext>[]): this;
18
+ post<TExtra extends object>(path: string, middleware: DeriveMiddleware<TContext & RhythmRouterContext, TExtra>, ...handlers: RouteHandler<TContext & TExtra>[]): this;
19
+ post(path: string, ...handlers: RouteHandler<TContext>[]): this;
20
+ put<TExtra extends object>(path: string, middleware: DeriveMiddleware<TContext & RhythmRouterContext, TExtra>, ...handlers: RouteHandler<TContext & TExtra>[]): this;
21
+ put(path: string, ...handlers: RouteHandler<TContext>[]): this;
22
+ patch<TExtra extends object>(path: string, middleware: DeriveMiddleware<TContext & RhythmRouterContext, TExtra>, ...handlers: RouteHandler<TContext & TExtra>[]): this;
23
+ patch(path: string, ...handlers: RouteHandler<TContext>[]): this;
24
+ delete<TExtra extends object>(path: string, middleware: DeriveMiddleware<TContext & RhythmRouterContext, TExtra>, ...handlers: RouteHandler<TContext & TExtra>[]): this;
25
+ delete(path: string, ...handlers: RouteHandler<TContext>[]): this;
26
+ middleware(): Middleware<TContext>;
21
27
  }
22
28
  //#endregion
@@ -1,29 +1,21 @@
1
1
  import { createNode, insertRoute, joinPath, lookupRoute } from "./radix-tree.js";
2
2
  import { compose } from "@rhythmjs/rhythm/compose";
3
3
  //#region src/rhythm-router.ts
4
- var RhythmRouter = class RhythmRouter {
4
+ var RhythmRouter = class {
5
5
  #options;
6
6
  #entries = [];
7
- #composed = null;
8
7
  constructor(options = {}) {
9
8
  this.#options = options;
10
9
  }
11
10
  get #prefix() {
12
11
  return this.#options.prefix ?? "";
13
12
  }
14
- use(arg) {
15
- if (arg instanceof RhythmRouter) for (const entry of arg.#entries) this.#entries.push(entry.kind === "route" ? {
16
- ...entry,
17
- path: joinPath(this.#prefix, entry.path)
18
- } : entry);
19
- else {
20
- if (typeof arg !== "function") throw new TypeError("middleware must be a function!");
21
- this.#entries.push({
22
- kind: "middleware",
23
- fn: arg
24
- });
25
- }
26
- this.#composed = null;
13
+ use(fn) {
14
+ if (typeof fn !== "function") throw new TypeError("middleware must be a function!");
15
+ this.#entries.push({
16
+ kind: "middleware",
17
+ fn
18
+ });
27
19
  return this;
28
20
  }
29
21
  #route(method, path, handlers) {
@@ -33,7 +25,6 @@ var RhythmRouter = class RhythmRouter {
33
25
  path: joinPath(this.#prefix, path),
34
26
  handlers
35
27
  });
36
- this.#composed = null;
37
28
  return this;
38
29
  }
39
30
  get(path, ...handlers) {
@@ -52,7 +43,6 @@ var RhythmRouter = class RhythmRouter {
52
43
  return this.#route("DELETE", path, handlers);
53
44
  }
54
45
  #compile() {
55
- if (this.#composed) return this.#composed;
56
46
  const dispatchFor = (tree) => {
57
47
  return async (ctx, next) => {
58
48
  const match = lookupRoute(tree, ctx.request.method, new URL(ctx.request.url).pathname);
@@ -84,12 +74,12 @@ var RhythmRouter = class RhythmRouter {
84
74
  }
85
75
  stack.push(dispatchFor(tree));
86
76
  }
87
- this.#composed = compose(stack);
88
- return this.#composed;
77
+ return compose(stack);
89
78
  }
90
- routes() {
79
+ middleware() {
80
+ const fn = this.#compile();
91
81
  return async (ctx, next) => {
92
- await this.#compile()(ctx, next);
82
+ await fn(ctx, next);
93
83
  };
94
84
  }
95
85
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rhythmjs/router",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "description": "Radix-tree HTTP router for the Rhythm middleware kernel, with Bun, Node, and Deno adapters.",
5
5
  "homepage": "https://rhythm.js.org/router",
6
6
  "license": "ISC",
@@ -49,7 +49,7 @@
49
49
  "access": "public"
50
50
  },
51
51
  "dependencies": {
52
- "@rhythmjs/rhythm": "0.0.3"
52
+ "@rhythmjs/rhythm": "0.0.4"
53
53
  },
54
54
  "devDependencies": {
55
55
  "@types/node": "^26.6.3",