5.0.3 • Published 3 months ago

@jbiskur/nestjs-async-module v5.0.3

Weekly downloads
20
License
MIT
Repository
github
Last release
3 months ago

nestjs-async-module

The Async Module is a utility class, that simplifies creating dynamic NestJS modules.

Installation

Install using npm.

npm install --save-dev @jbiskur/nestjs-async-module

using yarn.

yarn add --dev @jbiskur/nestjs-async-module

Usage

The library provides a utility class and an options factory

//...nestjs imports
import { AsyncModule, AsyncOptions } from "@jbiskur/nestjs-async-module";

// interface for the module
interface ExampleOptions {
  name: string;
}

@Module({
  imports: [],
  controllers: [],
  providers: [],
  exports: [],
})
class ExampleAsyncModule extends createAsyncModule<ExampleOptions>() {}

to then use the module you just use the register async and then typescript uses the interface for intellisense.

@module({
  imports: [
    ExampleAsyncModule.registerAsync(
      {
        useFactory: () => ({
          name: "hello world",
        }),
      },
      ExampleAsyncModule
    ),
  ],
})
export class AppModule {}

Accessing Options

To access options registered with this dynamic module can be done with the following:

@Injectable()
class ExampleService {
  constructor(private readonly options: ModuleOptions<ExampleOptions>) {}

  testOptions() {
    return this.options.get().name;
  }
}

The ModuleOptions service discovers all tokens noted with uppercase _OPTIONS within the current module, and they are flattened into the return of the get() method. TypeScript is informed via the template parameter ModuleOptions<TOptions>. These options are not validated on retrieval.

Facilitate Injecting Services

another use-case is to purely use it to facilitate injecting services to dynamic modules.

//...nestjs imports
import { AsyncModule, AsyncOptions } from "@jbiskur/nestjs-async-module";

@Module({
  imports: [],
  controllers: [],
  providers: [],
  exports: [],
})
class ExampleAsyncModule extends createAsyncModule() {}

using it is then as simple as the following.

@module({
  imports: [
    ExampleAsyncModule.registerAsync(
      {
        imports: [SomeModule],
        inject: [AService],
      },
      ExampleAsyncModule
    ),
  ],
})
export class AppModule {}
5.0.3

3 months ago

5.0.2

6 months ago

5.0.1

11 months ago

5.0.0

11 months ago

4.1.0

1 year ago

3.12.1

1 year ago

3.12.0

1 year ago

4.0.0

1 year ago

4.0.2

1 year ago

3.12.2

1 year ago

3.11.0

1 year ago

3.13.0

1 year ago

3.9.0

2 years ago

3.10.1

2 years ago

3.8.0

2 years ago

3.10.0

2 years ago

3.10.2

2 years ago

3.7.0

2 years ago

3.6.0

2 years ago

3.5.0

2 years ago

3.4.0

2 years ago

3.3.1

2 years ago

3.2.2

2 years ago

3.3.0

2 years ago

3.2.1

2 years ago

3.2.5

2 years ago

3.2.4

2 years ago

3.4.1

2 years ago

3.2.3

2 years ago

3.1.1

3 years ago

3.1.0

3 years ago

3.0.0

3 years ago

2.4.1

3 years ago

2.4.2

3 years ago

2.3.0

3 years ago

2.4.0

3 years ago

2.3.1

3 years ago

2.2.1

3 years ago

2.2.2

3 years ago

2.1.6

3 years ago

2.1.5

3 years ago

2.1.4

3 years ago

2.1.3

3 years ago

2.1.2

3 years ago

2.1.1

3 years ago

2.1.0

3 years ago

1.1.5

3 years ago

2.0.0

3 years ago

1.1.4

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.2

3 years ago