You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When fetching a value since it is of type any one should validate the typing.
In our project we're currently utilizing Zod for this.
Type Validation Example
import{Injectable}from'@nestjs/common'import{ConfigService}from'@nestjs/config'import{UptechGrowthBookTypescriptWrapper}from'@uptechworks/uptech-growthbook-sdk-typescript'import{z}from'zod'
@Injectable()exportclassToglsService{// Make sure to add a default value to: src/bootstrap.tspublicstaticsomeFeatureFlagKey='some-feature-flag'publicstaticsomeFeatureFlagDefault=123privateinstance: UptechGrowthBookTypescriptWrapperconstructor(privateconfigService: ConfigService){constapiHost=this.configService.getOrThrow('GROWTHBOOK_API_HOST')constclientKey=this.configService.getOrThrow('GROWTHBOOK_CLIENT_KEY')this.instance=newUptechGrowthBookTypescriptWrapper(apiHost,clientKey)}publicgetsomeFeature(): number{constvalue=this.instance.value(ToglsService.someFeatureFlagDefault)constsafeParsedNumber=z.number().safeParse(value)if(safeParsedNumber.success){returnsafeParsedNumber.data}returnToglsService.someFeatureFlagDefault}}
Over time this validation of typing and returning a default on failure of parsing will become redundant. It would be nice to have a new method in the library like numberValue, stringValue, jsonValue to help facilitate this pattern and available types that GrowthBook provides.
It's possible that a custom result type should be returned rather than just a primitive type. It could always contain a value, but also include a property to show if the parsing was successful or not so that the consumer could determine if they want to handle it with something such as logging or reporting. Another option would be to follow a pattern like Zod and have standard parsing that throws, and a safe parsing that would always has a value. Another idea around this could be to potentially have an optional default value when fetching the key, so that the caller can easily get the value or their default (if not provided it could utilize the init values instead) -- or follow a pattern where it could return null so the user always has to handle it.
Maybe there could be a way to utilize generics and couple them to the defaults from init, so that when fetching by that key that initialized the values it would provide the proper return type.
The text was updated successfully, but these errors were encountered:
When fetching a value since it is of type
any
one should validate the typing.In our project we're currently utilizing
Zod
for this.Type Validation Example
Over time this validation of typing and returning a default on failure of parsing will become redundant. It would be nice to have a new method in the library like
numberValue
,stringValue
,jsonValue
to help facilitate this pattern and available types that GrowthBook provides.It's possible that a custom result type should be returned rather than just a primitive type. It could always contain a value, but also include a property to show if the parsing was successful or not so that the consumer could determine if they want to handle it with something such as logging or reporting. Another option would be to follow a pattern like Zod and have standard parsing that throws, and a safe parsing that would always has a value. Another idea around this could be to potentially have an optional default value when fetching the key, so that the caller can easily get the value or their default (if not provided it could utilize the init values instead) -- or follow a pattern where it could return null so the user always has to handle it.
Maybe there could be a way to utilize generics and couple them to the defaults from
init
, so that when fetching by that key that initialized the values it would provide the proper return type.The text was updated successfully, but these errors were encountered: