serverstruct 0.2.0 → 0.3.0

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/README.md CHANGED
@@ -2,9 +2,7 @@
2
2
 
3
3
  ⚡️ Typesafe and modular servers with [Hono](https://github.com/honojs/hono).
4
4
 
5
- Serverstruct is a simple tool for building fast, modular and typesafe server applications.
6
-
7
- It provides structure to your Hono application without any limitations. It also supports dependency injection using [Hollywood DI](https://github.com/eriicafes/hollywood-di).
5
+ Serverstruct is a simple tool for building fast, modular and typesafe server applications with Hono and [Hollywood DI](https://github.com/eriicafes/hollywood-di).
8
6
 
9
7
  ## Installation
10
8
 
@@ -25,9 +23,9 @@ npm i serverstruct hono hollywood-di
25
23
  ### A simple app
26
24
 
27
25
  ```ts
28
- import { createRoute } from "serverstruct";
26
+ import { createModule } from "serverstruct";
29
27
 
30
- const app = createRoute()
28
+ const app = createModule()
31
29
  .route((app) => {
32
30
  return app.get("/", (c) => c.text("Hello world!"));
33
31
  })
@@ -36,83 +34,103 @@ const app = createRoute()
36
34
  export default app;
37
35
  ```
38
36
 
39
- ### An app with subroutes
37
+ ### An app with submodules
40
38
 
41
39
  ```ts
42
- import { createRoute } from "serverstruct";
40
+ import { createModule } from "serverstruct";
43
41
 
44
42
  // users routes
45
- const users = createRoute().route((app) => {
43
+ const users = createModule().route((app) => {
46
44
  return app.get("/", (c) => c.text("users"));
47
45
  });
48
46
 
49
47
  // posts routes
50
- const posts = createRoute().route((app) => {
48
+ const posts = createModule().route((app) => {
51
49
  return app.get("/", (c) => c.text("posts"));
52
50
  });
53
51
 
54
- const app = createRoute()
55
- .subroutes({ users, posts })
56
- .route((app, container, routes) => {
52
+ const app = createModule()
53
+ .submodules({ users, posts })
54
+ .route((app, container, modules) => {
57
55
  return app
58
56
  .get("/", (c) => c.text("Hello world!"))
59
- .route("/users", routes.users)
60
- .route("/posts", routes.posts);
57
+ .route("/users", modules.users)
58
+ .route("/posts", modules.posts);
61
59
  })
62
60
  .app();
63
61
 
64
62
  export default app;
65
63
  ```
66
64
 
67
- ## Route
65
+ ## Module
68
66
 
69
- A route returns a new Hono app and may compose other routes with `subroutes<{ ... }>()`. If you intend to utilize dependency injection, a route can require it's dependencies with `use<{ ... }>()` and provide new dependencies with `provide({ ... })`. A route with dependencies can only be added as a subroute to another route if that route satisfies it's dependencies.
67
+ A module is a Hono app that may require dependencies or provide dependencies of it's own. A module may compose other submodules. A module with dependencies can only be used as a submodule to another module if that module satisfies it's dependencies.
70
68
 
71
69
  ```ts
72
- const auth = createRoute().route((app) => {
70
+ import { createModule } from "serverstruct";
71
+
72
+ const auth = createModule().route((app) => {
73
+ return app; // chain route handlers here
74
+ });
75
+
76
+ const users = createModule().route((app) => {
73
77
  return app; // chain route handlers here
74
78
  });
75
79
 
76
- const users = createRoute().route((app) => {
80
+ const app = createModule()
81
+ .submodules({ auth, users })
82
+ .route((app, container, modules) => {
83
+ return app.route("/auth", modules.auth).route("/users", modules.users);
84
+ })
85
+ .app();
86
+ ```
87
+
88
+ Submodules are not automatically added to the Hono app, you will have to manually mount each route. This helps in preserving Hono's type inference through method chaining.
89
+
90
+ You may also pass a custom Hono app to createModule.
91
+
92
+ ```ts
93
+ import { Hono } from "hono";
94
+ import { createModule } from "serverstruct";
95
+
96
+ const auth = createModule(new Hono().basePath("/auth")).route((app) => {
77
97
  return app; // chain route handlers here
78
98
  });
79
99
 
80
- const posts = createRoute().route((app) => {
100
+ const users = createModule(new Hono().basePath("/users")).route((app) => {
81
101
  return app; // chain route handlers here
82
102
  });
83
103
 
84
- const app = createRoute()
85
- .subroutes({ auth, users, posts })
86
- .route((app, container, routes) => {
87
- return app
88
- .route("/auth", routes.auth)
89
- .route("/users", routes.users)
90
- .route("/posts", routes.posts);
104
+ const app = createModule()
105
+ .submodules({ auth, users })
106
+ .route((app, container, modules) => {
107
+ return app.route("", modules.auth).route("", modules.users);
91
108
  })
92
109
  .app();
93
110
  ```
94
111
 
95
- Subroutes are not automatically registered. You will have to manually add the route for the desired path. This also allows for Hono's type inference through method chaining.
96
-
97
112
  ## Dependency Injection
98
113
 
99
- Serverstruct is designed to work with [Hollywood DI](https://github.com/eriicafes/hollywood-di). Routes can define their dependencies using `use`, and also register new tokens using `provide` which creates a child container.
114
+ Serverstruct is designed to work with [Hollywood DI](https://github.com/eriicafes/hollywood-di). Modules can define their dependencies using `use`, and also register new tokens using `provide`.
100
115
 
101
- A root container can also be passed to the route. If no container is explicitly provided the first call to provide creates a root container and futher calls to provide will inherit from it.
116
+ A root container can also be passed to a module. If no container is explicitly provided the first call to provide creates a root container and futher calls to provide will inherit from it.
102
117
 
103
118
  ### Use
104
119
 
105
- Define route dependencies. The route can then only be used in a context that satisfies it's dependencies.
120
+ Define module dependencies. The module can then only be used in a context that satisfies it's dependencies.
106
121
 
107
122
  You can only call `use` once and only before calling `provide`.
108
123
 
109
124
  ```ts
125
+ import { Hollywood } from "hollywood-di";
126
+ import { createModule } from "serverstruct";
127
+
110
128
  interface Counter {
111
129
  count: number;
112
130
  increment(): void;
113
131
  }
114
132
 
115
- const route = createRoute()
133
+ const countModule = createModule()
116
134
  .use<{ counter: Counter }>()
117
135
  .route((app, container) => {
118
136
  return app.get("/", (c) => {
@@ -128,43 +146,40 @@ class LinearCounter {
128
146
  }
129
147
  }
130
148
 
131
- // as a subroute
132
- const app = createRoute()
133
- .provide({ counter: LinearCounter }) // the main app provides the dependency
134
- .subroutes({ countRoute: route })
135
- .route((app, _, routes) => {
136
- return app.route("/count", routes.countRoute);
149
+ // as a submodule
150
+ const app = createModule()
151
+ .provide({ counter: LinearCounter })
152
+ .submodules({ count: countModule })
153
+ .route((app, _, modules) => {
154
+ return app.route("", modules.count);
137
155
  })
138
156
  .app();
139
157
 
140
158
  // or as the main app
141
- const container = Hollywood.create({ counter: LinearCounter }); // the container provides the dependency
142
- const app = route.app(container);
159
+ const container = Hollywood.create({ counter: LinearCounter });
160
+ const app = countModule.app(container);
143
161
  ```
144
162
 
145
- Calling `Route.app()` returns the Hono instance from the route, therefore if the route has dependencies (by calling `use`), a container that satisfies those dependencies must be provided as seen in the example above.
163
+ Calling `Module.app()` returns the Hono app, so if the module has dependencies (by calling `use`), a container that satisfies those dependencies must be provided as seen in the example above.
146
164
 
147
165
  ### Provide
148
166
 
149
- Provide creates a new child container. Registered tokens can then be used in the route and in futher calls to `provide`. See more about register tokens in [Hollywood DI](https://github.com/eriicafes/hollywood-di#tokens).
167
+ Provide creates a new child container. Registered tokens can then be used in the module and in futher calls to `provide`. See more about register tokens in [Hollywood DI](https://github.com/eriicafes/hollywood-di#tokens).
150
168
 
151
169
  ```ts
152
- import { defineInit } from "hollywood-di";
153
- import { createRoute } from "serverstruct";
170
+ import { createModule } from "serverstruct";
154
171
 
155
172
  class Foo {}
156
173
  class Bar {
157
- public static init = defineInit(Bar).args("foo");
158
-
159
- constructor(public foo: Foo) {}
174
+ constructor(public ctx: { foo: Foo }) {}
160
175
  }
161
176
 
162
- const route = createRoute()
177
+ const module = createModule()
163
178
  .provide({
164
179
  foo: Foo,
165
180
  bar: Bar,
166
181
  })
167
- .route((app, container) => {
182
+ .module((app, container) => {
168
183
  return app;
169
184
  });
170
185
  ```
@@ -174,21 +189,23 @@ const route = createRoute()
174
189
  Serverstruct can be added to an existing Hono app.
175
190
 
176
191
  ```ts
177
- // routes/posts.ts
178
- export const postsRoute = createRoute().route((app) => {
192
+ // modules/posts.ts
193
+ import { createModule } from "serverstruct";
194
+
195
+ export const postsModule = createModule().route((app) => {
179
196
  return app.get("/", (c) => {
180
197
  return c.text("posts");
181
198
  });
182
199
  });
183
200
 
201
+ // main.ts
184
202
  import { Hono } from "hono";
185
- import { createRoute } from "serverstruct";
186
- import { postsRoute } from "./routes/posts";
203
+ import { postsModule } from "./modules/posts";
187
204
 
188
205
  const app = new Hono();
189
206
 
190
207
  app.get("/", (c) => c.text("Hello world!"));
191
- app.route("/posts", postsRoute.app());
208
+ app.route("/posts", postsModule.app());
192
209
 
193
210
  export default app;
194
211
  ```
package/dist/index.d.mts CHANGED
@@ -1,7 +1,9 @@
1
- import { AnyHollywood, HollywoodOf, RegisterTokens, InferContainer, ContainerOptions, Hollywood } from 'hollywood-di';
2
- import { Env, Schema, Hono } from 'hono';
3
- import { BlankEnv, BlankSchema } from 'hono/types';
1
+ import { HollywoodOf, AnyHollywood, RegisterTokens, InferContainer, ContainerOptions, Hollywood } from 'hollywood-di';
2
+ import { Hono } from 'hono';
4
3
 
4
+ type Spread<T> = {
5
+ [K in keyof T]: T[K];
6
+ } & {};
5
7
  type Merge<T, To> = T & Omit<To, keyof T>;
6
8
  type IsEmptyObject<T> = keyof T extends never ? true : false;
7
9
  type KnownKey<T> = string extends T ? never : number extends T ? never : symbol extends T ? never : T;
@@ -9,33 +11,36 @@ type KnownMappedKeys<T> = {
9
11
  [K in keyof T as KnownKey<K>]: T[K];
10
12
  } & {};
11
13
 
12
- type Subroute<T extends Record<string, any> = any> = {
13
- app: (container: IsEmptyObject<T> extends true ? HollywoodOf<any> | undefined : HollywoodOf<T>) => Hono<any, any>;
14
+ type SubModule<T extends Record<string, any> = any> = {
15
+ app: (container: IsEmptyObject<T> extends true ? HollywoodOf<any> | undefined : HollywoodOf<T>) => Hono<any, any, any>;
14
16
  };
15
- type Subroutes<TContainer extends Record<string, any> = any> = Record<string, Subroute<TContainer>>;
16
- type InferSubroutes<R extends Subroutes = {}> = {
17
- [K in keyof R]: R[K] extends Route<any, infer App, any> ? App : never;
17
+ type SubModules<TContainer extends Record<string, any> = any> = Record<string, SubModule<TContainer>>;
18
+ type InferSubModules<R extends SubModules = {}> = {
19
+ [K in keyof R]: R[K] extends Module<infer App, any, any> ? App : never;
18
20
  } & {};
19
- declare class Route<Deps extends Record<string, any>, App extends Hono<any, any>, Routes extends Subroutes> {
20
- private route;
21
- private context;
22
- constructor(route: (app: Hono<any, any>, container: any, routes: InferSubroutes<Routes>) => App, context: [
21
+ declare class Module<App extends Hono<any, any, any>, Deps extends Record<string, any>, Modules extends SubModules> {
22
+ private _context;
23
+ private _route;
24
+ constructor(_context: [
23
25
  tokens: {
24
26
  tokens: RegisterTokens<any, any>;
25
27
  options?: ContainerOptions;
26
28
  } | undefined,
27
- routes: Subroutes<any>
28
- ][]);
29
+ routes: SubModules<any>
30
+ ][], _route: (container: Deps, modules: InferSubModules<Modules>) => App);
29
31
  app(container: IsEmptyObject<Deps> extends true ? HollywoodOf<any> | undefined : HollywoodOf<Deps>): App;
30
- app(container?: never): IsEmptyObject<Deps> extends true ? App : never;
32
+ app(): IsEmptyObject<Deps> extends true ? App : never;
31
33
  }
32
- declare class RouteBuilder<Deps extends Record<string, any>, Container extends AnyHollywood | undefined, E extends Env, S extends Schema, Routes extends Subroutes> {
34
+ declare class ModuleBuilder<App extends Hono<any, any, any>, Deps extends Record<string, any>, Modules extends SubModules, Container extends AnyHollywood | undefined> {
35
+ private app;
36
+ constructor(app: App);
33
37
  private context;
34
- use<T extends Container extends AnyHollywood ? never : Record<string, any>>(): Container extends AnyHollywood ? never : RouteBuilder<T, HollywoodOf<T>, E, S, Routes>;
35
- provide<T extends Record<string, any> = {}>(tokens: RegisterTokens<T, Container extends AnyHollywood ? InferContainer<Container> : Deps>, options?: ContainerOptions): RouteBuilder<Deps, Container extends AnyHollywood ? Hollywood<T, InferContainer<Container>> : Hollywood<T, {}>, E, S, Routes>;
36
- subroutes<T extends Subroutes<Container extends AnyHollywood ? KnownMappedKeys<InferContainer<Container>> : Deps>>(routes: T): RouteBuilder<Deps, Container, E, S, Merge<T, Routes> extends infer T_1 ? { [K in keyof T_1]: Merge<T, Routes>[K]; } : never>;
37
- route<App extends Hono<any, any>>(fn: (app: Hono<E, S>, container: Container extends AnyHollywood ? KnownMappedKeys<InferContainer<Container>> : Deps, routes: InferSubroutes<Routes>) => App): Route<Deps, App, Routes>;
38
+ use<T extends Container extends AnyHollywood ? never : Record<string, any>>(): Container extends AnyHollywood ? never : ModuleBuilder<App, T, Modules, HollywoodOf<T>>;
39
+ provide<T extends Record<string, any> = {}>(tokens: RegisterTokens<T, Container extends AnyHollywood ? InferContainer<Container> : Deps>, options?: ContainerOptions): ModuleBuilder<App, Deps, Modules, Container extends AnyHollywood ? Hollywood<T, InferContainer<Container>> : Hollywood<T, {}>>;
40
+ submodules<T extends SubModules<Container extends AnyHollywood ? KnownMappedKeys<InferContainer<Container>> : Deps>>(modules: T): ModuleBuilder<App, Deps, Spread<Merge<T, Modules>>, Container>;
41
+ route<T extends Hono<any, any, any>>(fn: (app: App, container: Container extends AnyHollywood ? KnownMappedKeys<InferContainer<Container>> : Deps, modules: InferSubModules<Modules>) => T): Module<T, Deps, Modules>;
38
42
  }
39
- declare function createRoute<E extends Env = BlankEnv>(): RouteBuilder<{}, undefined, E, BlankSchema, {}>;
43
+ declare function createModule<App extends Hono<any, any, any> = Hono>(app: App): ModuleBuilder<App, {}, {}, undefined>;
44
+ declare function createModule(): ModuleBuilder<Hono, {}, {}, undefined>;
40
45
 
41
- export { createRoute };
46
+ export { createModule };
package/dist/index.d.ts CHANGED
@@ -1,7 +1,9 @@
1
- import { AnyHollywood, HollywoodOf, RegisterTokens, InferContainer, ContainerOptions, Hollywood } from 'hollywood-di';
2
- import { Env, Schema, Hono } from 'hono';
3
- import { BlankEnv, BlankSchema } from 'hono/types';
1
+ import { HollywoodOf, AnyHollywood, RegisterTokens, InferContainer, ContainerOptions, Hollywood } from 'hollywood-di';
2
+ import { Hono } from 'hono';
4
3
 
4
+ type Spread<T> = {
5
+ [K in keyof T]: T[K];
6
+ } & {};
5
7
  type Merge<T, To> = T & Omit<To, keyof T>;
6
8
  type IsEmptyObject<T> = keyof T extends never ? true : false;
7
9
  type KnownKey<T> = string extends T ? never : number extends T ? never : symbol extends T ? never : T;
@@ -9,33 +11,36 @@ type KnownMappedKeys<T> = {
9
11
  [K in keyof T as KnownKey<K>]: T[K];
10
12
  } & {};
11
13
 
12
- type Subroute<T extends Record<string, any> = any> = {
13
- app: (container: IsEmptyObject<T> extends true ? HollywoodOf<any> | undefined : HollywoodOf<T>) => Hono<any, any>;
14
+ type SubModule<T extends Record<string, any> = any> = {
15
+ app: (container: IsEmptyObject<T> extends true ? HollywoodOf<any> | undefined : HollywoodOf<T>) => Hono<any, any, any>;
14
16
  };
15
- type Subroutes<TContainer extends Record<string, any> = any> = Record<string, Subroute<TContainer>>;
16
- type InferSubroutes<R extends Subroutes = {}> = {
17
- [K in keyof R]: R[K] extends Route<any, infer App, any> ? App : never;
17
+ type SubModules<TContainer extends Record<string, any> = any> = Record<string, SubModule<TContainer>>;
18
+ type InferSubModules<R extends SubModules = {}> = {
19
+ [K in keyof R]: R[K] extends Module<infer App, any, any> ? App : never;
18
20
  } & {};
19
- declare class Route<Deps extends Record<string, any>, App extends Hono<any, any>, Routes extends Subroutes> {
20
- private route;
21
- private context;
22
- constructor(route: (app: Hono<any, any>, container: any, routes: InferSubroutes<Routes>) => App, context: [
21
+ declare class Module<App extends Hono<any, any, any>, Deps extends Record<string, any>, Modules extends SubModules> {
22
+ private _context;
23
+ private _route;
24
+ constructor(_context: [
23
25
  tokens: {
24
26
  tokens: RegisterTokens<any, any>;
25
27
  options?: ContainerOptions;
26
28
  } | undefined,
27
- routes: Subroutes<any>
28
- ][]);
29
+ routes: SubModules<any>
30
+ ][], _route: (container: Deps, modules: InferSubModules<Modules>) => App);
29
31
  app(container: IsEmptyObject<Deps> extends true ? HollywoodOf<any> | undefined : HollywoodOf<Deps>): App;
30
- app(container?: never): IsEmptyObject<Deps> extends true ? App : never;
32
+ app(): IsEmptyObject<Deps> extends true ? App : never;
31
33
  }
32
- declare class RouteBuilder<Deps extends Record<string, any>, Container extends AnyHollywood | undefined, E extends Env, S extends Schema, Routes extends Subroutes> {
34
+ declare class ModuleBuilder<App extends Hono<any, any, any>, Deps extends Record<string, any>, Modules extends SubModules, Container extends AnyHollywood | undefined> {
35
+ private app;
36
+ constructor(app: App);
33
37
  private context;
34
- use<T extends Container extends AnyHollywood ? never : Record<string, any>>(): Container extends AnyHollywood ? never : RouteBuilder<T, HollywoodOf<T>, E, S, Routes>;
35
- provide<T extends Record<string, any> = {}>(tokens: RegisterTokens<T, Container extends AnyHollywood ? InferContainer<Container> : Deps>, options?: ContainerOptions): RouteBuilder<Deps, Container extends AnyHollywood ? Hollywood<T, InferContainer<Container>> : Hollywood<T, {}>, E, S, Routes>;
36
- subroutes<T extends Subroutes<Container extends AnyHollywood ? KnownMappedKeys<InferContainer<Container>> : Deps>>(routes: T): RouteBuilder<Deps, Container, E, S, Merge<T, Routes> extends infer T_1 ? { [K in keyof T_1]: Merge<T, Routes>[K]; } : never>;
37
- route<App extends Hono<any, any>>(fn: (app: Hono<E, S>, container: Container extends AnyHollywood ? KnownMappedKeys<InferContainer<Container>> : Deps, routes: InferSubroutes<Routes>) => App): Route<Deps, App, Routes>;
38
+ use<T extends Container extends AnyHollywood ? never : Record<string, any>>(): Container extends AnyHollywood ? never : ModuleBuilder<App, T, Modules, HollywoodOf<T>>;
39
+ provide<T extends Record<string, any> = {}>(tokens: RegisterTokens<T, Container extends AnyHollywood ? InferContainer<Container> : Deps>, options?: ContainerOptions): ModuleBuilder<App, Deps, Modules, Container extends AnyHollywood ? Hollywood<T, InferContainer<Container>> : Hollywood<T, {}>>;
40
+ submodules<T extends SubModules<Container extends AnyHollywood ? KnownMappedKeys<InferContainer<Container>> : Deps>>(modules: T): ModuleBuilder<App, Deps, Spread<Merge<T, Modules>>, Container>;
41
+ route<T extends Hono<any, any, any>>(fn: (app: App, container: Container extends AnyHollywood ? KnownMappedKeys<InferContainer<Container>> : Deps, modules: InferSubModules<Modules>) => T): Module<T, Deps, Modules>;
38
42
  }
39
- declare function createRoute<E extends Env = BlankEnv>(): RouteBuilder<{}, undefined, E, BlankSchema, {}>;
43
+ declare function createModule<App extends Hono<any, any, any> = Hono>(app: App): ModuleBuilder<App, {}, {}, undefined>;
44
+ declare function createModule(): ModuleBuilder<Hono, {}, {}, undefined>;
40
45
 
41
- export { createRoute };
46
+ export { createModule };
package/dist/index.js CHANGED
@@ -18,25 +18,24 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
 
20
20
  // src/index.ts
21
- var src_exports = {};
22
- __export(src_exports, {
23
- createRoute: () => createRoute
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ createModule: () => createModule
24
24
  });
25
- module.exports = __toCommonJS(src_exports);
25
+ module.exports = __toCommonJS(index_exports);
26
26
 
27
- // src/route.ts
27
+ // src/module.ts
28
28
  var import_hollywood_di = require("hollywood-di");
29
29
  var import_hono = require("hono");
30
- var Route = class {
31
- constructor(route, context) {
32
- this.route = route;
33
- this.context = context;
30
+ var Module = class {
31
+ constructor(_context, _route) {
32
+ this._context = _context;
33
+ this._route = _route;
34
34
  }
35
35
  app(container) {
36
- var _a;
37
36
  let currentContainer = container;
38
- const resolvedRoutes = {};
39
- for (const [tokens, subroutes] of this.context) {
37
+ const resolvedModules = {};
38
+ for (const [tokens, submodules] of this._context) {
40
39
  let subContainer = currentContainer;
41
40
  if (tokens && subContainer) {
42
41
  subContainer = import_hollywood_di.Hollywood.createWithParent(
@@ -48,20 +47,20 @@ var Route = class {
48
47
  subContainer = import_hollywood_di.Hollywood.create(tokens.tokens, tokens.options);
49
48
  }
50
49
  currentContainer = subContainer;
51
- for (const [key, subroute] of Object.entries(subroutes)) {
52
- resolvedRoutes[key] = subroute.app(subContainer);
50
+ for (const [key, submodule] of Object.entries(submodules)) {
51
+ resolvedModules[key] = submodule.app(subContainer);
53
52
  }
54
53
  }
55
- const instances = (_a = currentContainer == null ? void 0 : currentContainer.instances) != null ? _a : {};
56
- return this.route(
57
- new import_hono.Hono(),
54
+ const instances = currentContainer?.instances ?? {};
55
+ return this._route(
58
56
  instances,
59
- resolvedRoutes
57
+ resolvedModules
60
58
  );
61
59
  }
62
60
  };
63
- var RouteBuilder = class {
64
- constructor() {
61
+ var ModuleBuilder = class {
62
+ constructor(app) {
63
+ this.app = app;
65
64
  this.context = [];
66
65
  }
67
66
  use() {
@@ -71,23 +70,28 @@ var RouteBuilder = class {
71
70
  this.context.push([{ tokens, options }, {}]);
72
71
  return this;
73
72
  }
74
- subroutes(routes) {
75
- if (!this.context.length)
76
- this.context.push([void 0, routes]);
73
+ submodules(modules) {
74
+ if (!this.context.length) this.context.push([void 0, modules]);
77
75
  else {
78
- const [, subroutes] = this.context[this.context.length - 1];
79
- Object.assign(subroutes, routes);
76
+ const [, submodules] = this.context[this.context.length - 1];
77
+ Object.assign(submodules, modules);
80
78
  }
81
79
  return this;
82
80
  }
83
81
  route(fn) {
84
- return new Route(fn, this.context);
82
+ return new Module(this.context, (container, modules) => {
83
+ return fn(
84
+ this.app,
85
+ container,
86
+ modules
87
+ );
88
+ });
85
89
  }
86
90
  };
87
- function createRoute() {
88
- return new RouteBuilder();
91
+ function createModule(app) {
92
+ return new ModuleBuilder(app ?? new import_hono.Hono());
89
93
  }
90
94
  // Annotate the CommonJS export names for ESM import in node:
91
95
  0 && (module.exports = {
92
- createRoute
96
+ createModule
93
97
  });
package/dist/index.mjs CHANGED
@@ -1,18 +1,17 @@
1
- // src/route.ts
1
+ // src/module.ts
2
2
  import {
3
3
  Hollywood
4
4
  } from "hollywood-di";
5
5
  import { Hono } from "hono";
6
- var Route = class {
7
- constructor(route, context) {
8
- this.route = route;
9
- this.context = context;
6
+ var Module = class {
7
+ constructor(_context, _route) {
8
+ this._context = _context;
9
+ this._route = _route;
10
10
  }
11
11
  app(container) {
12
- var _a;
13
12
  let currentContainer = container;
14
- const resolvedRoutes = {};
15
- for (const [tokens, subroutes] of this.context) {
13
+ const resolvedModules = {};
14
+ for (const [tokens, submodules] of this._context) {
16
15
  let subContainer = currentContainer;
17
16
  if (tokens && subContainer) {
18
17
  subContainer = Hollywood.createWithParent(
@@ -24,20 +23,20 @@ var Route = class {
24
23
  subContainer = Hollywood.create(tokens.tokens, tokens.options);
25
24
  }
26
25
  currentContainer = subContainer;
27
- for (const [key, subroute] of Object.entries(subroutes)) {
28
- resolvedRoutes[key] = subroute.app(subContainer);
26
+ for (const [key, submodule] of Object.entries(submodules)) {
27
+ resolvedModules[key] = submodule.app(subContainer);
29
28
  }
30
29
  }
31
- const instances = (_a = currentContainer == null ? void 0 : currentContainer.instances) != null ? _a : {};
32
- return this.route(
33
- new Hono(),
30
+ const instances = currentContainer?.instances ?? {};
31
+ return this._route(
34
32
  instances,
35
- resolvedRoutes
33
+ resolvedModules
36
34
  );
37
35
  }
38
36
  };
39
- var RouteBuilder = class {
40
- constructor() {
37
+ var ModuleBuilder = class {
38
+ constructor(app) {
39
+ this.app = app;
41
40
  this.context = [];
42
41
  }
43
42
  use() {
@@ -47,22 +46,27 @@ var RouteBuilder = class {
47
46
  this.context.push([{ tokens, options }, {}]);
48
47
  return this;
49
48
  }
50
- subroutes(routes) {
51
- if (!this.context.length)
52
- this.context.push([void 0, routes]);
49
+ submodules(modules) {
50
+ if (!this.context.length) this.context.push([void 0, modules]);
53
51
  else {
54
- const [, subroutes] = this.context[this.context.length - 1];
55
- Object.assign(subroutes, routes);
52
+ const [, submodules] = this.context[this.context.length - 1];
53
+ Object.assign(submodules, modules);
56
54
  }
57
55
  return this;
58
56
  }
59
57
  route(fn) {
60
- return new Route(fn, this.context);
58
+ return new Module(this.context, (container, modules) => {
59
+ return fn(
60
+ this.app,
61
+ container,
62
+ modules
63
+ );
64
+ });
61
65
  }
62
66
  };
63
- function createRoute() {
64
- return new RouteBuilder();
67
+ function createModule(app) {
68
+ return new ModuleBuilder(app ?? new Hono());
65
69
  }
66
70
  export {
67
- createRoute
71
+ createModule
68
72
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "serverstruct",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Type safe and modular servers with Hono",
5
5
  "private": false,
6
6
  "main": "./dist/index.js",
@@ -30,16 +30,16 @@
30
30
  },
31
31
  "homepage": "https://github.com/eriicafes/serverstruct#readme",
32
32
  "devDependencies": {
33
- "@changesets/cli": "^2.26.2",
34
- "@types/node": "^20.4.2",
35
- "@vitest/coverage-v8": "^1.6.0",
33
+ "@changesets/cli": "^2.27.12",
34
+ "@types/node": "^20.17.17",
35
+ "@vitest/coverage-v8": "^3.0.5",
36
36
  "shx": "^0.3.4",
37
- "tsup": "^7.1.0",
38
- "typescript": "^5.1.6",
39
- "vitest": "^1.6.0"
37
+ "tsup": "^8.3.6",
38
+ "typescript": "^5.7.3",
39
+ "vitest": "^3.0.5"
40
40
  },
41
41
  "peerDependencies": {
42
- "hollywood-di": ">= 0.5.2",
42
+ "hollywood-di": ">= 0.6.1",
43
43
  "hono": ">= 4.0.0"
44
44
  },
45
45
  "scripts": {