Skip to content

Commit

Permalink
Add default value for adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyBitz committed Dec 17, 2024
1 parent 103ad1e commit 8e5225e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
14 changes: 9 additions & 5 deletions packages/adapter-launchdarkly/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,20 @@ export function createLaunchDarklyAdapter({
const edgeConfigClient = createClient(edgeConfigConnectionString);
const ldClient = init(ldClientSideKey, edgeConfigClient);

return function launchDarklyAdapter<ValueType>({
defaultValue,
}: AdapterOptions<ValueType> = {}): Adapter<ValueType, LDContext> {
return function launchDarklyAdapter<ValueType>(
options: AdapterOptions<ValueType> = {},
): Adapter<ValueType, LDContext> {
return {
origin(key) {
return `https://app.launchdarkly.com/projects/${ldProject}/flags/${key}/`;
},
async decide({ key, entities }): Promise<ValueType> {
async decide({ key, entities, defaultValue }): Promise<ValueType> {
await ldClient.waitForInitialization();
return ldClient.variation(key, entities!, defaultValue) as ValueType;
return ldClient.variation(
key,
entities!,
options.defaultValue ?? defaultValue,
) as ValueType;
},
};
};
Expand Down
1 change: 1 addition & 0 deletions packages/flags/src/next/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ function getRun<ValueType, EntitiesType>(
try {
decisionPromise = Promise.resolve<ValueType>(
decide({
defaultValue: definition.defaultValue,
headers: readonlyHeaders,
cookies: readonlyCookies,
entities,
Expand Down
1 change: 1 addition & 0 deletions packages/flags/src/sveltekit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export function flag<T>(definition: FlagDeclaration<T, unknown>): Flag<T> {
{
headers: sealHeaders(store.event.request.headers),
cookies: sealCookies(store.event.request.headers),
defaultValue: definition.defaultValue,
},
// @ts-expect-error not part of the type, but we supply it for convenience
{ event: store.event },
Expand Down
2 changes: 2 additions & 0 deletions packages/flags/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export type GenerousOption<T> = boolean extends T
export type Decide<ValueType, EntitiesType> = (
params: FlagParamsType & {
entities?: EntitiesType;
defaultValue?: ValueType;
},
) => Promise<ValueType> | ValueType;

Expand All @@ -150,6 +151,7 @@ export interface Adapter<ValueType, EntitiesType> {
entities?: EntitiesType;
headers: ReadonlyHeaders;
cookies: ReadonlyRequestCookies;
defaultValue?: ValueType;
}) => Promise<ValueType> | ValueType;
}

Expand Down

0 comments on commit 8e5225e

Please sign in to comment.