serverstruct 0.1.1 → 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
@@ -1,3 +1,211 @@
1
- # Commandstruct
1
+ # Serverstruct
2
2
 
3
- Type safe and modular servers with [Hono](https://github.com/honojs/hono).
3
+ ⚡️ Typesafe and modular servers with [Hono](https://github.com/honojs/hono).
4
+
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).
6
+
7
+ ## Installation
8
+
9
+ Serverstruct requires you to install hono.
10
+
11
+ ```sh
12
+ npm i serverstruct hono
13
+ ```
14
+
15
+ To make use of dependency injection provided by serverstruct, also install hollywood-di.
16
+
17
+ ```sh
18
+ npm i serverstruct hono hollywood-di
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ ### A simple app
24
+
25
+ ```ts
26
+ import { createModule } from "serverstruct";
27
+
28
+ const app = createModule()
29
+ .route((app) => {
30
+ return app.get("/", (c) => c.text("Hello world!"));
31
+ })
32
+ .app();
33
+
34
+ export default app;
35
+ ```
36
+
37
+ ### An app with submodules
38
+
39
+ ```ts
40
+ import { createModule } from "serverstruct";
41
+
42
+ // users routes
43
+ const users = createModule().route((app) => {
44
+ return app.get("/", (c) => c.text("users"));
45
+ });
46
+
47
+ // posts routes
48
+ const posts = createModule().route((app) => {
49
+ return app.get("/", (c) => c.text("posts"));
50
+ });
51
+
52
+ const app = createModule()
53
+ .submodules({ users, posts })
54
+ .route((app, container, modules) => {
55
+ return app
56
+ .get("/", (c) => c.text("Hello world!"))
57
+ .route("/users", modules.users)
58
+ .route("/posts", modules.posts);
59
+ })
60
+ .app();
61
+
62
+ export default app;
63
+ ```
64
+
65
+ ## Module
66
+
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.
68
+
69
+ ```ts
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) => {
77
+ return app; // chain route handlers here
78
+ });
79
+
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) => {
97
+ return app; // chain route handlers here
98
+ });
99
+
100
+ const users = createModule(new Hono().basePath("/users")).route((app) => {
101
+ return app; // chain route handlers here
102
+ });
103
+
104
+ const app = createModule()
105
+ .submodules({ auth, users })
106
+ .route((app, container, modules) => {
107
+ return app.route("", modules.auth).route("", modules.users);
108
+ })
109
+ .app();
110
+ ```
111
+
112
+ ## Dependency Injection
113
+
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`.
115
+
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.
117
+
118
+ ### Use
119
+
120
+ Define module dependencies. The module can then only be used in a context that satisfies it's dependencies.
121
+
122
+ You can only call `use` once and only before calling `provide`.
123
+
124
+ ```ts
125
+ import { Hollywood } from "hollywood-di";
126
+ import { createModule } from "serverstruct";
127
+
128
+ interface Counter {
129
+ count: number;
130
+ increment(): void;
131
+ }
132
+
133
+ const countModule = createModule()
134
+ .use<{ counter: Counter }>()
135
+ .route((app, container) => {
136
+ return app.get("/", (c) => {
137
+ container.counter.increment();
138
+ return c.text(`Count is: ${container.counter.count}`);
139
+ });
140
+ });
141
+
142
+ class LinearCounter {
143
+ public count = 0;
144
+ public increment() {
145
+ this.count++;
146
+ }
147
+ }
148
+
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);
155
+ })
156
+ .app();
157
+
158
+ // or as the main app
159
+ const container = Hollywood.create({ counter: LinearCounter });
160
+ const app = countModule.app(container);
161
+ ```
162
+
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.
164
+
165
+ ### Provide
166
+
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).
168
+
169
+ ```ts
170
+ import { createModule } from "serverstruct";
171
+
172
+ class Foo {}
173
+ class Bar {
174
+ constructor(public ctx: { foo: Foo }) {}
175
+ }
176
+
177
+ const module = createModule()
178
+ .provide({
179
+ foo: Foo,
180
+ bar: Bar,
181
+ })
182
+ .module((app, container) => {
183
+ return app;
184
+ });
185
+ ```
186
+
187
+ ## Incremental Adoption
188
+
189
+ Serverstruct can be added to an existing Hono app.
190
+
191
+ ```ts
192
+ // modules/posts.ts
193
+ import { createModule } from "serverstruct";
194
+
195
+ export const postsModule = createModule().route((app) => {
196
+ return app.get("/", (c) => {
197
+ return c.text("posts");
198
+ });
199
+ });
200
+
201
+ // main.ts
202
+ import { Hono } from "hono";
203
+ import { postsModule } from "./modules/posts";
204
+
205
+ const app = new Hono();
206
+
207
+ app.get("/", (c) => c.text("Hello world!"));
208
+ app.route("/posts", postsModule.app());
209
+
210
+ export default app;
211
+ ```
package/dist/index.d.mts CHANGED
@@ -1,57 +1,46 @@
1
- import { InitFactory, InstantiableConstructor, RegisterTokens, inferContainer, Hollywood, AnyHollywood } from 'hollywood-di';
2
- import { Env, Hono } from 'hono';
1
+ import { HollywoodOf, AnyHollywood, RegisterTokens, InferContainer, ContainerOptions, Hollywood } from 'hollywood-di';
2
+ import { Hono } from 'hono';
3
3
 
4
- type IsAny<T> = unknown extends T & string ? true : false;
5
- type WithoutTrailingSlash<T extends string> = T extends `${infer P}/` ? P : T;
6
- type WithoutLeadingSlash<T extends string> = T extends `/${infer P}` ? P : T;
7
- type WithMinimumSlash<T extends string> = T extends "" ? "/" : T;
8
- type MergePath<P extends string, T extends string> = IsAny<T> extends true ? any : WithMinimumSlash<WithoutTrailingSlash<`${WithoutTrailingSlash<P>}/${WithoutLeadingSlash<T>}`>>;
4
+ type Spread<T> = {
5
+ [K in keyof T]: T[K];
6
+ } & {};
7
+ type Merge<T, To> = T & Omit<To, keyof T>;
8
+ type IsEmptyObject<T> = keyof T extends never ? true : false;
9
+ type KnownKey<T> = string extends T ? never : number extends T ? never : symbol extends T ? never : T;
10
+ type KnownMappedKeys<T> = {
11
+ [K in keyof T as KnownKey<K>]: T[K];
12
+ } & {};
9
13
 
10
- type Router<BasePath extends string = "/", Path extends string = "/", E extends Env = Env, S = {}> = Hono<E, S, MergePath<BasePath, Path>>;
11
- declare class ControllerDef<BasePath extends string = "/", Path extends string = "/", E extends Env = Env, S = {}, Out = S> {
12
- path: Path;
13
- route: (router: Router<BasePath, Path, E, S>) => Router<BasePath, Path, E, Out>;
14
- constructor(path: Path, route: (router: Router<BasePath, Path, E, S>) => Router<BasePath, Path, E, Out>);
15
- mountTo(parent: Hono<any, any, BasePath>): void;
16
- }
17
- type Controller<BasePath extends string, Path extends string, T extends Record<string, any> = {}, E extends Env = Env, S = {}, Out = S> = InitFactory<T, ControllerDef<BasePath, Path, E, S, Out>>;
18
- type inferController<T extends Controller<any, any, any, any, any>> = T extends Controller<infer BasePath, infer Path, any, infer E, any, infer Out> ? Router<BasePath, Path, E, Out> : never;
19
- type ControllerInit<T> = <U extends Record<string, any>>(constructor: InstantiableConstructor<T, U>) => U;
20
- type ControllerContext<TContainer, TRouter> = {
21
- router: TRouter;
22
- container: TContainer;
23
- /**
24
- * Initialize an instantiable constructor with the controller's container.
25
- *
26
- * Also automatically binds class methods to their instance using `auto-bind`.
27
- */
28
- init: ControllerInit<TContainer>;
29
- };
30
- type ControllerBuilder<BasePath extends string = "/", Path extends string = "/", T extends Record<string, any> = {}, E extends Env = Env, S = {}> = {
31
- build<Out>(config: (ctx: ControllerContext<T, Router<BasePath, Path, E, S>>) => Router<BasePath, Path, E, Out>): Controller<BasePath, Path, T, E, S, Out>;
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>;
32
16
  };
33
- declare function createController<BasePath extends string = "/", Path extends string = "/", T extends Record<string, any> = {}, E extends Env = Env, S = {}>(path: Path): ControllerBuilder<BasePath, Path, T, E, S>;
34
-
35
- interface Module<BasePath extends string, Path extends string, P extends Record<string, any>> {
36
- path: Path;
37
- tokens: RegisterTokens<any, P>;
38
- controllers: Controller<MergePath<BasePath, Path>, any, P, any, any, any>[];
39
- submodules?: Module<MergePath<BasePath, Path>, any, P>[];
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;
20
+ } & {};
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: [
25
+ tokens: {
26
+ tokens: RegisterTokens<any, any>;
27
+ options?: ContainerOptions;
28
+ } | undefined,
29
+ routes: SubModules<any>
30
+ ][], _route: (container: Deps, modules: InferSubModules<Modules>) => App);
31
+ app(container: IsEmptyObject<Deps> extends true ? HollywoodOf<any> | undefined : HollywoodOf<Deps>): App;
32
+ app(): IsEmptyObject<Deps> extends true ? App : never;
40
33
  }
41
- interface ModuleConfig<BasePath extends string, Path extends string, T extends Record<string, any>, P extends Record<string, any>> {
42
- tokens: RegisterTokens<T, P>;
43
- controllers: Controller<MergePath<BasePath, Path>, any, inferContainer<Hollywood<T, Hollywood<P, any>>>, any, any, any>[];
44
- submodules?: Module<MergePath<BasePath, Path>, any, inferContainer<Hollywood<T, Hollywood<P, any>>>>[];
45
- }
46
- type ModuleBuilder<BasePath extends string = "/", Path extends string = "/", P extends Record<string, any> = {}> = {
47
- build<T extends Record<string, any> = {}>(config: ModuleConfig<BasePath, Path, T, P>): Module<BasePath, Path, P>;
48
- };
49
- declare function createModule<BasePath extends string = "/", Path extends string = "/", P extends Record<string, any> = {}>(path: Path): ModuleBuilder<BasePath, Path, P>;
50
-
51
- interface Serverstruct<Container extends AnyHollywood, BasePath extends string> {
52
- modules(...modules: Module<BasePath, any, inferContainer<Container>>[]): this;
53
- controllers(...controllers: Controller<BasePath, any, inferContainer<Container>, any, any, any>[]): this;
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);
37
+ private context;
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>;
54
42
  }
55
- declare function serverstruct<App extends Hono<any, any, BasePath>, Container extends AnyHollywood, BasePath extends string = "/">(app: App, container: Container, _base?: BasePath): Serverstruct<Container, BasePath>;
43
+ declare function createModule<App extends Hono<any, any, any> = Hono>(app: App): ModuleBuilder<App, {}, {}, undefined>;
44
+ declare function createModule(): ModuleBuilder<Hono, {}, {}, undefined>;
56
45
 
57
- export { Controller, ControllerContext, Module, ModuleConfig, Router, Serverstruct, createController, createModule, inferController, serverstruct };
46
+ export { createModule };
package/dist/index.d.ts CHANGED
@@ -1,57 +1,46 @@
1
- import { InitFactory, InstantiableConstructor, RegisterTokens, inferContainer, Hollywood, AnyHollywood } from 'hollywood-di';
2
- import { Env, Hono } from 'hono';
1
+ import { HollywoodOf, AnyHollywood, RegisterTokens, InferContainer, ContainerOptions, Hollywood } from 'hollywood-di';
2
+ import { Hono } from 'hono';
3
3
 
4
- type IsAny<T> = unknown extends T & string ? true : false;
5
- type WithoutTrailingSlash<T extends string> = T extends `${infer P}/` ? P : T;
6
- type WithoutLeadingSlash<T extends string> = T extends `/${infer P}` ? P : T;
7
- type WithMinimumSlash<T extends string> = T extends "" ? "/" : T;
8
- type MergePath<P extends string, T extends string> = IsAny<T> extends true ? any : WithMinimumSlash<WithoutTrailingSlash<`${WithoutTrailingSlash<P>}/${WithoutLeadingSlash<T>}`>>;
4
+ type Spread<T> = {
5
+ [K in keyof T]: T[K];
6
+ } & {};
7
+ type Merge<T, To> = T & Omit<To, keyof T>;
8
+ type IsEmptyObject<T> = keyof T extends never ? true : false;
9
+ type KnownKey<T> = string extends T ? never : number extends T ? never : symbol extends T ? never : T;
10
+ type KnownMappedKeys<T> = {
11
+ [K in keyof T as KnownKey<K>]: T[K];
12
+ } & {};
9
13
 
10
- type Router<BasePath extends string = "/", Path extends string = "/", E extends Env = Env, S = {}> = Hono<E, S, MergePath<BasePath, Path>>;
11
- declare class ControllerDef<BasePath extends string = "/", Path extends string = "/", E extends Env = Env, S = {}, Out = S> {
12
- path: Path;
13
- route: (router: Router<BasePath, Path, E, S>) => Router<BasePath, Path, E, Out>;
14
- constructor(path: Path, route: (router: Router<BasePath, Path, E, S>) => Router<BasePath, Path, E, Out>);
15
- mountTo(parent: Hono<any, any, BasePath>): void;
16
- }
17
- type Controller<BasePath extends string, Path extends string, T extends Record<string, any> = {}, E extends Env = Env, S = {}, Out = S> = InitFactory<T, ControllerDef<BasePath, Path, E, S, Out>>;
18
- type inferController<T extends Controller<any, any, any, any, any>> = T extends Controller<infer BasePath, infer Path, any, infer E, any, infer Out> ? Router<BasePath, Path, E, Out> : never;
19
- type ControllerInit<T> = <U extends Record<string, any>>(constructor: InstantiableConstructor<T, U>) => U;
20
- type ControllerContext<TContainer, TRouter> = {
21
- router: TRouter;
22
- container: TContainer;
23
- /**
24
- * Initialize an instantiable constructor with the controller's container.
25
- *
26
- * Also automatically binds class methods to their instance using `auto-bind`.
27
- */
28
- init: ControllerInit<TContainer>;
29
- };
30
- type ControllerBuilder<BasePath extends string = "/", Path extends string = "/", T extends Record<string, any> = {}, E extends Env = Env, S = {}> = {
31
- build<Out>(config: (ctx: ControllerContext<T, Router<BasePath, Path, E, S>>) => Router<BasePath, Path, E, Out>): Controller<BasePath, Path, T, E, S, Out>;
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>;
32
16
  };
33
- declare function createController<BasePath extends string = "/", Path extends string = "/", T extends Record<string, any> = {}, E extends Env = Env, S = {}>(path: Path): ControllerBuilder<BasePath, Path, T, E, S>;
34
-
35
- interface Module<BasePath extends string, Path extends string, P extends Record<string, any>> {
36
- path: Path;
37
- tokens: RegisterTokens<any, P>;
38
- controllers: Controller<MergePath<BasePath, Path>, any, P, any, any, any>[];
39
- submodules?: Module<MergePath<BasePath, Path>, any, P>[];
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;
20
+ } & {};
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: [
25
+ tokens: {
26
+ tokens: RegisterTokens<any, any>;
27
+ options?: ContainerOptions;
28
+ } | undefined,
29
+ routes: SubModules<any>
30
+ ][], _route: (container: Deps, modules: InferSubModules<Modules>) => App);
31
+ app(container: IsEmptyObject<Deps> extends true ? HollywoodOf<any> | undefined : HollywoodOf<Deps>): App;
32
+ app(): IsEmptyObject<Deps> extends true ? App : never;
40
33
  }
41
- interface ModuleConfig<BasePath extends string, Path extends string, T extends Record<string, any>, P extends Record<string, any>> {
42
- tokens: RegisterTokens<T, P>;
43
- controllers: Controller<MergePath<BasePath, Path>, any, inferContainer<Hollywood<T, Hollywood<P, any>>>, any, any, any>[];
44
- submodules?: Module<MergePath<BasePath, Path>, any, inferContainer<Hollywood<T, Hollywood<P, any>>>>[];
45
- }
46
- type ModuleBuilder<BasePath extends string = "/", Path extends string = "/", P extends Record<string, any> = {}> = {
47
- build<T extends Record<string, any> = {}>(config: ModuleConfig<BasePath, Path, T, P>): Module<BasePath, Path, P>;
48
- };
49
- declare function createModule<BasePath extends string = "/", Path extends string = "/", P extends Record<string, any> = {}>(path: Path): ModuleBuilder<BasePath, Path, P>;
50
-
51
- interface Serverstruct<Container extends AnyHollywood, BasePath extends string> {
52
- modules(...modules: Module<BasePath, any, inferContainer<Container>>[]): this;
53
- controllers(...controllers: Controller<BasePath, any, inferContainer<Container>, any, any, any>[]): this;
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);
37
+ private context;
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>;
54
42
  }
55
- declare function serverstruct<App extends Hono<any, any, BasePath>, Container extends AnyHollywood, BasePath extends string = "/">(app: App, container: Container, _base?: BasePath): Serverstruct<Container, BasePath>;
43
+ declare function createModule<App extends Hono<any, any, any> = Hono>(app: App): ModuleBuilder<App, {}, {}, undefined>;
44
+ declare function createModule(): ModuleBuilder<Hono, {}, {}, undefined>;
56
45
 
57
- export { Controller, ControllerContext, Module, ModuleConfig, Router, Serverstruct, createController, createModule, inferController, serverstruct };
46
+ export { createModule };
package/dist/index.js CHANGED
@@ -1,27 +1,8 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
- var __defProps = Object.defineProperties;
5
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
7
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
8
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
9
- var __getProtoOf = Object.getPrototypeOf;
10
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
11
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
12
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
13
- var __spreadValues = (a, b) => {
14
- for (var prop in b || (b = {}))
15
- if (__hasOwnProp.call(b, prop))
16
- __defNormalProp(a, prop, b[prop]);
17
- if (__getOwnPropSymbols)
18
- for (var prop of __getOwnPropSymbols(b)) {
19
- if (__propIsEnum.call(b, prop))
20
- __defNormalProp(a, prop, b[prop]);
21
- }
22
- return a;
23
- };
24
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
25
6
  var __export = (target, all) => {
26
7
  for (var name in all)
27
8
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -34,100 +15,83 @@ var __copyProps = (to, from, except, desc) => {
34
15
  }
35
16
  return to;
36
17
  };
37
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
38
- // If the importer is in node compatibility mode or this is not an ESM
39
- // file that has been converted to a CommonJS file using a Babel-
40
- // compatible transform (i.e. "__esModule" has not been set), then set
41
- // "default" to the CommonJS "module.exports" for node compatibility.
42
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
43
- mod
44
- ));
45
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
46
19
 
47
20
  // src/index.ts
48
- var src_exports = {};
49
- __export(src_exports, {
50
- createController: () => createController,
51
- createModule: () => createModule,
52
- serverstruct: () => serverstruct
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ createModule: () => createModule
53
24
  });
54
- module.exports = __toCommonJS(src_exports);
55
- var import_hono2 = require("hono");
25
+ module.exports = __toCommonJS(index_exports);
56
26
 
57
- // src/controller.ts
58
- var import_auto_bind = __toESM(require("auto-bind"));
27
+ // src/module.ts
59
28
  var import_hollywood_di = require("hollywood-di");
60
29
  var import_hono = require("hono");
61
- var ControllerDef = class {
62
- constructor(path, route) {
63
- this.path = path;
64
- this.route = route;
30
+ var Module = class {
31
+ constructor(_context, _route) {
32
+ this._context = _context;
33
+ this._route = _route;
65
34
  }
66
- mountTo(parent) {
67
- const router = new import_hono.Hono();
68
- const mountedRouter = this.route(router);
69
- parent.route(this.path, mountedRouter);
35
+ app(container) {
36
+ let currentContainer = container;
37
+ const resolvedModules = {};
38
+ for (const [tokens, submodules] of this._context) {
39
+ let subContainer = currentContainer;
40
+ if (tokens && subContainer) {
41
+ subContainer = import_hollywood_di.Hollywood.createWithParent(
42
+ subContainer,
43
+ tokens.tokens,
44
+ tokens.options
45
+ );
46
+ } else if (tokens) {
47
+ subContainer = import_hollywood_di.Hollywood.create(tokens.tokens, tokens.options);
48
+ }
49
+ currentContainer = subContainer;
50
+ for (const [key, submodule] of Object.entries(submodules)) {
51
+ resolvedModules[key] = submodule.app(subContainer);
52
+ }
53
+ }
54
+ const instances = currentContainer?.instances ?? {};
55
+ return this._route(
56
+ instances,
57
+ resolvedModules
58
+ );
70
59
  }
71
60
  };
72
- function createController(path) {
73
- return {
74
- build(config) {
75
- return {
76
- init(container) {
77
- const init = (constructor) => {
78
- const instance = import_hollywood_di.Hollywood.initConstructor(constructor, container);
79
- return (0, import_auto_bind.default)(instance);
80
- };
81
- return new ControllerDef(path, (router) => config({ router, container, init }));
82
- }
83
- };
84
- }
85
- };
86
- }
87
-
88
- // src/module.ts
89
- function createModule(path) {
90
- return {
91
- build(config) {
92
- return __spreadProps(__spreadValues({}, config), { path });
93
- }
94
- };
95
- }
96
-
97
- // src/index.ts
98
- function serverstruct(app, container, _base) {
99
- return {
100
- controllers(...controllers) {
101
- registerControllers(app, container, controllers);
102
- return this;
103
- },
104
- modules(...modules) {
105
- for (const module2 of modules) {
106
- registerModule(app, container, module2);
107
- }
108
- return this;
61
+ var ModuleBuilder = class {
62
+ constructor(app) {
63
+ this.app = app;
64
+ this.context = [];
65
+ }
66
+ use() {
67
+ return this;
68
+ }
69
+ provide(tokens, options) {
70
+ this.context.push([{ tokens, options }, {}]);
71
+ return this;
72
+ }
73
+ submodules(modules) {
74
+ if (!this.context.length) this.context.push([void 0, modules]);
75
+ else {
76
+ const [, submodules] = this.context[this.context.length - 1];
77
+ Object.assign(submodules, modules);
109
78
  }
110
- };
111
- }
112
- function registerControllers(app, container, controllers) {
113
- for (const controllerInit of controllers) {
114
- const controller = container.resolve(controllerInit);
115
- controller.mountTo(app);
79
+ return this;
116
80
  }
117
- }
118
- function registerModule(app, container, module2) {
119
- var _a;
120
- const moduleContainer = container.createChild(module2.tokens);
121
- const subapp = new import_hono2.Hono();
122
- registerControllers(subapp, moduleContainer, module2.controllers);
123
- for (const submodule of (_a = module2.submodules) != null ? _a : []) {
124
- registerModule(subapp, moduleContainer, submodule);
81
+ route(fn) {
82
+ return new Module(this.context, (container, modules) => {
83
+ return fn(
84
+ this.app,
85
+ container,
86
+ modules
87
+ );
88
+ });
125
89
  }
126
- app.route(module2.path, subapp);
90
+ };
91
+ function createModule(app) {
92
+ return new ModuleBuilder(app ?? new import_hono.Hono());
127
93
  }
128
94
  // Annotate the CommonJS export names for ESM import in node:
129
95
  0 && (module.exports = {
130
- createController,
131
- createModule,
132
- serverstruct
96
+ createModule
133
97
  });
package/dist/index.mjs CHANGED
@@ -1,99 +1,72 @@
1
- var __defProp = Object.defineProperty;
2
- var __defProps = Object.defineProperties;
3
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
- var __spreadValues = (a, b) => {
9
- for (var prop in b || (b = {}))
10
- if (__hasOwnProp.call(b, prop))
11
- __defNormalProp(a, prop, b[prop]);
12
- if (__getOwnPropSymbols)
13
- for (var prop of __getOwnPropSymbols(b)) {
14
- if (__propIsEnum.call(b, prop))
15
- __defNormalProp(a, prop, b[prop]);
16
- }
17
- return a;
18
- };
19
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
-
21
- // src/index.ts
22
- import { Hono as Hono2 } from "hono";
23
-
24
- // src/controller.ts
25
- import autoBind from "auto-bind";
26
- import { Hollywood } from "hollywood-di";
1
+ // src/module.ts
2
+ import {
3
+ Hollywood
4
+ } from "hollywood-di";
27
5
  import { Hono } from "hono";
28
- var ControllerDef = class {
29
- constructor(path, route) {
30
- this.path = path;
31
- this.route = route;
6
+ var Module = class {
7
+ constructor(_context, _route) {
8
+ this._context = _context;
9
+ this._route = _route;
32
10
  }
33
- mountTo(parent) {
34
- const router = new Hono();
35
- const mountedRouter = this.route(router);
36
- parent.route(this.path, mountedRouter);
11
+ app(container) {
12
+ let currentContainer = container;
13
+ const resolvedModules = {};
14
+ for (const [tokens, submodules] of this._context) {
15
+ let subContainer = currentContainer;
16
+ if (tokens && subContainer) {
17
+ subContainer = Hollywood.createWithParent(
18
+ subContainer,
19
+ tokens.tokens,
20
+ tokens.options
21
+ );
22
+ } else if (tokens) {
23
+ subContainer = Hollywood.create(tokens.tokens, tokens.options);
24
+ }
25
+ currentContainer = subContainer;
26
+ for (const [key, submodule] of Object.entries(submodules)) {
27
+ resolvedModules[key] = submodule.app(subContainer);
28
+ }
29
+ }
30
+ const instances = currentContainer?.instances ?? {};
31
+ return this._route(
32
+ instances,
33
+ resolvedModules
34
+ );
37
35
  }
38
36
  };
39
- function createController(path) {
40
- return {
41
- build(config) {
42
- return {
43
- init(container) {
44
- const init = (constructor) => {
45
- const instance = Hollywood.initConstructor(constructor, container);
46
- return autoBind(instance);
47
- };
48
- return new ControllerDef(path, (router) => config({ router, container, init }));
49
- }
50
- };
51
- }
52
- };
53
- }
54
-
55
- // src/module.ts
56
- function createModule(path) {
57
- return {
58
- build(config) {
59
- return __spreadProps(__spreadValues({}, config), { path });
60
- }
61
- };
62
- }
63
-
64
- // src/index.ts
65
- function serverstruct(app, container, _base) {
66
- return {
67
- controllers(...controllers) {
68
- registerControllers(app, container, controllers);
69
- return this;
70
- },
71
- modules(...modules) {
72
- for (const module of modules) {
73
- registerModule(app, container, module);
74
- }
75
- return this;
37
+ var ModuleBuilder = class {
38
+ constructor(app) {
39
+ this.app = app;
40
+ this.context = [];
41
+ }
42
+ use() {
43
+ return this;
44
+ }
45
+ provide(tokens, options) {
46
+ this.context.push([{ tokens, options }, {}]);
47
+ return this;
48
+ }
49
+ submodules(modules) {
50
+ if (!this.context.length) this.context.push([void 0, modules]);
51
+ else {
52
+ const [, submodules] = this.context[this.context.length - 1];
53
+ Object.assign(submodules, modules);
76
54
  }
77
- };
78
- }
79
- function registerControllers(app, container, controllers) {
80
- for (const controllerInit of controllers) {
81
- const controller = container.resolve(controllerInit);
82
- controller.mountTo(app);
55
+ return this;
83
56
  }
84
- }
85
- function registerModule(app, container, module) {
86
- var _a;
87
- const moduleContainer = container.createChild(module.tokens);
88
- const subapp = new Hono2();
89
- registerControllers(subapp, moduleContainer, module.controllers);
90
- for (const submodule of (_a = module.submodules) != null ? _a : []) {
91
- registerModule(subapp, moduleContainer, submodule);
57
+ route(fn) {
58
+ return new Module(this.context, (container, modules) => {
59
+ return fn(
60
+ this.app,
61
+ container,
62
+ modules
63
+ );
64
+ });
92
65
  }
93
- app.route(module.path, subapp);
66
+ };
67
+ function createModule(app) {
68
+ return new ModuleBuilder(app ?? new Hono());
94
69
  }
95
70
  export {
96
- createController,
97
- createModule,
98
- serverstruct
71
+ createModule
99
72
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "serverstruct",
3
- "version": "0.1.1",
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",
@@ -14,9 +14,8 @@
14
14
  }
15
15
  },
16
16
  "keywords": [
17
- "command",
18
- "cli",
19
- "sade",
17
+ "hono",
18
+ "server",
20
19
  "hollywood-di",
21
20
  "typescript"
22
21
  ],
@@ -30,29 +29,25 @@
30
29
  "url": "https://github.com/eriicafes/serverstruct/issues"
31
30
  },
32
31
  "homepage": "https://github.com/eriicafes/serverstruct#readme",
33
- "dependencies": {
34
- "auto-bind": "^5.0.1"
35
- },
36
32
  "devDependencies": {
37
- "@changesets/cli": "^2.26.2",
38
- "@hono/node-server": "^1.1.0",
39
- "@types/node": "^20.4.2",
40
- "@types/supertest": "^2.0.12",
33
+ "@changesets/cli": "^2.27.12",
34
+ "@types/node": "^20.17.17",
35
+ "@vitest/coverage-v8": "^3.0.5",
41
36
  "shx": "^0.3.4",
42
- "supertest": "^6.3.3",
43
- "tsup": "^7.1.0",
44
- "typescript": "^5.1.6",
45
- "vitest": "^0.33.0"
37
+ "tsup": "^8.3.6",
38
+ "typescript": "^5.7.3",
39
+ "vitest": "^3.0.5"
46
40
  },
47
41
  "peerDependencies": {
48
- "hollywood-di": ">= 0.2.1",
49
- "hono": ">= 3.3.0"
42
+ "hollywood-di": ">= 0.6.1",
43
+ "hono": ">= 4.0.0"
50
44
  },
51
45
  "scripts": {
52
46
  "prebuild": "shx rm -rf dist",
53
47
  "build": "tsc --noEmit && tsup src/index.ts --format esm,cjs --dts",
54
48
  "release": "pnpm run build && changeset publish",
55
49
  "watch": "vitest",
56
- "test": "vitest run"
50
+ "test": "vitest run",
51
+ "test:coverage": "vitest run --coverage"
57
52
  }
58
53
  }
package/CHANGELOG.md DELETED
@@ -1,7 +0,0 @@
1
- # serverstruct
2
-
3
- ## 0.1.1
4
-
5
- ### Patch Changes
6
-
7
- - 609146c: Bump CI node version to v18