Skip to main content

Nest.js SDK Usage

Npm package version GitHub

DevCycle Client

With the DevCycleModule imported, the DevCycleClient can be injected into your controllers or providers.

The Nest.js SDK is a wrapper for DevCycle's Node.js SDK. For more information about methods available on the DevCycleClient, see the Node.js Usage documentation.

import { DevCycleClient } from '@devcycle/nestjs-server-sdk'

export class MyController {
constructor(
private readonly devcycleClient: DevCycleClient
) {}

async update() {
const user = {
user_id: 'user1@devcycle.com',
name: 'user 1 name',
customData: {
customKey: 'customValue',
},
}
const variable = this.devcycleClient.variable(user, 'test-variable', false)
}
}

DevCycle Service

With the DevCycleModule imported, the DevCycleService can be injected into your controllers or providers. The DevCycleService methods evaluate variables with the user returned from your userFactory, so you don't need to specify a user each time a method is called.

import { DevCycleService } from '@devcycle/nestjs-server-sdk'

export class MyService {
constructor(
private readonly devcycleService: DevCycleService,
) {}

async update() {
const enabled = this.devcycleService.isEnabled('allow-feature-edits')
if (enabled) {
// do something
}
}
}

variableValue

The variableValue method accepts a variable key and default value, and returns the served value.

const value = this.devcycleService.variableValue('variable-key', 'hello world')

isEnabled

The isEnabled method accepts a key for a boolean variable. The default value is always false when using the isEnabled method.

const enabled = this.devcycleService.isEnabled('boolean-variable')

getUser

The getUser method returns the user object from your userFactory.

const devcycleUser = this.devcycleService.getUser()

Decorators

DevCycle decorators evaluate variables with the user returned from your userFactory, so you don't need to specify a user each time a decorator is used.

VariableValue

The VariableValue decorator can be used to access variable values directly in your route handlers.

async findAll(
@VariableValue({ key: 'test-variable', default: false }) testValue: boolean,
) {
if (testValue) {
// do something
}
}

RequireVariableValue

The RequireVariableValue decorator can be used to guard an endpoint or controller. If the user is not served the specified value, the request will return a 404 NotFound as though the endpoint does not exist.

@RequireVariableValue({
'test-variable': true
})
async findAll() {
...
}

Enabling Beta Realtime Updates

warning

This feature is in beta, and may not function as expected. Please ensure that you have the latest version of the SDK.

This functionality will reduce the number of polling requests that are made to the DevCycle Config CDN, and instead will use a long-lived HTTP connection (Server Sent Events) to receive updates when there is a new config available. This reduces outbound network traffic, as well as optimizes the SDK for efficiency.

To enable Beta Realtime Updates, pass in the enableBetaRealtimeUpdates option to the SDK initialization:

import { DevCycleModule } from '@devcycle/nestjs-server-sdk'

DevCycleModule.forRoot({
key: '<DEVCYCLE_SERVER_SDK_KEY>',
options: {
enableBetaRealtimeUpdates: true
}
})