Skip to content

Commit

Permalink
Merge pull request #7 from Karibash/feature/option-def-separate
Browse files Browse the repository at this point in the history
Make the type definitions of settings be split and defined separately
  • Loading branch information
Karibash authored Jun 28, 2022
2 parents 399a0f1 + 720b177 commit 5f2e30f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 42 deletions.
5 changes: 5 additions & 0 deletions .changeset/dull-ants-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nestjs-modular/inject": patch
---

Make the type definitions of settings be split and defined separately
21 changes: 9 additions & 12 deletions examples/inject/src/modules/core/core.module.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
import { Global, Module } from '@nestjs/common';
import { InjectModule } from '@nestjs-modular/inject';
import { InjectConditions, InjectModule } from '@nestjs-modular/inject';
import path from 'path';

const isMock = process.env.APP_ENV === 'mock';
const repositoryType = isMock ? 'mock' : 'inmemory';

const repositories: InjectConditions = {
path: path.resolve(__dirname, `../../infrastructures/repositories/${isMock ? 'mock' : 'inmemory'}`),
includeFileNames: [/\.repository$/],
includeExportNames: [/RepositoryProvider$/],
};

@Global()
@Module({
imports: [
InjectModule.forRootAsync({
global: true,
providers: {
path: path.resolve(__dirname, `../../infrastructures/repositories/${repositoryType}`),
includeFileNames: [/\.repository$/],
includeExportNames: [/RepositoryProvider$/],
},
exports: {
path: path.resolve(__dirname, `../../infrastructures/repositories/${repositoryType}`),
includeFileNames: [/\.repository$/],
includeExportNames: [/RepositoryProvider$/],
},
providers: repositories,
exports: repositories,
}),
],
})
Expand Down
20 changes: 9 additions & 11 deletions examples/inject/src/modules/tasks/tasks.module.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import { Module } from '@nestjs/common';
import { InjectModule } from '@nestjs-modular/inject';
import { InjectConditions, InjectModule } from '@nestjs-modular/inject';
import path from 'path';

import { TasksController } from './tasks.controller';

const useCases: InjectConditions = {
path: path.resolve(__dirname, './usecases'),
includeFileNames: [/\.usecase$/],
includeExportNames: [/UseCase$/],
};

@Module({
imports: [
InjectModule.forRootAsync({
providers: {
path: path.resolve(__dirname, './usecases'),
includeFileNames: [/\.usecase$/],
includeExportNames: [/UseCase$/],
},
exports: {
path: path.resolve(__dirname, './usecases'),
includeFileNames: [/\.usecase$/],
includeExportNames: [/UseCase$/],
},
providers: useCases,
exports: useCases,
}),
],
controllers: [
Expand Down
20 changes: 9 additions & 11 deletions examples/inject/src/modules/users/users.module.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import { Module } from '@nestjs/common';
import { InjectModule } from '@nestjs-modular/inject';
import { InjectConditions, InjectModule } from '@nestjs-modular/inject';
import path from 'path';

import { UsersController } from './users.controller';

const useCases: InjectConditions = {
path: path.resolve(__dirname, './usecases'),
includeFileNames: [/\.usecase$/],
includeExportNames: [/UseCase$/],
};

@Module({
imports: [
InjectModule.forRootAsync({
providers: {
path: path.resolve(__dirname, './usecases'),
includeFileNames: [/\.usecase$/],
includeExportNames: [/UseCase$/],
},
exports: {
path: path.resolve(__dirname, './usecases'),
includeFileNames: [/\.usecase$/],
includeExportNames: [/UseCase$/],
},
providers: useCases,
exports: useCases,
}),
],
controllers: [
Expand Down
18 changes: 10 additions & 8 deletions packages/inject/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const someTests = (value: string, terms: Array<string | RegExp>, _default: boole
});
};

const getInjectables = async <T>(conditions?: InjectConditions<T[]>): Promise<T[]> => {
const getInjectables = async <T>(conditions?: InjectOptions<T[]>): Promise<T[]> => {
const mergedConditions = {
injects: [],
includeFileNames: [],
Expand Down Expand Up @@ -71,9 +71,7 @@ const getInjectables = async <T>(conditions?: InjectConditions<T[]>): Promise<T[
return [...injectables, ...mergedConditions.injects];
};

export type InjectConditions<T> = {
injects?: T;
} & {
export type InjectConditions = {
path: string;
includeFileNames?: Array<string | RegExp>;
excludeFileNames?: Array<string | RegExp>;
Expand All @@ -83,11 +81,15 @@ export type InjectConditions<T> = {
excludeExportNames?: Array<string | RegExp>;
};

export type InjectOptions<T> = InjectConditions & {
injects?: T;
};

export type InjectModuleOptions = Pick<DynamicModule, 'global'> & {
imports?: InjectConditions<Exclude<ModuleMetadata['imports'], undefined>>;
controllers?: InjectConditions<Exclude<ModuleMetadata['controllers'], undefined>>;
providers?: InjectConditions<Exclude<ModuleMetadata['providers'], undefined>>;
exports?: InjectConditions<Exclude<ModuleMetadata['exports'], undefined>>;
imports?: InjectOptions<Exclude<ModuleMetadata['imports'], undefined>>;
controllers?: InjectOptions<Exclude<ModuleMetadata['controllers'], undefined>>;
providers?: InjectOptions<Exclude<ModuleMetadata['providers'], undefined>>;
exports?: InjectOptions<Exclude<ModuleMetadata['exports'], undefined>>;
};

@Module({})
Expand Down

0 comments on commit 5f2e30f

Please sign in to comment.