@kelviq/react-sdk 2.0.2 → 2.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.
@@ -1,146 +1,146 @@
1
- import { jsx as S, Fragment as M } from "react/jsx-runtime";
2
- import { createContext as Q, useState as $, useCallback as w, useEffect as ee, useMemo as te, useContext as re } from "react";
3
- const ne = "GET", se = 5e3, oe = 3, ie = 1e3;
4
- function O(n) {
1
+ import { jsx as L, Fragment as w } from "react/jsx-runtime";
2
+ import { createContext as se, useState as I, useCallback as R, useEffect as ae, useMemo as le, useContext as ce } from "react";
3
+ const ue = "GET", de = 5e3, fe = 3, me = 1e3;
4
+ function x(n) {
5
5
  const {
6
- method: s = ne,
7
- headers: o = {},
6
+ method: i = ue,
7
+ headers: s = {},
8
8
  // Renamed to avoid conflict with internal 'headers' variable
9
9
  body: t,
10
- timeout: E = se,
11
- maxRetries: g = oe,
12
- backoffBaseDelay: v = ie,
13
- queryParams: A,
14
- accessToken: l
10
+ timeout: m = de,
11
+ maxRetries: u = fe,
12
+ backoffBaseDelay: d = me,
13
+ queryParams: E,
14
+ accessToken: o
15
15
  // Use the direct accessToken
16
16
  } = n;
17
- let { url: d } = n, L = 0;
18
- return new Promise((R, P) => {
19
- if (A) {
20
- const a = new URLSearchParams();
21
- for (const m in A)
22
- A[m] !== void 0 && a.append(m, String(A[m]));
23
- a.toString() && (d += (d.includes("?") ? "&" : "?") + a.toString());
17
+ let { url: a } = n, g = 0;
18
+ return new Promise((q, P) => {
19
+ if (E) {
20
+ const v = new URLSearchParams();
21
+ for (const S in E)
22
+ E[S] !== void 0 && v.append(S, String(E[S]));
23
+ v.toString() && (a += (a.includes("?") ? "&" : "?") + v.toString());
24
24
  }
25
- function C() {
26
- const a = new XMLHttpRequest();
27
- a.open(s, d, !0), a.timeout = E;
28
- const m = {
25
+ function b() {
26
+ const v = new XMLHttpRequest();
27
+ v.open(i, a, !0), v.timeout = m;
28
+ const S = {
29
29
  Accept: "application/json",
30
30
  "X-Requested-With": "XMLHttpRequest",
31
- ...o
31
+ ...s
32
32
  // Apply custom headers, allowing them to override defaults
33
33
  };
34
- s !== "GET" && t && typeof t == "object" && !(t instanceof FormData) && (m["Content-Type"] || (m["Content-Type"] = "application/json")), l && (m.Authorization = `Bearer ${l}`);
35
- for (const [i, f] of Object.entries(m))
36
- a.setRequestHeader(i, f);
37
- a.onload = function() {
38
- const { status: i, statusText: f, responseText: T } = a;
39
- if (i >= 200 && i < 300)
34
+ i !== "GET" && t && typeof t == "object" && !(t instanceof FormData) && (S["Content-Type"] || (S["Content-Type"] = "application/json")), o && (S.Authorization = `Bearer ${o}`);
35
+ for (const [l, p] of Object.entries(S))
36
+ v.setRequestHeader(l, p);
37
+ v.onload = function() {
38
+ const { status: l, statusText: p, responseText: y } = v;
39
+ if (l >= 200 && l < 300)
40
40
  try {
41
- const b = T ? JSON.parse(T) : {};
42
- R({ status: i, statusText: f, data: b });
43
- } catch (b) {
44
- const q = b instanceof Error ? b : new Error(String(b));
41
+ const A = y ? JSON.parse(y) : {};
42
+ q({ status: l, statusText: p, data: A });
43
+ } catch (A) {
44
+ const K = A instanceof Error ? A : new Error(String(A));
45
45
  console.error(
46
- `Kelviq SDK (apiRequest): Invalid JSON response for URL ${d}. Error: ${q.message}. Response: ${T}`
46
+ `Kelviq SDK (apiRequest): Invalid JSON response for URL ${a}. Error: ${K.message}. Response: ${y}`
47
47
  ), P(
48
48
  new Error(
49
- `Invalid JSON response: ${q.message}`
49
+ `Invalid JSON response: ${K.message}`
50
50
  )
51
51
  );
52
52
  }
53
53
  else
54
- p(
55
- `Request to ${d} failed with status ${i} ${f}. Response: ${T}`
54
+ $(
55
+ `Request to ${a} failed with status ${l} ${p}. Response: ${y}`
56
56
  );
57
- }, a.onerror = () => p(
58
- `Network error for URL ${d}. The request could not be completed.`
59
- ), a.ontimeout = () => p(`Request to ${d} timed out.`);
60
- function p(i) {
61
- if (L < g) {
62
- L++;
63
- const f = Math.min(
64
- v * Math.pow(2, L - 1),
57
+ }, v.onerror = () => $(
58
+ `Network error for URL ${a}. The request could not be completed.`
59
+ ), v.ontimeout = () => $(`Request to ${a} timed out.`);
60
+ function $(l) {
61
+ if (g < u) {
62
+ g++;
63
+ const p = Math.min(
64
+ d * Math.pow(2, g - 1),
65
65
  3e4
66
66
  // Cap retry delay at 30 seconds
67
67
  );
68
68
  console.warn(
69
- `Kelviq SDK (apiRequest): Retrying request to ${d}. Attempt ${L}/${g}. Error: ${i}. Retrying in ${f}ms.`
70
- ), setTimeout(C, f);
69
+ `Kelviq SDK (apiRequest): Retrying request to ${a}. Attempt ${g}/${u}. Error: ${l}. Retrying in ${p}ms.`
70
+ ), setTimeout(b, p);
71
71
  } else
72
72
  P(
73
73
  new Error(
74
- `${i} (Max retries ${g} reached)`
74
+ `${l} (Max retries ${u} reached)`
75
75
  )
76
76
  );
77
77
  }
78
78
  try {
79
- let i = t;
80
- t && typeof t == "object" && !(t instanceof FormData) && m["Content-Type"] === "application/json" && (i = JSON.stringify(t)), a.send(i);
81
- } catch (i) {
82
- const f = i instanceof Error ? i : new Error(String(i));
79
+ let l = t;
80
+ t && typeof t == "object" && !(t instanceof FormData) && S["Content-Type"] === "application/json" && (l = JSON.stringify(t)), v.send(l);
81
+ } catch (l) {
82
+ const p = l instanceof Error ? l : new Error(String(l));
83
83
  console.error(
84
- `Kelviq SDK (apiRequest): Error sending request to ${d}.`,
85
- f
86
- ), P(new Error(`Failed to send request: ${f.message}`));
84
+ `Kelviq SDK (apiRequest): Error sending request to ${a}.`,
85
+ p
86
+ ), P(new Error(`Failed to send request: ${p.message}`));
87
87
  }
88
88
  }
89
- C();
89
+ b();
90
90
  });
91
91
  }
92
- function ae(n, s) {
93
- const o = s[0].featureType;
94
- if (o === "METER") {
95
- let t = 0, E = 0, g = !1;
96
- for (const A of s) {
97
- const l = A, d = typeof l.usageLimit == "number" ? l.usageLimit : null, L = typeof l.currentUsage == "number" ? l.currentUsage : 0;
98
- d === null && (t = null), t !== null && (t += d), E += L, l.hardLimit && (g = !0);
92
+ function ge(n, i) {
93
+ const s = i[0].featureType;
94
+ if (s === "METER") {
95
+ let t = 0, m = 0, u = !1;
96
+ for (const E of i) {
97
+ const o = E, a = typeof o.usageLimit == "number" ? o.usageLimit : null, g = typeof o.currentUsage == "number" ? o.currentUsage : 0;
98
+ a === null && (t = null), t !== null && (t += a), m += g, o.hardLimit && (u = !0);
99
99
  }
100
- const v = t !== null ? t - E : null;
100
+ const d = t !== null ? t - m : null;
101
101
  return {
102
102
  featureId: n,
103
- featureType: o,
104
- hasAccess: v === null || v > 0,
105
- currentUsage: E,
103
+ featureType: s,
104
+ hasAccess: d === null || d > 0,
105
+ currentUsage: m,
106
106
  usageLimit: t,
107
- remaining: v,
108
- hardLimit: g,
109
- items: s
107
+ remaining: d,
108
+ hardLimit: u,
109
+ items: i
110
110
  };
111
111
  }
112
- if (o === "CUSTOMIZABLE") {
113
- const t = s[0];
112
+ if (s === "CUSTOMIZABLE") {
113
+ const t = i[0];
114
114
  return {
115
115
  featureId: n,
116
- featureType: o,
117
- hasAccess: s.some((E) => E.hasAccess),
116
+ featureType: s,
117
+ hasAccess: i.some((m) => m.hasAccess),
118
118
  currentUsage: typeof t.currentUsage == "number" ? t.currentUsage : 0,
119
119
  usageLimit: typeof t.usageLimit == "number" ? t.usageLimit : null,
120
120
  remaining: typeof t.remaining == "number" ? t.remaining : null,
121
121
  hardLimit: t.hardLimit === !0,
122
- items: s
122
+ items: i
123
123
  };
124
124
  }
125
125
  return {
126
126
  featureId: n,
127
- featureType: o,
128
- hasAccess: s.some((t) => t.hasAccess),
127
+ featureType: s,
128
+ hasAccess: i.some((t) => t.hasAccess),
129
129
  currentUsage: 0,
130
130
  usageLimit: null,
131
131
  remaining: null,
132
132
  hardLimit: !1,
133
- items: s
133
+ items: i
134
134
  };
135
135
  }
136
- function le(n) {
137
- const s = {};
136
+ function pe(n) {
137
+ const i = {};
138
138
  if (!n || !Array.isArray(n.entitlements))
139
139
  return console.warn(
140
140
  "Kelviq SDK: Invalid or empty entitlements array in API response.",
141
141
  n
142
- ), s;
143
- const o = {};
142
+ ), i;
143
+ const s = {};
144
144
  for (const t of n.entitlements) {
145
145
  if (!t || typeof t.featureId != "string" || typeof t.featureType != "string") {
146
146
  console.warn(
@@ -149,16 +149,16 @@ function le(n) {
149
149
  );
150
150
  continue;
151
151
  }
152
- o[t.featureId] || (o[t.featureId] = []), o[t.featureId].push(t);
152
+ s[t.featureId] || (s[t.featureId] = []), s[t.featureId].push(t);
153
153
  }
154
- for (const t in o)
155
- s[t] = ae(
154
+ for (const t in s)
155
+ i[t] = ge(
156
156
  t,
157
- o[t]
157
+ s[t]
158
158
  );
159
- return s;
159
+ return i;
160
160
  }
161
- const V = {
161
+ const re = {
162
162
  allEntitlements: {
163
163
  data: null,
164
164
  isLoading: !0,
@@ -193,93 +193,108 @@ const V = {
193
193
  refreshCustomer: () => (console.warn(
194
194
  "refreshCustomer called on default KelviqContext. Ensure KelviqProvider is properly set up."
195
195
  ), Promise.resolve()),
196
+ pricing: {
197
+ data: null,
198
+ isLoading: !1,
199
+ error: null
200
+ },
201
+ refreshPricing: () => (console.warn(
202
+ "refreshPricing called on default KelviqContext. Ensure KelviqProvider is properly set up."
203
+ ), Promise.resolve()),
196
204
  isLoading: !0,
197
205
  error: null,
198
206
  apiUrl: "",
199
207
  environment: "production",
200
208
  entitlementsPath: "",
201
209
  customerId: ""
202
- }, Y = Q(
203
- V
204
- ), ue = "https://edge.api.kelviq.com/api/v1/", ce = "https://edge.sandboxapi.kelviq.com/api/v1/", de = "https://api.kelviq.com/api/v1/", fe = "https://sandboxapi.kelviq.com/api/v1/", me = "entitlements/", pe = ({
210
+ }, te = se(
211
+ re
212
+ ), he = "https://edge.api.kelviq.com/api/v1/", Ee = "https://edge.sandboxapi.kelviq.com/api/v1/", Le = "https://api.kelviq.com/api/v1/", ve = "https://sandboxapi.kelviq.com/api/v1/", ye = "entitlements/", Ae = "monetization/product-offering/", Se = ({
205
213
  children: n,
206
- apiUrl: s,
207
- entitlementsPath: o = me,
214
+ apiUrl: i,
215
+ entitlementsPath: s = ye,
208
216
  customerId: t,
209
- environment: E = "production",
210
- accessToken: g,
211
- config: v = {}
217
+ environment: m = "production",
218
+ accessToken: u,
219
+ productId: d,
220
+ plansEnabled: E,
221
+ config: o = {}
212
222
  }) => {
213
223
  const {
214
- onError: A,
215
- maxRetries: l,
216
- timeout: d,
217
- backoffBaseDelay: L,
218
- fetchEntitlementsOnMount: R = !0,
219
- fetchSubscriptionsOnMount: P = !1,
220
- fetchCustomerOnMount: C = !1
221
- } = v, a = s || (E === "sandbox" ? ce : ue), m = E === "sandbox" ? fe : de, [p, i] = $({
224
+ onError: a,
225
+ maxRetries: g,
226
+ timeout: q,
227
+ backoffBaseDelay: P,
228
+ fetchEntitlementsOnMount: b = !0,
229
+ fetchSubscriptionsOnMount: v = !1,
230
+ fetchCustomerOnMount: S = !1,
231
+ fetchPricingOnMount: $ = !1
232
+ } = o, l = i || (m === "sandbox" ? Ee : he), p = m === "sandbox" ? ve : Le, [y, A] = I({
222
233
  data: null,
223
- isLoading: !!a && R,
234
+ isLoading: !!l && b,
224
235
  error: null
225
- }), [f, T] = $(null), [b, q] = $({
236
+ }), [K, ie] = I(null), [k, M] = I({
226
237
  data: null,
227
- isLoading: !!a && P,
238
+ isLoading: !!l && v,
228
239
  error: null
229
- }), [N, I] = $({
240
+ }), [X, D] = I({
230
241
  data: null,
231
- isLoading: !!a && C,
242
+ isLoading: !!l && S,
232
243
  error: null
233
- }), [D, U] = $(
234
- !!a && R
235
- ), [k, x] = $(null), h = w(
236
- (u, e, r) => {
244
+ }), [z, _] = I({
245
+ data: null,
246
+ isLoading: !!l && $,
247
+ error: null
248
+ }), [F, T] = I(
249
+ !!l && b
250
+ ), [W, j] = I(null), h = R(
251
+ (f, e, r) => {
237
252
  const c = new Error(
238
- `Kelviq SDK (${e}): ${u}`
253
+ `Kelviq SDK (${e}): ${f}`
239
254
  );
240
- console.error(c.message, r || ""), x((y) => y || c), A && A(c);
255
+ console.error(c.message, r || ""), j((U) => U || c), a && a(c);
241
256
  },
242
- [A]
243
- ), _ = w(() => {
244
- if (!a) {
257
+ [a]
258
+ ), O = R(() => {
259
+ if (!l) {
245
260
  const r = "API URL not configured. Cannot fetch entitlements.";
246
- return i({
261
+ return A({
247
262
  data: null,
248
263
  isLoading: !1,
249
264
  error: new Error(r)
250
- }), h(r, "fetchAllEntitlements"), U(!1), Promise.reject(new Error(r));
265
+ }), h(r, "fetchAllEntitlements"), T(!1), Promise.reject(new Error(r));
251
266
  }
252
267
  if (!t) {
253
268
  const r = "CustomerId must be provided as props to KelviqProvider.";
254
- return i(() => ({
269
+ return A(() => ({
255
270
  data: null,
256
271
  isLoading: !1,
257
272
  error: new Error(r)
258
- })), h(r, "fetchAllEntitlements"), U(!1), Promise.reject(new Error(r));
273
+ })), h(r, "fetchAllEntitlements"), T(!1), Promise.reject(new Error(r));
259
274
  }
260
- i((r) => ({
275
+ A((r) => ({
261
276
  ...r,
262
277
  isLoading: !0,
263
278
  error: null
264
- })), U(!0), x(null);
265
- const u = `${a.replace(/\/$/, "")}/${o.replace(/^\//, "")}`;
266
- return O({
267
- url: u,
279
+ })), T(!0), j(null);
280
+ const f = `${l.replace(/\/$/, "")}/${s.replace(/^\//, "")}`;
281
+ return x({
282
+ url: f,
268
283
  method: "GET",
269
- timeout: d,
270
- maxRetries: l,
271
- backoffBaseDelay: L,
272
- accessToken: g,
284
+ timeout: q,
285
+ maxRetries: g,
286
+ backoffBaseDelay: P,
287
+ accessToken: u,
273
288
  queryParams: {
274
289
  customer_id: t
275
290
  }
276
291
  }).then((r) => {
277
292
  if (r && r.data && Array.isArray(r.data.entitlements)) {
278
- T(r.data);
279
- const c = le(
293
+ ie(r.data);
294
+ const c = pe(
280
295
  r.data
281
296
  );
282
- i({
297
+ A({
283
298
  data: c,
284
299
  isLoading: !1,
285
300
  error: null
@@ -288,7 +303,7 @@ const V = {
288
303
  const c = new Error(
289
304
  "Received empty, malformed, or invalid data structure from entitlements API."
290
305
  );
291
- throw i({
306
+ throw A({
292
307
  data: null,
293
308
  isLoading: !1,
294
309
  error: c
@@ -300,27 +315,27 @@ const V = {
300
315
  }
301
316
  }).catch((r) => {
302
317
  const c = r instanceof Error ? r : new Error(String(r));
303
- return i((y) => ({
304
- ...y,
318
+ return A((U) => ({
319
+ ...U,
305
320
  isLoading: !1,
306
321
  error: c
307
322
  })), h(c.message, "fetchAllEntitlements", r), Promise.reject(c);
308
323
  }).finally(() => {
309
- U(!1);
324
+ T(!1);
310
325
  });
311
326
  }, [
312
- a,
313
- t,
314
- o,
315
- d,
316
327
  l,
317
- L,
328
+ t,
329
+ s,
330
+ q,
318
331
  g,
332
+ P,
333
+ u,
319
334
  h
320
- ]), j = w(() => {
321
- if (!m) {
335
+ ]), B = R(() => {
336
+ if (!p) {
322
337
  const e = "Main API URL not configured. Cannot fetch subscriptions.";
323
- return q({
338
+ return M({
324
339
  data: null,
325
340
  isLoading: !1,
326
341
  error: new Error(e)
@@ -328,29 +343,29 @@ const V = {
328
343
  }
329
344
  if (!t) {
330
345
  const e = "CustomerId must be provided as props to KelviqProvider.";
331
- return q({
346
+ return M({
332
347
  data: null,
333
348
  isLoading: !1,
334
349
  error: new Error(e)
335
350
  }), h(e, "fetchSubscriptions"), Promise.reject(new Error(e));
336
351
  }
337
- q((e) => ({
352
+ M((e) => ({
338
353
  ...e,
339
354
  isLoading: !0,
340
355
  error: null
341
356
  }));
342
- const u = `${m.replace(/\/$/, "")}/subscriptions/`;
343
- return O({
344
- url: u,
357
+ const f = `${p.replace(/\/$/, "")}/subscriptions/`;
358
+ return x({
359
+ url: f,
345
360
  method: "GET",
346
- timeout: d,
347
- maxRetries: l,
348
- backoffBaseDelay: L,
349
- accessToken: g,
361
+ timeout: q,
362
+ maxRetries: g,
363
+ backoffBaseDelay: P,
364
+ accessToken: u,
350
365
  queryParams: { customer_id: t }
351
366
  }).then((e) => {
352
367
  if (e && e.data && Array.isArray(e.data.results))
353
- q({
368
+ M({
354
369
  data: e.data.results,
355
370
  isLoading: !1,
356
371
  error: null
@@ -359,7 +374,7 @@ const V = {
359
374
  const r = new Error(
360
375
  "Received empty, malformed, or invalid data structure from subscriptions API."
361
376
  );
362
- throw q({
377
+ throw M({
363
378
  data: null,
364
379
  isLoading: !1,
365
380
  error: r
@@ -371,7 +386,7 @@ const V = {
371
386
  }
372
387
  }).catch((e) => {
373
388
  const r = e instanceof Error ? e : new Error(String(e));
374
- return q((c) => ({
389
+ return M((c) => ({
375
390
  ...c,
376
391
  isLoading: !1,
377
392
  error: r
@@ -382,17 +397,17 @@ const V = {
382
397
  ), Promise.reject(r);
383
398
  });
384
399
  }, [
385
- m,
400
+ p,
386
401
  t,
387
- d,
388
- l,
389
- L,
402
+ q,
390
403
  g,
404
+ P,
405
+ u,
391
406
  h
392
- ]), F = w(() => {
393
- if (!m) {
407
+ ]), N = R(() => {
408
+ if (!p) {
394
409
  const e = "Main API URL not configured. Cannot fetch customer.";
395
- return I({
410
+ return D({
396
411
  data: null,
397
412
  isLoading: !1,
398
413
  error: new Error(e)
@@ -400,28 +415,28 @@ const V = {
400
415
  }
401
416
  if (!t) {
402
417
  const e = "CustomerId must be provided as props to KelviqProvider.";
403
- return I({
418
+ return D({
404
419
  data: null,
405
420
  isLoading: !1,
406
421
  error: new Error(e)
407
422
  }), h(e, "fetchCustomer"), Promise.reject(new Error(e));
408
423
  }
409
- I((e) => ({
424
+ D((e) => ({
410
425
  ...e,
411
426
  isLoading: !0,
412
427
  error: null
413
428
  }));
414
- const u = `${m.replace(/\/$/, "")}/customers/${t}/`;
415
- return O({
416
- url: u,
429
+ const f = `${p.replace(/\/$/, "")}/customers/${t}/`;
430
+ return x({
431
+ url: f,
417
432
  method: "GET",
418
- timeout: d,
419
- maxRetries: l,
420
- backoffBaseDelay: L,
421
- accessToken: g
433
+ timeout: q,
434
+ maxRetries: g,
435
+ backoffBaseDelay: P,
436
+ accessToken: u
422
437
  }).then((e) => {
423
438
  if (e && e.data)
424
- I({
439
+ D({
425
440
  data: e.data,
426
441
  isLoading: !1,
427
442
  error: null
@@ -430,7 +445,7 @@ const V = {
430
445
  const r = new Error(
431
446
  "Received empty or invalid data from customer API."
432
447
  );
433
- throw I({
448
+ throw D({
434
449
  data: null,
435
450
  isLoading: !1,
436
451
  error: r
@@ -442,207 +457,368 @@ const V = {
442
457
  }
443
458
  }).catch((e) => {
444
459
  const r = e instanceof Error ? e : new Error(String(e));
445
- return I((c) => ({
460
+ return D((c) => ({
446
461
  ...c,
447
462
  isLoading: !1,
448
463
  error: r
449
464
  })), h(r.message, "fetchCustomer", e), Promise.reject(r);
450
465
  });
451
466
  }, [
452
- m,
467
+ p,
453
468
  t,
469
+ q,
470
+ g,
471
+ P,
472
+ u,
473
+ h
474
+ ]), G = R(() => {
475
+ if (!p) {
476
+ const r = "Main API URL not configured. Cannot fetch pricing.";
477
+ return _({
478
+ data: null,
479
+ isLoading: !1,
480
+ error: new Error(r)
481
+ }), h(r, "fetchPricing"), Promise.reject(new Error(r));
482
+ }
483
+ if (!d) {
484
+ const r = "productId must be provided as a prop to KelviqProvider to fetch pricing.";
485
+ return _({
486
+ data: null,
487
+ isLoading: !1,
488
+ error: new Error(r)
489
+ }), h(r, "fetchPricing"), Promise.reject(new Error(r));
490
+ }
491
+ _((r) => ({
492
+ ...r,
493
+ isLoading: !0,
494
+ error: null
495
+ }));
496
+ const f = `${p.replace(/\/$/, "")}/${Ae.replace(/^\//, "")}${d}/`, e = {};
497
+ return t && (e.customer_id = t), E && (e.plans_enabled = E), x({
498
+ url: f,
499
+ method: "GET",
500
+ timeout: q,
501
+ maxRetries: g,
502
+ backoffBaseDelay: P,
503
+ accessToken: u,
504
+ queryParams: e
505
+ }).then((r) => {
506
+ if (r && r.data && Array.isArray(r.data.plans))
507
+ _({
508
+ data: r.data,
509
+ isLoading: !1,
510
+ error: null
511
+ });
512
+ else {
513
+ const c = new Error(
514
+ "Received empty, malformed, or invalid data structure from pricing API."
515
+ );
516
+ throw _({
517
+ data: null,
518
+ isLoading: !1,
519
+ error: c
520
+ }), h(
521
+ c.message,
522
+ "fetchPricing",
523
+ r
524
+ ), c;
525
+ }
526
+ }).catch((r) => {
527
+ const c = r instanceof Error ? r : new Error(String(r));
528
+ return _((U) => ({
529
+ ...U,
530
+ isLoading: !1,
531
+ error: c
532
+ })), h(c.message, "fetchPricing", r), Promise.reject(c);
533
+ });
534
+ }, [
535
+ p,
454
536
  d,
455
- l,
456
- L,
537
+ t,
538
+ E,
539
+ q,
457
540
  g,
541
+ P,
542
+ u,
458
543
  h
459
544
  ]);
460
- ee(() => {
461
- let u = !0;
462
- if (!a) {
463
- if (u) {
545
+ ae(() => {
546
+ let f = !0;
547
+ if (!l) {
548
+ if (f) {
464
549
  const e = new Error(
465
550
  "KelviqProvider: `apiBaseUrl` must be provided in config."
466
551
  );
467
- x(e), U(!1), i((r) => ({
552
+ j(e), T(!1), A((r) => ({
468
553
  ...r,
469
554
  isLoading: !1,
470
555
  error: e
471
- })), v.onError && v.onError(e);
556
+ })), o.onError && o.onError(e);
472
557
  }
473
558
  return;
474
559
  }
475
- return R ? _().catch((e) => {
476
- u && D && U(!1), console.error(
560
+ return b ? O().catch((e) => {
561
+ f && F && T(!1), console.error(
477
562
  "Kelviq SDK: Initial entitlement fetch failed.",
478
563
  e
479
564
  );
480
- }) : u && (i((e) => ({
565
+ }) : f && (A((e) => ({
481
566
  ...e,
482
567
  isLoading: !1,
483
568
  data: null,
484
569
  error: null
485
- })), U(!1)), P && j().catch((e) => {
570
+ })), T(!1)), v && B().catch((e) => {
486
571
  console.error(
487
572
  "Kelviq SDK: Initial subscriptions fetch failed.",
488
573
  e
489
574
  );
490
- }), C && F().catch((e) => {
575
+ }), S && N().catch((e) => {
491
576
  console.error(
492
577
  "Kelviq SDK: Initial customer fetch failed.",
493
578
  e
494
579
  );
580
+ }), $ && G().catch((e) => {
581
+ console.error(
582
+ "Kelviq SDK: Initial pricing fetch failed.",
583
+ e
584
+ );
495
585
  }), () => {
496
- u = !1;
586
+ f = !1;
497
587
  };
498
- }, [a, R, P, C, v.onError]);
499
- const G = w(
500
- () => p.data ? p.data : {},
501
- [p.data]
502
- ), H = w(
503
- () => f ? {
504
- customerId: f.customerId,
505
- entitlements: f.entitlements
588
+ }, [l, b, v, S, $]);
589
+ const J = R(
590
+ () => y.data ? y.data : {},
591
+ [y.data]
592
+ ), Q = R(
593
+ () => K ? {
594
+ customerId: K.customerId,
595
+ entitlements: K.entitlements
506
596
  } : null,
507
- [f]
508
- ), X = w(
509
- (u) => f ? {
510
- customerId: f.customerId,
511
- entitlements: f.entitlements.filter(
512
- (e) => e.featureId === u
597
+ [K]
598
+ ), V = R(
599
+ (f) => K ? {
600
+ customerId: K.customerId,
601
+ entitlements: K.entitlements.filter(
602
+ (e) => e.featureId === f
513
603
  )
514
604
  } : null,
515
- [f]
516
- ), W = w(
517
- (u) => p.data ? p.data[u] ?? null : null,
518
- [p.data]
519
- ), z = w(
520
- (u, e) => {
521
- i((r) => {
605
+ [K]
606
+ ), Y = R(
607
+ (f) => y.data ? y.data[f] ?? null : null,
608
+ [y.data]
609
+ ), Z = R(
610
+ (f, e) => {
611
+ A((r) => {
522
612
  if (!r.data) return r;
523
- const c = r.data[u];
613
+ const c = r.data[f];
524
614
  if (!c) return r;
525
- const y = { ...c, ...e };
526
- return ("usageLimit" in e || "currentUsage" in e) && (y.remaining = y.usageLimit !== null ? y.usageLimit - y.currentUsage : null), {
615
+ const U = { ...c, ...e };
616
+ return ("usageLimit" in e || "currentUsage" in e) && (U.remaining = U.usageLimit !== null ? U.usageLimit - U.currentUsage : null), {
527
617
  ...r,
528
618
  data: {
529
619
  ...r.data,
530
- [u]: y
620
+ [f]: U
531
621
  }
532
622
  };
533
623
  });
534
624
  },
535
625
  []
536
- ), J = w(
537
- (u) => {
538
- if (!p.data) return !1;
539
- const e = p.data[u];
626
+ ), ee = R(
627
+ (f) => {
628
+ if (!y.data) return !1;
629
+ const e = y.data[f];
540
630
  return e ? e.hasAccess : !1;
541
631
  },
542
- [p.data]
543
- ), Z = te(
632
+ [y.data]
633
+ ), oe = le(
544
634
  () => ({
545
- allEntitlements: p,
546
- refreshAllEntitlements: _,
547
- getEntitlements: G,
548
- getEntitlement: W,
549
- getRawEntitlements: H,
550
- getRawEntitlement: X,
551
- hasAccess: J,
552
- updateEntitlement: z,
553
- subscriptions: b,
554
- refreshSubscriptions: j,
555
- customer: N,
556
- refreshCustomer: F,
557
- isLoading: D,
558
- error: k,
559
- environment: E,
560
- apiUrl: a,
561
- entitlementsPath: o,
635
+ allEntitlements: y,
636
+ refreshAllEntitlements: O,
637
+ getEntitlements: J,
638
+ getEntitlement: Y,
639
+ getRawEntitlements: Q,
640
+ getRawEntitlement: V,
641
+ hasAccess: ee,
642
+ updateEntitlement: Z,
643
+ subscriptions: k,
644
+ refreshSubscriptions: B,
645
+ customer: X,
646
+ refreshCustomer: N,
647
+ pricing: z,
648
+ refreshPricing: G,
649
+ isLoading: F,
650
+ error: W,
651
+ environment: m,
652
+ apiUrl: l,
653
+ entitlementsPath: s,
562
654
  customerId: t
563
655
  }),
564
656
  [
565
- p,
566
- _,
567
- G,
568
- W,
569
- H,
570
- X,
657
+ y,
658
+ O,
571
659
  J,
572
- z,
573
- b,
574
- j,
660
+ Y,
661
+ Q,
662
+ V,
663
+ ee,
664
+ Z,
665
+ k,
666
+ B,
667
+ X,
575
668
  N,
669
+ z,
670
+ G,
576
671
  F,
577
- D,
578
- k,
579
- E,
580
- a,
581
- o,
672
+ W,
673
+ m,
674
+ l,
675
+ s,
582
676
  t
583
677
  ]
584
678
  );
585
- return /* @__PURE__ */ S(Y.Provider, { value: Z, children: n });
586
- }, K = () => {
587
- const n = re(Y);
588
- if (n === void 0 || n === V)
679
+ return /* @__PURE__ */ L(te.Provider, { value: oe, children: n });
680
+ }, C = () => {
681
+ const n = ce(te);
682
+ if (n === void 0 || n === re)
589
683
  throw new Error(
590
684
  "useKelviq must be used within an initialized KelviqProvider."
591
685
  );
592
686
  return n;
593
- }, B = ({
687
+ }, H = ({
594
688
  featureId: n,
595
- children: s,
596
- fallback: o = null,
689
+ children: i,
690
+ fallback: s = null,
597
691
  loadingComponent: t,
598
- condition: E
692
+ condition: m
599
693
  }) => {
600
694
  const {
601
- getEntitlement: g,
602
- isLoading: v,
603
- error: A
604
- } = K(), l = g(n);
605
- return v && !l ? /* @__PURE__ */ S(M, { children: t !== void 0 ? t : o }) : A && !l ? /* @__PURE__ */ S(M, { children: o }) : !l || !l.hasAccess || E && !E(l) ? /* @__PURE__ */ S(M, { children: o }) : typeof s == "function" ? /* @__PURE__ */ S(M, { children: s(l) }) : /* @__PURE__ */ S(M, { children: s });
606
- }, he = (n) => /* @__PURE__ */ S(B, { ...n }), Le = (n) => {
607
- const s = (o) => o.hasAccess && (o.remaining === null || o.remaining > 0);
608
- return /* @__PURE__ */ S(
609
- B,
695
+ getEntitlement: u,
696
+ isLoading: d,
697
+ error: E
698
+ } = C(), o = u(n);
699
+ return d && !o ? /* @__PURE__ */ L(w, { children: t !== void 0 ? t : s }) : E && !o ? /* @__PURE__ */ L(w, { children: s }) : !o || !o.hasAccess || m && !m(o) ? /* @__PURE__ */ L(w, { children: s }) : typeof i == "function" ? /* @__PURE__ */ L(w, { children: i(o) }) : /* @__PURE__ */ L(w, { children: i });
700
+ }, be = (n) => /* @__PURE__ */ L(H, { ...n }), Ke = (n) => {
701
+ const i = (s) => s.hasAccess && (s.remaining === null || s.remaining > 0);
702
+ return /* @__PURE__ */ L(
703
+ H,
610
704
  {
611
705
  ...n,
612
- condition: n.condition || s
706
+ condition: n.condition || i
613
707
  }
614
708
  );
615
- }, ve = (n) => /* @__PURE__ */ S(B, { ...n }), Ae = () => {
616
- const { allEntitlements: n } = K();
709
+ }, Ue = (n) => /* @__PURE__ */ L(H, { ...n }), Re = () => {
710
+ const { allEntitlements: n } = C();
617
711
  return n;
618
- }, we = (n) => {
619
- const { getEntitlement: s } = K();
620
- return s(n);
621
- }, qe = (n) => {
622
- const { getEntitlement: s } = K();
623
- return s(n);
624
- }, ye = (n) => {
625
- const { getEntitlement: s } = K();
626
- return s(n);
627
- }, Se = () => {
628
- const { subscriptions: n } = K();
712
+ }, Ce = (n) => {
713
+ const { getEntitlement: i } = C();
714
+ return i(n);
715
+ }, $e = (n) => {
716
+ const { getEntitlement: i } = C();
717
+ return i(n);
718
+ }, Te = (n) => {
719
+ const { getEntitlement: i } = C();
720
+ return i(n);
721
+ }, Ie = () => {
722
+ const { subscriptions: n } = C();
629
723
  return n;
630
- }, be = () => {
631
- const { customer: n } = K();
724
+ }, Me = () => {
725
+ const { customer: n } = C();
632
726
  return n;
727
+ }, ne = () => {
728
+ const { pricing: n } = C();
729
+ return n;
730
+ }, Pe = (n, i, s = {}) => {
731
+ if (typeof n != "number")
732
+ return "";
733
+ const {
734
+ compact: t = !1,
735
+ locale: m = s.pricingLocale || "en-US",
736
+ includeCurrencySymbol: u = !0
737
+ } = s;
738
+ try {
739
+ const d = {
740
+ notation: t ? "compact" : "standard",
741
+ compactDisplay: "short"
742
+ }, o = new Intl.NumberFormat(m, d).format(n);
743
+ return u ? `${i}${o}` : o;
744
+ } catch {
745
+ const d = n.toString();
746
+ return u ? `${i}${d}` : d;
747
+ }
748
+ }, De = ({
749
+ planIdentifier: n,
750
+ billingPeriod: i,
751
+ children: s,
752
+ formatOptions: t,
753
+ loadingComponent: m = null,
754
+ fallback: u = null
755
+ }) => {
756
+ const { data: d, isLoading: E } = ne();
757
+ if (E)
758
+ return /* @__PURE__ */ L(w, { children: m });
759
+ if (!d || !Array.isArray(d.plans))
760
+ return /* @__PURE__ */ L(w, { children: u });
761
+ const o = d.plans.find(
762
+ (b) => b.identifier === n && b.enabled
763
+ );
764
+ if (!o)
765
+ return /* @__PURE__ */ L(w, { children: u });
766
+ const a = o.price.charges.find(
767
+ (b) => b.chargePeriod === i
768
+ ) ?? null, g = a ? a.priceData.amount : 0, q = o.price.priceType === "FREE", P = q ? "Free" : Pe(g, d.currencySymbol, {
769
+ ...t,
770
+ pricingLocale: d.pricingLocale
771
+ });
772
+ return /* @__PURE__ */ L(w, { children: s({
773
+ plan: o,
774
+ charge: a,
775
+ amount: g,
776
+ formattedPrice: P,
777
+ currencySymbol: d.currencySymbol,
778
+ currencyCode: d.currencyCode,
779
+ pricingLocale: d.pricingLocale,
780
+ isFree: q,
781
+ hasFreeTrial: o.price.freeTrial,
782
+ trialPeriod: o.price.trialPeriod
783
+ }) });
784
+ }, _e = ({
785
+ planIdentifier: n,
786
+ children: i,
787
+ loadingComponent: s = null,
788
+ fallback: t = null,
789
+ featureType: m
790
+ }) => {
791
+ const { data: u, isLoading: d } = ne();
792
+ if (d)
793
+ return /* @__PURE__ */ L(w, { children: s });
794
+ if (!u || !Array.isArray(u.plans))
795
+ return /* @__PURE__ */ L(w, { children: t });
796
+ const E = u.plans.find(
797
+ (a) => a.identifier === n && a.enabled
798
+ );
799
+ if (!E)
800
+ return /* @__PURE__ */ L(w, { children: t });
801
+ let o = E.features.filter((a) => a.enabled);
802
+ return m && (o = o.filter((a) => a.featureType === m)), o.length === 0 ? /* @__PURE__ */ L(w, { children: t }) : /* @__PURE__ */ L(w, { children: o.map(
803
+ (a, g) => i({ feature: a, plan: E, index: g })
804
+ ) });
633
805
  };
634
806
  export {
635
- Y as KelviqContext,
636
- pe as KelviqProvider,
637
- ve as ShowWhenBooleanEntitled,
638
- he as ShowWhenCustomizableEntitled,
639
- Le as ShowWhenMeteredEntitled,
640
- V as defaultKelviqContextValue,
641
- Ae as useAllEntitlements,
642
- we as useBooleanEntitlement,
643
- be as useCustomer,
644
- qe as useCustomizableEntitlement,
645
- K as useKelviq,
646
- ye as useMeteredEntitlement,
647
- Se as useSubscriptions
807
+ _e as KQFeatureList,
808
+ De as KQPrice,
809
+ te as KelviqContext,
810
+ Se as KelviqProvider,
811
+ Ue as ShowWhenBooleanEntitled,
812
+ be as ShowWhenCustomizableEntitled,
813
+ Ke as ShowWhenMeteredEntitled,
814
+ re as defaultKelviqContextValue,
815
+ Pe as kqFormatPrice,
816
+ Re as useAllEntitlements,
817
+ Ce as useBooleanEntitlement,
818
+ Me as useCustomer,
819
+ $e as useCustomizableEntitlement,
820
+ C as useKelviq,
821
+ Te as useMeteredEntitlement,
822
+ ne as usePricing,
823
+ Ie as useSubscriptions
648
824
  };