@flipfeatureflag/react 0.1.1

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,17 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { PropsWithChildren } from 'react';
3
+ import { FlipFlagClient, FlipFlagClientOptions, SdkFlagEvaluation, FlagsStatus } from '@flipfeatureflag/core';
4
+ export { FlipFlagClient, FlipFlagClientOptions } from '@flipfeatureflag/core';
5
+
6
+ interface FlipFlagProviderProps {
7
+ client?: FlipFlagClient;
8
+ config?: FlipFlagClientOptions;
9
+ }
10
+ declare function FlipFlagProvider({ client, config, children }: PropsWithChildren<FlipFlagProviderProps>): react_jsx_runtime.JSX.Element;
11
+ declare function useFlipFlagClient(): FlipFlagClient;
12
+ declare function useFlagsStatus(): FlagsStatus;
13
+ declare function useFlag(flagKey: string, defaultValue?: boolean): boolean;
14
+ declare function useVariant(flagKey: string, defaultValue?: SdkFlagEvaluation["value"]): SdkFlagEvaluation;
15
+ declare function useAllFlags(): Record<string, SdkFlagEvaluation>;
16
+
17
+ export { FlipFlagProvider, useAllFlags, useFlag, useFlagsStatus, useFlipFlagClient, useVariant };
@@ -0,0 +1,17 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { PropsWithChildren } from 'react';
3
+ import { FlipFlagClient, FlipFlagClientOptions, SdkFlagEvaluation, FlagsStatus } from '@flipfeatureflag/core';
4
+ export { FlipFlagClient, FlipFlagClientOptions } from '@flipfeatureflag/core';
5
+
6
+ interface FlipFlagProviderProps {
7
+ client?: FlipFlagClient;
8
+ config?: FlipFlagClientOptions;
9
+ }
10
+ declare function FlipFlagProvider({ client, config, children }: PropsWithChildren<FlipFlagProviderProps>): react_jsx_runtime.JSX.Element;
11
+ declare function useFlipFlagClient(): FlipFlagClient;
12
+ declare function useFlagsStatus(): FlagsStatus;
13
+ declare function useFlag(flagKey: string, defaultValue?: boolean): boolean;
14
+ declare function useVariant(flagKey: string, defaultValue?: SdkFlagEvaluation["value"]): SdkFlagEvaluation;
15
+ declare function useAllFlags(): Record<string, SdkFlagEvaluation>;
16
+
17
+ export { FlipFlagProvider, useAllFlags, useFlag, useFlagsStatus, useFlipFlagClient, useVariant };
package/dist/index.js ADDED
@@ -0,0 +1,135 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.tsx
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ FlipFlagClient: () => import_core.FlipFlagClient,
34
+ FlipFlagProvider: () => FlipFlagProvider,
35
+ useAllFlags: () => useAllFlags,
36
+ useFlag: () => useFlag,
37
+ useFlagsStatus: () => useFlagsStatus,
38
+ useFlipFlagClient: () => useFlipFlagClient,
39
+ useVariant: () => useVariant
40
+ });
41
+ module.exports = __toCommonJS(index_exports);
42
+ var import_react = __toESM(require("react"));
43
+ var import_js = require("@flipfeatureflag/js");
44
+ var import_core = require("@flipfeatureflag/core");
45
+ var import_jsx_runtime = require("react/jsx-runtime");
46
+ var FlipFlagContext = import_react.default.createContext(null);
47
+ var FlipFlagStatusContext = import_react.default.createContext({ ready: false });
48
+ function FlipFlagProvider({ client, config, children }) {
49
+ const { resolvedClient, ownsClient } = (0, import_react.useMemo)(() => {
50
+ if (client) {
51
+ return { resolvedClient: client, ownsClient: false };
52
+ }
53
+ if (!config) {
54
+ throw new Error("flipFeatureFlagProvider requires client or config");
55
+ }
56
+ return { resolvedClient: (0, import_js.createClient)(config), ownsClient: true };
57
+ }, [client, config]);
58
+ const [status, setStatus] = (0, import_react.useState)(resolvedClient.getStatus());
59
+ (0, import_react.useEffect)(() => {
60
+ let mounted = true;
61
+ const handleUpdate = () => {
62
+ if (mounted) {
63
+ setStatus(resolvedClient.getStatus());
64
+ }
65
+ };
66
+ resolvedClient.on("ready", handleUpdate);
67
+ resolvedClient.on("update", handleUpdate);
68
+ resolvedClient.on("error", handleUpdate);
69
+ void resolvedClient.start();
70
+ return () => {
71
+ mounted = false;
72
+ resolvedClient.off("ready", handleUpdate);
73
+ resolvedClient.off("update", handleUpdate);
74
+ resolvedClient.off("error", handleUpdate);
75
+ if (ownsClient) {
76
+ resolvedClient.stop();
77
+ }
78
+ };
79
+ }, [resolvedClient, ownsClient]);
80
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(FlipFlagContext.Provider, { value: resolvedClient, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(FlipFlagStatusContext.Provider, { value: status, children }) });
81
+ }
82
+ function useFlipFlagClient() {
83
+ const client = import_react.default.useContext(FlipFlagContext);
84
+ if (!client) {
85
+ throw new Error("useFlipFlagClient must be used within flipFeatureFlagProvider");
86
+ }
87
+ return client;
88
+ }
89
+ function useFlagsStatus() {
90
+ return import_react.default.useContext(FlipFlagStatusContext);
91
+ }
92
+ function useFlag(flagKey, defaultValue = false) {
93
+ const client = useFlipFlagClient();
94
+ const [value, setValue] = (0, import_react.useState)(() => client.isEnabled(flagKey, defaultValue));
95
+ (0, import_react.useEffect)(() => {
96
+ const update = () => {
97
+ setValue(client.isEnabled(flagKey, defaultValue));
98
+ };
99
+ client.on("update", update);
100
+ return () => client.off("update", update);
101
+ }, [client, flagKey, defaultValue]);
102
+ return value;
103
+ }
104
+ function useVariant(flagKey, defaultValue = false) {
105
+ const client = useFlipFlagClient();
106
+ const [variant, setVariant] = (0, import_react.useState)(() => client.getVariant(flagKey, defaultValue));
107
+ (0, import_react.useEffect)(() => {
108
+ const update = () => {
109
+ setVariant(client.getVariant(flagKey, defaultValue));
110
+ };
111
+ client.on("update", update);
112
+ return () => client.off("update", update);
113
+ }, [client, flagKey, defaultValue]);
114
+ return variant;
115
+ }
116
+ function useAllFlags() {
117
+ const client = useFlipFlagClient();
118
+ const [flags, setFlags] = (0, import_react.useState)(() => client.getAllFlags());
119
+ (0, import_react.useEffect)(() => {
120
+ const update = () => setFlags(client.getAllFlags());
121
+ client.on("update", update);
122
+ return () => client.off("update", update);
123
+ }, [client]);
124
+ return flags;
125
+ }
126
+ // Annotate the CommonJS export names for ESM import in node:
127
+ 0 && (module.exports = {
128
+ FlipFlagClient,
129
+ FlipFlagProvider,
130
+ useAllFlags,
131
+ useFlag,
132
+ useFlagsStatus,
133
+ useFlipFlagClient,
134
+ useVariant
135
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,94 @@
1
+ // src/index.tsx
2
+ import React, { useEffect, useMemo, useState } from "react";
3
+ import { createClient as createBrowserClient } from "@flipfeatureflag/js";
4
+ import { FlipFlagClient } from "@flipfeatureflag/core";
5
+ import { jsx } from "react/jsx-runtime";
6
+ var FlipFlagContext = React.createContext(null);
7
+ var FlipFlagStatusContext = React.createContext({ ready: false });
8
+ function FlipFlagProvider({ client, config, children }) {
9
+ const { resolvedClient, ownsClient } = useMemo(() => {
10
+ if (client) {
11
+ return { resolvedClient: client, ownsClient: false };
12
+ }
13
+ if (!config) {
14
+ throw new Error("flipFeatureFlagProvider requires client or config");
15
+ }
16
+ return { resolvedClient: createBrowserClient(config), ownsClient: true };
17
+ }, [client, config]);
18
+ const [status, setStatus] = useState(resolvedClient.getStatus());
19
+ useEffect(() => {
20
+ let mounted = true;
21
+ const handleUpdate = () => {
22
+ if (mounted) {
23
+ setStatus(resolvedClient.getStatus());
24
+ }
25
+ };
26
+ resolvedClient.on("ready", handleUpdate);
27
+ resolvedClient.on("update", handleUpdate);
28
+ resolvedClient.on("error", handleUpdate);
29
+ void resolvedClient.start();
30
+ return () => {
31
+ mounted = false;
32
+ resolvedClient.off("ready", handleUpdate);
33
+ resolvedClient.off("update", handleUpdate);
34
+ resolvedClient.off("error", handleUpdate);
35
+ if (ownsClient) {
36
+ resolvedClient.stop();
37
+ }
38
+ };
39
+ }, [resolvedClient, ownsClient]);
40
+ return /* @__PURE__ */ jsx(FlipFlagContext.Provider, { value: resolvedClient, children: /* @__PURE__ */ jsx(FlipFlagStatusContext.Provider, { value: status, children }) });
41
+ }
42
+ function useFlipFlagClient() {
43
+ const client = React.useContext(FlipFlagContext);
44
+ if (!client) {
45
+ throw new Error("useFlipFlagClient must be used within flipFeatureFlagProvider");
46
+ }
47
+ return client;
48
+ }
49
+ function useFlagsStatus() {
50
+ return React.useContext(FlipFlagStatusContext);
51
+ }
52
+ function useFlag(flagKey, defaultValue = false) {
53
+ const client = useFlipFlagClient();
54
+ const [value, setValue] = useState(() => client.isEnabled(flagKey, defaultValue));
55
+ useEffect(() => {
56
+ const update = () => {
57
+ setValue(client.isEnabled(flagKey, defaultValue));
58
+ };
59
+ client.on("update", update);
60
+ return () => client.off("update", update);
61
+ }, [client, flagKey, defaultValue]);
62
+ return value;
63
+ }
64
+ function useVariant(flagKey, defaultValue = false) {
65
+ const client = useFlipFlagClient();
66
+ const [variant, setVariant] = useState(() => client.getVariant(flagKey, defaultValue));
67
+ useEffect(() => {
68
+ const update = () => {
69
+ setVariant(client.getVariant(flagKey, defaultValue));
70
+ };
71
+ client.on("update", update);
72
+ return () => client.off("update", update);
73
+ }, [client, flagKey, defaultValue]);
74
+ return variant;
75
+ }
76
+ function useAllFlags() {
77
+ const client = useFlipFlagClient();
78
+ const [flags, setFlags] = useState(() => client.getAllFlags());
79
+ useEffect(() => {
80
+ const update = () => setFlags(client.getAllFlags());
81
+ client.on("update", update);
82
+ return () => client.off("update", update);
83
+ }, [client]);
84
+ return flags;
85
+ }
86
+ export {
87
+ FlipFlagClient,
88
+ FlipFlagProvider,
89
+ useAllFlags,
90
+ useFlag,
91
+ useFlagsStatus,
92
+ useFlipFlagClient,
93
+ useVariant
94
+ };
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@flipfeatureflag/react",
3
+ "version": "0.1.1",
4
+ "description": "flipFeatureFlag React SDK",
5
+ "main": "dist/index.cjs",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.cjs"
13
+ }
14
+ },
15
+ "sideEffects": false,
16
+ "scripts": {
17
+ "build": "tsup src/index.tsx --format esm,cjs --dts --external react"
18
+ },
19
+ "peerDependencies": {
20
+ "react": ">=17"
21
+ },
22
+ "dependencies": {
23
+ "@flipfeatureflag/core": "0.1.0",
24
+ "@flipfeatureflag/js": "0.1.0"
25
+ }
26
+ }
package/src/index.tsx ADDED
@@ -0,0 +1,115 @@
1
+ import React, { PropsWithChildren, useEffect, useMemo, useState } from "react";
2
+ import { FlipFlagClient, FlipFlagClientOptions, FlagsStatus, SdkFlagEvaluation } from "@flipfeatureflag/core";
3
+ import { createClient as createBrowserClient } from "@flipfeatureflag/js";
4
+
5
+ interface FlipFlagProviderProps {
6
+ client?: FlipFlagClient;
7
+ config?: FlipFlagClientOptions;
8
+ }
9
+
10
+ const FlipFlagContext = React.createContext<FlipFlagClient | null>(null);
11
+ const FlipFlagStatusContext = React.createContext<FlagsStatus>({ ready: false });
12
+
13
+ export function FlipFlagProvider({ client, config, children }: PropsWithChildren<FlipFlagProviderProps>) {
14
+ const { resolvedClient, ownsClient } = useMemo(() => {
15
+ if (client) {
16
+ return { resolvedClient: client, ownsClient: false };
17
+ }
18
+ if (!config) {
19
+ throw new Error("flipFeatureFlagProvider requires client or config");
20
+ }
21
+ return { resolvedClient: createBrowserClient(config), ownsClient: true };
22
+ }, [client, config]);
23
+
24
+ const [status, setStatus] = useState<FlagsStatus>(resolvedClient.getStatus());
25
+
26
+ useEffect(() => {
27
+ let mounted = true;
28
+
29
+ const handleUpdate = () => {
30
+ if (mounted) {
31
+ setStatus(resolvedClient.getStatus());
32
+ }
33
+ };
34
+
35
+ resolvedClient.on("ready", handleUpdate);
36
+ resolvedClient.on("update", handleUpdate);
37
+ resolvedClient.on("error", handleUpdate);
38
+
39
+ void resolvedClient.start();
40
+
41
+ return () => {
42
+ mounted = false;
43
+ resolvedClient.off("ready", handleUpdate);
44
+ resolvedClient.off("update", handleUpdate);
45
+ resolvedClient.off("error", handleUpdate);
46
+ if (ownsClient) {
47
+ resolvedClient.stop();
48
+ }
49
+ };
50
+ }, [resolvedClient, ownsClient]);
51
+
52
+ return (
53
+ <FlipFlagContext.Provider value={resolvedClient}>
54
+ <FlipFlagStatusContext.Provider value={status}>{children}</FlipFlagStatusContext.Provider>
55
+ </FlipFlagContext.Provider>
56
+ );
57
+ }
58
+
59
+ export function useFlipFlagClient(): FlipFlagClient {
60
+ const client = React.useContext(FlipFlagContext);
61
+ if (!client) {
62
+ throw new Error("useFlipFlagClient must be used within flipFeatureFlagProvider");
63
+ }
64
+ return client;
65
+ }
66
+
67
+ export function useFlagsStatus(): FlagsStatus {
68
+ return React.useContext(FlipFlagStatusContext);
69
+ }
70
+
71
+ export function useFlag(flagKey: string, defaultValue = false): boolean {
72
+ const client = useFlipFlagClient();
73
+ const [value, setValue] = useState(() => client.isEnabled(flagKey, defaultValue));
74
+
75
+ useEffect(() => {
76
+ const update = () => {
77
+ setValue(client.isEnabled(flagKey, defaultValue));
78
+ };
79
+ client.on("update", update);
80
+ return () => client.off("update", update);
81
+ }, [client, flagKey, defaultValue]);
82
+
83
+ return value;
84
+ }
85
+
86
+ export function useVariant(flagKey: string, defaultValue: SdkFlagEvaluation["value"] = false): SdkFlagEvaluation {
87
+ const client = useFlipFlagClient();
88
+ const [variant, setVariant] = useState(() => client.getVariant(flagKey, defaultValue));
89
+
90
+ useEffect(() => {
91
+ const update = () => {
92
+ setVariant(client.getVariant(flagKey, defaultValue));
93
+ };
94
+ client.on("update", update);
95
+ return () => client.off("update", update);
96
+ }, [client, flagKey, defaultValue]);
97
+
98
+ return variant;
99
+ }
100
+
101
+ export function useAllFlags(): Record<string, SdkFlagEvaluation> {
102
+ const client = useFlipFlagClient();
103
+ const [flags, setFlags] = useState(() => client.getAllFlags());
104
+
105
+ useEffect(() => {
106
+ const update = () => setFlags(client.getAllFlags());
107
+ client.on("update", update);
108
+ return () => client.off("update", update);
109
+ }, [client]);
110
+
111
+ return flags;
112
+ }
113
+
114
+ export type { FlipFlagClientOptions } from "@flipfeatureflag/core";
115
+ export { FlipFlagClient } from "@flipfeatureflag/core";
package/tsconfig.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "jsx": "react-jsx"
5
+ },
6
+ "include": ["src"]
7
+ }