Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typing when retrieving values #16

Open
ctsstc opened this issue Mar 10, 2023 · 0 comments
Open

Typing when retrieving values #16

ctsstc opened this issue Mar 10, 2023 · 0 comments

Comments

@ctsstc
Copy link
Member

ctsstc commented Mar 10, 2023

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()
export class ToglsService {
  // Make sure to add a default value to: src/bootstrap.ts
  public static someFeatureFlagKey = 'some-feature-flag'
  public static someFeatureFlagDefault = 123

  private instance: UptechGrowthBookTypescriptWrapper

  constructor(private configService: ConfigService) {
    const apiHost = this.configService.getOrThrow('GROWTHBOOK_API_HOST')
    const clientKey = this.configService.getOrThrow('GROWTHBOOK_CLIENT_KEY')
    this.instance = new UptechGrowthBookTypescriptWrapper(apiHost, clientKey)
  }

  public get someFeature(): number {
    const value = this.instance.value(ToglsService.someFeatureFlagDefault)

    const safeParsedNumber = z.number().safeParse(value)

    if (safeParsedNumber.success) {
      return safeParsedNumber.data
    }

    return ToglsService.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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant