Skip to content

Commit

Permalink
fix: vercel edge config adapter to calls class-transform (#1009)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajwootto authored Dec 5, 2024
1 parent dadf523 commit 5c116fa
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/shared/vercel-edge-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"@devcycle/types": "*",
"@vercel/edge-config": "^1.2.0"
},
"dependencies": {
"class-transformer": "^0.5.1"
},
"type": "commonjs",
"main": "./src/index.js",
"typings": "./src/index.d.ts"
Expand Down
57 changes: 57 additions & 0 deletions lib/shared/vercel-edge-config/src/edge-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,63 @@ describe('EdgeConfigSource', () => {
})
})

it('transforms raw data into a valid ConfigBody', async () => {
const get = jest.fn()
const edgeConfigSource = new EdgeConfigSource(
fromPartial({
get,
}),
)

get.mockResolvedValue({
key: 'value',
features: [
{
configuration: {
targets: [
{
rollout: {
startDate: '2024-12-05T20:36:26.086Z',
},
},
],
},
},
],
lastModified: 'some date',
})

const result = await edgeConfigSource.getConfig(
'sdk-key',
'server',
false,
)

expect(result).toEqual({
config: {
key: 'value',
lastModified: 'some date',
features: [
{
configuration: {
targets: [
{
rollout: {
startDate: new Date(
'2024-12-05T20:36:26.086Z',
),
},
},
],
},
},
],
},
lastModified: 'some date',
metaData: { resLastModified: 'some date' },
})
})

it('returns null when the existing config date is newer', async () => {
const get = jest.fn()
const edgeConfigSource = new EdgeConfigSource(
Expand Down
3 changes: 2 additions & 1 deletion lib/shared/vercel-edge-config/src/edge-config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EdgeConfigClient, EdgeConfigValue } from '@vercel/edge-config'
import { ConfigBody, ConfigSource, UserError } from '@devcycle/types'
import { plainToInstance } from 'class-transformer'

export class EdgeConfigSource extends ConfigSource {
constructor(private edgeConfigClient: EdgeConfigClient) {
Expand Down Expand Up @@ -44,7 +45,7 @@ export class EdgeConfigSource extends ConfigSource {
this.configLastModified = config['lastModified'] as string

return {
config: config as unknown as ConfigBody,
config: plainToInstance(ConfigBody, config),
metaData: { resLastModified: this.configLastModified },
lastModified: this.configLastModified,
}
Expand Down
2 changes: 2 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4829,6 +4829,8 @@ __metadata:
"@devcycle/vercel-edge-config@workspace:lib/shared/vercel-edge-config":
version: 0.0.0-use.local
resolution: "@devcycle/vercel-edge-config@workspace:lib/shared/vercel-edge-config"
dependencies:
class-transformer: ^0.5.1
peerDependencies:
"@devcycle/types": "*"
"@vercel/edge-config": ^1.2.0
Expand Down

0 comments on commit 5c116fa

Please sign in to comment.