Skip to content

Commit

Permalink
Allow to set the default value through the adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyBitz committed Dec 17, 2024
1 parent 7f6a98b commit aa7955f
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions packages/adapter-launchdarkly/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { init, type LDContext } from '@launchdarkly/vercel-server-sdk';

export type { LDContext };

interface AdapterOptions<ValueType> {
defaultValue?: ValueType;
}

let defaultLaunchDarklyAdapter:
| ReturnType<typeof createLaunchDarklyAdapter>
| undefined;
Expand All @@ -30,23 +34,24 @@ export function createLaunchDarklyAdapter({
const edgeConfigClient = createClient(edgeConfigConnectionString);
const ldClient = init(ldClientSideKey, edgeConfigClient);

return function launchDarklyAdapter<ValueType>(): Adapter<
ValueType,
LDContext
> {
return function launchDarklyAdapter<ValueType>({
defaultValue,
}: AdapterOptions<ValueType> = {}): Adapter<ValueType, LDContext> {
return {
origin(key) {
return `https://app.launchdarkly.com/projects/${ldProject}/flags/${key}/`;
},
async decide({ key, entities }): Promise<ValueType> {
await ldClient.waitForInitialization();
return ldClient.variation(key, entities!, undefined) as ValueType;
return ldClient.variation(key, entities!, defaultValue) as ValueType;
},
};
};
}

export function launchDarkly<ValueType>(): Adapter<ValueType, LDContext> {
export function launchDarkly<ValueType>(
options: AdapterOptions<ValueType>,
): Adapter<ValueType, LDContext> {
if (!defaultLaunchDarklyAdapter) {
const edgeConfigConnectionString = assertEnv('EDGE_CONFIG');
const ldClientSideKey = assertEnv('LD_CLIENT_SIDE_KEY');
Expand All @@ -58,5 +63,5 @@ export function launchDarkly<ValueType>(): Adapter<ValueType, LDContext> {
});
}

return defaultLaunchDarklyAdapter();
return defaultLaunchDarklyAdapter(options);
}

0 comments on commit aa7955f

Please sign in to comment.