Skip to content

Commit df4936a

Browse files
committed
refactor: adding new compat interfaces
- Added new compat interfaces so that toolbar could recognize browser v3 and v4 clients. - Changed import paths to use '@launchdarkly/toolbar' and updated related types. > Eventually we should probably provide this compat layer in the browser SDK code itself.
1 parent 8f0902f commit df4936a

10 files changed

Lines changed: 107 additions & 10 deletions

File tree

packages/demo/src/hooks/useLaunchDarklyProvider.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useState, useEffect, useRef } from 'react';
22
import { asyncWithLDProvider } from 'launchdarkly-react-client-sdk';
3-
import type { LDPlugin } from '@launchdarkly/js-client-sdk';
3+
import type { LDPlugin } from '@launchdarkly/toolbar';
44
import { DEMO_CONFIG, demoLog } from '../config/demo';
55
import { startMockWorker, stopMockWorker } from '../mocks';
66

@@ -38,16 +38,12 @@ export function useLaunchDarklyProvider(props: UseLaunchDarklyProviderProps): Us
3838
}
3939

4040
// Initialize LaunchDarkly provider
41-
// launchdarkly-react-client-sdk@3.9.0 still uses old types from launchdarkly-js-sdk-common,
42-
// but our plugins implement the new @launchdarkly/js-client-sdk types
4341
const Provider = await asyncWithLDProvider({
4442
clientSideID,
4543
options: {
4644
baseUrl,
4745
streamUrl,
4846
eventsUrl,
49-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
50-
// @ts-ignore - Type mismatch: launchdarkly-react-client-sdk@3.9.0 uses old types from launchdarkly-js-sdk-common
5147
plugins,
5248
},
5349
});

packages/toolbar/src/core/ui/Toolbar/context/api/ContextsProvider.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ export const ContextsProvider = ({ children }: { children: React.ReactNode }) =>
238238
// and setContext would skip the identify call due to equality check
239239
isSettingContextRef.current = true;
240240
ldClient
241-
.identify(savedActiveContext)
241+
.identify(savedActiveContext)!
242242
.then(() => {
243243
// Ensure state is in sync (it should already be from useState initialization)
244244
setActiveContextId(savedStableId);

packages/toolbar/src/core/utils/urlOverrides.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* The state includes: flag overrides, contexts, settings, starred flags, etc.
66
*/
77

8-
import { LDContext } from 'launchdarkly-js-client-sdk';
8+
import { LDContext } from '@launchdarkly/js-client-sdk';
99
import { ToolbarSettings } from '../ui/Toolbar/utils/localStorage';
1010

1111
/** Current version of the shared state format */
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import type {
2+
EvaluationSeriesContext,
3+
EvaluationSeriesData,
4+
LDEvaluationReason,
5+
LDFlagValue,
6+
Hook as LDHook,
7+
} from '@launchdarkly/js-client-sdk'
8+
9+
/**
10+
* Interface for extending SDK functionality via hooks that is compatible with both javascript client versions (<=3.x and >=4.x).
11+
*/
12+
export type Hook = Omit<LDHook, 'afterEvaluation'> & {
13+
/**
14+
* This method is called during the execution of the variation method
15+
* after the flag value has been determined. The method is executed synchronously.
16+
*
17+
* @param hookContext Contains read-only information about the evaluation
18+
* being performed.
19+
* @param data A record associated with each stage of hook invocations. Each
20+
* stage is called with the data of the previous stage for a series.
21+
* @param detail The result of the evaluation. This value should not be
22+
* modified.
23+
* @returns Data to use when executing the next state of the hook in the evaluation series. It is
24+
* recommended to expand the previous input into the return. This helps ensure your stage remains
25+
* compatible moving forward as more stages are added.
26+
* ```js
27+
* return {...data, "my-new-field": /*my data/*}
28+
* ```
29+
*/
30+
afterEvaluation?(
31+
hookContext: EvaluationSeriesContext,
32+
data: EvaluationSeriesData,
33+
detail: {
34+
/**
35+
* The result of the flag evaluation. This will be either one of the flag's variations or
36+
* the default value that was passed to `LDClient.variationDetail`.
37+
*/
38+
value: LDFlagValue
39+
/**
40+
* The index of the returned value within the flag's list of variations, e.g. 0 for the
41+
* first variation-- or `null` if the default value was returned.
42+
*/
43+
variationIndex?: number | null
44+
/**
45+
* An object describing the main factor that influenced the flag evaluation value.
46+
*/
47+
reason?: LDEvaluationReason | null
48+
},
49+
): EvaluationSeriesData
50+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { LDFlagSet, LDIdentifyResult } from "@launchdarkly/js-client-sdk";
2+
import type { Hook } from "./Hook"
3+
4+
/**
5+
* Interface for the LaunchDarkly client that is compatible with both javascript client versions (<=3.x and >=4.x).
6+
*/
7+
export interface LDClient {
8+
track(key: string, data?: any, metricValue?: number): void
9+
identify(ctx: any): Promise<LDIdentifyResult> | Promise<LDFlagSet> | Promise<void>
10+
addHook(hook: Hook): void
11+
allFlags(): LDFlagSet;
12+
getContext(): any;
13+
on(key: string, callback: (...args: any[]) => void): void;
14+
off(key: string, callback: (...args: any[]) => void): void;
15+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { LDPluginEnvironmentMetadata, LDPluginMetadata } from "@launchdarkly/js-client-sdk"
2+
import { LDClient} from "./LDClient"
3+
import type { Hook } from "./Hook"
4+
5+
/**
6+
* Interface for LaunchDarkly plugins that are compatible with both javascript client versions (<=3.x and >=4.x).
7+
*/
8+
export interface LDPlugin {
9+
/**
10+
* Returns the metadata for the plugin.
11+
*/
12+
getMetadata(): LDPluginMetadata
13+
14+
/**
15+
* Registers the plugin with the LaunchDarkly client.
16+
* @param client - The LaunchDarkly client.
17+
* @param environmentMetadata - The environment metadata.
18+
*/
19+
register(
20+
client: LDClient,
21+
environmentMetadata?: LDPluginEnvironmentMetadata,
22+
): void
23+
24+
/**
25+
* Returns the hooks for the plugin.
26+
* @param metadata - The environment metadata.
27+
* @returns The hooks for the plugin.
28+
*/
29+
getHooks?(metadata: LDPluginEnvironmentMetadata): Hook[]
30+
}

packages/toolbar/src/types/plugins/eventInterceptionPlugin.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import type { Hook, LDClient, LDPluginEnvironmentMetadata, LDPluginMetadata } from '@launchdarkly/js-client-sdk';
1+
import type { LDPluginEnvironmentMetadata, LDPluginMetadata } from '@launchdarkly/js-client-sdk';
22
import { AfterTrackHook, AfterIdentifyHook, AfterEvaluationHook, EventStore } from '../hooks';
3+
import type { LDClient } from '../compat/LDClient';
4+
import type { Hook } from '../compat/Hook';
35
import type { EventFilter, ProcessedEvent } from '../events';
46
import type { IEventInterceptionPlugin } from './plugins';
57
import { ANALYTICS_EVENT_PREFIX } from '../analytics';

packages/toolbar/src/types/plugins/flagOverridePlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import type {
44
LDFlagSet,
55
Hook,
66
LDPluginEnvironmentMetadata,
7-
LDClient,
87
} from '@launchdarkly/js-client-sdk';
98
import type { IFlagOverridePlugin } from './plugins';
9+
import { LDClient } from '../compat/LDClient';
1010

1111
/**
1212
* Configuration options for the FlagOverridePlugin
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export * from './eventInterceptionPlugin';
22
export * from './flagOverridePlugin';
33
export * from './plugins';
4+
export * from '../compat/LDPlugin';
5+
export * from '../compat/LDClient';

packages/toolbar/src/types/plugins/plugins.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import type { LDClient, LDFlagSet, LDFlagValue, LDPlugin, LDDebugOverride } from '@launchdarkly/js-client-sdk';
1+
import type { LDFlagSet, LDFlagValue, LDDebugOverride } from '@launchdarkly/js-client-sdk';
2+
import type { LDPlugin } from '../compat/LDPlugin';
3+
import type { LDClient } from '../compat/LDClient';
24
import type { ProcessedEvent } from '../events';
35

46
export interface IFlagOverridePlugin extends LDPlugin, LDDebugOverride {

0 commit comments

Comments
 (0)