@launchdarkly/react-sdk 0.0.0 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -0
- package/LICENSE +13 -0
- package/README.md +21 -0
- package/dist/index.cjs +2 -0
- package/dist/index.d.cts +367 -0
- package/dist/index.d.ts +367 -0
- package/dist/index.js +2 -0
- package/dist/metafile-cjs.json +1 -0
- package/dist/metafile-esm.json +1 -0
- package/dist/server.cjs +1 -0
- package/dist/server.d.cts +197 -0
- package/dist/server.d.ts +197 -0
- package/dist/server.js +1 -0
- package/package.json +76 -3
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
import { LDWaitForInitializationResult, LDClient, LDContextStrict, LDOptions, LDStartOptions, LDContext, LDFlagSet, LDEvaluationDetailTyped } from '@launchdarkly/js-client-sdk';
|
|
2
|
+
export * from '@launchdarkly/js-client-sdk';
|
|
3
|
+
import React$1 from 'react';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Initialization state of the client. This type should be consistent with
|
|
7
|
+
* the `status` field of the `LDWaitForInitializationResult` type.
|
|
8
|
+
*/
|
|
9
|
+
type InitializedState = LDWaitForInitializationResult['status'] | 'initializing' | 'unknown';
|
|
10
|
+
/**
|
|
11
|
+
* The LaunchDarkly client interface for React.
|
|
12
|
+
*/
|
|
13
|
+
interface LDReactClient extends LDClient {
|
|
14
|
+
/**
|
|
15
|
+
* Returns the initialization state of the client. This function is helpful to determine
|
|
16
|
+
* whether LDClient can be used to evaluate flags on initial component render.
|
|
17
|
+
*
|
|
18
|
+
* @see {@link LDWaitForInitializationResult} for the possible values and their meaning
|
|
19
|
+
*
|
|
20
|
+
* @returns {InitializedState} The initialization state of the client.
|
|
21
|
+
*/
|
|
22
|
+
getInitializationState(): InitializedState;
|
|
23
|
+
/**
|
|
24
|
+
* Returns the error that caused initialization to fail, if any.
|
|
25
|
+
* Only set when `getInitializationState()` returns `'failed'`.
|
|
26
|
+
*/
|
|
27
|
+
getInitializationError(): Error | undefined;
|
|
28
|
+
/**
|
|
29
|
+
* Subscribes to context changes triggered by `identify()`. The callback is invoked
|
|
30
|
+
* after each successful `identify()` call with the new resolved context.
|
|
31
|
+
*
|
|
32
|
+
* @param callback Function called with the new context after each successful identify.
|
|
33
|
+
* @returns An unsubscribe function. Call it to stop receiving notifications.
|
|
34
|
+
*/
|
|
35
|
+
onContextChange(callback: (context: LDContextStrict) => void): () => void;
|
|
36
|
+
/**
|
|
37
|
+
* Subscribes to initialization status changes triggered when the client is initialized.
|
|
38
|
+
*
|
|
39
|
+
* @param callback Function called with the initialization result.
|
|
40
|
+
* @returns An unsubscribe function. Call it to stop receiving notifications.
|
|
41
|
+
*/
|
|
42
|
+
onInitializationStatusChange(callback: (result: LDWaitForInitializationResult) => void): () => void;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The React context interface for the LaunchDarkly client.
|
|
46
|
+
*/
|
|
47
|
+
interface LDReactClientContextValue {
|
|
48
|
+
/**
|
|
49
|
+
* The LaunchDarkly client.
|
|
50
|
+
*/
|
|
51
|
+
client: LDReactClient;
|
|
52
|
+
/**
|
|
53
|
+
* The LaunchDarkly context. This will be undefined if the client is not initialized.
|
|
54
|
+
*/
|
|
55
|
+
context?: LDContextStrict;
|
|
56
|
+
/**
|
|
57
|
+
* The initialization state of the client.
|
|
58
|
+
*/
|
|
59
|
+
initializedState: InitializedState;
|
|
60
|
+
/**
|
|
61
|
+
* The error that caused the client to fail to initialize. Only set when `initializedState` is `'failed'`.
|
|
62
|
+
*/
|
|
63
|
+
error?: Error;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The LaunchDarkly client context provider interface for React.
|
|
67
|
+
* This will be the type that is returned from our createContext function.
|
|
68
|
+
*/
|
|
69
|
+
type LDReactClientContext = React$1.Context<LDReactClientContextValue>;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Initialization options for the LaunchDarkly React SDK.
|
|
73
|
+
*/
|
|
74
|
+
interface LDReactClientOptions extends LDOptions {
|
|
75
|
+
/**
|
|
76
|
+
* Whether the React SDK should transform flag keys into camel-cased format.
|
|
77
|
+
* Using camel-cased flag keys allow for easier use as prop values, however,
|
|
78
|
+
* these keys won't directly match the flag keys as known to LaunchDarkly.
|
|
79
|
+
* Consequently, flag key collisions may be possible and the Code References feature
|
|
80
|
+
* will not function properly.
|
|
81
|
+
*
|
|
82
|
+
* This is true by default, meaning that keys will automatically be converted to camel-case.
|
|
83
|
+
*
|
|
84
|
+
* For more information, see the React SDK Reference Guide on
|
|
85
|
+
* [flag keys](https://docs.launchdarkly.com/sdk/client-side/react/react-web#flag-keys).
|
|
86
|
+
*
|
|
87
|
+
* @deprecated This option is deprecated and will be removed in a future major version.
|
|
88
|
+
*
|
|
89
|
+
* @see https://docs.launchdarkly.com/sdk/client-side/react/react-web#flag-keys
|
|
90
|
+
*/
|
|
91
|
+
useCamelCaseFlagKeys?: boolean;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Options for creating a React Provider.
|
|
95
|
+
*/
|
|
96
|
+
interface LDReactProviderOptions {
|
|
97
|
+
/**
|
|
98
|
+
* Options for the LaunchDarkly client.
|
|
99
|
+
*
|
|
100
|
+
* @remarks
|
|
101
|
+
* This option is used to pass options to the LaunchDarkly client.
|
|
102
|
+
*
|
|
103
|
+
* @see {@link LDReactClientOptions} for the possible options
|
|
104
|
+
*/
|
|
105
|
+
ldOptions?: LDReactClientOptions;
|
|
106
|
+
/**
|
|
107
|
+
* Options for starting the LaunchDarkly client.
|
|
108
|
+
*
|
|
109
|
+
* @remarks
|
|
110
|
+
* This option is especially useful if you choose to not defer
|
|
111
|
+
* initialization and want to start the client immediately.
|
|
112
|
+
*
|
|
113
|
+
* @see {@link LDStartOptions} for the possible options
|
|
114
|
+
*/
|
|
115
|
+
startOptions?: LDStartOptions;
|
|
116
|
+
/**
|
|
117
|
+
* If set to true, the LDClient will not start automatically.
|
|
118
|
+
*
|
|
119
|
+
* @default false
|
|
120
|
+
*
|
|
121
|
+
* If initialization is deferred, then the LDClient can be started manually
|
|
122
|
+
* by calling the `start` function.
|
|
123
|
+
*/
|
|
124
|
+
deferInitialization?: boolean;
|
|
125
|
+
/**
|
|
126
|
+
* This option allow developers to provide their own named react context for
|
|
127
|
+
* the launchdarkly client. This is useful for cases where you want to have multiple
|
|
128
|
+
* clients in the same application. If not provided, the default context will be used.
|
|
129
|
+
*
|
|
130
|
+
* @see {@link LDReactClientContext} for the possible values and their meaning
|
|
131
|
+
*
|
|
132
|
+
* @returns {LDReactClientContext} The react context for the LaunchDarkly client.
|
|
133
|
+
*/
|
|
134
|
+
reactContext?: LDReactClientContext;
|
|
135
|
+
/**
|
|
136
|
+
* Bootstrap data from the server. Pass the result of `flagsState.toJSON()` obtained
|
|
137
|
+
* from the server SDK's `allFlagsState` method.
|
|
138
|
+
*
|
|
139
|
+
* When provided, the client immediately uses these values before the first network
|
|
140
|
+
* response arrives — eliminating the flag-fetch waterfall on page load.
|
|
141
|
+
*
|
|
142
|
+
* This is merged into `startOptions.bootstrap` when the client is started. If both
|
|
143
|
+
* `bootstrap` and `startOptions.bootstrap` are provided, this top-level value takes
|
|
144
|
+
* precedence.
|
|
145
|
+
*/
|
|
146
|
+
bootstrap?: unknown;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
declare function initLDReactContext(): LDReactClientContext;
|
|
150
|
+
/**
|
|
151
|
+
* The LaunchDarkly React context.
|
|
152
|
+
*/
|
|
153
|
+
declare const LDReactContext: LDReactClientContext;
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Creates a new LaunchDarkly React provider wrapping an existing client instance.
|
|
157
|
+
*
|
|
158
|
+
* @remarks
|
|
159
|
+
* **NOTE:** We recommend using the convenience factory function {@link createLDReactProvider}
|
|
160
|
+
* instead of this function if you can.
|
|
161
|
+
*
|
|
162
|
+
* This factory function is provided to allow the caller to use an existing client
|
|
163
|
+
* instance. When using this function, the caller is responsible for calling `client.start()`
|
|
164
|
+
* before or after mounting.
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* ```tsx
|
|
168
|
+
* import { createClient, createLDReactProviderWithClient, initLDReactContext } from '@launchdarkly/react-sdk';
|
|
169
|
+
*
|
|
170
|
+
* const client = createClient(clientSideID, context);
|
|
171
|
+
* client.start();
|
|
172
|
+
* const LDProvider = createLDReactProviderWithClient(client);
|
|
173
|
+
* ```
|
|
174
|
+
*
|
|
175
|
+
* For multiple client instances, pass a custom React context:
|
|
176
|
+
* ```tsx
|
|
177
|
+
* const ReactContext = initLDReactContext();
|
|
178
|
+
* const LDProvider = createLDReactProviderWithClient(client, ReactContext);
|
|
179
|
+
* ```
|
|
180
|
+
*
|
|
181
|
+
* @param client launchdarkly client instance @see {@link createClient}
|
|
182
|
+
* @param ReactContext optional launchdarkly react context @see {@link initLDReactContext}
|
|
183
|
+
* @returns {React.FC<{ children: React.ReactNode }>} The LaunchDarkly React Client provider.
|
|
184
|
+
*/
|
|
185
|
+
declare function createLDReactProviderWithClient(client: LDReactClient, ReactContext?: React$1.Context<LDReactClientContextValue>): React$1.FC<{
|
|
186
|
+
children: React$1.ReactNode;
|
|
187
|
+
}>;
|
|
188
|
+
/**
|
|
189
|
+
* Creates a new LaunchDarkly React provider, creating the client internally.
|
|
190
|
+
*
|
|
191
|
+
* By default the client is started immediately (before the provider mounts).
|
|
192
|
+
* Pass `deferInitialization: true` in options to opt out of auto-start and call
|
|
193
|
+
* `client.start()` yourself via `useLDClient()`.
|
|
194
|
+
*
|
|
195
|
+
* @example
|
|
196
|
+
* ```tsx
|
|
197
|
+
* import { createLDReactProvider } from '@launchdarkly/react-sdk';
|
|
198
|
+
*
|
|
199
|
+
* const LDProvider = createLDReactProvider('your-client-side-id', { kind: 'user', key: 'user-key' });
|
|
200
|
+
*
|
|
201
|
+
* function Root() {
|
|
202
|
+
* return (
|
|
203
|
+
* <LDProvider>
|
|
204
|
+
* <App />
|
|
205
|
+
* </LDProvider>
|
|
206
|
+
* );
|
|
207
|
+
* }
|
|
208
|
+
* ```
|
|
209
|
+
*
|
|
210
|
+
* @param clientSideID launchdarkly client-side ID
|
|
211
|
+
* @param context the initial LaunchDarkly context
|
|
212
|
+
* @param options optional provider and client options
|
|
213
|
+
* @returns {React.FC<{ children: React.ReactNode }>} The LaunchDarkly React Client provider.
|
|
214
|
+
*/
|
|
215
|
+
declare function createLDReactProvider(clientSideID: string, context: LDContext, options?: LDReactProviderOptions): React$1.FC<{
|
|
216
|
+
children: React$1.ReactNode;
|
|
217
|
+
}>;
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Creates a new instance of the LaunchDarkly client for React.
|
|
221
|
+
*
|
|
222
|
+
* @remarks
|
|
223
|
+
* **NOTE:** We recommend using the convenience factory function {@link createLDReactProvider}
|
|
224
|
+
* instead of this function to create your client instance if you can.
|
|
225
|
+
*
|
|
226
|
+
* This factory function is provided to allow the caller to have more control over their client instance.
|
|
227
|
+
* When using this function, the caller is responsible for:
|
|
228
|
+
* - calling `client.start()` before or after mounting.
|
|
229
|
+
* - subscribing to client lifecycle events.
|
|
230
|
+
*
|
|
231
|
+
* Refer to {@link createLDReactProviderWithClient} for the default behavior.
|
|
232
|
+
*
|
|
233
|
+
* @example
|
|
234
|
+
* ```tsx
|
|
235
|
+
* import { createClient } from '@launchdarkly/react';
|
|
236
|
+
* const client = createClient(clientSideID, context, options);
|
|
237
|
+
*
|
|
238
|
+
* await client.start();
|
|
239
|
+
* ```
|
|
240
|
+
*
|
|
241
|
+
* @param clientSideID launchdarkly client side id @see https://launchdarkly.com/docs/sdk/concepts/client-side-server-side#client-side-id
|
|
242
|
+
* @param context launchdarkly context @see https://launchdarkly.com/docs/sdk/concepts/context
|
|
243
|
+
* @param options options for the client @see {@link LDReactClientOptions}
|
|
244
|
+
* @returns the new client instance @see {@link LDReactClient}
|
|
245
|
+
*/
|
|
246
|
+
declare function createClient(clientSideID: string, context: LDContext, options?: LDReactClientOptions): LDReactClient;
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Returns all feature flags for the current context.
|
|
250
|
+
*
|
|
251
|
+
* @param reactContext Optional React context to read from. Defaults to the global `LDReactContext`.
|
|
252
|
+
* @returns All current flag values, optionally with camelCased keys, wrapped in a proxy that
|
|
253
|
+
* records evaluations.
|
|
254
|
+
*
|
|
255
|
+
* @deprecated This hook is provided to ease migration from older versions of the React SDK.
|
|
256
|
+
* For better performance, migrate to the typed variation hooks (`useBoolVariation`,
|
|
257
|
+
* `useStringVariation`, `useNumberVariation`, `useJsonVariation`) or use `useLDClient`
|
|
258
|
+
* with the client's `allFlags` method directly. This hook will be removed in a future major version.
|
|
259
|
+
*/
|
|
260
|
+
declare function useFlags<T extends LDFlagSet = LDFlagSet>(reactContext?: React.Context<LDReactClientContextValue>): T;
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Represents the current initialization state of the LaunchDarkly client.
|
|
264
|
+
*/
|
|
265
|
+
type InitializationStatus = {
|
|
266
|
+
status: 'unknown';
|
|
267
|
+
} | {
|
|
268
|
+
status: 'initializing';
|
|
269
|
+
} | {
|
|
270
|
+
status: 'complete';
|
|
271
|
+
} | {
|
|
272
|
+
status: 'timeout';
|
|
273
|
+
} | {
|
|
274
|
+
status: 'failed';
|
|
275
|
+
error: Error;
|
|
276
|
+
};
|
|
277
|
+
/**
|
|
278
|
+
* Returns the initialization status of the LaunchDarkly client.
|
|
279
|
+
*
|
|
280
|
+
* @param reactContext Optional React context to read from. Defaults to the global `LDReactContext`.
|
|
281
|
+
* @returns An {@link InitializationStatus} object with the current state (and error, if failed).
|
|
282
|
+
*/
|
|
283
|
+
declare function useInitializationStatus(reactContext?: React.Context<LDReactClientContextValue>): InitializationStatus;
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Returns the LaunchDarkly client instance from the nearest provider.
|
|
287
|
+
*
|
|
288
|
+
* @param reactContext Optional React context to read from. Defaults to the global `LDReactContext`.
|
|
289
|
+
* @returns The {@link LDReactClient} instance.
|
|
290
|
+
*/
|
|
291
|
+
declare function useLDClient(reactContext?: React.Context<LDReactClientContextValue>): LDReactClient;
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Returns the boolean variation of a feature flag.
|
|
295
|
+
*
|
|
296
|
+
* @param key The feature flag key.
|
|
297
|
+
* @param defaultValue The value to return if the flag is not available.
|
|
298
|
+
* @param reactContext Optional React context to read from. Defaults to the global `LDReactContext`.
|
|
299
|
+
* @returns The boolean flag value, or `defaultValue` if the flag is unavailable.
|
|
300
|
+
*/
|
|
301
|
+
declare function useBoolVariation(key: string, defaultValue: boolean, reactContext?: React.Context<LDReactClientContextValue>): boolean;
|
|
302
|
+
/**
|
|
303
|
+
* Returns the string variation of a feature flag.
|
|
304
|
+
*
|
|
305
|
+
* @param key The feature flag key.
|
|
306
|
+
* @param defaultValue The value to return if the flag is not available.
|
|
307
|
+
* @param reactContext Optional React context to read from. Defaults to the global `LDReactContext`.
|
|
308
|
+
* @returns The string flag value, or `defaultValue` if the flag is unavailable.
|
|
309
|
+
*/
|
|
310
|
+
declare function useStringVariation(key: string, defaultValue: string, reactContext?: React.Context<LDReactClientContextValue>): string;
|
|
311
|
+
/**
|
|
312
|
+
* Returns the numeric variation of a feature flag.
|
|
313
|
+
*
|
|
314
|
+
* @param key The feature flag key.
|
|
315
|
+
* @param defaultValue The value to return if the flag is not available.
|
|
316
|
+
* @param reactContext Optional React context to read from. Defaults to the global `LDReactContext`.
|
|
317
|
+
* @returns The number flag value, or `defaultValue` if the flag is unavailable.
|
|
318
|
+
*/
|
|
319
|
+
declare function useNumberVariation(key: string, defaultValue: number, reactContext?: React.Context<LDReactClientContextValue>): number;
|
|
320
|
+
/**
|
|
321
|
+
* Returns the JSON variation of a feature flag.
|
|
322
|
+
*
|
|
323
|
+
* @param key The feature flag key.
|
|
324
|
+
* @param defaultValue The value to return if the flag is not available.
|
|
325
|
+
* @param reactContext Optional React context to read from. Defaults to the global `LDReactContext`.
|
|
326
|
+
* @returns The JSON flag value, or `defaultValue` if the flag is unavailable.
|
|
327
|
+
*/
|
|
328
|
+
declare function useJsonVariation<T = unknown>(key: string, defaultValue: T, reactContext?: React.Context<LDReactClientContextValue>): T;
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Returns the boolean variation and evaluation detail of a feature flag.
|
|
332
|
+
*
|
|
333
|
+
* @param key The feature flag key.
|
|
334
|
+
* @param defaultValue The value to return if the flag is not available.
|
|
335
|
+
* @param reactContext Optional React context to read from. Defaults to the global `LDReactContext`.
|
|
336
|
+
* @returns The evaluation detail including `value`, `variationIndex`, and `reason`.
|
|
337
|
+
*/
|
|
338
|
+
declare function useBoolVariationDetail(key: string, defaultValue: boolean, reactContext?: React.Context<LDReactClientContextValue>): LDEvaluationDetailTyped<boolean>;
|
|
339
|
+
/**
|
|
340
|
+
* Returns the string variation and evaluation detail of a feature flag.
|
|
341
|
+
*
|
|
342
|
+
* @param key The feature flag key.
|
|
343
|
+
* @param defaultValue The value to return if the flag is not available.
|
|
344
|
+
* @param reactContext Optional React context to read from. Defaults to the global `LDReactContext`.
|
|
345
|
+
* @returns The evaluation detail including `value`, `variationIndex`, and `reason`.
|
|
346
|
+
*/
|
|
347
|
+
declare function useStringVariationDetail(key: string, defaultValue: string, reactContext?: React.Context<LDReactClientContextValue>): LDEvaluationDetailTyped<string>;
|
|
348
|
+
/**
|
|
349
|
+
* Returns the numeric variation and evaluation detail of a feature flag.
|
|
350
|
+
*
|
|
351
|
+
* @param key The feature flag key.
|
|
352
|
+
* @param defaultValue The value to return if the flag is not available.
|
|
353
|
+
* @param reactContext Optional React context to read from. Defaults to the global `LDReactContext`.
|
|
354
|
+
* @returns The evaluation detail including `value`, `variationIndex`, and `reason`.
|
|
355
|
+
*/
|
|
356
|
+
declare function useNumberVariationDetail(key: string, defaultValue: number, reactContext?: React.Context<LDReactClientContextValue>): LDEvaluationDetailTyped<number>;
|
|
357
|
+
/**
|
|
358
|
+
* Returns the JSON variation and evaluation detail of a feature flag.
|
|
359
|
+
*
|
|
360
|
+
* @param key The feature flag key.
|
|
361
|
+
* @param defaultValue The value to return if the flag is not available.
|
|
362
|
+
* @param reactContext Optional React context to read from. Defaults to the global `LDReactContext`.
|
|
363
|
+
* @returns The evaluation detail including `value`, `variationIndex`, and `reason`.
|
|
364
|
+
*/
|
|
365
|
+
declare function useJsonVariationDetail<T = unknown>(key: string, defaultValue: T, reactContext?: React.Context<LDReactClientContextValue>): LDEvaluationDetailTyped<T>;
|
|
366
|
+
|
|
367
|
+
export { type InitializationStatus, type InitializedState, type LDReactClient, type LDReactClientContext, type LDReactClientContextValue, type LDReactClientOptions, LDReactContext, type LDReactProviderOptions, createClient, createLDReactProvider, createLDReactProviderWithClient, initLDReactContext, useBoolVariation, useBoolVariationDetail, useFlags, useInitializationStatus, useJsonVariation, useJsonVariationDetail, useLDClient, useNumberVariation, useNumberVariationDetail, useStringVariation, useStringVariationDetail };
|