Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: useValue + useClass injectables #81

Merged
merged 3 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions spec/auth-guard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { DanetApplication } from '../src/app.ts';
import { GLOBAL_GUARD } from '../src/guard/constants.ts';
import { UseGuard } from '../src/guard/decorator.ts';
import { AuthGuard } from '../src/guard/interface.ts';
import { TokenInjector } from '../src/injector/injectable/constructor.ts';
import { Injectable } from '../src/injector/injectable/decorator.ts';
import { Module } from '../src/module/decorator.ts';
import { Controller, Get } from '../src/router/controller/decorator.ts';
Expand Down Expand Up @@ -133,7 +132,7 @@ class GlobalAuthController {
@Module({
imports: [],
controllers: [GlobalAuthController],
injectables: [new TokenInjector(GlobalGuard, GLOBAL_GUARD), SimpleService],
injectables: [{ useClass: GlobalGuard, token: GLOBAL_GUARD }, SimpleService],
})
class GlobalAuthModule {}

Expand Down
37 changes: 32 additions & 5 deletions spec/injection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { DanetApplication } from '../src/app.ts';
import { GLOBAL_GUARD } from '../src/guard/constants.ts';
import { AuthGuard } from '../src/guard/interface.ts';
import { Inject } from '../src/injector/decorator.ts';
import { TokenInjector } from '../src/injector/injectable/constructor.ts';
import { Injectable, SCOPE } from '../src/injector/injectable/decorator.ts';
import { Module, ModuleOptions } from '../src/module/decorator.ts';
import { Controller, Get, Post } from '../src/router/controller/decorator.ts';
Expand All @@ -22,6 +21,11 @@ Deno.test('Injection', async (testContext) => {
id: string;
}

interface ConfigurationObject {
name: string;
port: number;
}

@Injectable()
class GlobalGuard implements AuthGuard {
canActivate(context: HttpContext): boolean {
Expand Down Expand Up @@ -81,11 +85,13 @@ Deno.test('Injection', async (testContext) => {
constructor(
public child2: GlobalInjectable,
@Inject('DB_SERVICE') public dbService: IDBService,
@Inject('CONFIGURATION') public injectedPlainObject: ConfigurationObject,
) {
}

@Get('')
getMethod() {
return `${this.injectedPlainObject.name} and ${this.injectedPlainObject.port}`;
}

@Post('/post/')
Expand All @@ -100,7 +106,17 @@ Deno.test('Injection', async (testContext) => {
controllers: [SingletonController],
injectables: [
GlobalInjectable,
new TokenInjector(DatabaseService, 'DB_SERVICE'),
{
token: 'DB_SERVICE',
useClass: DatabaseService,
},
{
token: 'CONFIGURATION',
useValue: {
name: 'toto',
port: '4000',
},
},
],
};
}
Expand All @@ -112,7 +128,10 @@ Deno.test('Injection', async (testContext) => {
injectables: [
Child1,
GlobalInjectable,
new TokenInjector(GlobalGuard, GLOBAL_GUARD),
{
token: GLOBAL_GUARD,
useClass: GlobalGuard,
},
],
};

Expand All @@ -134,7 +153,7 @@ Deno.test('Injection', async (testContext) => {
assertRejects(() => failingApp.init(ModuleWithMissingProvider));
},
);

const app = new DanetApplication();
await app.init(FirstModule);

Expand Down Expand Up @@ -183,5 +202,13 @@ Deno.test('Injection', async (testContext) => {
assertInstanceOf(globalGuard, GlobalGuard);
});


await testContext.step(
'inject plain object when using useValue',
async () => {
const firstInstance = await app.get<SingletonController>(
SingletonController,
)!;
assertEquals(firstInstance.getMethod(), 'toto and 4000');
},
);
});
8 changes: 2 additions & 6 deletions spec/lifecycle-hook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Module } from '../src/module/decorator.ts';
import { Controller } from '../src/router/controller/decorator.ts';

Deno.test('Lifecycle hooks', async (testContext) => {

let moduleOnAppBootstrapCalled = false;

@Injectable({ scope: SCOPE.GLOBAL })
Expand Down Expand Up @@ -45,7 +44,6 @@ Deno.test('Lifecycle hooks', async (testContext) => {
],
})
class MyModule implements OnAppBootstrap {

onAppBootstrap(): void | Promise<void> {
moduleOnAppBootstrapCalled = true;
}
Expand All @@ -62,11 +60,9 @@ Deno.test('Lifecycle hooks', async (testContext) => {
},
);

await testContext.step('call module onAppBoostrap hook',
() => {
await testContext.step('call module onAppBoostrap hook', () => {
assertEquals(moduleOnAppBootstrapCalled, true);
}
)
});

await testContext.step(
'call global controller onAppBootstrap hook',
Expand Down
6 changes: 4 additions & 2 deletions spec/scoped-lifecycle-hook-another-order.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Module } from '../src/module/decorator.ts';
import { Controller, Get } from '../src/router/controller/decorator.ts';
import { HttpContext } from '../src/router/router.ts';
import { Inject } from '../src/injector/decorator.ts';
import { TokenInjector } from '../src/injector/injectable/constructor.ts';

Deno.test('Scoped Lifecycle hooks other order', async (testContext) => {
interface ScopedInjectableInterface {
Expand Down Expand Up @@ -59,7 +58,10 @@ Deno.test('Scoped Lifecycle hooks other order', async (testContext) => {
@Module({
controllers: [ScopedController, SideEffectController],
injectables: [
new TokenInjector(ScopedInjectable, 'SCOPED_TOKEN'),
{
token: 'SCOPED_TOKEN',
useClass: ScopedInjectable,
},
InjectableUsingScoped,
],
})
Expand Down
6 changes: 4 additions & 2 deletions spec/scoped-lifecycle-hook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Module } from '../src/module/decorator.ts';
import { Controller, Get } from '../src/router/controller/decorator.ts';
import { HttpContext } from '../src/router/router.ts';
import { Inject } from '../src/injector/decorator.ts';
import { TokenInjector } from '../src/injector/injectable/constructor.ts';

Deno.test('Scoped Lifecycle hooks', async (testContext) => {
interface ScopedInjectableInterface {
Expand Down Expand Up @@ -60,7 +59,10 @@ Deno.test('Scoped Lifecycle hooks', async (testContext) => {
controllers: [ScopedController, SideEffectController],
injectables: [
InjectableUsingScoped,
new TokenInjector(ScopedInjectable, 'SCOPED_TOKEN'),
{
useClass: ScopedInjectable,
token: 'SCOPED_TOKEN',
},
],
})
class ParentBeforeScopedModule {}
Expand Down
27 changes: 15 additions & 12 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,24 @@ export class DanetApplication {
// deno-lint-ignore no-explicit-any
const possibleModuleInstance = ModuleInstanceOrConstructor as any;
let instance: ModuleInstance;

if (!(possibleModuleInstance).imports
&& !(possibleModuleInstance).injectables) {
instance = new (ModuleInstanceOrConstructor as Constructor)() as ModuleInstance;
const metadata: ModuleOptions = MetadataHelper.getMetadata<ModuleOptions>(
moduleMetadataKey,
ModuleInstanceOrConstructor,
);
instance.controllers = metadata.controllers;
instance.imports = metadata.imports;
instance.injectables = metadata.injectables;

if (
!possibleModuleInstance.imports &&
!possibleModuleInstance.injectables
) {
instance =
new (ModuleInstanceOrConstructor as Constructor)() as ModuleInstance;
const metadata: ModuleOptions = MetadataHelper.getMetadata<ModuleOptions>(
moduleMetadataKey,
ModuleInstanceOrConstructor,
);
instance.controllers = metadata.controllers;
instance.imports = metadata.imports;
instance.injectables = metadata.injectables;
} else {
instance = ModuleInstanceOrConstructor as ModuleInstance;
}

for (const module in instance?.imports) {
// deno-lint-ignore no-explicit-any
await this.bootstrap(instance.imports[module as any]);
Expand Down
6 changes: 4 additions & 2 deletions src/events/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ export class EventEmitterModule implements OnAppBootstrap, OnAppClose {
emitter.unsubscribe();
}

// deno-lint-ignore no-explicit-any
// deno-lint-ignore no-explicit-any
private registerAvailableEventListeners(injectableInstance: any) {
const methods = Object.getOwnPropertyNames(injectableInstance.constructor.prototype);
const methods = Object.getOwnPropertyNames(
injectableInstance.constructor.prototype,
);
const emitter = injector.get<EventEmitter>(EventEmitter);

for (const method of methods) {
Expand Down
13 changes: 9 additions & 4 deletions src/injector/injectable/constructor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { Constructor } from '../../utils/constructor.ts';

export type InjectableConstructor = Constructor;
export class TokenInjector {
constructor(public useClass: InjectableConstructor, public token: string) {
}
}
export type UseClassInjector = {
useClass: InjectableConstructor;
token: string;
};
export type UseValueInjector = {
// deno-lint-ignore no-explicit-any
useValue: any;
token: string;
};
Loading
Loading