Skip to content

Commit f696cb9

Browse files
committed
feat(sdk): simplify init api and update tracking endpoints
1 parent 342becd commit f696cb9

8 files changed

Lines changed: 32 additions & 11 deletions

File tree

.changeset/calm-symbols-write.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
"@flagix/js-sdk": minor
3+
"@flagix/react": minor
4+
"@flagix/evaluation-core": minor
5+
---
6+
7+
\*Simplified SDK initialization by making apiBaseUrl internal
8+
9+
- Added support for \_\_internal_baseUrl for local development.
10+
11+
- Updated event endpoints to /sync to improve reliability.

.changeset/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"changelog": "@changesets/cli/changelog",
44
"commit": false,
55
"fixed": [],
6-
"linked": [],
6+
"linked": [["@flagix/evaluation-core", "@flagix/js-sdk", "@flagix/react"]],
77
"access": "public",
88
"baseBranch": "main",
99
"updateInternalDependencies": "patch",

apps/web/app/providers.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const queryClient = new QueryClient({
1616

1717
const SDK_OPTIONS = {
1818
apiKey: env.FLAGIX_SDK_API_KEY as string,
19-
apiBaseUrl: env.FLAGIX_SDK_BASE_URL as string,
19+
__internal_baseUrl: env.FLAGIX_SDK_BASE_URL,
2020
initialContext: {
2121
sessionId: "session-12345",
2222
platform: "web",

sdk/javascript/src/client.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { evaluateFlag, resolveIdentifier } from "@flagix/evaluation-core";
22
import {
33
EVENT_TO_LISTEN,
4+
FLAGIX_API_URL,
45
type FlagUpdateType,
56
REMOVE_TRAILING_SLASH,
67
} from "@/lib/constants";
@@ -15,6 +16,7 @@ import type {
1516
FlagConfig,
1617
FlagixClientOptions,
1718
FlagVariation,
19+
InternalFlagixOptions,
1820
VariationValue,
1921
} from "@/types";
2022

@@ -24,7 +26,7 @@ import type {
2426
*/
2527
export class FlagixClient {
2628
private readonly apiKey: string;
27-
private readonly apiBaseUrl: string;
29+
private readonly apiBaseUrl: string = FLAGIX_API_URL;
2830
private readonly localCache = new Map<string, FlagConfig>();
2931
private context: EvaluationContext;
3032
private isInitialized = false;
@@ -40,8 +42,11 @@ export class FlagixClient {
4042
private isConnectingSSE = false;
4143

4244
constructor(options: FlagixClientOptions) {
45+
const internalOpts = options as InternalFlagixOptions;
46+
this.apiBaseUrl =
47+
internalOpts.__internal_baseUrl?.replace(REMOVE_TRAILING_SLASH, "") ??
48+
FLAGIX_API_URL;
4349
this.apiKey = options.apiKey;
44-
this.apiBaseUrl = options.apiBaseUrl.replace(REMOVE_TRAILING_SLASH, "");
4550
this.context = options.initialContext || {};
4651
this.emitter = new FlagixEventEmitter();
4752
setLogLevel(options.logs?.level ?? "none");

sdk/javascript/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { FLAG_UPDATE_EVENT } from "@/lib/emitter";
33
import { log } from "@/lib/logger";
44
import type {
55
EvaluationContext,
6-
FlagixClientOptions,
6+
InternalFlagixOptions,
77
VariationValue,
88
} from "@/types";
99

@@ -16,7 +16,7 @@ export const Flagix = {
1616
/**
1717
* Initializes the Flagix SDK, fetches all flags, and sets up an SSE connection.
1818
*/
19-
async initialize(options: FlagixClientOptions): Promise<void> {
19+
async initialize(options: InternalFlagixOptions): Promise<void> {
2020
// this check ensures that we are able to watch for api key changes and re-initialize accordingly
2121
// this ensures we dont use stale clients across different api keys
2222
if (clientInstance) {

sdk/javascript/src/lib/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ export type FlagUpdateType =
1010
| "RULE_DELETED";
1111

1212
export const EVENT_TO_LISTEN = "flag-update";
13+
export const FLAGIX_API_URL = "https://api.flagix.com";

sdk/javascript/src/types.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ export type {
1111
export type FlagixClientOptions = {
1212
/** The API Key for the environment */
1313
apiKey: string;
14-
/** The base URL for the flag evaluation API */
15-
apiBaseUrl: string;
1614
/** Optional context attributes */
1715
initialContext?: EvaluationContext;
1816
/** Enable SDK logging */
1917
logs?: {
2018
level?: LogLevel; // default: "none"
2119
};
2220
};
21+
22+
export type InternalFlagixOptions = FlagixClientOptions & {
23+
__internal_baseUrl?: string;
24+
};

sdk/react/src/flagix-provider.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
type EvaluationContext,
55
Flagix,
66
type FlagixClientOptions,
7+
type InternalFlagixOptions,
78
} from "@flagix/js-sdk";
89
import type React from "react";
910
import { createContext, useContext, useEffect, useMemo, useState } from "react";
@@ -28,13 +29,14 @@ export const FlagixProvider = ({
2829
}: FlagixProviderProps) => {
2930
const [isReady, setIsReady] = useState(() => Flagix.isInitialized());
3031
const [error, setError] = useState<Error | null>(null);
31-
const { apiKey, apiBaseUrl } = options;
32+
const internalOptions = options as InternalFlagixOptions;
33+
const { apiKey, __internal_baseUrl } = internalOptions;
3234

3335
// biome-ignore lint/correctness/useExhaustiveDependencies: <>
3436
useEffect(() => {
3537
let mounted = true;
3638

37-
Flagix.initialize(options)
39+
Flagix.initialize(internalOptions)
3840
.then(() => {
3941
if (mounted) {
4042
setIsReady(true);
@@ -52,7 +54,7 @@ export const FlagixProvider = ({
5254
mounted = false;
5355
Flagix.close();
5456
};
55-
}, [apiKey, apiBaseUrl]);
57+
}, [apiKey, __internal_baseUrl]);
5658

5759
useEffect(() => {
5860
if (isReady && context) {

0 commit comments

Comments
 (0)