Table of contents
Usage
$ npm install --save nestjs-unleash
Import the module with UnleashModule.forRoot(...)
or UnleashModule.forRootAsync(...)
.
Synchronous configuration
Use UnleashModule.forRoot()
. Available ptions are described in the UnleashModuleOptions interface.
@Module({
imports: [
UnleashModule.forRoot({
url: "https://example.com/unleash",
appName: "my-app-name",
instanceId: "my-unique-instance",
}),
],
})
export class MyModule {}
Asynchronous configuration
If you want to use retrieve you Unleash options dynamically, use UnleashModule.forRootAsync()
. Use useFactory
and inject
to import your dependencies. Example using the ConfigService
:
@Module({
imports: [
UnleashModule.forRootAsync({
useFactory: (config: ConfigService) => ({
url: config.get("UNLEASH_URL"),
appName: config.get("UNLEASH_APP_NAME"),
instanceId: config.get("UNLEASH_INSTANCE_ID"),
refreshInterval: config.get("UNLEASH_REFRESH_INTERVAL"),
metricsInterval: config.get("UNLEASH_METRICS_INTERVAL"),
}),
inject: [ConfigService],
}),
],
})
export class MyModule {}
Usage in controllers or providers
In your controller use the UnleashService
or the @IfEnabled(...)
route decorator:
import { UnleashService } from "nestjs-unleash";
@Controller()
@UseGuards(UserGuard)
export class AppController {
constructor(private readonly unleash: UnleashService) {}
@Get("/")
index(): string {
return this.unleash.isEnabled("test")
? "feature is active"
: "feature is not active";
}
@IfEnabled("test")
@Get("/foo")
getFoo(): string {
return "my foo";
}
}
Configuration
NestJS-Unleash can be configured with the following options:
interface UnleashModuleOptions {
global?: boolean;
url: string;
appName: string;
instanceId: string;
http?: AxiosRequestConfig;
refreshInterval?: number;
metricsInterval?: number;
strategies?: Type<UnleashStrategy>[];
disableRegistration?: boolean;
userIdFactory?: (request: Request<{ id: string }>) => string;
}
Default strategies
This module supports the official standard activation strategies. They do not need to be activated separately and work out of the box.
Custom strategies
In order to create a custom strategy you have to create a class wich inplements the UnleashStrategy
interface:
import { UnleashContext } from "nestjs-unleash";
export interface UnleashStrategy {
name: string;
isEnabled(parameters: unknown, context: UnleashContext): boolean;
}
Example custom strategy:
import { Injectable } from "@nestjs/common";
import { UnleashContext, UnleashStrategy } from "nestjs-unleash";
@Injectable()
export class MyCustomStrategy implements UnleashStrategy {
name = "MyCustomStrategy";
isEnabled(parameters: any, context: UnleashContext): boolean {
return Math.random() < 0.5;
}
}
Now you can use it your module setup as follows:
import { MyCustomStrategy } from "./my-custom-strategy";
@Module({
imports: [
UnleashModule.forRoot({
strategies: [MyCustomStrategy],
}),
],
})
export class ApplicationModule {}
License
nestjs-unleash is distributed under the MIT license. See LICENSE for details.