@vexillo/react-sdk 2.1.0 → 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.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/package.json CHANGED
@@ -1,19 +1,19 @@
1
1
  {
2
2
  "name": "@vexillo/react-sdk",
3
- "version": "2.1.0",
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": [
File without changes