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 +210 -2
- package/dist/index.d.mts +40 -51
- package/dist/index.d.ts +40 -51
- package/dist/index.js +63 -99
- package/dist/index.mjs +62 -89
- package/package.json +13 -18
- package/CHANGELOG.md +0 -7
package/README.md
CHANGED
|
@@ -1,3 +1,211 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Serverstruct
|
|
2
2
|
|
|
3
|
-
|
|
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 {
|
|
2
|
-
import {
|
|
1
|
+
import { HollywoodOf, AnyHollywood, RegisterTokens, InferContainer, ContainerOptions, Hollywood } from 'hollywood-di';
|
|
2
|
+
import { Hono } from 'hono';
|
|
3
3
|
|
|
4
|
-
type
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
type
|
|
8
|
-
type
|
|
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
|
|
11
|
-
|
|
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
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
|
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 {
|
|
46
|
+
export { createModule };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,57 +1,46 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { HollywoodOf, AnyHollywood, RegisterTokens, InferContainer, ContainerOptions, Hollywood } from 'hollywood-di';
|
|
2
|
+
import { Hono } from 'hono';
|
|
3
3
|
|
|
4
|
-
type
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
type
|
|
8
|
-
type
|
|
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
|
|
11
|
-
|
|
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
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
|
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 {
|
|
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
|
|
49
|
-
__export(
|
|
50
|
-
|
|
51
|
-
createModule: () => createModule,
|
|
52
|
-
serverstruct: () => serverstruct
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
createModule: () => createModule
|
|
53
24
|
});
|
|
54
|
-
module.exports = __toCommonJS(
|
|
55
|
-
var import_hono2 = require("hono");
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
56
26
|
|
|
57
|
-
// src/
|
|
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
|
|
62
|
-
constructor(
|
|
63
|
-
this.
|
|
64
|
-
this.
|
|
30
|
+
var Module = class {
|
|
31
|
+
constructor(_context, _route) {
|
|
32
|
+
this._context = _context;
|
|
33
|
+
this._route = _route;
|
|
65
34
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
const
|
|
69
|
-
|
|
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
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
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
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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
|
-
|
|
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
|
-
|
|
131
|
-
createModule,
|
|
132
|
-
serverstruct
|
|
96
|
+
createModule
|
|
133
97
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -1,99 +1,72 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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
|
|
29
|
-
constructor(
|
|
30
|
-
this.
|
|
31
|
-
this.
|
|
6
|
+
var Module = class {
|
|
7
|
+
constructor(_context, _route) {
|
|
8
|
+
this._context = _context;
|
|
9
|
+
this._route = _route;
|
|
32
10
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const
|
|
36
|
-
|
|
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
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
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
|
-
|
|
66
|
+
};
|
|
67
|
+
function createModule(app) {
|
|
68
|
+
return new ModuleBuilder(app ?? new Hono());
|
|
94
69
|
}
|
|
95
70
|
export {
|
|
96
|
-
|
|
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.
|
|
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
|
-
"
|
|
18
|
-
"
|
|
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.
|
|
38
|
-
"@
|
|
39
|
-
"@
|
|
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
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"
|
|
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.
|
|
49
|
-
"hono": ">=
|
|
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
|
}
|