@coloneldev/framework 0.1.1 → 0.1.2
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 +3 -2
- package/package.json +1 -1
- package/src/Container/Container.ts +84 -0
- package/src/Http/Kernel.container.test.ts +51 -0
- package/src/Http/Kernel.ts +6 -5
- package/src/index.ts +1 -0
package/README.md
CHANGED
|
@@ -27,10 +27,11 @@ The package currently exports framework primitives from the root entry:
|
|
|
27
27
|
## Example
|
|
28
28
|
|
|
29
29
|
```ts
|
|
30
|
-
import { Kernel, Router } from "@coloneldev/framework";
|
|
30
|
+
import { Container, Kernel, Router } from "@coloneldev/framework";
|
|
31
31
|
|
|
32
32
|
const router = new Router();
|
|
33
|
-
const
|
|
33
|
+
const container = new Container();
|
|
34
|
+
const kernel = new Kernel(router, [], {}, container);
|
|
34
35
|
```
|
|
35
36
|
|
|
36
37
|
## License
|
package/package.json
CHANGED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
export type Newable<T> = new (...args: any[]) => T;
|
|
2
|
+
export type Token<T = unknown> = string | symbol | Newable<T>;
|
|
3
|
+
|
|
4
|
+
type InjectableNewable<T> = Newable<T> & {
|
|
5
|
+
inject?: Token[];
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
type Factory<T> = (container: Container) => T;
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
interface Binding<T = unknown> {
|
|
13
|
+
factory: Factory<T>;
|
|
14
|
+
singleton: boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const isNewable = <T>(value: Token<T>): value is Newable<T> => {
|
|
18
|
+
return typeof value === "function";
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export class Container {
|
|
22
|
+
private bindings = new Map<Token, Binding>();
|
|
23
|
+
private instances = new Map<Token, unknown>();
|
|
24
|
+
|
|
25
|
+
bind<T>(token: Token<T>, factory: Factory<T>): this {
|
|
26
|
+
this.bindings.set(token, { factory, singleton: false });
|
|
27
|
+
this.instances.delete(token);
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
singleton<T>(token: Token<T>, factory: Factory<T>): this {
|
|
32
|
+
this.bindings.set(token, { factory, singleton: true });
|
|
33
|
+
return this;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
instance<T>(token: Token<T>, value: T): this {
|
|
37
|
+
this.instances.set(token, value);
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
has<T>(token: Token<T>): boolean {
|
|
42
|
+
return this.instances.has(token) || this.bindings.has(token);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
make<T>(token: Token<T>): T {
|
|
46
|
+
if (this.instances.has(token)) {
|
|
47
|
+
return this.instances.get(token) as T;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const binding = this.bindings.get(token);
|
|
51
|
+
if (binding) {
|
|
52
|
+
const value = binding.factory(this) as T;
|
|
53
|
+
|
|
54
|
+
if (binding.singleton) {
|
|
55
|
+
this.instances.set(token, value);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return value;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (isNewable(token)) {
|
|
62
|
+
const injectableToken = token as InjectableNewable<T>;
|
|
63
|
+
const dependencies = (injectableToken.inject ?? []).map((dependencyToken) =>
|
|
64
|
+
this.make(dependencyToken)
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
return new injectableToken(...dependencies);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
throw new Error(`Container binding not found for token: ${String(token)}`);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
forget<T>(token: Token<T>): this {
|
|
74
|
+
this.bindings.delete(token);
|
|
75
|
+
this.instances.delete(token);
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
flush(): this {
|
|
80
|
+
this.bindings.clear();
|
|
81
|
+
this.instances.clear();
|
|
82
|
+
return this;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { describe, expect, it } from "bun:test";
|
|
2
|
+
import { Container } from "../Container/Container";
|
|
3
|
+
import { Kernel } from "./Kernel";
|
|
4
|
+
import { Router } from "./Router";
|
|
5
|
+
|
|
6
|
+
class MessageService {
|
|
7
|
+
value(): string {
|
|
8
|
+
return "resolved-via-container";
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
class HomeController {
|
|
13
|
+
static inject = [MessageService];
|
|
14
|
+
|
|
15
|
+
constructor(private messageService: MessageService) {}
|
|
16
|
+
|
|
17
|
+
index(): Record<string, string> {
|
|
18
|
+
return { message: this.messageService.value() };
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
describe("Kernel container integration", () => {
|
|
23
|
+
it("resolves controller dependencies via static inject during request dispatch", async () => {
|
|
24
|
+
const router = new Router();
|
|
25
|
+
router.get("/", "HomeController@index");
|
|
26
|
+
|
|
27
|
+
const container = new Container();
|
|
28
|
+
container.singleton(MessageService, () => new MessageService());
|
|
29
|
+
|
|
30
|
+
const kernel = new Kernel(
|
|
31
|
+
router,
|
|
32
|
+
[],
|
|
33
|
+
{
|
|
34
|
+
controllerResolver: async (name: string) => {
|
|
35
|
+
if (name !== "HomeController") {
|
|
36
|
+
throw new Error(`Unexpected controller: ${name}`);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return HomeController;
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
container
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
const response = await kernel.handle(new Request("http://localhost/"));
|
|
46
|
+
const body = await response.json();
|
|
47
|
+
|
|
48
|
+
expect(response.status).toBe(200);
|
|
49
|
+
expect(body).toEqual({ message: "resolved-via-container" });
|
|
50
|
+
});
|
|
51
|
+
});
|
package/src/Http/Kernel.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { HttpRequest } from './HttpRequest';
|
|
2
2
|
import { Router } from './Router';
|
|
3
|
-
//import { Container } from '../Container/Container';
|
|
4
3
|
import type { HttpMethod } from './types/HttpMethod';
|
|
5
4
|
import { join, resolve } from 'node:path';
|
|
6
5
|
import ejs from 'ejs';
|
|
7
6
|
import type { RouteHandler } from './types/RouteHandler';
|
|
7
|
+
import type { Container } from '../Container/Container';
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
|
|
10
|
+
type ControllerClass = new (...args: any[]) => object;
|
|
10
11
|
type ControllerResolver = (name: string) => Promise<ControllerClass>;
|
|
11
12
|
|
|
12
13
|
interface KernelOptions {
|
|
@@ -37,9 +38,9 @@ const safeViewPath = (view: string) => view.replace(/\.\./g, '').replace(/\/+/g,
|
|
|
37
38
|
export class Kernel {
|
|
38
39
|
constructor(
|
|
39
40
|
private router = new Router(),
|
|
40
|
-
//private container = new Container(),
|
|
41
41
|
private middleware: Array<Function> = [],
|
|
42
|
-
private options: KernelOptions = {}
|
|
42
|
+
private options: KernelOptions = {},
|
|
43
|
+
private container: Container,
|
|
43
44
|
) {}
|
|
44
45
|
|
|
45
46
|
private async resolveController(name: string): Promise<ControllerClass> {
|
|
@@ -131,7 +132,7 @@ export class Kernel {
|
|
|
131
132
|
|
|
132
133
|
const ImportedControllerClass = await this.resolveController(controllerName!);
|
|
133
134
|
|
|
134
|
-
const controllerInstance =
|
|
135
|
+
const controllerInstance = this.container.make(ImportedControllerClass);
|
|
135
136
|
|
|
136
137
|
const action = (controllerInstance as any)[method!] as (req: HttpRequest) => Promise<Response> | Response;
|
|
137
138
|
|
package/src/index.ts
CHANGED