Skip to content

Commit

Permalink
feat: add custom data generator (#432)
Browse files Browse the repository at this point in the history
* add type generator for custom data properties

* tests

* update
  • Loading branch information
ajwootto authored Sep 19, 2024
1 parent 6f5eaf7 commit 0153dfa
Show file tree
Hide file tree
Showing 9 changed files with 379 additions and 38 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ $ npm install -g @devcycle/cli
$ dvc COMMAND
running command...
$ dvc (--version)
@devcycle/cli/5.18.0 linux-x64 node-v20.10.0
@devcycle/cli/5.18.0 darwin-arm64 node-v20.10.0
$ dvc --help [COMMAND]
USAGE
$ dvc COMMAND
Expand Down
4 changes: 3 additions & 1 deletion docs/generate.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ Generate Variable Types from the management API
USAGE
$ dvc generate types [--config-path <value>] [--auth-path <value>] [--repo-config-path <value>] [--client-id
<value>] [--client-secret <value>] [--project <value>] [--no-api] [--headless] [--output-dir <value>] [--react]
[--nextjs] [--old-repos] [--include-descriptions] [--obfuscate] [--include-deprecation-warnings]
[--nextjs] [--old-repos] [--include-descriptions] [--strict-custom-data] [--obfuscate]
[--include-deprecation-warnings]
FLAGS
--include-deprecation-warnings Include @deprecated tags for variables of completed features
Expand All @@ -24,6 +25,7 @@ FLAGS
@devcycle/devcycle-js-sdk)
--output-dir=<value> [default: .] Directory to output the generated types to
--react Generate types for use with React
--strict-custom-data Generate stricter custom data types
GLOBAL FLAGS
--auth-path=<value> Override the default location to look for an auth.yml file
Expand Down
8 changes: 7 additions & 1 deletion oclif.manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "5.17.0",
"version": "5.18.0",
"commands": {
"authCommand": {
"id": "authCommand",
Expand Down Expand Up @@ -2245,6 +2245,12 @@
"description": "Include variable descriptions in the variable information comment",
"allowNo": false
},
"strict-custom-data": {
"name": "strict-custom-data",
"type": "boolean",
"description": "Generate stricter custom data types",
"allowNo": false
},
"obfuscate": {
"name": "obfuscate",
"type": "boolean",
Expand Down
13 changes: 13 additions & 0 deletions src/api/customProperties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import apiClient from './apiClient'
import { buildHeaders } from './common'

const BASE_URL = '/v1/projects/:project/customProperties'

export const fetchCustomProperties = async (token: string, project_id: string) => {
return apiClient.get(`${BASE_URL}`, {
headers: buildHeaders(token),
params: {
project: project_id,
},
})
}
2 changes: 1 addition & 1 deletion src/api/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type FeatureConfig = z.infer<typeof schemas.FeatureConfig>
export type Audience = z.infer<typeof schemas.Audience>
export type Target = z.infer<typeof schemas.Target>
export type Override = z.infer<typeof schemas.Override>

export type CustomProperty = z.infer<typeof schemas.CustomProperty>
export type CreateEnvironmentParams = z.infer<
typeof schemas.CreateEnvironmentDto
>
Expand Down
17 changes: 17 additions & 0 deletions src/api/zodClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,23 @@ const CustomProperty = z.object({
_project: z.string(),
_createdBy: z.string(),
propertyKey: z.string(),
schema: z
.object({
schemaType: z.enum(['enum']),
required: z.boolean().optional(),
enumSchema: z
.object({
allowedValues: z.array(
z.object({
label: z.string(),
value: z.union([z.string(), z.number()]),
}),
),
allowAdditionalValues: z.boolean().optional(),
})
.optional(),
})
.optional(),
type: z.enum(['String', 'Boolean', 'Number']),
createdAt: z.string().datetime(),
updatedAt: z.string().datetime(),
Expand Down
177 changes: 176 additions & 1 deletion src/commands/generate/__snapshots__/types.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,8 +1,150 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`generate types correctly generates JS SDK types 1`] = `
exports[`generate types correctly generates JS SDK types with custom data type 1`] = `
"import { DevCycleJSON } from '@devcycle/js-client-sdk'
export type CustomData = {
'customString': string
'customEnum'?: |
// Option 1
'option1' |
// Option 2
'option2'
'numberEnum'?: |
// Option 1
1 |
// Option 2
2 |
number
'regularNumber'?: number
}
export type DVCVariableTypes = {
/**
* key: enum-var
* description: Different ways to say hello
* created by: User 1
* created on: 2021-07-04
*/
'enum-var': 'Hello' | 'Hey' | 'Hi'
/**
* key: regex-var
* created by: User 2
* created on: 2021-07-04
*/
'regex-var': string
/**
* key: string-var
* created by: User 1
* created on: 2021-07-04
*/
'string-var': string
/**
* key: boolean-var
* created by: User 2
* created on: 2021-07-04
*/
'boolean-var': boolean
/**
* key: number-var
* created by: User 1
* created on: 2021-07-04
*/
'number-var': number
/**
* key: json-var
* created by: Unknown User
* created on: 2021-07-04
*/
'json-var': DevCycleJSON
/**
* key: deprecated-var
* created by: Unknown User
* created on: 2021-07-04
* @deprecated This variable is part of complete feature \\"Completed Feature\\" and should be cleaned up.
*/
'deprecated-var': string
}
/**
* key: enum-var
* description: Different ways to say hello
* created by: User 1
* created on: 2021-07-04
*/
export const ENUM_VAR = 'enum-var' as const
/**
* key: regex-var
* created by: User 2
* created on: 2021-07-04
*/
export const REGEX_VAR = 'regex-var' as const
/**
* key: string-var
* created by: User 1
* created on: 2021-07-04
*/
export const STRING_VAR = 'string-var' as const
/**
* key: boolean-var
* created by: User 2
* created on: 2021-07-04
*/
export const BOOLEAN_VAR = 'boolean-var' as const
/**
* key: number-var
* created by: User 1
* created on: 2021-07-04
*/
export const NUMBER_VAR = 'number-var' as const
/**
* key: json-var
* created by: Unknown User
* created on: 2021-07-04
*/
export const JSON_VAR = 'json-var' as const
/**
* key: deprecated-var
* created by: Unknown User
* created on: 2021-07-04
* @deprecated This variable is part of complete feature \\"Completed Feature\\" and should be cleaned up.
*/
export const DEPRECATED_VAR = 'deprecated-var' as const
"
`;

exports[`generate types correctly generates JS SDK types with custom data type in strict mode 1`] = `
"import { DevCycleJSON } from '@devcycle/js-client-sdk'
export type CustomData = {
'customString': string
'customEnum'?: |
// Option 1
'option1' |
// Option 2
'option2'
'numberEnum'?: |
// Option 1
1 |
// Option 2
2 |
number
'regularNumber'?: number
}
export type DVCVariableTypes = {
/**
* key: enum-var
Expand Down Expand Up @@ -115,6 +257,9 @@ export const DEPRECATED_VAR = 'deprecated-var' as const
exports[`generate types correctly generates JS SDK types with obfuscated keys 1`] = `
"import { DevCycleJSON } from '@devcycle/js-client-sdk'
export type CustomData = {
}
export type DVCVariableTypes = {
'dvc_obfs_8397a7cc0bd9847dab4cfcffc90d01385e21fec6f1d3999e3aaac2a137d69964': 'Hello' | 'Hey' | 'Hi'
'dvc_obfs_cabd691e35fffe317483d72108898be69e03c7611dfb035660e4f71b9ed8cc44': string
Expand Down Expand Up @@ -188,6 +333,21 @@ exports[`generate types correctly generates Next.js SDK types 1`] = `
DevCycleJSON
} from '@devcycle/nextjs-sdk'
export type CustomData = {
'customString': string
'customEnum'?: |
// Option 1
'option1' |
// Option 2
'option2'
'numberEnum'?: |
// Option 1
1 |
// Option 2
2 |
number
'regularNumber'?: number
}
export type UseVariableValue = <
K extends string & keyof DVCVariableTypes
Expand Down Expand Up @@ -326,6 +486,21 @@ exports[`generate types correctly generates React SDK types 1`] = `
DevCycleJSON
} from '@devcycle/react-client-sdk'
export type CustomData = {
'customString': string
'customEnum'?: |
// Option 1
'option1' |
// Option 2
'option2'
'numberEnum'?: |
// Option 1
1 |
// Option 2
2 |
number
'regularNumber'?: number
}
export type UseVariableValue = <
K extends string & keyof DVCVariableTypes
Expand Down
Loading

0 comments on commit 0153dfa

Please sign in to comment.