5.1.1 • Published 8 months ago

@jbiskur/nestjs-async-module v5.1.1

Weekly downloads
20
License
MIT
Repository
github
Last release
8 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.1.1

8 months ago

5.1.0

8 months ago

5.0.3

1 year ago

5.0.2

2 years ago

5.0.1

2 years ago

5.0.0

2 years ago

4.1.0

2 years ago

3.12.1

2 years ago

3.12.0

2 years ago

4.0.0

2 years ago

4.0.2

2 years ago

3.12.2

2 years ago

3.11.0

2 years ago

3.13.0

2 years ago

3.9.0

3 years ago

3.10.1

3 years ago

3.8.0

3 years ago

3.10.0

3 years ago

3.10.2

3 years ago

3.7.0

3 years ago

3.6.0

3 years ago

3.5.0

3 years ago

3.4.0

4 years ago

3.3.1

4 years ago

3.2.2

4 years ago

3.3.0

4 years ago

3.2.1

4 years ago

3.2.5

4 years ago

3.2.4

4 years ago

3.4.1

4 years ago

3.2.3

4 years ago

3.1.1

4 years ago

3.1.0

4 years ago

3.0.0

4 years ago

2.4.1

4 years ago

2.4.2

4 years ago

2.3.0

4 years ago

2.4.0

4 years ago

2.3.1

4 years ago

2.2.1

4 years ago

2.2.2

4 years ago

2.1.6

4 years ago

2.1.5

4 years ago

2.1.4

4 years ago

2.1.3

4 years ago

2.1.2

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

1.1.5

4 years ago

2.0.0

4 years ago

1.1.4

4 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.2

5 years ago