Skip to content

Commit

Permalink
feat(app): allow normalized providers to be exported in modules (#425)
Browse files Browse the repository at this point in the history
Currently, it's only possible to export interface providers using `this.addExport(provider)` in modules, where `provider` refers to an interface provider.
This aims to try to solve that by allowing `NormalizedProvider`s to be exported.
  • Loading branch information
marcus-sa committed Oct 1, 2023
1 parent e90f02c commit 855a185
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/app/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* You should have received a copy of the MIT License along with this program.
*/

import { InjectorModule, ProviderWithScope, Token } from '@deepkit/injector';
import { InjectorModule, ProviderWithScope, Token, NormalizedProvider } from '@deepkit/injector';
import { AbstractClassType, ClassType, CustomError, ExtractClassType, isClass } from '@deepkit/core';
import { EventListener, EventToken } from '@deepkit/event';
import { WorkflowDefinition } from '@deepkit/workflow';
Expand All @@ -23,7 +23,7 @@ export interface MiddlewareConfig {

export type MiddlewareFactory = () => MiddlewareConfig;

export type ExportType = AbstractClassType | string | AppModule<any> | Type;
export type ExportType = AbstractClassType | string | AppModule<any> | Type | NormalizedProvider;

/**
* @reflection never
Expand Down
18 changes: 17 additions & 1 deletion packages/app/tests/module.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, test } from '@jest/globals';
import { Minimum, MinLength } from '@deepkit/type';
import { injectorReference } from '@deepkit/injector';
import { injectorReference, provide } from '@deepkit/injector';
import { ServiceContainer } from '../src/service-container.js';
import { ClassType } from '@deepkit/core';
import { AppModule, createModule } from '../src/module.js';
Expand Down Expand Up @@ -248,6 +248,22 @@ test('same module loaded twice', () => {
}
});

test('interface provider can be exported', () => {
interface Test {}

const TEST = {};

const Test = provide<Test>({ useValue: TEST });

const test = new AppModule({ providers: [Test], exports: [Test] });

const app = new AppModule({ imports: [test] });

const serviceContainer = new ServiceContainer(app);

serviceContainer.getInjector(app).get<Test>().toBe(TEST);
});

test('non-exported providers can not be overwritten', () => {
class SubClass {
}
Expand Down

0 comments on commit 855a185

Please sign in to comment.