@mergedapp/feature-flags 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.js ADDED
@@ -0,0 +1,213 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // src/react/provider.tsx
5
+ import { createContext, useEffect, useMemo, useState, useSyncExternalStore } from "react";
6
+ var FeatureFlagContext = /* @__PURE__ */ createContext(null);
7
+ function createFeatureFlagLoadState(params) {
8
+ return {
9
+ status: params.status,
10
+ isLoading: params.status === "loading",
11
+ isReady: params.status === "ready",
12
+ error: params.error ?? params.runtimeStatus.lastError,
13
+ source: params.runtimeStatus.source,
14
+ isStale: params.runtimeStatus.isStale,
15
+ lastSuccessfulRefreshAt: params.runtimeStatus.lastSuccessfulRefreshAt,
16
+ tokenExpiresAt: params.runtimeStatus.tokenExpiresAt
17
+ };
18
+ }
19
+ __name(createFeatureFlagLoadState, "createFeatureFlagLoadState");
20
+ function shouldRenderFeatureFlagLoadingFallback(params) {
21
+ return params.blockUntilReady && params.loadState.isLoading;
22
+ }
23
+ __name(shouldRenderFeatureFlagLoadingFallback, "shouldRenderFeatureFlagLoadingFallback");
24
+ function createSnapshotStore(params) {
25
+ let cachedFlags = params.initialFlags ?? params.client.getSnapshot();
26
+ let hasObservedClientUpdate = false;
27
+ function areSnapshotsEqual(left, right) {
28
+ if (left.length !== right.length) {
29
+ return false;
30
+ }
31
+ for (let index = 0; index < left.length; index++) {
32
+ const leftFlag = left[index];
33
+ const rightFlag = right[index];
34
+ if (leftFlag?.id !== rightFlag?.id || leftFlag?.name !== rightFlag?.name || leftFlag?.type !== rightFlag?.type || leftFlag?.teamId !== rightFlag?.teamId || leftFlag?.enabled !== rightFlag?.enabled || JSON.stringify(leftFlag?.value) !== JSON.stringify(rightFlag?.value)) {
35
+ return false;
36
+ }
37
+ }
38
+ return true;
39
+ }
40
+ __name(areSnapshotsEqual, "areSnapshotsEqual");
41
+ return {
42
+ subscribe(onStoreChange) {
43
+ return params.client.subscribe(() => {
44
+ hasObservedClientUpdate = true;
45
+ onStoreChange();
46
+ });
47
+ },
48
+ getSnapshot() {
49
+ const current = params.client.getSnapshot();
50
+ const shouldAdoptClientSnapshot = current.length > 0 || !params.initialFlags || hasObservedClientUpdate;
51
+ if (shouldAdoptClientSnapshot && !areSnapshotsEqual(current, cachedFlags)) {
52
+ cachedFlags = current;
53
+ }
54
+ return cachedFlags;
55
+ },
56
+ getServerSnapshot() {
57
+ return params.initialFlags ?? [];
58
+ }
59
+ };
60
+ }
61
+ __name(createSnapshotStore, "createSnapshotStore");
62
+ function FeatureFlagProvider(props) {
63
+ const { client, blockUntilReady, loadingFallback, initialFlags, children } = props;
64
+ const [status, setStatus] = useState(initialFlags ? "ready" : "loading");
65
+ const [error, setError] = useState(null);
66
+ useEffect(() => {
67
+ let isDisposed = false;
68
+ setStatus(initialFlags ? "ready" : "loading");
69
+ setError(null);
70
+ client.initialize().then(() => {
71
+ if (isDisposed) {
72
+ return;
73
+ }
74
+ setStatus("ready");
75
+ }).catch((initializationError) => {
76
+ if (isDisposed) {
77
+ return;
78
+ }
79
+ setStatus("error");
80
+ setError(initializationError instanceof Error ? initializationError : new Error("Unknown feature flag initialization error."));
81
+ });
82
+ return () => {
83
+ isDisposed = true;
84
+ client.destroy();
85
+ };
86
+ }, [
87
+ client,
88
+ initialFlags
89
+ ]);
90
+ const snapshotStore = useMemo(() => createSnapshotStore({
91
+ client,
92
+ initialFlags
93
+ }), [
94
+ client,
95
+ initialFlags
96
+ ]);
97
+ const flags = useSyncExternalStore(snapshotStore.subscribe, snapshotStore.getSnapshot, snapshotStore.getServerSnapshot);
98
+ const runtimeStatus = useSyncExternalStore(client.subscribe, client.getStatus.bind(client), client.getStatus.bind(client));
99
+ const loadState = useMemo(() => createFeatureFlagLoadState({
100
+ status,
101
+ error,
102
+ runtimeStatus
103
+ }), [
104
+ error,
105
+ runtimeStatus,
106
+ status
107
+ ]);
108
+ const value = useMemo(() => ({
109
+ client,
110
+ flags,
111
+ loadState
112
+ }), [
113
+ client,
114
+ flags,
115
+ loadState
116
+ ]);
117
+ if (shouldRenderFeatureFlagLoadingFallback({
118
+ blockUntilReady,
119
+ loadState
120
+ })) {
121
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, loadingFallback ?? null);
122
+ }
123
+ return /* @__PURE__ */ React.createElement(FeatureFlagContext.Provider, {
124
+ value
125
+ }, children);
126
+ }
127
+ __name(FeatureFlagProvider, "FeatureFlagProvider");
128
+
129
+ // src/react/hooks.ts
130
+ import { useContext } from "react";
131
+ function useFeatureFlagContext() {
132
+ const context = useContext(FeatureFlagContext);
133
+ if (!context) {
134
+ throw new Error("Feature flag hooks and components must be used within a <FeatureFlagProvider>.");
135
+ }
136
+ return context;
137
+ }
138
+ __name(useFeatureFlagContext, "useFeatureFlagContext");
139
+ function resolveFlagFromContext(params) {
140
+ return params.flags.find((flag) => flag.id === params.idOrName || flag.name === params.idOrName) ?? params.client.getFlag(params.idOrName);
141
+ }
142
+ __name(resolveFlagFromContext, "resolveFlagFromContext");
143
+ function useFeatureFlagStatus() {
144
+ const { loadState } = useFeatureFlagContext();
145
+ return loadState;
146
+ }
147
+ __name(useFeatureFlagStatus, "useFeatureFlagStatus");
148
+ function createTypedHooks() {
149
+ function TypedFeatureFlagProvider(props) {
150
+ return FeatureFlagProvider(props);
151
+ }
152
+ __name(TypedFeatureFlagProvider, "TypedFeatureFlagProvider");
153
+ function typedUseFeatureFlag(name) {
154
+ const { client, flags } = useFeatureFlagContext();
155
+ const flag = resolveFlagFromContext({
156
+ client,
157
+ flags,
158
+ idOrName: name
159
+ });
160
+ return {
161
+ enabled: flag?.enabled ?? false,
162
+ value: flag?.value
163
+ };
164
+ }
165
+ __name(typedUseFeatureFlag, "typedUseFeatureFlag");
166
+ function typedUseFeatureFlags() {
167
+ const { flags } = useFeatureFlagContext();
168
+ return flags;
169
+ }
170
+ __name(typedUseFeatureFlags, "typedUseFeatureFlags");
171
+ function typedUseFeatureFlagClient() {
172
+ const { client } = useFeatureFlagContext();
173
+ return client;
174
+ }
175
+ __name(typedUseFeatureFlagClient, "typedUseFeatureFlagClient");
176
+ function typedUseFeatureFlagStatus() {
177
+ return useFeatureFlagStatus();
178
+ }
179
+ __name(typedUseFeatureFlagStatus, "typedUseFeatureFlagStatus");
180
+ const TypedFeatureFlag = /* @__PURE__ */ __name((props) => {
181
+ const { children, fallback, loading, matchValue, name } = props;
182
+ const status = typedUseFeatureFlagStatus();
183
+ const { enabled, value } = typedUseFeatureFlag(name);
184
+ if (status.isLoading) {
185
+ return loading ?? fallback ?? null;
186
+ }
187
+ const shouldRenderChildren = typeof matchValue === "undefined" ? enabled : Object.is(value, matchValue);
188
+ if (!shouldRenderChildren) {
189
+ return fallback ?? null;
190
+ }
191
+ if (typeof children === "function") {
192
+ return children({
193
+ enabled,
194
+ value,
195
+ status
196
+ });
197
+ }
198
+ return children;
199
+ }, "TypedFeatureFlag");
200
+ return {
201
+ FeatureFlagProvider: TypedFeatureFlagProvider,
202
+ FeatureFlag: TypedFeatureFlag,
203
+ useFeatureFlag: typedUseFeatureFlag,
204
+ useFeatureFlags: typedUseFeatureFlags,
205
+ useFeatureFlagClient: typedUseFeatureFlagClient,
206
+ useFeatureFlagStatus: typedUseFeatureFlagStatus
207
+ };
208
+ }
209
+ __name(createTypedHooks, "createTypedHooks");
210
+ export {
211
+ FeatureFlagProvider,
212
+ createTypedHooks
213
+ };
package/package.json ADDED
@@ -0,0 +1,68 @@
1
+ {
2
+ "name": "@mergedapp/feature-flags",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "description": "Type-safe client SDK for Merged feature flags with ES256 JWT verification",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/lubeorg/lube-monorepo",
9
+ "directory": "packages/feature-flags-sdk"
10
+ },
11
+ "homepage": "https://github.com/lubeorg/lube-monorepo/tree/main/packages/feature-flags-sdk",
12
+ "bugs": {
13
+ "url": "https://github.com/lubeorg/lube-monorepo/issues"
14
+ },
15
+ "main": "./dist/index.cjs",
16
+ "module": "./dist/index.js",
17
+ "types": "./dist/index.d.ts",
18
+ "bin": {
19
+ "merged-ff": "./dist/cli.js"
20
+ },
21
+ "exports": {
22
+ ".": {
23
+ "types": "./dist/index.d.ts",
24
+ "import": "./dist/index.js",
25
+ "require": "./dist/index.cjs"
26
+ },
27
+ "./react": {
28
+ "types": "./dist/react.d.ts",
29
+ "import": "./dist/react.js",
30
+ "require": "./dist/react.cjs"
31
+ }
32
+ },
33
+ "publishConfig": {
34
+ "access": "public"
35
+ },
36
+ "files": [
37
+ "dist"
38
+ ],
39
+ "scripts": {
40
+ "build": "tsup",
41
+ "dev": "tsup --watch",
42
+ "test": "vitest run",
43
+ "check-types": "tsc --noEmit"
44
+ },
45
+ "dependencies": {
46
+ "jose": "^6.0.0"
47
+ },
48
+ "peerDependencies": {
49
+ "react": "^18.0.0 || ^19.0.0"
50
+ },
51
+ "peerDependenciesMeta": {
52
+ "react": {
53
+ "optional": true
54
+ }
55
+ },
56
+ "devDependencies": {
57
+ "@repo/types-v2": "workspace:*",
58
+ "@repo/typescript-config": "workspace:*",
59
+ "@types/node": "^25.5.0",
60
+ "@types/react": "^19.1.4",
61
+ "@types/react-dom": "^19.1.5",
62
+ "react": "^19.2.0",
63
+ "react-dom": "^19.2.0",
64
+ "tsup": "^8.0.0",
65
+ "typescript": "^5.9.3",
66
+ "vitest": "^3.0.5"
67
+ }
68
+ }