-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgetFrontendFlags.ts
42 lines (38 loc) · 1.08 KB
/
getFrontendFlags.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
import type { IToggle, IMutableContext } from "unleash-proxy-client";
import { UnleashClient } from "unleash-proxy-client";
import { getDefaultClientConfig } from "./utils";
/**
* Create a client-side SDK instance and get evaluated flags from Unleash Proxy or Client API
* @see clientOptions https://github.com/Unleash/unleash-proxy-client-js#available-options
*/
export const getFrontendFlags = async (
config?: Partial<ConstructorParameters<typeof UnleashClient>[0]>
) =>
new Promise<{ toggles: IToggle[] }>((resolve, reject) => {
const unleash = new UnleashClient({
...getDefaultClientConfig(),
...(config || {}),
disableRefresh: true,
disableMetrics: true,
});
unleash.on("ready", () => {
resolve({ toggles: unleash.getAllToggles() });
unleash.stop();
});
unleash.on(
"error",
(
error:
| Error
| {
type: "HttpError";
code: number;
}
| unknown
) => {
reject(error);
unleash.stop();
}
);
unleash.start();
});