@flagix/js-sdk 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,33 @@
1
+ import type { EventSourceInit } from "eventsource";
2
+ import { log } from "@/lib/logger";
3
+
4
+ export type FlagStreamConnection = {
5
+ onopen: ((this: FlagStreamConnection, ev: Event) => void) | null;
6
+ onerror: ((this: FlagStreamConnection, ev: Event) => void) | null;
7
+ addEventListener: (
8
+ type: string,
9
+ listener: (event: MessageEvent) => void
10
+ ) => void;
11
+ close: () => void;
12
+ };
13
+
14
+ export async function createEventSource(
15
+ url: string,
16
+ apiKey: string
17
+ ): Promise<FlagStreamConnection | null> {
18
+ if (typeof window !== "undefined" && "EventSource" in window) {
19
+ return new EventSource(`${url}?apiKey=${apiKey}`) as FlagStreamConnection;
20
+ }
21
+
22
+ try {
23
+ // Dynamic import to prevent bundlers from including it in client code
24
+ const { EventSource: NodeEventSource } = await import("eventsource");
25
+
26
+ return new NodeEventSource(url, {
27
+ headers: { "X-Api-Key": apiKey },
28
+ } as EventSourceInit) as FlagStreamConnection;
29
+ } catch (error) {
30
+ log("warn", "[Flagix SDK] SSE not supported in this environment.", error);
31
+ return null;
32
+ }
33
+ }
package/src/types.ts ADDED
@@ -0,0 +1,22 @@
1
+ import type { EvaluationContext } from "@flagix/evaluation-core";
2
+ import type { LogLevel } from "@/lib/logger";
3
+
4
+ export type {
5
+ EvaluationContext,
6
+ FlagConfig,
7
+ FlagVariation,
8
+ VariationValue,
9
+ } from "@flagix/evaluation-core";
10
+
11
+ export type FlagixClientOptions = {
12
+ /** The API Key for the environment */
13
+ apiKey: string;
14
+ /** The base URL for the flag evaluation API */
15
+ apiBaseUrl: string;
16
+ /** Optional context attributes */
17
+ initialContext?: EvaluationContext;
18
+ /** Enable SDK logging */
19
+ logs?: {
20
+ level?: LogLevel; // default: "none"
21
+ };
22
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "target": "ES2020",
5
+ "module": "NodeNext",
6
+ "moduleResolution": "NodeNext",
7
+ "outDir": "dist",
8
+ "rootDir": "src",
9
+ "declaration": true,
10
+ "strict": true,
11
+ "esModuleInterop": true,
12
+ "skipLibCheck": true,
13
+ "allowSyntheticDefaultImports": true,
14
+ "typeRoots": ["./node_modules/@types", "./types"],
15
+ "baseUrl": ".",
16
+ "paths": {
17
+ "@/*": ["./src/*"]
18
+ }
19
+ },
20
+ "include": ["src", "src/types", "../../types"]
21
+ }