Skip to content

Commit

Permalink
feat: enable configuring custom environments for launchdarkly
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Jiang authored and Andrew Jiang committed Jan 15, 2025
1 parent 5c43336 commit 658789b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
28 changes: 25 additions & 3 deletions packages/fern-docs/bundle/src/server/ld-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ interface LaunchDarklyInfo {
contextEndpoint: string;
context: ld.LDContext | undefined;
defaultFlags: Record<string, unknown> | undefined;
options:
| {
baseUrl?: string;
streamUrl?: string;
eventsUrl?: string;
}
| undefined;
}

export async function withLaunchDarkly(
Expand All @@ -69,15 +76,21 @@ export async function withLaunchDarkly(
node,
rawCookie
);
const options = {
baseUrl: launchDarklyConfig.options?.["base-url"],
streamUrl: launchDarklyConfig.options?.["stream-url"],
eventsUrl: launchDarklyConfig.options?.["events-url"],
};
const defaultFlags = launchDarklyConfig["sdk-key"]
? await fetchInitialFlags(launchDarklyConfig["sdk-key"], context)
? await fetchInitialFlags(launchDarklyConfig["sdk-key"], context, options)
: undefined;
return [
{
clientSideId: launchDarklyConfig["client-side-id"],
contextEndpoint: launchDarklyConfig["context-endpoint"],
context,
defaultFlags,
options,
},
// Note: if sdk-key is set, then middleware will automatically switch to 100% getServerSideProps
// because getServerSideProps must determine whether any given page should be rendered or not.
Expand Down Expand Up @@ -122,10 +135,19 @@ export const createLdPredicate = async ({

function fetchInitialFlags(
sdkKey: string,
context: ld.LDContext
context: ld.LDContext,
options?: {
baseUrl?: string;
streamUrl?: string;
eventsUrl?: string;
}
): Promise<Record<string, unknown>> | undefined {
try {
const ldClient = ld.init(sdkKey);
const ldClient = ld.init(sdkKey, {
baseUri: options?.baseUrl,
streamUri: options?.streamUrl,
eventsUri: options?.eventsUrl,
});
return ldClient.allFlagsState(context).then((flags) => flags.allValues());
} catch (error) {
console.error(error);
Expand Down
8 changes: 8 additions & 0 deletions packages/fern-docs/edge-config/src/getLaunchDarklySettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ const LaunchDarklyEdgeConfigSchema = z.object({
// we should add a check to make sure the target domain is trusted. Trust should always be granted manually by a fern engineer,
// so it should be managed in edge config, or FGA.
"context-endpoint": z.string(),

options: z
.object({
"base-url": z.string().optional(),
"stream-url": z.string().optional(),
"events-url": z.string().optional(),
})
.optional(),
});

export type LaunchDarklyEdgeConfig = z.infer<
Expand Down
7 changes: 7 additions & 0 deletions packages/fern-docs/ui/src/atoms/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ export interface LaunchDarklyInfo {
contextEndpoint: string;
context: LDContext | undefined;
defaultFlags: Record<string, unknown> | undefined;
options:
| {
baseUrl: string | undefined;
streamUrl: string | undefined;
eventsUrl: string | undefined;
}
| undefined;
}

export interface FeatureFlagsConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const FeatureFlagProvider: FC<FeatureFlagProviderProps> = ({
contextEndpoint={launchDarklyInfo.contextEndpoint}
defaultContext={launchDarklyInfo.context}
defaultFlags={launchDarklyInfo.defaultFlags}
options={launchDarklyInfo.options}
>
{children}
</LDFeatureFlagProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,28 @@ interface Props extends PropsWithChildren {
defaultContext?: LDContext;

defaultFlags?: Record<string, unknown>;

options?: {
baseUrl?: string;
streamUrl?: string;
eventsUrl?: string;
};
}

export const LDFeatureFlagProvider: FC<Props> = ({
clientSideId,
contextEndpoint,
defaultContext,
defaultFlags,
options,
children,
}) => {
return (
<LDProvider
clientSideID={clientSideId}
context={defaultContext}
flags={defaultFlags}
options={options}
>
<IdentifyWrapper
contextEndpoint={contextEndpoint}
Expand Down

0 comments on commit 658789b

Please sign in to comment.