Skip to content

Commit

Permalink
feat: use server-side API for metrics if available (#91)
Browse files Browse the repository at this point in the history
* feat: use server-side api for metrics if available
* simplify flags client config
* refactor: rename backend url to server url for consistency
  • Loading branch information
Tymek authored Oct 16, 2024
1 parent b1db5c1 commit a24e2e2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
19 changes: 16 additions & 3 deletions lib/src/flagsClient.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
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
*/
export const flagsClient = (toggles = [] as IToggle[]) => {
const client = new UnleashClient({
bootstrap: toggles,
...getDefaultClientConfig(),
fetch: () => null,
...getMetricsConfig(),
createAbortController: () => null,
refreshInterval: 0,
metricsInterval: 0,
Expand Down
8 changes: 4 additions & 4 deletions lib/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -61,4 +61,4 @@ export const getDefaultClientConfig = () => {
};

export const removeTrailingSlash = (url?: string) =>
url && url.replace(/\/$/, "");
url?.replace(/\/$/, "");

0 comments on commit a24e2e2

Please sign in to comment.