serverstruct 0.1.0 → 0.2.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 +193 -2
- package/dist/index.d.mts +35 -51
- package/dist/index.d.ts +35 -51
- package/dist/index.js +55 -95
- package/dist/index.mjs +57 -88
- package/package.json +15 -20
package/README.md
CHANGED
|
@@ -1,3 +1,194 @@
|
|
|
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.
|
|
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).
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
Serverstruct requires you to install hono.
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
npm i serverstruct hono
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
To make use of dependency injection provided by serverstruct, also install hollywood-di.
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
npm i serverstruct hono hollywood-di
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
### A simple app
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { createRoute } from "serverstruct";
|
|
29
|
+
|
|
30
|
+
const app = createRoute()
|
|
31
|
+
.route((app) => {
|
|
32
|
+
return app.get("/", (c) => c.text("Hello world!"));
|
|
33
|
+
})
|
|
34
|
+
.app();
|
|
35
|
+
|
|
36
|
+
export default app;
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### An app with subroutes
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import { createRoute } from "serverstruct";
|
|
43
|
+
|
|
44
|
+
// users routes
|
|
45
|
+
const users = createRoute().route((app) => {
|
|
46
|
+
return app.get("/", (c) => c.text("users"));
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// posts routes
|
|
50
|
+
const posts = createRoute().route((app) => {
|
|
51
|
+
return app.get("/", (c) => c.text("posts"));
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const app = createRoute()
|
|
55
|
+
.subroutes({ users, posts })
|
|
56
|
+
.route((app, container, routes) => {
|
|
57
|
+
return app
|
|
58
|
+
.get("/", (c) => c.text("Hello world!"))
|
|
59
|
+
.route("/users", routes.users)
|
|
60
|
+
.route("/posts", routes.posts);
|
|
61
|
+
})
|
|
62
|
+
.app();
|
|
63
|
+
|
|
64
|
+
export default app;
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Route
|
|
68
|
+
|
|
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.
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
const auth = createRoute().route((app) => {
|
|
73
|
+
return app; // chain route handlers here
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const users = createRoute().route((app) => {
|
|
77
|
+
return app; // chain route handlers here
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const posts = createRoute().route((app) => {
|
|
81
|
+
return app; // chain route handlers here
|
|
82
|
+
});
|
|
83
|
+
|
|
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);
|
|
91
|
+
})
|
|
92
|
+
.app();
|
|
93
|
+
```
|
|
94
|
+
|
|
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
|
+
## Dependency Injection
|
|
98
|
+
|
|
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.
|
|
100
|
+
|
|
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.
|
|
102
|
+
|
|
103
|
+
### Use
|
|
104
|
+
|
|
105
|
+
Define route dependencies. The route can then only be used in a context that satisfies it's dependencies.
|
|
106
|
+
|
|
107
|
+
You can only call `use` once and only before calling `provide`.
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
interface Counter {
|
|
111
|
+
count: number;
|
|
112
|
+
increment(): void;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const route = createRoute()
|
|
116
|
+
.use<{ counter: Counter }>()
|
|
117
|
+
.route((app, container) => {
|
|
118
|
+
return app.get("/", (c) => {
|
|
119
|
+
container.counter.increment();
|
|
120
|
+
return c.text(`Count is: ${container.counter.count}`);
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
class LinearCounter {
|
|
125
|
+
public count = 0;
|
|
126
|
+
public increment() {
|
|
127
|
+
this.count++;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
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);
|
|
137
|
+
})
|
|
138
|
+
.app();
|
|
139
|
+
|
|
140
|
+
// or as the main app
|
|
141
|
+
const container = Hollywood.create({ counter: LinearCounter }); // the container provides the dependency
|
|
142
|
+
const app = route.app(container);
|
|
143
|
+
```
|
|
144
|
+
|
|
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.
|
|
146
|
+
|
|
147
|
+
### Provide
|
|
148
|
+
|
|
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).
|
|
150
|
+
|
|
151
|
+
```ts
|
|
152
|
+
import { defineInit } from "hollywood-di";
|
|
153
|
+
import { createRoute } from "serverstruct";
|
|
154
|
+
|
|
155
|
+
class Foo {}
|
|
156
|
+
class Bar {
|
|
157
|
+
public static init = defineInit(Bar).args("foo");
|
|
158
|
+
|
|
159
|
+
constructor(public foo: Foo) {}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const route = createRoute()
|
|
163
|
+
.provide({
|
|
164
|
+
foo: Foo,
|
|
165
|
+
bar: Bar,
|
|
166
|
+
})
|
|
167
|
+
.route((app, container) => {
|
|
168
|
+
return app;
|
|
169
|
+
});
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## Incremental Adoption
|
|
173
|
+
|
|
174
|
+
Serverstruct can be added to an existing Hono app.
|
|
175
|
+
|
|
176
|
+
```ts
|
|
177
|
+
// routes/posts.ts
|
|
178
|
+
export const postsRoute = createRoute().route((app) => {
|
|
179
|
+
return app.get("/", (c) => {
|
|
180
|
+
return c.text("posts");
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
import { Hono } from "hono";
|
|
185
|
+
import { createRoute } from "serverstruct";
|
|
186
|
+
import { postsRoute } from "./routes/posts";
|
|
187
|
+
|
|
188
|
+
const app = new Hono();
|
|
189
|
+
|
|
190
|
+
app.get("/", (c) => c.text("Hello world!"));
|
|
191
|
+
app.route("/posts", postsRoute.app());
|
|
192
|
+
|
|
193
|
+
export default app;
|
|
194
|
+
```
|
package/dist/index.d.mts
CHANGED
|
@@ -1,57 +1,41 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Env, Hono } from 'hono';
|
|
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';
|
|
3
4
|
|
|
4
|
-
type
|
|
5
|
-
type
|
|
6
|
-
type
|
|
7
|
-
type
|
|
8
|
-
|
|
5
|
+
type Merge<T, To> = T & Omit<To, keyof T>;
|
|
6
|
+
type IsEmptyObject<T> = keyof T extends never ? true : false;
|
|
7
|
+
type KnownKey<T> = string extends T ? never : number extends T ? never : symbol extends T ? never : T;
|
|
8
|
+
type KnownMappedKeys<T> = {
|
|
9
|
+
[K in keyof T as KnownKey<K>]: T[K];
|
|
10
|
+
} & {};
|
|
9
11
|
|
|
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>;
|
|
12
|
+
type Subroute<T extends Record<string, any> = any> = {
|
|
13
|
+
app: (container: IsEmptyObject<T> extends true ? HollywoodOf<any> | undefined : HollywoodOf<T>) => Hono<any, any>;
|
|
32
14
|
};
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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;
|
|
18
|
+
} & {};
|
|
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: [
|
|
23
|
+
tokens: {
|
|
24
|
+
tokens: RegisterTokens<any, any>;
|
|
25
|
+
options?: ContainerOptions;
|
|
26
|
+
} | undefined,
|
|
27
|
+
routes: Subroutes<any>
|
|
28
|
+
][]);
|
|
29
|
+
app(container: IsEmptyObject<Deps> extends true ? HollywoodOf<any> | undefined : HollywoodOf<Deps>): App;
|
|
30
|
+
app(container?: never): IsEmptyObject<Deps> extends true ? App : never;
|
|
40
31
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
46
|
-
|
|
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;
|
|
32
|
+
declare class RouteBuilder<Deps extends Record<string, any>, Container extends AnyHollywood | undefined, E extends Env, S extends Schema, Routes extends Subroutes> {
|
|
33
|
+
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>;
|
|
54
38
|
}
|
|
55
|
-
declare function
|
|
39
|
+
declare function createRoute<E extends Env = BlankEnv>(): RouteBuilder<{}, undefined, E, BlankSchema, {}>;
|
|
56
40
|
|
|
57
|
-
export {
|
|
41
|
+
export { createRoute };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,57 +1,41 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Env, Hono } from 'hono';
|
|
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';
|
|
3
4
|
|
|
4
|
-
type
|
|
5
|
-
type
|
|
6
|
-
type
|
|
7
|
-
type
|
|
8
|
-
|
|
5
|
+
type Merge<T, To> = T & Omit<To, keyof T>;
|
|
6
|
+
type IsEmptyObject<T> = keyof T extends never ? true : false;
|
|
7
|
+
type KnownKey<T> = string extends T ? never : number extends T ? never : symbol extends T ? never : T;
|
|
8
|
+
type KnownMappedKeys<T> = {
|
|
9
|
+
[K in keyof T as KnownKey<K>]: T[K];
|
|
10
|
+
} & {};
|
|
9
11
|
|
|
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>;
|
|
12
|
+
type Subroute<T extends Record<string, any> = any> = {
|
|
13
|
+
app: (container: IsEmptyObject<T> extends true ? HollywoodOf<any> | undefined : HollywoodOf<T>) => Hono<any, any>;
|
|
32
14
|
};
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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;
|
|
18
|
+
} & {};
|
|
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: [
|
|
23
|
+
tokens: {
|
|
24
|
+
tokens: RegisterTokens<any, any>;
|
|
25
|
+
options?: ContainerOptions;
|
|
26
|
+
} | undefined,
|
|
27
|
+
routes: Subroutes<any>
|
|
28
|
+
][]);
|
|
29
|
+
app(container: IsEmptyObject<Deps> extends true ? HollywoodOf<any> | undefined : HollywoodOf<Deps>): App;
|
|
30
|
+
app(container?: never): IsEmptyObject<Deps> extends true ? App : never;
|
|
40
31
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
46
|
-
|
|
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;
|
|
32
|
+
declare class RouteBuilder<Deps extends Record<string, any>, Container extends AnyHollywood | undefined, E extends Env, S extends Schema, Routes extends Subroutes> {
|
|
33
|
+
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>;
|
|
54
38
|
}
|
|
55
|
-
declare function
|
|
39
|
+
declare function createRoute<E extends Env = BlankEnv>(): RouteBuilder<{}, undefined, E, BlankSchema, {}>;
|
|
56
40
|
|
|
57
|
-
export {
|
|
41
|
+
export { createRoute };
|
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,79 @@ 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
21
|
var src_exports = {};
|
|
49
22
|
__export(src_exports, {
|
|
50
|
-
|
|
51
|
-
createModule: () => createModule,
|
|
52
|
-
serverstruct: () => serverstruct
|
|
23
|
+
createRoute: () => createRoute
|
|
53
24
|
});
|
|
54
25
|
module.exports = __toCommonJS(src_exports);
|
|
55
|
-
var import_hono2 = require("hono");
|
|
56
26
|
|
|
57
|
-
// src/
|
|
58
|
-
var import_auto_bind = __toESM(require("auto-bind"));
|
|
27
|
+
// src/route.ts
|
|
59
28
|
var import_hollywood_di = require("hollywood-di");
|
|
60
29
|
var import_hono = require("hono");
|
|
61
|
-
var
|
|
62
|
-
constructor(
|
|
63
|
-
this.path = path;
|
|
30
|
+
var Route = class {
|
|
31
|
+
constructor(route, context) {
|
|
64
32
|
this.route = route;
|
|
33
|
+
this.context = context;
|
|
65
34
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
35
|
+
app(container) {
|
|
36
|
+
var _a;
|
|
37
|
+
let currentContainer = container;
|
|
38
|
+
const resolvedRoutes = {};
|
|
39
|
+
for (const [tokens, subroutes] of this.context) {
|
|
40
|
+
let subContainer = currentContainer;
|
|
41
|
+
if (tokens && subContainer) {
|
|
42
|
+
subContainer = import_hollywood_di.Hollywood.createWithParent(
|
|
43
|
+
subContainer,
|
|
44
|
+
tokens.tokens,
|
|
45
|
+
tokens.options
|
|
46
|
+
);
|
|
47
|
+
} else if (tokens) {
|
|
48
|
+
subContainer = import_hollywood_di.Hollywood.create(tokens.tokens, tokens.options);
|
|
49
|
+
}
|
|
50
|
+
currentContainer = subContainer;
|
|
51
|
+
for (const [key, subroute] of Object.entries(subroutes)) {
|
|
52
|
+
resolvedRoutes[key] = subroute.app(subContainer);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const instances = (_a = currentContainer == null ? void 0 : currentContainer.instances) != null ? _a : {};
|
|
56
|
+
return this.route(
|
|
57
|
+
new import_hono.Hono(),
|
|
58
|
+
instances,
|
|
59
|
+
resolvedRoutes
|
|
60
|
+
);
|
|
70
61
|
}
|
|
71
62
|
};
|
|
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;
|
|
63
|
+
var RouteBuilder = class {
|
|
64
|
+
constructor() {
|
|
65
|
+
this.context = [];
|
|
66
|
+
}
|
|
67
|
+
use() {
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
provide(tokens, options) {
|
|
71
|
+
this.context.push([{ tokens, options }, {}]);
|
|
72
|
+
return this;
|
|
73
|
+
}
|
|
74
|
+
subroutes(routes) {
|
|
75
|
+
if (!this.context.length)
|
|
76
|
+
this.context.push([void 0, routes]);
|
|
77
|
+
else {
|
|
78
|
+
const [, subroutes] = this.context[this.context.length - 1];
|
|
79
|
+
Object.assign(subroutes, routes);
|
|
109
80
|
}
|
|
110
|
-
|
|
111
|
-
}
|
|
112
|
-
function registerControllers(app, container, controllers) {
|
|
113
|
-
for (const controllerInit of controllers) {
|
|
114
|
-
const controller = container.resolve(controllerInit);
|
|
115
|
-
controller.mountTo(app);
|
|
81
|
+
return this;
|
|
116
82
|
}
|
|
117
|
-
|
|
118
|
-
|
|
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);
|
|
83
|
+
route(fn) {
|
|
84
|
+
return new Route(fn, this.context);
|
|
125
85
|
}
|
|
126
|
-
|
|
86
|
+
};
|
|
87
|
+
function createRoute() {
|
|
88
|
+
return new RouteBuilder();
|
|
127
89
|
}
|
|
128
90
|
// Annotate the CommonJS export names for ESM import in node:
|
|
129
91
|
0 && (module.exports = {
|
|
130
|
-
|
|
131
|
-
createModule,
|
|
132
|
-
serverstruct
|
|
92
|
+
createRoute
|
|
133
93
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -1,99 +1,68 @@
|
|
|
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/route.ts
|
|
2
|
+
import {
|
|
3
|
+
Hollywood
|
|
4
|
+
} from "hollywood-di";
|
|
27
5
|
import { Hono } from "hono";
|
|
28
|
-
var
|
|
29
|
-
constructor(
|
|
30
|
-
this.path = path;
|
|
6
|
+
var Route = class {
|
|
7
|
+
constructor(route, context) {
|
|
31
8
|
this.route = route;
|
|
9
|
+
this.context = context;
|
|
32
10
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
11
|
+
app(container) {
|
|
12
|
+
var _a;
|
|
13
|
+
let currentContainer = container;
|
|
14
|
+
const resolvedRoutes = {};
|
|
15
|
+
for (const [tokens, subroutes] of this.context) {
|
|
16
|
+
let subContainer = currentContainer;
|
|
17
|
+
if (tokens && subContainer) {
|
|
18
|
+
subContainer = Hollywood.createWithParent(
|
|
19
|
+
subContainer,
|
|
20
|
+
tokens.tokens,
|
|
21
|
+
tokens.options
|
|
22
|
+
);
|
|
23
|
+
} else if (tokens) {
|
|
24
|
+
subContainer = Hollywood.create(tokens.tokens, tokens.options);
|
|
25
|
+
}
|
|
26
|
+
currentContainer = subContainer;
|
|
27
|
+
for (const [key, subroute] of Object.entries(subroutes)) {
|
|
28
|
+
resolvedRoutes[key] = subroute.app(subContainer);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const instances = (_a = currentContainer == null ? void 0 : currentContainer.instances) != null ? _a : {};
|
|
32
|
+
return this.route(
|
|
33
|
+
new Hono(),
|
|
34
|
+
instances,
|
|
35
|
+
resolvedRoutes
|
|
36
|
+
);
|
|
37
37
|
}
|
|
38
38
|
};
|
|
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;
|
|
39
|
+
var RouteBuilder = class {
|
|
40
|
+
constructor() {
|
|
41
|
+
this.context = [];
|
|
42
|
+
}
|
|
43
|
+
use() {
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
provide(tokens, options) {
|
|
47
|
+
this.context.push([{ tokens, options }, {}]);
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
subroutes(routes) {
|
|
51
|
+
if (!this.context.length)
|
|
52
|
+
this.context.push([void 0, routes]);
|
|
53
|
+
else {
|
|
54
|
+
const [, subroutes] = this.context[this.context.length - 1];
|
|
55
|
+
Object.assign(subroutes, routes);
|
|
76
56
|
}
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
function registerControllers(app, container, controllers) {
|
|
80
|
-
for (const controllerInit of controllers) {
|
|
81
|
-
const controller = container.resolve(controllerInit);
|
|
82
|
-
controller.mountTo(app);
|
|
57
|
+
return this;
|
|
83
58
|
}
|
|
84
|
-
|
|
85
|
-
|
|
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);
|
|
59
|
+
route(fn) {
|
|
60
|
+
return new Route(fn, this.context);
|
|
92
61
|
}
|
|
93
|
-
|
|
62
|
+
};
|
|
63
|
+
function createRoute() {
|
|
64
|
+
return new RouteBuilder();
|
|
94
65
|
}
|
|
95
66
|
export {
|
|
96
|
-
|
|
97
|
-
createModule,
|
|
98
|
-
serverstruct
|
|
67
|
+
createRoute
|
|
99
68
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "serverstruct",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Type safe and modular servers with Hono",
|
|
5
5
|
"private": false,
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -13,17 +13,9 @@
|
|
|
13
13
|
"default": "./dist/index.js"
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
|
-
"scripts": {
|
|
17
|
-
"prebuild": "shx rm -rf dist",
|
|
18
|
-
"build": "tsc --noEmit && tsup src/index.ts --format esm,cjs --dts",
|
|
19
|
-
"release": "pnpm run build && changeset publish",
|
|
20
|
-
"watch": "vitest",
|
|
21
|
-
"test": "vitest run"
|
|
22
|
-
},
|
|
23
16
|
"keywords": [
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"sade",
|
|
17
|
+
"hono",
|
|
18
|
+
"server",
|
|
27
19
|
"hollywood-di",
|
|
28
20
|
"typescript"
|
|
29
21
|
],
|
|
@@ -37,22 +29,25 @@
|
|
|
37
29
|
"url": "https://github.com/eriicafes/serverstruct/issues"
|
|
38
30
|
},
|
|
39
31
|
"homepage": "https://github.com/eriicafes/serverstruct#readme",
|
|
40
|
-
"dependencies": {
|
|
41
|
-
"auto-bind": "^5.0.1"
|
|
42
|
-
},
|
|
43
32
|
"devDependencies": {
|
|
44
33
|
"@changesets/cli": "^2.26.2",
|
|
45
|
-
"@hono/node-server": "^1.1.0",
|
|
46
34
|
"@types/node": "^20.4.2",
|
|
47
|
-
"@
|
|
35
|
+
"@vitest/coverage-v8": "^1.6.0",
|
|
48
36
|
"shx": "^0.3.4",
|
|
49
|
-
"supertest": "^6.3.3",
|
|
50
37
|
"tsup": "^7.1.0",
|
|
51
38
|
"typescript": "^5.1.6",
|
|
52
|
-
"vitest": "^
|
|
39
|
+
"vitest": "^1.6.0"
|
|
53
40
|
},
|
|
54
41
|
"peerDependencies": {
|
|
55
|
-
"hollywood-di": ">= 0.2
|
|
56
|
-
"hono": ">=
|
|
42
|
+
"hollywood-di": ">= 0.5.2",
|
|
43
|
+
"hono": ">= 4.0.0"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"prebuild": "shx rm -rf dist",
|
|
47
|
+
"build": "tsc --noEmit && tsup src/index.ts --format esm,cjs --dts",
|
|
48
|
+
"release": "pnpm run build && changeset publish",
|
|
49
|
+
"watch": "vitest",
|
|
50
|
+
"test": "vitest run",
|
|
51
|
+
"test:coverage": "vitest run --coverage"
|
|
57
52
|
}
|
|
58
53
|
}
|