Feature Flags
To use the FeatureFlag module you first need to load its configuration in your app module and make sure CONFIGCAT_SDK_KEY is set in your production environment.
import { Module } from '@nestjs/common'
import { ConfigModule } from '@island.is/nest/config'
import { FeatureFlagConfig } from '@island.is/nest/feature-flags'
â
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
load: [FeatureFlagConfig],
}),
],
})
export class AppModule {}
Then you can import it anywhere you need one of its exports:
import { Module } from '@nestjs/common'
import { FeatureFlagModule } from '@island.is/nest/feature-flags'
â
@Module({
imports: [FeatureFlagModule],
})
export class SomeModule {}
The service wraps getValue with support for our server-side User objects for checking if they have access to a feature.
import { FeatureFlagService, Features } from '@island.is/nest/feature-flags'
import { User } from '@island.is/auth-nest-tools'
â
export class YourService {
constructor(private readonly featureFlagService: FeatureFlagService) {}
â
async maybeDoSomething(user: User) {
const canDoSomething = await this.featureFlagService.getValue(
Features.someFeature,
false,
user,
)
â
if (canDoIt) {
return this.doSomething()
}
}
}
If your feature has new API endpoints, you can use the FeatureFlagGuard to disable them completely:
import { Controller, Get, UseGuards } from '@nestjs/common'
import {
FeatureFlagGuard,
FeatureFlag,
Features,
} from '@island.is/nest/feature-flags'
â
@UseGuards(IdsUserGuard, FeatureFlagGuard)
@Controller('some')
export class SomeController {
@Get()
@FeatureFlag(Features.someFeature)
getSomething() {
// Will only reach here if someFeature is turned on, either globally or for the authenticated user.
}
}
The guard works both for GraphQL resolvers as well as REST controllers. You can also feature flag an entire resolver or controller:
import { Controller, Get, UseGuards } from '@nestjs/common'
import {
FeatureFlagGuard,
FeatureFlag,
Features,
} from '@island.is/nest/feature-flags'
â
@UseGuards(IdsUserGuard, FeatureFlagGuard)
@FeatureFlag(Features.someFeature)
@Controller('some')
export class SomeController {
@Get()
getSomething() {
// Will only reach here if someFeature is turned on, either globally or for the authenticated user.
}
}
The FeatureFlagGuard MUST be listed after IdsUserGuard in the
@UseGuards()
decorator if you want it to support user-specific feature flags.If you need access to the low level client and getValue functions, you can dependency inject it like this:
import { Inject } from '@nestjs/common'
import {
FeatureFlagClient,
FEATURE_FLAG_CLIENT,
} from '@island.is/nest/feature-flags'
â
export class YourService {
constructor(
@Inject(FEATURE_FLAG_CLIENT)
private readonly client: FeatureFlagClient,
) {}
}
Last modified 1yr ago