-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add useVariation and useVariationDetail hooks for react univer…
…sal (#501)
- Loading branch information
Showing
16 changed files
with
129 additions
and
27 deletions.
There are no files selected for viewing
15 changes: 6 additions & 9 deletions
15
packages/sdk/react-universal/example/app/components/helloClientComponent.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 2 additions & 3 deletions
5
packages/sdk/react-universal/example/app/components/helloServerComponent.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
// TODO: Implement variation and typed variation hooks. | ||
export * from './variation'; | ||
|
||
export * from './useLDClient'; |
1 change: 1 addition & 0 deletions
1
packages/sdk/react-universal/src/client/hooks/variation/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './useVariation'; |
54 changes: 54 additions & 0 deletions
54
packages/sdk/react-universal/src/client/hooks/variation/useTypedVariation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import type { LDEvaluationDetailTyped } from '@launchdarkly/js-client-sdk-common'; | ||
|
||
import { useLDClient } from '../useLDClient'; | ||
|
||
export const useTypedVariation = <T extends boolean | number | string | unknown>( | ||
key: string, | ||
defaultValue: T, | ||
): T => { | ||
const ldClient = useLDClient(); | ||
|
||
switch (typeof defaultValue) { | ||
// case 'boolean': | ||
// return ldClient.boolVariation(key, defaultValue as boolean) as T; | ||
// case 'number': | ||
// return ldClient.numberVariation(key, defaultValue as number) as T; | ||
// case 'string': | ||
// return ldClient.stringVariation(key, defaultValue as string) as T; | ||
// case 'undefined': | ||
// case 'object': | ||
// return ldClient.jsonVariation(key, defaultValue) as T; | ||
default: | ||
return ldClient.variation(key, defaultValue); | ||
} | ||
}; | ||
|
||
export const useTypedVariationDetail = <T extends boolean | number | string | unknown>( | ||
key: string, | ||
defaultValue: T, | ||
): LDEvaluationDetailTyped<T> => { | ||
const ldClient = useLDClient(); | ||
|
||
switch (typeof defaultValue) { | ||
// case 'boolean': | ||
// return ldClient.boolVariationDetail( | ||
// key, | ||
// defaultValue as boolean, | ||
// ) as LDEvaluationDetailTyped<T>; | ||
// case 'number': | ||
// return ldClient.numberVariationDetail( | ||
// key, | ||
// defaultValue as number, | ||
// ) as LDEvaluationDetailTyped<T>; | ||
// case 'string': | ||
// return ldClient.stringVariationDetail( | ||
// key, | ||
// defaultValue as string, | ||
// ) as LDEvaluationDetailTyped<T>; | ||
// case 'undefined': | ||
// case 'object': | ||
// return ldClient.jsonVariationDetail(key, defaultValue) as LDEvaluationDetailTyped<T>; | ||
default: | ||
return ldClient.variationDetail(key, defaultValue) as LDEvaluationDetailTyped<T>; | ||
} | ||
}; |
14 changes: 14 additions & 0 deletions
14
packages/sdk/react-universal/src/client/hooks/variation/useVariation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { useTypedVariation, useTypedVariationDetail } from './useTypedVariation'; | ||
|
||
export const useVariation = (key: string, defaultValue?: boolean) => | ||
useTypedVariation<any>(key, defaultValue); | ||
|
||
/** | ||
* Note that this will only work if you have set `withReasons` to true in {@link LDOptions}. | ||
* Otherwise, the `reason` property of the result will be null. | ||
* | ||
* @param key | ||
* @param defaultValue | ||
*/ | ||
export const useVariationDetail = (key: string, defaultValue?: boolean) => | ||
useTypedVariationDetail<any>(key, defaultValue); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './variation'; | ||
|
||
export * from './useLDClientRsc'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/sdk/react-universal/src/server/hooks/variation/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './useVariationRsc'; |
13 changes: 13 additions & 0 deletions
13
packages/sdk/react-universal/src/server/hooks/variation/useVariationRsc.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import type { LDContext, LDFlagValue } from '@launchdarkly/node-server-sdk'; | ||
|
||
import { useLDClientRsc } from '../useLDClientRsc'; | ||
|
||
export const useVariationRsc = async (key: string, context: LDContext, def?: LDFlagValue) => { | ||
const ldc = await useLDClientRsc(context); | ||
return ldc.variation(key, def); | ||
}; | ||
|
||
export const useVariationDetailRsc = async (key: string, context: LDContext, def?: LDFlagValue) => { | ||
const ldc = await useLDClientRsc(context); | ||
return ldc.variationDetail(key, def); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export * from './initNodeSdk'; | ||
export * from './useLDClientRsc'; | ||
export * from './hooks'; | ||
export * from './getBootstrap'; |