/
index.ts
65 lines (58 loc) · 2.12 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* This is the API reference for the Vercel LaunchDarkly SDK.
*
* In typical usage, you will call {@link init} once at startup time to obtain an instance of
* {@link LDClient}, which provides access to all of the SDK's functionality.
*
* For more information, see the SDK reference guide.
*
* @packageDocumentation
*/
import type { EdgeConfigClient } from '@vercel/edge-config';
import {
BasicLogger,
EdgeFeatureStore,
EdgeProvider,
init as initEdge,
LDClient,
LDOptions,
} from '@launchdarkly/js-server-sdk-common-edge';
import createPlatformInfo from './createPlatformInfo';
export * from '@launchdarkly/js-server-sdk-common-edge';
export type { LDClient };
/**
* Creates an instance of the Vercel LaunchDarkly client.
*
* Applications should instantiate a single instance for the lifetime of the worker.
* The client will begin attempting to connect to the configured Vercel Edge Config as
* soon as it is created. To determine when it is ready to use, call {@link LDClient.waitForInitialization}.
*
* **Important:** Do **not** try to instantiate `LDClient` with its constructor
* (`new LDClient()/new LDClientImpl()/new LDClient()`); the SDK does not currently support
* this.
*
* @param sdkKey
* The client side SDK key. This is only used to query the edgeConfig above,
* not to connect with LaunchDarkly servers.
* @param edgeConfig
* The Vercel Edge Config client configured for LaunchDarkly.
* @param options
* Optional configuration settings. The only supported option is logger.
* @return
* The new {@link LDClient} instance.
*/
export const init = (sdkKey: string, edgeConfig: EdgeConfigClient, options: LDOptions = {}) => {
const logger = options.logger ?? BasicLogger.get();
// vercel does not support string gets so we have to stringify it
const edgeProvider: EdgeProvider = {
get: async (rootKey: string) => {
const json = await edgeConfig.get(rootKey);
return json ? JSON.stringify(json) : null;
},
};
return initEdge(sdkKey, createPlatformInfo(), {
featureStore: new EdgeFeatureStore(edgeProvider, sdkKey, 'Vercel', logger),
logger,
...options,
});
};