@josemarinho/unleash
v2.0.0
Published
NestJS library integrate with Unleash
Readme
Description
Nest Lib To Communicate With Unleash Feature Toggle.
Installation
$ npm install @josemarinho/unleashRunning the app
Once the installation process is complete, we can import the UnleashModule into the root AppModule.
import { Module } from '@nestjs/common';
import { UnleashModule } from '@josemarinho/unleash';
@Module({
imports: [
UnleashModule.forRoot({
appName: 'app-name',
url: 'https://unleash-api-url.com',
apiKey: 'your-api-key',
strategies: [new ActiveForUserWithEmailStrategy()], // Your custom strategy
bootstrap: {
data: [
{
name: 'feature-x',
enabled: true,
strategies: [],
},
],
disableBootstrapOverride: true,
},
global: false, // default is true
}),
],
})
export class AppModule {}
Custom Strategies
You can define and use custom strategies by extending the Strategy class from unleash-client. Example from Docs Unleash Client - https://docs.getunleash.io/reference/sdks/node#custom-strategies
import { initialize, Strategy } from 'unleash-client';
class ActiveForUserWithEmailStrategy extends Strategy {
constructor() {
super('ActiveForUserWithEmail');
}
isEnabled(parameters, context) {
return parameters.emails.indexOf(context.email) !== -1;
}
}
Utilization
To retrieve feature toggles from Unleash, simply inject the UnleashService into your service or controller and use the available methods.
✅ Checking if a feature is enabled
import { Injectable } from '@nestjs/common';
import { UnleashService } from '@josemarinho/unleash';
@Injectable()
export class AppService {
constructor(private readonly unleash: UnleashService) {}
isFeatureEnabled(toggleName: string): boolean {
return this.unleash.isEnabled(toggleName);
}
}🔍 Accessing the Unleash client directly (optional)
If you need to access advanced methods like getVariant, you can retrieve the internal Unleash client instance:
getFeatureVariant(toggleName: string) {
const client = this.unleash.getClient();
return client.getVariant(toggleName);
}The UnleashService also handles cleanup automatically on application shutdown to avoid memory leaks.
After your app it's ready to running.
