4.0.2 • Published 1 year ago

@nodiator/nest v4.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

NestJS integration for Nodiator

Utilizes Nest's DI system to manage providers and mediator instances.

Table of contents

Installation

npm i @nodiator/core @nodiator/nest
# or
yarn add @nodiator/core @nodiator/nest

Global Configuration

Nest module allows to manage multiple instances of mediators. Configuration shared by all of them can me defined via

// app.module.ts
import { MediatorModule } from '@nodiator/nest';

@Module({
  imports: [
    MediatorModule.forRoot({
      extensions: [...],
      dynamicOptions: () => ({
        requests: { timeout: 1000 },
      }),
    }),
    CatsModule,
  ],
})
export class AppModule {}

Usage

// cats.controller.ts
import { Mediator } from '@nodiator/nest';

@Controller('cats')
export class CatsController {
  constructor(@InjectMediator() private readonly mediator: Mediator) {}

  @Get()
  getAllCats() {
    return firstValueFrom(this.mediator.request(new GetAllCatsUseCase()));
  }
}
// cats.module.ts
import { MediatorModule } from '@nodiator/nest';

@Module({
  imports: [
    MediatorModule.forFeature(CatsModule, {
      extensions: [...],
      dynamicOptions: () => ({
        requests: { timeout: 1000 },
      }),
    }),
  ],
  providers: [GetAllCatsUseCaseHandler],
  controllers: [CatsController],
})
export class CatsModule {}

Mediator module resolves all providers available in the scope of given module.

Exporting Mediators

Above example shows how to inject local module's mediator. To access instance from another module use

// dogs.module.ts
import { MediatorModule } from '@nodiator/nest';

@Module({
  imports: [MediatorModule.forFeature(DogsModule)],
  exports: [MediatorModule], // export configured dogs mediator
})
export class DogsModule {}
// cats.module.ts
import { MediatorModule } from '@nodiator/nest';

@Module({
  imports: [
    MediatorModule.forFeature(CatsModule),
    DogsModule, // import dogs mediator into cats module
  ],
  providers: [GetAllCatsUseCaseHandler],
  controllers: [CatsController],
})
export class CatsModule {}
import { Mediator } from '@nodiator/nest';

@Controller('cats')
export class CatsController {
  constructor(
    @InjectMediator() private readonly catsMediator: Mediator,
    @InjectMediator(DogsModule) private readonly dogsMediator: Mediator
  ) {}

  ...
}

Custom Namespaces

Mediators can be provided and injected using custom string identifier. It' useful for creating abstraction from module imports dependencies.

// cats.module.ts
import { MediatorModule } from '@nodiator/nest';

@Module({
  imports: [
    MediatorModule.forFeature(CatsModule, {
      namespace: 'CATS_NAMESPACE',
      extensions: [...],
      dynamicOptions: () => ({
        requests: { timeout: 1000 },
      }),
    }),
  ],
  providers: [GetAllCatsUseCaseHandler],
  controllers: [CatsController],
})
export class CatsModule {}
// cats.controller.ts
import { Mediator } from '@nodiator/nest';

@Controller('cats')
export class CatsController {
  constructor(@InjectMediator('CATS_NAMESPACE') private readonly mediator: Mediator) {}

  @Get()
  getAllCats() {
    return firstValueFrom(this.mediator.request(new GetAllCatsUseCase()));
  }
}

Logger

The package exports logger integration if nest module is used in combination with logger extension.

// app.module.ts
import { LoggerExtension } from '@nodiator/extension-logger';
import { MediatorModule, NestMediatorLogger } from '@nodiator/nest';

@Module({
  imports: [
    MediatorModule.forRoot({
      extensions: [new LoggerExtension({ logger: new NestMediatorLogger() })],
    }),
    CatsModule,
  ],
})
export class AppModule {}

License

This project is licensed under the MIT License - see the LICENSE file for details.

4.0.2-dev.3

1 year ago

4.0.2-dev.1

1 year ago

4.0.3-dev.1

1 year ago

4.0.2

1 year ago

4.0.1-dev.1

1 year ago

4.0.1

1 year ago

4.0.0

1 year ago

3.2.2-dev.8

1 year ago

3.2.2-dev.7

1 year ago

3.2.2-dev.5

1 year ago

3.2.1

1 year ago

3.2.0

1 year ago

3.1.1

1 year ago

3.0.2

1 year ago

3.1.0

1 year ago

3.0.1

1 year ago

3.0.0

1 year ago

2.2.1

2 years ago

2.2.0

2 years ago

2.1.1

2 years ago

2.1.0

2 years ago

2.0.0

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago