Skip to content

Commit

Permalink
Remove dependency on js-client-sdk for React generated types (#410)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmawillis authored Jul 3, 2024
1 parent 77430b6 commit 17dfa73
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
7 changes: 4 additions & 3 deletions src/commands/generate/__snapshots__/types.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,11 @@ export const JSON_VAR = 'dvc_obfs_a991871dbce45b30da1e125ae28b59eb74c1da1f48ad6a
`;

exports[`generate types correctly generates React SDK types 1`] = `
"import { DVCVariable, DVCVariableValue } from '@devcycle/js-client-sdk'
import {
"import {
useVariable as originalUseVariable,
useVariableValue as originalUseVariableValue
useVariableValue as originalUseVariableValue,
DVCVariable,
DVCVariableValue
} from '@devcycle/react-client-sdk'
Expand Down
30 changes: 20 additions & 10 deletions src/commands/generate/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,26 @@ import { createHash } from 'crypto'
import path from 'path'

const reactImports = (oldRepos: boolean) => {
const jsRepo = oldRepos
? '@devcycle/devcycle-js-sdk'
: '@devcycle/js-client-sdk'
const reactRepo = oldRepos
? '@devcycle/devcycle-react-sdk'
: '@devcycle/react-client-sdk'
return `import { DVCVariable, DVCVariableValue } from '${jsRepo}'
if (oldRepos) {
return `import { DVCVariable, DVCVariableValue } from '@devcycle/devcycle-js-sdk'
import {
useVariable as originalUseVariable,
useVariableValue as originalUseVariableValue
} from '${reactRepo}'
} from '@devcycle/devcycle-react-sdk'
`
} else {
return `import {
useVariable as originalUseVariable,
useVariableValue as originalUseVariableValue,
DVCVariable,
DVCVariableValue
} from '@devcycle/react-client-sdk'
`
}
}

const reactOverrides = `
export type UseVariableValue = <
K extends string & keyof DVCVariableTypes
Expand Down Expand Up @@ -193,7 +199,9 @@ export default class GenerateTypes extends Base {
}

private getVariableKeyAndType(variable: Variable) {
return `'${this.obfuscate ? this.encryptKey(variable) : variable.key}': ${getVariableType(variable)}`
return `'${
this.obfuscate ? this.encryptKey(variable) : variable.key
}': ${getVariableType(variable)}`
}

private getVariableInfoComment(
Expand Down Expand Up @@ -237,7 +245,9 @@ export const ${constantName} = '${hashedKey}' as const`
}

private encryptKey(variable: Variable) {
return `dvc_obfs_${createHash('sha256').update(variable._id).digest('hex')}`
return `dvc_obfs_${createHash('sha256')
.update(variable._id)
.digest('hex')}`
}

private updateFileLocation() {
Expand Down

0 comments on commit 17dfa73

Please sign in to comment.