diff --git a/lib/src/flagsClient.ts b/lib/src/flagsClient.ts index d6ccd93..9b50263 100644 --- a/lib/src/flagsClient.ts +++ b/lib/src/flagsClient.ts @@ -1,5 +1,19 @@ import { UnleashClient, type IToggle } from "unleash-proxy-client"; -import { getDefaultClientConfig } from "./utils"; +import { getDefaultClientConfig, getServerBaseUrl } from "./utils"; + +const getMetricsConfig = () => { + const serverUrl = getServerBaseUrl() + + if (serverUrl && process.env.UNLEASH_SERVER_API_TOKEN){ + return { + ...getDefaultClientConfig(), + url: serverUrl, + clientKey: process.env.UNLEASH_SERVER_API_TOKEN, + } + } + + return getDefaultClientConfig() +} /** * Simplified client SDK to work offline with pre-evaluated flags @@ -7,8 +21,7 @@ import { getDefaultClientConfig } from "./utils"; export const flagsClient = (toggles = [] as IToggle[]) => { const client = new UnleashClient({ bootstrap: toggles, - ...getDefaultClientConfig(), - fetch: () => null, + ...getMetricsConfig(), createAbortController: () => null, refreshInterval: 0, metricsInterval: 0, diff --git a/lib/src/utils.ts b/lib/src/utils.ts index 8f9718c..2529fce 100644 --- a/lib/src/utils.ts +++ b/lib/src/utils.ts @@ -5,8 +5,8 @@ * * @see https://github.com/Bruce17/safe-compare/blob/a96e0fb1dd1b6e998f657b43987c5b7a6d48186e/index.js#L12-L38 */ -export const safeCompare = function safeCompare(a: string, b: string) { - let strA = String(a); +export const safeCompare = (a: string, b: string) => { + const strA = String(a); let strB = String(b); const lenA = strA.length; let result = 0; @@ -38,7 +38,7 @@ const getFrontendUrl = () => process.env.UNLEASH_FRONTEND_API_URL || process.env.NEXT_PUBLIC_UNLEASH_FRONTEND_API_URL; -const getServerBaseUrl = () => +export const getServerBaseUrl = () => process.env.UNLEASH_SERVER_API_URL || process.env.NEXT_PUBLIC_UNLEASH_SERVER_API_URL; @@ -61,4 +61,4 @@ export const getDefaultClientConfig = () => { }; export const removeTrailingSlash = (url?: string) => - url && url.replace(/\/$/, ""); + url?.replace(/\/$/, "");