Skip to main content

Nest.js SDK Getting Started

Npm package version GitHub

To use the DevCycle Server SDK in your project, import the DevCycleModule from the @devcycle/nestjs-server-sdk. We recommend adding the module to the imports of your root app module, so that the DevCycle client is available globally within your application.

Example:

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

DevCycleModule.forRoot({
key: '<DEVCYCLE_SERVER_SDK_KEY>'
})

You may also use forRootAsync if you would like to use a factory to inject dependencies. For example, using Nest's ConfigService to populate the SDK key:

import { ConfigService } from '@nestjs/config'

DevCycleModule.forRootAsync({
useFactory: (config: ConfigService) => ({
key: config.get('DEVCYCLE_SERVER_SDK_KEY'),
}),
inject: [ConfigService]
}),

User Factory

To use the decorators provided by the SDK, you will need to define a userFactory when registering the DevCycleModule. The userFactory is a function which accepts the current ExecutionContext as a parameter and returns a DevCycle User object. The user factory will be evaluated as a global interceptor, and the resulting user will be used when evaluating variables with the @VariableValue and @RequireVariableValue decorators.

DevCycleUser Typescript Schema

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

DevCycleModule.forRoot({
key: '<DEVCYCLE_SERVER_SDK_KEY>',
userFactory: (context: ExecutionContext) => {
// Example building a user object based on the request context
const req = context.switchToHttp().getRequest()
return {
user_id: req.user.id,
email: req.user.email,
}
}
})

Initialization Options

The SDK exposes various initialization options which can be set when registering the DevCycleModule:

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

DevCycleModule.forRoot({
key: '<DEVCYCLE_SERVER_SDK_KEY>',
options: {
// Insert Options
}
})
DevCycle OptionTypeDescription
loggerDevCycleLoggerLogger override to replace default logger
logLevelStringSet log level of the default logger. Options are: debug, info, warn, error. Defaults to info.
enableCloudBucketingBooleanSwitches the SDK to use Cloud Bucketing (via the DevCycle Bucketing API) instead of Local Bucketing.
enableEdgeDBBooleanEnables the usage of EdgeDB for DevCycle that syncs User Data to DevCycle.
NOTE: This is only available with Cloud Bucketing.
configPollingIntervalMSNumberControls the polling interval in milliseconds to fetch new environment config changes, defaults to 10 seconds, minimum value is 1 second.
configPollingTimeoutMSNumberControls the request timeout to fetch new environment config changes, defaults to 5 seconds, must be less than the configPollingIntervalMS value, minimum value is 1 second.
eventFlushIntervalMSNumberControls the interval between flushing events to the DevCycle servers, defaults to 30 seconds.
disableAutomaticEventLoggingBooleanDisables logging of sdk generated events (e.g. aggVariableEvaluated, aggVariableDefaulted) to DevCycle.
disableCustomEventLoggingBooleanDisables logging of custom events, from track() method, and user data to DevCycle.
flushEventQueueSizeNumberControls the maximum size the event queue can grow to until a flush is forced. Defaults to 1000.
maxEventQueueSizeNumberControls the maximum size the event queue can grow to until events are dropped. Defaults to 2000.
apiProxyURLStringAllows the SDK to communicate with a proxy of DevCycle bucketing API / client SDK API.
enableBetaRealtimeUpdatesBooleanEnables the usage of Beta Realtime Updates for DevCycle. This feature is currently in beta.