Skip to content
This repository has been archived by the owner on Oct 18, 2022. It is now read-only.

Adding Dependency Injection system #15

Closed
wants to merge 46 commits into from
Closed

Conversation

ben-ryder
Copy link
Owner

@ben-ryder ben-ryder commented Jun 2, 2022

  • Adding DI system
    • Add system for controllers and their dependencies
    • Add system for middleware functions

@@ -32,6 +32,7 @@ module.exports = {
"semi": [
"error",
"always"
]
],
"@typescript-eslint/no-explicit-any": "off"
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove in favor of file level disabling

verbose: true,
silent: false,
transform: {
"^.+\\.tsx?$": "ts-jest",
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed, there's no .tsx files anywhere

@@ -0,0 +1 @@
import "reflect-metadata";
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comment explaining why this is needed for jest

@@ -14,5 +14,10 @@ export function Controller(routePrefix = ""): ClassDecorator {
if (!Reflect.hasMetadata(MetadataKeys.ROUTES, target)) {
Reflect.defineMetadata(MetadataKeys.ROUTES, [], target);
}

// Automatically setup dependency metadata here so users don't need to add @Injectable to controllers
const dependencyKey = Symbol.for(target.toString());
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add ability for users to set custom identifier via @Controller like you can do with @Injectable

// Automatically setup dependency metadata here so users don't need to add @Injectable to controllers
const dependencyKey = Symbol.for(target.toString());
Reflect.defineMetadata(MetadataKeys.DEPENDENCY_KEY, dependencyKey, target);
Reflect.defineMetadata(MetadataKeys.DEPENDENCY_CONFIG, {mode: "global"}, target);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to add metadata to target.prototype instead?

* @param options - options for customising how KangoJS works.
*/
constructor(app: Application, options: KangoJSOptions) {
this.app = app;
this.router = Router();
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to add via DI system?

this.router = Router();

// Setup dependency container
// no dependencies are added till .bootstrap to allow for overwrites of the Express router/app etc
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update comment if this isn't the case

* @param app - An Express application
* @param options - options for customising how KangoJS works.
*/
constructor(app: Application, options: KangoJSOptions) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should Express app creation be moved to within the framework?
This might make it easier in the future to add middleware level DI

}


export abstract class LoggerBase {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add jsdoc

identifier: "test-global"
})
// @ts-ignore
class TestSingletonDependency {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try and find way to make this quiet without ts-ignore.
Issue is that IDE is complaining that decorators are experimental even though I've got experimentalDecorators enabled in tsconfig

@ben-ryder ben-ryder linked an issue Jun 2, 2022 that may be closed by this pull request
@ben-ryder
Copy link
Owner Author

closing as old PR, feature already in v2 alpha

@ben-ryder ben-ryder closed this Oct 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement a dependency injection system
1 participant