@@ -13,6 +13,14 @@ interface AdapterOptions<ValueType> {
13
13
defaultValue ?: ValueType ;
14
14
}
15
15
16
+ type AdapterResponse = {
17
+ variation : < ValueType > (
18
+ options ?: AdapterOptions < ValueType > ,
19
+ ) => Adapter < ValueType , LDContext > ;
20
+ /** The LaunchDarkly client instance used by the adapter. */
21
+ ldClient : LDClient ;
22
+ } ;
23
+
16
24
let defaultLaunchDarklyAdapter :
17
25
| ReturnType < typeof createLaunchDarklyAdapter >
18
26
| undefined ;
@@ -35,23 +43,19 @@ export function createLaunchDarklyAdapter({
35
43
projectSlug : string ;
36
44
clientSideId : string ;
37
45
edgeConfigConnectionString : string ;
38
- } ) : {
39
- < ValueType > (
40
- options ?: AdapterOptions < ValueType > ,
41
- ) : Adapter < ValueType , LDContext > ;
42
- /** The LaunchDarkly client instance used by the adapter. */
43
- ldClient : LDClient ;
44
- } {
46
+ } ) : AdapterResponse {
45
47
const edgeConfigClient = createClient ( edgeConfigConnectionString ) ;
46
48
const ldClient = init ( clientSideId , edgeConfigClient ) ;
47
49
48
- const launchDarklyAdapter = function launchDarklyAdapter < ValueType > (
50
+ function origin ( key : string ) {
51
+ return `https://app.launchdarkly.com/projects/${ projectSlug } /flags/${ key } /` ;
52
+ }
53
+
54
+ function variation < ValueType > (
49
55
options : AdapterOptions < ValueType > = { } ,
50
56
) : Adapter < ValueType , LDContext > {
51
57
return {
52
- origin ( key ) {
53
- return `https://app.launchdarkly.com/projects/${ projectSlug } /flags/${ key } /` ;
54
- } ,
58
+ origin,
55
59
async decide ( { key, entities } ) : Promise < ValueType > {
56
60
await ldClient . waitForInitialization ( ) ;
57
61
return ldClient . variation (
@@ -61,16 +65,15 @@ export function createLaunchDarklyAdapter({
61
65
) as ValueType ;
62
66
} ,
63
67
} ;
64
- } ;
65
-
66
- launchDarklyAdapter . ldClient = ldClient ;
68
+ }
67
69
68
- return launchDarklyAdapter ;
70
+ return {
71
+ ldClient,
72
+ variation,
73
+ } ;
69
74
}
70
75
71
- export function ldAdapter < ValueType > (
72
- options ?: AdapterOptions < ValueType > ,
73
- ) : Adapter < ValueType , LDContext > {
76
+ function getOrCreateDeaultAdapter ( ) {
74
77
if ( ! defaultLaunchDarklyAdapter ) {
75
78
const edgeConfigConnectionString = assertEnv ( 'EDGE_CONFIG' ) ;
76
79
const clientSideId = assertEnv ( 'LAUNCHDARKLY_CLIENT_SIDE_ID' ) ;
@@ -83,9 +86,38 @@ export function ldAdapter<ValueType>(
83
86
} ) ;
84
87
}
85
88
86
- return defaultLaunchDarklyAdapter ( options ) ;
89
+ return defaultLaunchDarklyAdapter ;
87
90
}
88
91
92
+ /**
93
+ * The default LaunchDarkly adapter.
94
+ *
95
+ * This is a convenience object that pre-initializes the LaunchDarkly SDK and provides
96
+ * the adapter function for usage with the Flags SDK.
97
+ *
98
+ * This is the recommended way to use the LaunchDarkly adapter.
99
+ *
100
+ * ```ts
101
+ * // flags.ts
102
+ * import { flag } from 'flags/next';
103
+ * import { ldAdapter, type LDContext } from '@flags-sdk/launchdarkly';
104
+ *
105
+ * const flag = flag<boolean, LDContext>({
106
+ * key: 'my-flag',
107
+ * defaultValue: false,
108
+ * identify: () => ({ key: "user-123" }),
109
+ * adapter: ldAdapter.variation(),
110
+ * });
111
+ * ```
112
+ */
113
+ export const ldAdapter : AdapterResponse = {
114
+ variation : ( ...args ) => getOrCreateDeaultAdapter ( ) . variation ( ...args ) ,
115
+ get ldClient ( ) {
116
+ return getOrCreateDeaultAdapter ( ) . ldClient ;
117
+ } ,
118
+ } ;
119
+
120
+ /**
89
121
/**
90
122
* This is the previous name for the LaunchDarkly adapter.
91
123
*
0 commit comments