Colony Framework
This is a tool for dependency injection, using typescript in node.
Installation
npm install typescript --save-dev
and then npm install colony-framework --save
.
Usage
import { Injectable } from "colony-framework";
@Injectable
class MessageCreator {
GetMessage() {
return "Hello world";
}
}
And then for the subject.
import { Injectable } from "colony-framework";
@Injectable
export class TestController {
constructor(private readonly message_creator: MessageCreator) {
super();
}
async GET(query: unknown) {
return {
status: 200,
body: this.message_creator.GetMessage(),
};
}
}
Finaly build the tree.
import { TestController } "./test-controller";
import { Build } from "colony-framework";
const controller = Build(TestController);
Advanced usage
We can also use abstract base classes in the place of interfaces. This library uses reflect-metadata under the hood so interfaces are not currently available.
export default abstract class IMessageCreator {
abstract GetMessage(): string;
}
Can be used as
import { InjectableAs } from "colony-framework";
import IMessageCreator from "./i-message-creator";
@InjectableAs(IMessageCreator)
class MessageCreator extends IMessageCreator {
GetMessage() {
return "Hello world";
}
}
This can then be imported as IMessageCreator into other injectable classes.
We can also use custom initialisers on classes. These initialisers can be asynchronous.
import { InjectableWith } from "colony-framework";
@InjectableWith(() => new MessageCreator("Hello world"))
class MessageCreator {
constructor(private readonly message: string) {
super();
}
GetMessage() {
return this.message;
}
}
This can also take a second argument for as
so as to use abstract base classes.