Skip to content

Commit

Permalink
fix: use plain to instance instead
Browse files Browse the repository at this point in the history
  • Loading branch information
jsalaber committed Dec 3, 2024
1 parent 969a966 commit 2b35b6a
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 49 deletions.
51 changes: 50 additions & 1 deletion lib/shared/bucketing/__tests__/bucketing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1455,7 +1455,7 @@ describe('Rollout Logic', () => {
jest.useRealTimers()
})

it.only('should handle stepped rollout with 50% start and 100% later stage', () => {
it('should handle stepped rollout with 50% start and 100% later stage', () => {
const rollout: PublicRollout = {
type: 'stepped',
startDate: new Date(
Expand Down Expand Up @@ -1533,6 +1533,55 @@ describe('Rollout Logic', () => {

jest.useRealTimers()
})

it('should handle stepped rollout with a future start date', () => {
const rollout: PublicRollout = {
type: 'stepped',
startDate: new Date(
new Date().getTime() + 1000 * 60 * 60 * 24 * 7,
),
startPercentage: 1,
stages: [
{
type: 'discrete',
date: new Date(
new Date().getTime() + 1000 * 60 * 60 * 24 * 14,
),
percentage: 0,
},
],
}

// Before next stage - should be no one
jest.useFakeTimers().setSystemTime(new Date())
for (let i = 0; i < 100; i++) {
expect(
doesUserPassRollout({ rollout, boundedHash: i / 100 }),
).toBeFalsy()
}

// After start date - should pass all users
jest.useFakeTimers().setSystemTime(
new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 8),
)
for (let i = 0; i < 100; i++) {
expect(
doesUserPassRollout({ rollout, boundedHash: i / 100 }),
).toBeTruthy()
}

// After next stage - should be no one
jest.useFakeTimers().setSystemTime(
new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 15),
)
for (let i = 0; i < 100; i++) {
expect(
doesUserPassRollout({ rollout, boundedHash: i / 100 }),
).toBeFalsy()
}

jest.useRealTimers()
})
})

it('throws when given an empty rollout object', () => {
Expand Down
3 changes: 2 additions & 1 deletion sdk/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"@devcycle/react-client-sdk": "^1.30.0",
"@devcycle/types": "^1.19.0",
"hoist-non-react-statics": "^3.3.2",
"server-only": "^0.0.1"
"server-only": "^0.0.1",
"class-transformer": "^0.5.1"
},
"types": "./src/index.d.ts",
"exports": {
Expand Down
40 changes: 0 additions & 40 deletions sdk/nextjs/src/common/transformConfig.ts

This file was deleted.

6 changes: 3 additions & 3 deletions sdk/nextjs/src/pages/bucketing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +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'
import { plainToInstance } from 'class-transformer'

class CDNConfigSource extends ConfigSource {
async getConfig(
Expand All @@ -19,7 +19,7 @@ class CDNConfigSource extends ConfigSource {
throw new Error('Could not fetch config')
}
return {
config: await configResponse.json(),
config: plainToInstance(ConfigBody, await configResponse.json()),
lastModified: configResponse.headers.get('last-modified'),
metaData: {},
}
Expand Down Expand Up @@ -53,7 +53,7 @@ const bucketOrFetchConfig = async (

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

Expand Down
5 changes: 2 additions & 3 deletions sdk/nextjs/src/server/bucketing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ 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 @@ -51,7 +50,7 @@ const generateBucketedConfigCached = cache(
bucketedConfig: {
...generateBucketedConfig({
user: populatedUser,
config: transformConfig(config),
config,
}),
clientSDKKey,
sse: {
Expand All @@ -77,7 +76,7 @@ class CDNConfigSource extends ConfigSource {
obfuscated,
)
return {
config: config,
config,
lastModified: headers.get('last-modified'),
metaData: {},
}
Expand Down
3 changes: 2 additions & 1 deletion sdk/nextjs/src/server/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { DVCPopulatedUser } from '@devcycle/js-client-sdk'
import { serializeUserSearchParams } from '../common/serializeUser'
import { cache } from 'react'
import { BucketedUserConfig, ConfigBody } from '@devcycle/types'
import { plainToInstance } from 'class-transformer'

const getFetchUrl = (sdkKey: string, obfuscated: boolean) =>
`https://config-cdn.devcycle.com/config/v2/server/bootstrap/${
Expand Down Expand Up @@ -30,7 +31,7 @@ export const fetchCDNConfig = cache(
throw new Error('Could not fetch config: ' + responseText)
}
return {
config: (await response.json()) as ConfigBody,
config: plainToInstance(ConfigBody, await response.json()),
headers: response.headers,
}
},
Expand Down

0 comments on commit 2b35b6a

Please sign in to comment.