Skip to content

Commit

Permalink
fix: transform config to have proper Date objects
Browse files Browse the repository at this point in the history
  • Loading branch information
jsalaber committed Dec 2, 2024
1 parent cb102c0 commit 969a966
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 12 deletions.
11 changes: 6 additions & 5 deletions dev-apps/nextjs/app-router/app/devcycle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ const getUserIdentity = async () => {
}
}

const { getVariableValue, getClientContext } = setupDevCycle(
const { getVariableValue, getClientContext } = setupDevCycle({
serverSDKKey: process.env.NEXT_PUBLIC_DEVCYCLE_SERVER_SDK_KEY ?? '',
// SDK Key. This will be public and sent to the client, so you MUST use the client SDK key.
process.env.NEXT_PUBLIC_DEVCYCLE_CLIENT_SDK_KEY ?? '',
clientSDKKey: process.env.NEXT_PUBLIC_DEVCYCLE_CLIENT_SDK_KEY ?? '',
// pass your method for getting the user identity
getUserIdentity,
userGetter: getUserIdentity,
// pass any options you want to use for the DevCycle SDK
{
options: {
enableStreaming: process.env.NEXT_PUBLIC_ENABLE_STREAMING === '1',
},
)
})

export { getVariableValue, getClientContext, getUserIdentity }
14 changes: 10 additions & 4 deletions lib/shared/bucketing/__tests__/bucketing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1419,7 +1419,9 @@ describe('Rollout Logic', () => {
it('should handle stepped rollout with 0% start and 100% later stage', () => {
const rollout: PublicRollout = {
type: 'stepped',
startDate: new Date(),
startDate: new Date(
new Date().getTime() - 1000 * 60 * 60 * 24 * 7,
),
startPercentage: 0,
stages: [
{
Expand Down Expand Up @@ -1453,10 +1455,12 @@ describe('Rollout Logic', () => {
jest.useRealTimers()
})

it('should handle stepped rollout with 50% start and 100% later stage', () => {
it.only('should handle stepped rollout with 50% start and 100% later stage', () => {
const rollout: PublicRollout = {
type: 'stepped',
startDate: new Date(),
startDate: new Date(
new Date().getTime() - 1000 * 60 * 60 * 24 * 7,
),
startPercentage: 0.5,
stages: [
{
Expand Down Expand Up @@ -1494,7 +1498,9 @@ describe('Rollout Logic', () => {
it('should handle stepped rollout with 100% start and 0% later stage', () => {
const rollout: PublicRollout = {
type: 'stepped',
startDate: new Date(),
startDate: new Date(
new Date().getTime() - 1000 * 60 * 60 * 24 * 7,
),
startPercentage: 1,
stages: [
{
Expand Down
2 changes: 1 addition & 1 deletion lib/shared/bucketing/src/bucketing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export const getCurrentRolloutPercentage = (
: null)

if (!currentStage) {
return start
return 0
}

if (!nextStage || nextStage.type === 'discrete') {
Expand Down
40 changes: 40 additions & 0 deletions sdk/nextjs/src/common/transformConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { ConfigBody } from '@devcycle/types'

/**
* Transform the config to ensure all dates are valid Date objects
* @param configData
* @returns
*/
export const transformConfig = (configData: ConfigBody): ConfigBody => {
if (!configData.features || !configData.features.length) return configData

configData.features = configData.features.map((feature) => {
if (!feature || !feature.configuration.targets.length) return feature

feature.configuration.targets = feature.configuration.targets.map(
(target) => {
const { rollout } = target
if (!rollout) return target

rollout.startDate = transformDate(rollout.startDate)
if (!rollout.stages || !rollout.stages.length) return target

rollout.stages = rollout.stages.map((stage) => {
stage.date = transformDate(stage.date)
return stage
})
return target
},
)
return feature
})
return configData
}

const transformDate = (date: unknown): Date => {
if (!date) throw new Error('Missing rollout date in config')
if (date instanceof Date) return date
if (typeof date === 'string' || typeof date === 'number')
return new Date(date)
throw new Error('Missing rollout date in config')
}
3 changes: 2 additions & 1 deletion sdk/nextjs/src/pages/bucketing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { DevCycleUser, DVCPopulatedUser } from '@devcycle/js-client-sdk'
import { generateBucketedConfig } from '@devcycle/bucketing'
import { BucketedUserConfig, ConfigBody, ConfigSource } from '@devcycle/types'
import { fetchCDNConfig, sdkConfigAPI } from './requests.js'
import { transformConfig } from '../common/transformConfig.js'

class CDNConfigSource extends ConfigSource {
async getConfig(
Expand Down Expand Up @@ -52,7 +53,7 @@ const bucketOrFetchConfig = async (

return generateBucketedConfig({
user,
config,
config: transformConfig(config),
})
}

Expand Down
6 changes: 5 additions & 1 deletion sdk/nextjs/src/server/bucketing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
DevCycleNextOptions,
} from '../common/types'
import { ConfigBody, ConfigSource } from '@devcycle/types'
import { transformConfig } from '../common/transformConfig'

const getPopulatedUser = cache((user: DevCycleUser, userAgent?: string) => {
return new DVCPopulatedUser(
Expand Down Expand Up @@ -48,7 +49,10 @@ const generateBucketedConfigCached = cache(

return {
bucketedConfig: {
...generateBucketedConfig({ user: populatedUser, config }),
...generateBucketedConfig({
user: populatedUser,
config: transformConfig(config),
}),
clientSDKKey,
sse: {
url: config.sse
Expand Down

0 comments on commit 969a966

Please sign in to comment.