@vexillo/react-sdk 2.0.3 → 2.2.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/index.cjs CHANGED
@@ -27,7 +27,7 @@ __export(src_exports, {
27
27
  module.exports = __toCommonJS(src_exports);
28
28
 
29
29
  // src/provider.tsx
30
- var import_react = require("react");
30
+ var import_react2 = require("react");
31
31
 
32
32
  // src/fetch-flags.ts
33
33
  async function fetchFlags(baseUrl, apiKey) {
@@ -53,9 +53,42 @@ async function fetchFlags(baseUrl, apiKey) {
53
53
  }
54
54
  }
55
55
 
56
+ // src/use-promise.ts
57
+ var import_react = require("react");
58
+ function usePromise(promise) {
59
+ const major = parseInt(import_react.version.split(".")[0], 10);
60
+ if (major >= 19 && typeof import_react.use === "function") {
61
+ return (0, import_react.use)(promise);
62
+ }
63
+ return readPromise(promise);
64
+ }
65
+ function readPromise(promise) {
66
+ if (promise.status === "fulfilled") {
67
+ return promise.value;
68
+ }
69
+ if (promise.status === "rejected") {
70
+ throw promise.reason;
71
+ }
72
+ if (promise.status === "pending") {
73
+ throw promise;
74
+ }
75
+ promise.status = "pending";
76
+ promise.then(
77
+ (value) => {
78
+ promise.status = "fulfilled";
79
+ promise.value = value;
80
+ },
81
+ (reason) => {
82
+ promise.status = "rejected";
83
+ promise.reason = reason;
84
+ }
85
+ );
86
+ throw promise;
87
+ }
88
+
56
89
  // src/provider.tsx
57
90
  var import_jsx_runtime = require("react/jsx-runtime");
58
- var VexilloContext = (0, import_react.createContext)(null);
91
+ var VexilloContext = (0, import_react2.createContext)(null);
59
92
  var clientCache = /* @__PURE__ */ new Map();
60
93
  function getOrCreateFlagPromise(baseUrl, apiKey) {
61
94
  const key = `${baseUrl}__${apiKey}`;
@@ -85,11 +118,11 @@ function VexilloProvider({
85
118
  } else {
86
119
  flagPromise = getOrCreateFlagPromise(baseUrl, apiKey);
87
120
  }
88
- const flags = (0, import_react.use)(flagPromise);
121
+ const flags = usePromise(flagPromise);
89
122
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(VexilloContext.Provider, { value: { flags, fallbacks }, children });
90
123
  }
91
124
  function useVexilloContext() {
92
- const ctx = (0, import_react.useContext)(VexilloContext);
125
+ const ctx = (0, import_react2.useContext)(VexilloContext);
93
126
  if (ctx === null) {
94
127
  throw new Error("useFlag must be called inside a <VexilloProvider>.");
95
128
  }
package/dist/index.js ADDED
@@ -0,0 +1,146 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ VexilloProvider: () => VexilloProvider,
24
+ fetchFlags: () => fetchFlags,
25
+ useFlag: () => useFlag
26
+ });
27
+ module.exports = __toCommonJS(src_exports);
28
+
29
+ // src/provider.tsx
30
+ var import_react2 = require("react");
31
+
32
+ // src/fetch-flags.ts
33
+ async function fetchFlags(baseUrl, apiKey) {
34
+ try {
35
+ const res = await fetch(`${baseUrl}/api/flags`, {
36
+ headers: { Authorization: `Bearer ${apiKey}` }
37
+ });
38
+ if (!res.ok) {
39
+ console.warn(
40
+ `Vexillo: fetchFlags received status ${res.status} ${res.statusText}. Returning empty flags.`
41
+ );
42
+ return {};
43
+ }
44
+ const data = await res.json();
45
+ const map = {};
46
+ for (const f of data.flags) {
47
+ map[f.key] = f.enabled;
48
+ }
49
+ return map;
50
+ } catch (e) {
51
+ console.warn("Vexillo: fetchFlags failed. Returning empty flags.");
52
+ return {};
53
+ }
54
+ }
55
+
56
+ // src/use-promise.ts
57
+ var import_react = require("react");
58
+ function usePromise(promise) {
59
+ const major = parseInt(import_react.version.split(".")[0], 10);
60
+ if (major >= 19 && typeof import_react.use === "function") {
61
+ return (0, import_react.use)(promise);
62
+ }
63
+ return readPromise(promise);
64
+ }
65
+ function readPromise(promise) {
66
+ if (promise.status === "fulfilled") {
67
+ return promise.value;
68
+ }
69
+ if (promise.status === "rejected") {
70
+ throw promise.reason;
71
+ }
72
+ if (promise.status === "pending") {
73
+ throw promise;
74
+ }
75
+ promise.status = "pending";
76
+ promise.then(
77
+ (value) => {
78
+ promise.status = "fulfilled";
79
+ promise.value = value;
80
+ },
81
+ (reason) => {
82
+ promise.status = "rejected";
83
+ promise.reason = reason;
84
+ }
85
+ );
86
+ throw promise;
87
+ }
88
+
89
+ // src/provider.tsx
90
+ var import_jsx_runtime = require("react/jsx-runtime");
91
+ var VexilloContext = (0, import_react2.createContext)(null);
92
+ var clientCache = /* @__PURE__ */ new Map();
93
+ function getOrCreateFlagPromise(baseUrl, apiKey) {
94
+ const key = `${baseUrl}__${apiKey}`;
95
+ if (!clientCache.has(key)) {
96
+ clientCache.set(key, fetchFlags(baseUrl, apiKey));
97
+ }
98
+ return clientCache.get(key);
99
+ }
100
+ function VexilloProvider({
101
+ baseUrl,
102
+ apiKey,
103
+ initialFlags,
104
+ fallbacks = {},
105
+ children
106
+ }) {
107
+ const isServer = typeof window === "undefined";
108
+ if (initialFlags) {
109
+ const key = `${baseUrl}__${apiKey}`;
110
+ if (!isServer && !clientCache.has(key)) {
111
+ clientCache.set(key, Promise.resolve(initialFlags));
112
+ }
113
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(VexilloContext.Provider, { value: { flags: initialFlags, fallbacks }, children });
114
+ }
115
+ let flagPromise;
116
+ if (isServer) {
117
+ flagPromise = fetchFlags(baseUrl, apiKey);
118
+ } else {
119
+ flagPromise = getOrCreateFlagPromise(baseUrl, apiKey);
120
+ }
121
+ const flags = usePromise(flagPromise);
122
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(VexilloContext.Provider, { value: { flags, fallbacks }, children });
123
+ }
124
+ function useVexilloContext() {
125
+ const ctx = (0, import_react2.useContext)(VexilloContext);
126
+ if (ctx === null) {
127
+ throw new Error("useFlag must be called inside a <VexilloProvider>.");
128
+ }
129
+ return ctx;
130
+ }
131
+
132
+ // src/use-flag.ts
133
+ function useFlag(key) {
134
+ var _a;
135
+ const { flags, fallbacks } = useVexilloContext();
136
+ if (key in flags) {
137
+ return flags[key];
138
+ }
139
+ return (_a = fallbacks[key]) != null ? _a : false;
140
+ }
141
+ // Annotate the CommonJS export names for ESM import in node:
142
+ 0 && (module.exports = {
143
+ VexilloProvider,
144
+ fetchFlags,
145
+ useFlag
146
+ });
package/dist/index.mjs CHANGED
@@ -1,7 +1,6 @@
1
1
  // src/provider.tsx
2
2
  import {
3
3
  createContext,
4
- use,
5
4
  useContext
6
5
  } from "react";
7
6
 
@@ -29,6 +28,39 @@ async function fetchFlags(baseUrl, apiKey) {
29
28
  }
30
29
  }
31
30
 
31
+ // src/use-promise.ts
32
+ import { use, version } from "react";
33
+ function usePromise(promise) {
34
+ const major = parseInt(version.split(".")[0], 10);
35
+ if (major >= 19 && typeof use === "function") {
36
+ return use(promise);
37
+ }
38
+ return readPromise(promise);
39
+ }
40
+ function readPromise(promise) {
41
+ if (promise.status === "fulfilled") {
42
+ return promise.value;
43
+ }
44
+ if (promise.status === "rejected") {
45
+ throw promise.reason;
46
+ }
47
+ if (promise.status === "pending") {
48
+ throw promise;
49
+ }
50
+ promise.status = "pending";
51
+ promise.then(
52
+ (value) => {
53
+ promise.status = "fulfilled";
54
+ promise.value = value;
55
+ },
56
+ (reason) => {
57
+ promise.status = "rejected";
58
+ promise.reason = reason;
59
+ }
60
+ );
61
+ throw promise;
62
+ }
63
+
32
64
  // src/provider.tsx
33
65
  import { jsx } from "react/jsx-runtime";
34
66
  var VexilloContext = createContext(null);
@@ -61,7 +93,7 @@ function VexilloProvider({
61
93
  } else {
62
94
  flagPromise = getOrCreateFlagPromise(baseUrl, apiKey);
63
95
  }
64
- const flags = use(flagPromise);
96
+ const flags = usePromise(flagPromise);
65
97
  return /* @__PURE__ */ jsx(VexilloContext.Provider, { value: { flags, fallbacks }, children });
66
98
  }
67
99
  function useVexilloContext() {
package/package.json CHANGED
@@ -1,19 +1,19 @@
1
1
  {
2
2
  "name": "@vexillo/react-sdk",
3
- "version": "2.0.3",
4
- "type": "module",
3
+ "version": "2.2.0",
5
4
  "license": "MIT",
6
5
  "publishConfig": {
7
6
  "registry": "https://registry.npmjs.org",
8
7
  "access": "public"
9
8
  },
10
- "main": "./dist/index.cjs",
9
+ "main": "./dist/index.js",
11
10
  "types": "./dist/index.d.ts",
12
11
  "exports": {
13
12
  ".": {
14
13
  "types": "./dist/index.d.ts",
15
14
  "import": "./dist/index.mjs",
16
- "require": "./dist/index.cjs"
15
+ "require": "./dist/index.cjs",
16
+ "default": "./dist/index.mjs"
17
17
  }
18
18
  },
19
19
  "files": [
@@ -21,8 +21,8 @@
21
21
  "README.md"
22
22
  ],
23
23
  "peerDependencies": {
24
- "react": "^19",
25
- "react-dom": "^19"
24
+ "react": "^18 || ^19",
25
+ "react-dom": "^18 || ^19"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@testing-library/react": "^16",
File without changes