@geejay/use-feature-flags 1.0.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/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # use-feature-flags
2
+
3
+ A lightweight React hook for fetching feature flags from Supabase. It ships with sensible defaults so you can drop it in and start using feature flags immediately.
4
+
5
+ ## Installation
6
+
7
+ ```
8
+ npm install use-feature-flags
9
+ ```
10
+
11
+ or
12
+
13
+ ```
14
+ yarn add use-feature-flags
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```tsx
20
+ import { useFeatureFlags } from 'use-feature-flags';
21
+
22
+ export default function MyComponent() {
23
+ const { isActive, loading } = useFeatureFlags();
24
+
25
+ if (loading) return null;
26
+
27
+ return isActive('new-dashboard') ? <NewDashboard /> : <OldDashboard />;
28
+ }
29
+ ```
30
+
31
+ By default the hook connects to the Supabase instance defined in `DEFAULT_SUPABASE_URL`, `DEFAULT_SUPABASE_KEY` and `DEFAULT_TENANT_ID`. Update these constants in `useFeatureFlags.ts` if you need different defaults.
32
+
33
+ ## Building
34
+
35
+ Run the following inside the package directory:
36
+
37
+ ```
38
+ npm run build
39
+ ```
40
+
41
+ This compiles the TypeScript sources to the `dist` folder.
42
+
43
+ ## Publishing
44
+
45
+ After building, you can publish the package to npm:
46
+
47
+ ```
48
+ npm publish --access public
49
+ ```
50
+
51
+ Make sure you have configured your npm credentials before publishing.
52
+
@@ -0,0 +1,2 @@
1
+ export * from './useFeatureFlags';
2
+ export * from './store';
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from './useFeatureFlags';
2
+ export * from './store';
@@ -0,0 +1,17 @@
1
+ export type FeatureFlag = {
2
+ id: string;
3
+ key: string;
4
+ enabled: boolean;
5
+ environment: string;
6
+ };
7
+ export declare const featureFlagsAtom: import("jotai").PrimitiveAtom<{
8
+ flags: FeatureFlag[];
9
+ loading: boolean;
10
+ }> & {
11
+ init: {
12
+ flags: FeatureFlag[];
13
+ loading: boolean;
14
+ };
15
+ };
16
+ export declare function setApiKey(key: string): void;
17
+ export declare function getApiKey(): string | null;
package/dist/store.js ADDED
@@ -0,0 +1,13 @@
1
+ import { atom } from 'jotai';
2
+ export const featureFlagsAtom = atom({
3
+ flags: [],
4
+ loading: true,
5
+ });
6
+ // store.ts
7
+ let apiKey = null;
8
+ export function setApiKey(key) {
9
+ apiKey = key;
10
+ }
11
+ export function getApiKey() {
12
+ return apiKey;
13
+ }
@@ -0,0 +1,7 @@
1
+ export declare const DEFAULT_SUPABASE_URL = "https://khppgsehvvlukzfdqbuo.supabase.co";
2
+ export declare const DEFAULT_SUPABASE_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImtocHBnc2VodnZsdWt6ZmRxYnVvIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTI2ODU1MzQsImV4cCI6MjA2ODI2MTUzNH0.8Z4VY4HFMm95UgO21c-DnDkbLPN_0mbDBZJPExaghDk";
3
+ export declare function useFeatureFlags(passedKey?: string, environment?: string): {
4
+ isActive: (key: string) => boolean;
5
+ flags: import("./store").FeatureFlag[];
6
+ loading: boolean;
7
+ };
@@ -0,0 +1,78 @@
1
+ import { useEffect, useRef } from 'react';
2
+ import { useAtom } from 'jotai';
3
+ import { createClient } from '@supabase/supabase-js';
4
+ import { featureFlagsAtom, getApiKey, setApiKey } from './store';
5
+ // Default config
6
+ export const DEFAULT_SUPABASE_URL = 'https://khppgsehvvlukzfdqbuo.supabase.co';
7
+ export const DEFAULT_SUPABASE_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImtocHBnc2VodnZsdWt6ZmRxYnVvIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTI2ODU1MzQsImV4cCI6MjA2ODI2MTUzNH0.8Z4VY4HFMm95UgO21c-DnDkbLPN_0mbDBZJPExaghDk'; // Keep this secured
8
+ let initialized = false;
9
+ export function useFeatureFlags(passedKey, environment = typeof window !== 'undefined' ? window.location.hostname : 'localhost') {
10
+ const [state, setState] = useAtom(featureFlagsAtom);
11
+ const debounceTimeout = useRef(null);
12
+ if (passedKey && passedKey !== getApiKey()) {
13
+ setApiKey(passedKey);
14
+ }
15
+ const apiKey = getApiKey();
16
+ if (!apiKey) {
17
+ console.warn('API key is missing');
18
+ return {
19
+ isActive: () => false,
20
+ flags: [],
21
+ loading: false,
22
+ };
23
+ }
24
+ const supabase = createClient(DEFAULT_SUPABASE_URL, DEFAULT_SUPABASE_KEY);
25
+ const fetchFlags = async () => {
26
+ setState((prev) => ({ ...prev, loading: true }));
27
+ try {
28
+ const response = await fetch(`${DEFAULT_SUPABASE_URL}/functions/v1/get-feature-flags`, {
29
+ method: 'POST',
30
+ headers: {
31
+ 'Content-Type': 'application/json',
32
+ 'api-key': apiKey,
33
+ },
34
+ body: JSON.stringify({ environment }),
35
+ });
36
+ const { flags } = await response.json();
37
+ if (!response.ok || !Array.isArray(flags)) {
38
+ console.warn('Invalid response from edge function');
39
+ setState({ flags: [], loading: false });
40
+ return;
41
+ }
42
+ setState({ flags, loading: false });
43
+ }
44
+ catch (err) {
45
+ console.error('Unexpected error fetching flags:', err.message);
46
+ setState({ flags: [], loading: false });
47
+ }
48
+ };
49
+ useEffect(() => {
50
+ if (!apiKey || initialized)
51
+ return;
52
+ initialized = true;
53
+ fetchFlags();
54
+ const channel = supabase
55
+ .channel(`flags-${environment}`)
56
+ .on('postgres_changes', {
57
+ event: '*',
58
+ schema: 'public',
59
+ table: 'feature_flags',
60
+ }, () => {
61
+ if (debounceTimeout.current)
62
+ clearTimeout(debounceTimeout.current);
63
+ debounceTimeout.current = setTimeout(fetchFlags, 300);
64
+ })
65
+ .subscribe();
66
+ return () => {
67
+ if (debounceTimeout.current)
68
+ clearTimeout(debounceTimeout.current);
69
+ supabase.removeChannel(channel);
70
+ };
71
+ }, [environment, apiKey]);
72
+ const isActive = (key) => state.flags.some((flag) => flag.key === key && flag.enabled);
73
+ return {
74
+ isActive,
75
+ flags: state.flags,
76
+ loading: state.loading,
77
+ };
78
+ }
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "@geejay/use-feature-flags",
3
+ "version": "1.0.0",
4
+ "description": "React hook for feature flag management using Supabase",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": ["dist", "README.md"],
8
+ "scripts": {
9
+ "build": "tsc"
10
+ },
11
+ "keywords": ["feature flags", "supabase", "react", "hooks"],
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/your-org/use-feature-flags.git"
15
+ },
16
+ "author": "GeeJay",
17
+ "license": "MIT",
18
+ "dependencies": {
19
+ "@supabase/supabase-js": "^2.52.0",
20
+ "jotai": "^2.12.5"
21
+ },
22
+ "peerDependencies": {
23
+ "react": ">=16.8"
24
+ }
25
+ }