Skip to content

Instantly share code, notes, and snippets.

@nordfjord
Created October 15, 2022 17:48
Show Gist options
  • Save nordfjord/df230bde2cc6cc4f6aa19eead03d21bc to your computer and use it in GitHub Desktop.
Save nordfjord/df230bde2cc6cc4f6aa19eead03d21bc to your computer and use it in GitHub Desktop.
Nestjs Configcat
import { ConfigCatClient } from 'configcat-common/lib/ConfigCatClient';
@Controller('travel')
export class AppController {
constructor(
private readonly appService: AppService,
private readonly flags: ConfigCatClient
) {}
@Get('mileage')
async getMileageFeature(): Promise<Boolean> {
// Create a variable to store the state of the feature flag from ConfigCat.
// This variable will be automatically updated every 60 seconds by default.
const canShowMileageFeature = await this.flags.getValueAsync("canshowmileagefeature", false)
if (canShowMileageFeature) {
// When true is returned to the frontend, the Gas Mileage feature is rendered
return true
} else {
// When false is returned to the frontend, the Gas Mileage feature is not rendered. Instead, the subscription form will be rendered.
return false
}
}
}
import { Global, Module, OnModuleDestroy } from '@nestjs/common';
import { ConfigCatClient } from 'configcat-common/lib/ConfigCatClient';
import * as ConfigCat from 'configcat-node';
const configCatClient = {
provide: ConfigCatClient,
useFactory: () => {
// or any other method to get the key
const key = process.env.CONFIG_CAT_SDK_KEY;
return ConfigCat.createClient(key);
},
};
@Global()
@Module({ providers: [configCatClient], exports: [configCatClient] })
export class ConfigCatModule implements OnModuleDestroy {
constructor(private readonly flags: ConfigCatClient) {}
async onModuleDestroy(): Promise<void> {
this.flags.dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment