-
Notifications
You must be signed in to change notification settings - Fork 18
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
feat: Add typed variation methods. #288
feat: Add typed variation methods. #288
Conversation
This pull request has been linked to Shortcut Story #218820: Add typed variation methods to node server v9.. |
@@ -31,7 +31,8 @@ app.get('/', (req, res) => { | |||
'migrations', | |||
'event-sampling', | |||
'config-override-kind', | |||
'metric-kind' | |||
'metric-kind', | |||
'strongly-typed', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Run the typed contract tests.
contract-tests/sdkClientEntity.js
Outdated
switch(pe.valueType) { | ||
case "bool": | ||
return await client.boolVariationDetail(pe.flagKey, pe.context || pe.user, pe.defaultValue); | ||
case "int": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The contract tests support int and double, but we are using a single numberVariation.
@@ -160,6 +160,99 @@ describe('given an LDClient with test data', () => { | |||
const valueB = await client.variation('my-feature-flag-1', userContextObject, 'default'); | |||
expect(valueB).toEqual(true); | |||
}); | |||
|
|||
it('evaluates with jsonVariation', async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This just addresses the problem with any
in Typescript. Some people require using unknown
so they have to explicitly cast.
const stringRes: string = (await client.jsonVariation('flagkey', defaultUser, false)) as string; | ||
expect(stringRes).toBe('potato'); | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Below are all the typed method calls.
@@ -57,7 +57,7 @@ describe('given an LDClient with test data', () => { | |||
const defaultValue = Object.values(LDMigrationStage).find((item) => item !== value); | |||
// Verify the pre-condition that the default value is not the value under test. | |||
expect(defaultValue).not.toEqual(value); | |||
const res = await client.variationMigration( | |||
const res = await client.migrationVariation( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I decided to make this the same as the other SDKs with this change. This has not been released yet (for Yus).
} | ||
|
||
async boolVariation(key: string, context: LDContext, defaultValue: boolean): Promise<boolean> { | ||
return ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The approach taken with typedEval is to have it take a validator that checks the type and also indicates what the type was, for error messages. This matched nicely with the TypeValidators implementation.
If the type is wrong, then it generates the correct reason and evaluation events.
Co-authored-by: Yusinto Ngadiman <[email protected]>
This adds typed variation methods.
This works for both JS and TS, but may be especially nice for those that want to be more type safe in typescript.
resolves #285