@getflaggy/sdk 0.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.
package/dist/react.mjs ADDED
@@ -0,0 +1,95 @@
1
+ import {
2
+ FlaggyClient
3
+ } from "./chunk-HBGNXCEV.mjs";
4
+
5
+ // src/react/FlaggyProvider.tsx
6
+ import { useEffect, useRef, useState, useMemo } from "react";
7
+
8
+ // src/react/context.ts
9
+ import { createContext } from "react";
10
+ var FlaggyReactContext = createContext(null);
11
+
12
+ // src/react/FlaggyProvider.tsx
13
+ import { jsx } from "react/jsx-runtime";
14
+ function FlaggyProvider({
15
+ serverUrl,
16
+ apiKey,
17
+ context,
18
+ enableStreaming,
19
+ onError,
20
+ children
21
+ }) {
22
+ const clientRef = useRef(null);
23
+ const [ready, setReady] = useState(false);
24
+ const [error, setError] = useState(null);
25
+ const [, setVersion] = useState(0);
26
+ useEffect(() => {
27
+ const client = new FlaggyClient({
28
+ serverUrl,
29
+ apiKey,
30
+ context,
31
+ enableStreaming
32
+ });
33
+ clientRef.current = client;
34
+ setReady(false);
35
+ setError(null);
36
+ const unsubReady = client.on("ready", () => setReady(true));
37
+ const unsubError = client.on("error", (err) => {
38
+ setError(err);
39
+ onError?.(err);
40
+ });
41
+ const unsubChange = client.on("change", () => {
42
+ setVersion((v) => v + 1);
43
+ });
44
+ client.initialize();
45
+ return () => {
46
+ unsubReady();
47
+ unsubError();
48
+ unsubChange();
49
+ client.destroy();
50
+ clientRef.current = null;
51
+ };
52
+ }, [serverUrl, apiKey]);
53
+ const contextKey = context ? JSON.stringify(context) : "";
54
+ useEffect(() => {
55
+ if (clientRef.current && context && clientRef.current.ready) {
56
+ clientRef.current.setContext(context);
57
+ }
58
+ }, [contextKey]);
59
+ const value = useMemo(
60
+ () => clientRef.current ? { client: clientRef.current, ready, error } : null,
61
+ [ready, error]
62
+ );
63
+ if (!value) return null;
64
+ return /* @__PURE__ */ jsx(FlaggyReactContext.Provider, { value, children });
65
+ }
66
+
67
+ // src/react/useFlag.ts
68
+ import { useContext } from "react";
69
+ function useFlag(key, defaultValue) {
70
+ const ctx = useContext(FlaggyReactContext);
71
+ if (!ctx) {
72
+ return defaultValue;
73
+ }
74
+ return ctx.client.getFlag(key, defaultValue);
75
+ }
76
+
77
+ // src/react/useFlaggy.ts
78
+ import { useContext as useContext2 } from "react";
79
+ function useFlaggy() {
80
+ const ctx = useContext2(FlaggyReactContext);
81
+ if (!ctx) {
82
+ throw new Error("[flaggy] useFlaggy() must be used within a <FlaggyProvider>.");
83
+ }
84
+ return {
85
+ client: ctx.client,
86
+ ready: ctx.ready,
87
+ error: ctx.error
88
+ };
89
+ }
90
+ export {
91
+ FlaggyProvider,
92
+ useFlag,
93
+ useFlaggy
94
+ };
95
+ //# sourceMappingURL=react.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/react/FlaggyProvider.tsx","../src/react/context.ts","../src/react/useFlag.ts","../src/react/useFlaggy.ts"],"sourcesContent":["import { useEffect, useRef, useState, useMemo, type ReactNode } from 'react';\nimport { FlaggyClient } from '../client';\nimport { FlaggyReactContext } from './context';\nimport type { FlaggyContext } from '../types';\n\nexport interface FlaggyProviderProps {\n serverUrl: string;\n apiKey: string;\n context?: FlaggyContext;\n enableStreaming?: boolean;\n /** Called when an error occurs (init failure, SSE error, etc.) */\n onError?: (error: Error) => void;\n children: ReactNode;\n}\n\nexport function FlaggyProvider({\n serverUrl,\n apiKey,\n context,\n enableStreaming,\n onError,\n children,\n}: FlaggyProviderProps) {\n const clientRef = useRef<FlaggyClient | null>(null);\n const [ready, setReady] = useState(false);\n const [error, setError] = useState<Error | null>(null);\n const [, setVersion] = useState(0);\n\n // Create and initialize client when serverUrl or apiKey change\n useEffect(() => {\n const client = new FlaggyClient({\n serverUrl,\n apiKey,\n context,\n enableStreaming,\n });\n clientRef.current = client;\n setReady(false);\n setError(null);\n\n const unsubReady = client.on('ready', () => setReady(true));\n const unsubError = client.on('error', (err) => {\n setError(err);\n onError?.(err);\n });\n const unsubChange = client.on('change', () => {\n setVersion((v) => v + 1);\n });\n\n client.initialize();\n\n return () => {\n unsubReady();\n unsubError();\n unsubChange();\n client.destroy();\n clientRef.current = null;\n };\n }, [serverUrl, apiKey]);\n\n // Update context when it changes (deep comparison via JSON.stringify)\n const contextKey = context ? JSON.stringify(context) : '';\n useEffect(() => {\n if (clientRef.current && context && clientRef.current.ready) {\n clientRef.current.setContext(context);\n }\n }, [contextKey]);\n\n const value = useMemo(\n () =>\n clientRef.current\n ? { client: clientRef.current, ready, error }\n : null,\n [ready, error],\n );\n\n if (!value) return null;\n\n return (\n <FlaggyReactContext.Provider value={value}>\n {children}\n </FlaggyReactContext.Provider>\n );\n}\n","import { createContext } from 'react';\nimport type { FlaggyClient } from '../client';\n\nexport interface FlaggyContextValue {\n client: FlaggyClient;\n ready: boolean;\n error: Error | null;\n}\n\nexport const FlaggyReactContext = createContext<FlaggyContextValue | null>(null);\n","import { useContext } from 'react';\nimport { FlaggyReactContext } from './context';\nimport type { FlagValue } from '../types';\n\nexport function useFlag<T extends FlagValue>(key: string, defaultValue: T): T {\n const ctx = useContext(FlaggyReactContext);\n\n if (!ctx) {\n return defaultValue;\n }\n\n return ctx.client.getFlag(key, defaultValue);\n}\n","import { useContext } from 'react';\nimport { FlaggyReactContext } from './context';\n\nexport function useFlaggy() {\n const ctx = useContext(FlaggyReactContext);\n\n if (!ctx) {\n throw new Error('[flaggy] useFlaggy() must be used within a <FlaggyProvider>.');\n }\n\n return {\n client: ctx.client,\n ready: ctx.ready,\n error: ctx.error,\n };\n}\n"],"mappings":";;;;;AAAA,SAAS,WAAW,QAAQ,UAAU,eAA+B;;;ACArE,SAAS,qBAAqB;AASvB,IAAM,qBAAqB,cAAyC,IAAI;;;ADsE3E;AAhEG,SAAS,eAAe;AAAA,EAC7B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAAwB;AACtB,QAAM,YAAY,OAA4B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MAAM,WAAW,kBAAkB;AAEzC,MAAI,CAAC,KAAK;AACR,WAAO;AAAA,EACT;AAEA,SAAO,IAAI,OAAO,QAAQ,KAAK,YAAY;AAC7C;;;ACZA,SAAS,cAAAA,mBAAkB;AAGpB,SAAS,YAAY;AAC1B,QAAM,MAAMC,YAAW,kBAAkB;AAEzC,MAAI,CAAC,KAAK;AACR,UAAM,IAAI,MAAM,8DAA8D;AAAA,EAChF;AAEA,SAAO;AAAA,IACL,QAAQ,IAAI;AAAA,IACZ,OAAO,IAAI;AAAA,IACX,OAAO,IAAI;AAAA,EACb;AACF;","names":["useContext","useContext"]}
package/package.json ADDED
@@ -0,0 +1,62 @@
1
+ {
2
+ "name": "@getflaggy/sdk",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "description": "JavaScript/React SDK for the Flaggy feature flag server",
6
+ "license": "MIT",
7
+ "sideEffects": false,
8
+ "exports": {
9
+ ".": {
10
+ "import": {
11
+ "types": "./dist/index.d.mts",
12
+ "default": "./dist/index.mjs"
13
+ },
14
+ "require": {
15
+ "types": "./dist/index.d.cts",
16
+ "default": "./dist/index.cjs"
17
+ }
18
+ },
19
+ "./react": {
20
+ "import": {
21
+ "types": "./dist/react.d.mts",
22
+ "default": "./dist/react.mjs"
23
+ },
24
+ "require": {
25
+ "types": "./dist/react.d.cts",
26
+ "default": "./dist/react.cjs"
27
+ }
28
+ }
29
+ },
30
+ "main": "./dist/index.cjs",
31
+ "module": "./dist/index.mjs",
32
+ "types": "./dist/index.d.mts",
33
+ "files": [
34
+ "dist"
35
+ ],
36
+ "scripts": {
37
+ "build": "tsup",
38
+ "dev": "tsup --watch",
39
+ "typecheck": "tsc --noEmit",
40
+ "test": "vitest run",
41
+ "test:watch": "vitest",
42
+ "prepublishOnly": "npm run build"
43
+ },
44
+ "peerDependencies": {
45
+ "react": ">=18.0.0"
46
+ },
47
+ "peerDependenciesMeta": {
48
+ "react": {
49
+ "optional": true
50
+ }
51
+ },
52
+ "devDependencies": {
53
+ "@testing-library/react": "^16.0.0",
54
+ "@types/react": "^19.0.0",
55
+ "jsdom": "^25.0.0",
56
+ "react": "^19.0.0",
57
+ "react-dom": "^19.0.0",
58
+ "tsup": "^8.0.0",
59
+ "typescript": "^5.5.0",
60
+ "vitest": "^2.0.0"
61
+ }
62
+ }