@dismissible/react-client 1.0.0-canary.4.a9fb4a5 → 2.0.0-canary.5.88c27fb

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,59 +1,291 @@
1
1
  var he = Object.defineProperty, me = Object.defineProperties;
2
2
  var ye = Object.getOwnPropertyDescriptors;
3
- var J = Object.getOwnPropertySymbols;
4
- var te = Object.prototype.hasOwnProperty, re = Object.prototype.propertyIsEnumerable;
5
- var ee = (e, r, t) => r in e ? he(e, r, { enumerable: !0, configurable: !0, writable: !0, value: t }) : e[r] = t, b = (e, r) => {
6
- for (var t in r || (r = {}))
7
- te.call(r, t) && ee(e, t, r[t]);
8
- if (J)
9
- for (var t of J(r))
10
- re.call(r, t) && ee(e, t, r[t]);
11
- return e;
12
- }, E = (e, r) => me(e, ye(r));
13
- var G = (e, r) => {
14
- var t = {};
15
- for (var n in e)
16
- te.call(e, n) && r.indexOf(n) < 0 && (t[n] = e[n]);
17
- if (e != null && J)
18
- for (var n of J(e))
19
- r.indexOf(n) < 0 && re.call(e, n) && (t[n] = e[n]);
3
+ var M = Object.getOwnPropertySymbols;
4
+ var ee = Object.prototype.hasOwnProperty, te = Object.prototype.propertyIsEnumerable;
5
+ var Z = (t, r, e) => r in t ? he(t, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : t[r] = e, y = (t, r) => {
6
+ for (var e in r || (r = {}))
7
+ ee.call(r, e) && Z(t, e, r[e]);
8
+ if (M)
9
+ for (var e of M(r))
10
+ te.call(r, e) && Z(t, e, r[e]);
20
11
  return t;
12
+ }, R = (t, r) => me(t, ye(r));
13
+ var G = (t, r) => {
14
+ var e = {};
15
+ for (var s in t)
16
+ ee.call(t, s) && r.indexOf(s) < 0 && (e[s] = t[s]);
17
+ if (t != null && M)
18
+ for (var s of M(t))
19
+ r.indexOf(s) < 0 && te.call(t, s) && (e[s] = t[s]);
20
+ return e;
21
21
  };
22
- var $ = (e, r, t) => new Promise((n, o) => {
23
- var s = (f) => {
22
+ var C = (t, r, e) => new Promise((s, o) => {
23
+ var n = (f) => {
24
24
  try {
25
- c(t.next(f));
26
- } catch (d) {
27
- o(d);
25
+ a(e.next(f));
26
+ } catch (m) {
27
+ o(m);
28
28
  }
29
29
  }, i = (f) => {
30
30
  try {
31
- c(t.throw(f));
32
- } catch (d) {
33
- o(d);
31
+ a(e.throw(f));
32
+ } catch (m) {
33
+ o(m);
34
34
  }
35
- }, c = (f) => f.done ? n(f.value) : Promise.resolve(f.value).then(s, i);
36
- c((t = t.apply(e, r)).next());
35
+ }, a = (f) => f.done ? s(f.value) : Promise.resolve(f.value).then(n, i);
36
+ a((e = e.apply(t, r)).next());
37
37
  });
38
- import { jsx as U, jsxs as be } from "react/jsx-runtime";
39
- import { createContext as pe, useContext as we, useMemo as X, useRef as W, useState as z, useCallback as K, useEffect as Q } from "react";
40
- const ge = /\{[^{}]+\}/g, ve = () => {
41
- var e, r;
42
- return typeof process == "object" && Number.parseInt((r = (e = process == null ? void 0 : process.versions) == null ? void 0 : e.node) == null ? void 0 : r.substring(0, 2)) >= 18 && process.versions.undici;
38
+ import { jsx as H, jsxs as be } from "react/jsx-runtime";
39
+ import { createContext as we, useContext as pe, useRef as J, useState as z, useCallback as K, useEffect as Q, useMemo as re } from "react";
40
+ const se = (t, r, e) => {
41
+ try {
42
+ const s = `${r}_${t}`, o = localStorage.getItem(s);
43
+ if (!o) return null;
44
+ const { data: n, timestamp: i } = JSON.parse(o);
45
+ return e && Date.now() - i > e ? (localStorage.removeItem(s), null) : n;
46
+ } catch (s) {
47
+ return null;
48
+ }
49
+ }, V = (t, r, e) => {
50
+ try {
51
+ const s = `${e}_${t}`, o = {
52
+ data: r,
53
+ timestamp: Date.now()
54
+ };
55
+ localStorage.setItem(s, JSON.stringify(o));
56
+ } catch (s) {
57
+ console.warn("Failed to cache dismissible item:", s);
58
+ }
59
+ }, ne = (t, r) => {
60
+ try {
61
+ const e = `${r}_${t}`;
62
+ localStorage.removeItem(e);
63
+ } catch (e) {
64
+ console.warn("Failed to remove cached dismissible item:", e);
65
+ }
66
+ }, ce = we(
67
+ null
68
+ ), ge = () => {
69
+ const t = pe(ce);
70
+ if (!t)
71
+ throw new Error(
72
+ "useDismissibleContext must be used within a DismissibleProvider"
73
+ );
74
+ return t;
75
+ }, ve = "dismissible", Ee = (t, r = {}) => {
76
+ const {
77
+ initialData: e,
78
+ enableCache: s = !0,
79
+ cachePrefix: o = ve,
80
+ cacheExpiration: n
81
+ } = r, i = ge(), { userId: a, client: f, baseUrl: m } = i, d = `${a}-${t}`, A = J({
82
+ enableCache: s,
83
+ cachePrefix: o,
84
+ cacheExpiration: n
85
+ }), u = J(t), l = J(d), E = J(null), [$, j] = z(!1), [q, p] = z(), [x, S] = z(() => {
86
+ if (e) return e;
87
+ if (s) {
88
+ const c = se(
89
+ d,
90
+ o,
91
+ n
92
+ );
93
+ if (c) return c;
94
+ }
95
+ }), D = K(() => C(null, null, function* () {
96
+ var g;
97
+ if (s) {
98
+ const b = se(
99
+ d,
100
+ o,
101
+ n
102
+ );
103
+ if (b != null && b.dismissedAt) {
104
+ S(b), j(!1);
105
+ return;
106
+ }
107
+ }
108
+ (g = E.current) == null || g.abort();
109
+ const c = new AbortController();
110
+ E.current = c, j(!0), p(void 0);
111
+ try {
112
+ const b = yield i.getAuthHeaders(), O = yield f.getOrCreate({
113
+ userId: a,
114
+ itemId: t,
115
+ baseUrl: m,
116
+ authHeaders: b,
117
+ signal: c.signal
118
+ });
119
+ S(O), s && V(d, O, o);
120
+ } catch (b) {
121
+ if (b instanceof Error && b.name === "AbortError")
122
+ return;
123
+ p(
124
+ b instanceof Error ? b : new Error("Unknown error occurred")
125
+ );
126
+ } finally {
127
+ j(!1);
128
+ }
129
+ }), [
130
+ t,
131
+ a,
132
+ d,
133
+ s,
134
+ o,
135
+ n,
136
+ f,
137
+ m,
138
+ i
139
+ ]);
140
+ Q(() => {
141
+ const c = u.current !== t, g = l.current !== d;
142
+ c || g ? (u.current = t, l.current = d, D()) : e || D();
143
+ }, [t, d, e, D]), Q(() => () => {
144
+ var c;
145
+ (c = E.current) == null || c.abort();
146
+ }, []), Q(() => {
147
+ const c = A.current;
148
+ (c.enableCache !== s || c.cachePrefix !== o || c.cacheExpiration !== n) && (c.cachePrefix !== o && ne(d, c.cachePrefix), !s && c.enableCache && ne(d, c.cachePrefix), A.current = {
149
+ enableCache: s,
150
+ cachePrefix: o,
151
+ cacheExpiration: n
152
+ }, D());
153
+ }, [s, o, n, d, D]);
154
+ const k = K(() => C(null, null, function* () {
155
+ p(void 0);
156
+ try {
157
+ const c = yield i.getAuthHeaders(), g = yield f.dismiss({
158
+ userId: a,
159
+ itemId: t,
160
+ baseUrl: m,
161
+ authHeaders: c
162
+ });
163
+ S(g), s && V(d, g, o);
164
+ } catch (c) {
165
+ throw p(
166
+ c instanceof Error ? c : new Error("Failed to dismiss item")
167
+ ), c;
168
+ }
169
+ }), [
170
+ t,
171
+ a,
172
+ d,
173
+ s,
174
+ o,
175
+ f,
176
+ m,
177
+ i
178
+ ]), _ = K(() => C(null, null, function* () {
179
+ p(void 0);
180
+ try {
181
+ const c = yield i.getAuthHeaders(), g = yield f.restore({
182
+ userId: a,
183
+ itemId: t,
184
+ baseUrl: m,
185
+ authHeaders: c
186
+ });
187
+ S(g), s && V(d, g, o);
188
+ } catch (c) {
189
+ throw p(
190
+ c instanceof Error ? c : new Error("Failed to restore item")
191
+ ), c;
192
+ }
193
+ }), [
194
+ t,
195
+ a,
196
+ d,
197
+ s,
198
+ o,
199
+ f,
200
+ m,
201
+ i
202
+ ]);
203
+ return {
204
+ dismissedAt: x == null ? void 0 : x.dismissedAt,
205
+ dismiss: k,
206
+ restore: _,
207
+ isLoading: $,
208
+ error: q,
209
+ item: x
210
+ };
211
+ }, Re = () => /* @__PURE__ */ H("div", { className: "dismissible-loading", "aria-live": "polite", children: "Loading..." }), Ce = () => /* @__PURE__ */ H("div", { className: "dismissible-error", role: "alert", children: "Unable to load content. Please try again later." }), xe = ({ onDismiss: t, ariaLabel: r }) => /* @__PURE__ */ H(
212
+ "button",
213
+ {
214
+ className: "dismissible-button",
215
+ onClick: t,
216
+ "aria-label": r,
217
+ type: "button",
218
+ children: "×"
219
+ }
220
+ ), Fe = ({
221
+ itemId: t,
222
+ children: r,
223
+ onDismiss: e,
224
+ LoadingComponent: s = Re,
225
+ ErrorComponent: o = Ce,
226
+ DismissButtonComponent: n = xe,
227
+ enableCache: i,
228
+ cachePrefix: a,
229
+ cacheExpiration: f,
230
+ ignoreErrors: m = !1
231
+ }) => {
232
+ const { dismissedAt: d, isLoading: A, error: u, dismiss: l } = Ee(
233
+ t,
234
+ {
235
+ enableCache: i,
236
+ cachePrefix: a,
237
+ cacheExpiration: f
238
+ }
239
+ ), [E, $] = z(!1), [j, q] = z(t);
240
+ t !== j && (q(t), $(!1));
241
+ const p = () => C(null, null, function* () {
242
+ $(!0);
243
+ try {
244
+ yield l(), e == null || e();
245
+ } catch (x) {
246
+ $(!1);
247
+ }
248
+ });
249
+ return A && s ? /* @__PURE__ */ H(s, { itemId: t }) : A && !s ? null : u && o && !m ? /* @__PURE__ */ H(o, { itemId: t, error: u }) : d || E ? null : /* @__PURE__ */ be("div", { className: "dismissible-container", children: [
250
+ /* @__PURE__ */ H("div", { className: "dismissible-content", children: r }),
251
+ n ? /* @__PURE__ */ H(
252
+ n,
253
+ {
254
+ onDismiss: p,
255
+ ariaLabel: `Dismiss ${t}`
256
+ }
257
+ ) : null
258
+ ] });
259
+ }, Se = (t) => C(null, null, function* () {
260
+ if (typeof t == "function")
261
+ try {
262
+ const r = t();
263
+ return yield Promise.resolve(r);
264
+ } catch (r) {
265
+ console.warn("Failed to resolve JWT from function:", r);
266
+ return;
267
+ }
268
+ return t;
269
+ }), Ae = (t) => C(null, null, function* () {
270
+ const r = yield Se(t);
271
+ return r ? { Authorization: `Bearer ${r}` } : {};
272
+ }), $e = /\{[^{}]+\}/g, je = () => {
273
+ var t, r;
274
+ return typeof process == "object" && Number.parseInt((r = (t = process == null ? void 0 : process.versions) == null ? void 0 : t.node) == null ? void 0 : r.substring(0, 2)) >= 18 && process.versions.undici;
43
275
  };
44
- function Ee() {
276
+ function De() {
45
277
  return Math.random().toString(36).slice(2, 11);
46
278
  }
47
- function Re(e) {
48
- let j = b({}, e), {
279
+ function Ie(t) {
280
+ let A = y({}, t), {
49
281
  baseUrl: r = "",
50
- Request: t = globalThis.Request,
51
- fetch: n = globalThis.fetch,
282
+ Request: e = globalThis.Request,
283
+ fetch: s = globalThis.fetch,
52
284
  querySerializer: o,
53
- bodySerializer: s,
285
+ bodySerializer: n,
54
286
  headers: i,
55
- requestInitExt: c = void 0
56
- } = j, f = G(j, [
287
+ requestInitExt: a = void 0
288
+ } = A, f = G(A, [
57
289
  "baseUrl",
58
290
  "Request",
59
291
  "fetch",
@@ -62,23 +294,23 @@ function Re(e) {
62
294
  "headers",
63
295
  "requestInitExt"
64
296
  ]);
65
- c = ve() ? c : void 0, r = oe(r);
66
- const d = [];
67
- function g(l, a) {
68
- return $(this, null, function* () {
69
- var Z;
70
- const Y = a || {}, {
71
- baseUrl: R,
72
- fetch: x = n,
73
- Request: I = t,
74
- headers: C,
75
- params: v = {},
76
- parseAs: A = "json",
297
+ a = je() ? a : void 0, r = ae(r);
298
+ const m = [];
299
+ function d(u, l) {
300
+ return C(this, null, function* () {
301
+ var Y;
302
+ const X = l || {}, {
303
+ baseUrl: E,
304
+ fetch: $ = s,
305
+ Request: j = e,
306
+ headers: q,
307
+ params: p = {},
308
+ parseAs: x = "json",
77
309
  querySerializer: S,
78
- bodySerializer: k = s != null ? s : xe,
79
- body: F,
80
- middleware: u = []
81
- } = Y, y = G(Y, [
310
+ bodySerializer: D = n != null ? n : He,
311
+ body: k,
312
+ middleware: _ = []
313
+ } = X, c = G(X, [
82
314
  "baseUrl",
83
315
  "fetch",
84
316
  "Request",
@@ -90,630 +322,439 @@ function Re(e) {
90
322
  "body",
91
323
  "middleware"
92
324
  ]);
93
- let h = r;
94
- R && (h = (Z = oe(R)) != null ? Z : r);
95
- let H = typeof o == "function" ? o : ne(o);
96
- S && (H = typeof S == "function" ? S : ne(b(b({}, typeof o == "object" ? o : {}), S)));
97
- const P = F === void 0 ? void 0 : k(
98
- F,
325
+ let g = r;
326
+ E && (g = (Y = ae(E)) != null ? Y : r);
327
+ let b = typeof o == "function" ? o : oe(o);
328
+ S && (b = typeof S == "function" ? S : oe(y(y({}, typeof o == "object" ? o : {}), S)));
329
+ const O = k === void 0 ? void 0 : D(
330
+ k,
99
331
  // Note: we declare mergeHeaders() both here and below because it’s a bit of a chicken-or-egg situation:
100
332
  // bodySerializer() needs all headers so we aren’t dropping ones set by the user, however,
101
333
  // the result of this ALSO sets the lowest-priority content-type header. So we re-merge below,
102
334
  // setting the content-type at the very beginning to be overwritten.
103
335
  // Lastly, based on the way headers work, it’s not a simple “present-or-not” check becauase null intentionally un-sets headers.
104
- se(i, C, v.header)
105
- ), fe = se(
336
+ ie(i, q, p.header)
337
+ ), fe = ie(
106
338
  // with no body, we should not to set Content-Type
107
- P === void 0 || // if serialized body is FormData; browser will correctly set Content-Type & boundary expression
108
- P instanceof FormData ? {} : {
339
+ O === void 0 || // if serialized body is FormData; browser will correctly set Content-Type & boundary expression
340
+ O instanceof FormData ? {} : {
109
341
  "Content-Type": "application/json"
110
342
  },
111
343
  i,
112
- C,
113
- v.header
114
- ), D = [...d, ...u], de = E(b(b({
344
+ q,
345
+ p.header
346
+ ), I = [...m, ..._], de = R(y(y({
115
347
  redirect: "follow"
116
- }, f), y), {
117
- body: P,
348
+ }, f), c), {
349
+ body: O,
118
350
  headers: fe
119
351
  });
120
- let L, N, T = new I(
121
- Se(l, { baseUrl: h, params: v, querySerializer: H }),
352
+ let F, L, T = new j(
353
+ Ue(u, { baseUrl: g, params: p, querySerializer: b }),
122
354
  de
123
- ), m;
124
- for (const w in y)
125
- w in T || (T[w] = y[w]);
126
- if (D.length) {
127
- L = Ee(), N = Object.freeze({
128
- baseUrl: h,
129
- fetch: x,
130
- parseAs: A,
131
- querySerializer: H,
132
- bodySerializer: k
355
+ ), h;
356
+ for (const v in c)
357
+ v in T || (T[v] = c[v]);
358
+ if (I.length) {
359
+ F = De(), L = Object.freeze({
360
+ baseUrl: g,
361
+ fetch: $,
362
+ parseAs: x,
363
+ querySerializer: b,
364
+ bodySerializer: D
133
365
  });
134
- for (const w of D)
135
- if (w && typeof w == "object" && typeof w.onRequest == "function") {
136
- const p = yield w.onRequest({
366
+ for (const v of I)
367
+ if (v && typeof v == "object" && typeof v.onRequest == "function") {
368
+ const w = yield v.onRequest({
137
369
  request: T,
138
- schemaPath: l,
139
- params: v,
140
- options: N,
141
- id: L
370
+ schemaPath: u,
371
+ params: p,
372
+ options: L,
373
+ id: F
142
374
  });
143
- if (p)
144
- if (p instanceof I)
145
- T = p;
146
- else if (p instanceof Response) {
147
- m = p;
375
+ if (w)
376
+ if (w instanceof j)
377
+ T = w;
378
+ else if (w instanceof Response) {
379
+ h = w;
148
380
  break;
149
381
  } else
150
382
  throw new Error("onRequest: must return new Request() or Response() when modifying the request");
151
383
  }
152
384
  }
153
- if (!m) {
385
+ if (!h) {
154
386
  try {
155
- m = yield x(T, c);
156
- } catch (w) {
157
- let p = w;
158
- if (D.length)
159
- for (let q = D.length - 1; q >= 0; q--) {
160
- const M = D[q];
161
- if (M && typeof M == "object" && typeof M.onError == "function") {
162
- const O = yield M.onError({
387
+ h = yield $(T, a);
388
+ } catch (v) {
389
+ let w = v;
390
+ if (I.length)
391
+ for (let U = I.length - 1; U >= 0; U--) {
392
+ const N = I[U];
393
+ if (N && typeof N == "object" && typeof N.onError == "function") {
394
+ const P = yield N.onError({
163
395
  request: T,
164
- error: p,
165
- schemaPath: l,
166
- params: v,
167
- options: N,
168
- id: L
396
+ error: w,
397
+ schemaPath: u,
398
+ params: p,
399
+ options: L,
400
+ id: F
169
401
  });
170
- if (O) {
171
- if (O instanceof Response) {
172
- p = void 0, m = O;
402
+ if (P) {
403
+ if (P instanceof Response) {
404
+ w = void 0, h = P;
173
405
  break;
174
406
  }
175
- if (O instanceof Error) {
176
- p = O;
407
+ if (P instanceof Error) {
408
+ w = P;
177
409
  continue;
178
410
  }
179
411
  throw new Error("onError: must return new Response() or instance of Error");
180
412
  }
181
413
  }
182
414
  }
183
- if (p)
184
- throw p;
415
+ if (w)
416
+ throw w;
185
417
  }
186
- if (D.length)
187
- for (let w = D.length - 1; w >= 0; w--) {
188
- const p = D[w];
189
- if (p && typeof p == "object" && typeof p.onResponse == "function") {
190
- const q = yield p.onResponse({
418
+ if (I.length)
419
+ for (let v = I.length - 1; v >= 0; v--) {
420
+ const w = I[v];
421
+ if (w && typeof w == "object" && typeof w.onResponse == "function") {
422
+ const U = yield w.onResponse({
191
423
  request: T,
192
- response: m,
193
- schemaPath: l,
194
- params: v,
195
- options: N,
196
- id: L
424
+ response: h,
425
+ schemaPath: u,
426
+ params: p,
427
+ options: L,
428
+ id: F
197
429
  });
198
- if (q) {
199
- if (!(q instanceof Response))
430
+ if (U) {
431
+ if (!(U instanceof Response))
200
432
  throw new Error("onResponse: must return new Response() when modifying the response");
201
- m = q;
433
+ h = U;
202
434
  }
203
435
  }
204
436
  }
205
437
  }
206
- if (m.status === 204 || T.method === "HEAD" || m.headers.get("Content-Length") === "0")
207
- return m.ok ? { data: void 0, response: m } : { error: void 0, response: m };
208
- if (m.ok)
209
- return A === "stream" ? { data: m.body, response: m } : { data: yield m[A](), response: m };
210
- let B = yield m.text();
438
+ if (h.status === 204 || T.method === "HEAD" || h.headers.get("Content-Length") === "0")
439
+ return h.ok ? { data: void 0, response: h } : { error: void 0, response: h };
440
+ if (h.ok)
441
+ return x === "stream" ? { data: h.body, response: h } : { data: yield h[x](), response: h };
442
+ let B = yield h.text();
211
443
  try {
212
444
  B = JSON.parse(B);
213
- } catch (w) {
445
+ } catch (v) {
214
446
  }
215
- return { error: B, response: m };
447
+ return { error: B, response: h };
216
448
  });
217
449
  }
218
450
  return {
219
- request(l, a, R) {
220
- return g(a, E(b({}, R), { method: l.toUpperCase() }));
451
+ request(u, l, E) {
452
+ return d(l, R(y({}, E), { method: u.toUpperCase() }));
221
453
  },
222
454
  /** Call a GET endpoint */
223
- GET(l, a) {
224
- return g(l, E(b({}, a), { method: "GET" }));
455
+ GET(u, l) {
456
+ return d(u, R(y({}, l), { method: "GET" }));
225
457
  },
226
458
  /** Call a PUT endpoint */
227
- PUT(l, a) {
228
- return g(l, E(b({}, a), { method: "PUT" }));
459
+ PUT(u, l) {
460
+ return d(u, R(y({}, l), { method: "PUT" }));
229
461
  },
230
462
  /** Call a POST endpoint */
231
- POST(l, a) {
232
- return g(l, E(b({}, a), { method: "POST" }));
463
+ POST(u, l) {
464
+ return d(u, R(y({}, l), { method: "POST" }));
233
465
  },
234
466
  /** Call a DELETE endpoint */
235
- DELETE(l, a) {
236
- return g(l, E(b({}, a), { method: "DELETE" }));
467
+ DELETE(u, l) {
468
+ return d(u, R(y({}, l), { method: "DELETE" }));
237
469
  },
238
470
  /** Call a OPTIONS endpoint */
239
- OPTIONS(l, a) {
240
- return g(l, E(b({}, a), { method: "OPTIONS" }));
471
+ OPTIONS(u, l) {
472
+ return d(u, R(y({}, l), { method: "OPTIONS" }));
241
473
  },
242
474
  /** Call a HEAD endpoint */
243
- HEAD(l, a) {
244
- return g(l, E(b({}, a), { method: "HEAD" }));
475
+ HEAD(u, l) {
476
+ return d(u, R(y({}, l), { method: "HEAD" }));
245
477
  },
246
478
  /** Call a PATCH endpoint */
247
- PATCH(l, a) {
248
- return g(l, E(b({}, a), { method: "PATCH" }));
479
+ PATCH(u, l) {
480
+ return d(u, R(y({}, l), { method: "PATCH" }));
249
481
  },
250
482
  /** Call a TRACE endpoint */
251
- TRACE(l, a) {
252
- return g(l, E(b({}, a), { method: "TRACE" }));
483
+ TRACE(u, l) {
484
+ return d(u, R(y({}, l), { method: "TRACE" }));
253
485
  },
254
486
  /** Register middleware */
255
- use(...l) {
256
- for (const a of l)
257
- if (a) {
258
- if (typeof a != "object" || !("onRequest" in a || "onResponse" in a || "onError" in a))
487
+ use(...u) {
488
+ for (const l of u)
489
+ if (l) {
490
+ if (typeof l != "object" || !("onRequest" in l || "onResponse" in l || "onError" in l))
259
491
  throw new Error("Middleware must be an object with one of `onRequest()`, `onResponse() or `onError()`");
260
- d.push(a);
492
+ m.push(l);
261
493
  }
262
494
  },
263
495
  /** Unregister middleware */
264
- eject(...l) {
265
- for (const a of l) {
266
- const R = d.indexOf(a);
267
- R !== -1 && d.splice(R, 1);
496
+ eject(...u) {
497
+ for (const l of u) {
498
+ const E = m.indexOf(l);
499
+ E !== -1 && m.splice(E, 1);
268
500
  }
269
501
  }
270
502
  };
271
503
  }
272
- function _(e, r, t) {
504
+ function W(t, r, e) {
273
505
  if (r == null)
274
506
  return "";
275
507
  if (typeof r == "object")
276
508
  throw new Error(
277
509
  "Deeply-nested arrays/objects aren’t supported. Provide your own `querySerializer()` to handle these."
278
510
  );
279
- return `${e}=${(t == null ? void 0 : t.allowReserved) === !0 ? r : encodeURIComponent(r)}`;
511
+ return `${t}=${(e == null ? void 0 : e.allowReserved) === !0 ? r : encodeURIComponent(r)}`;
280
512
  }
281
- function ce(e, r, t) {
513
+ function le(t, r, e) {
282
514
  if (!r || typeof r != "object")
283
515
  return "";
284
- const n = [], o = {
516
+ const s = [], o = {
285
517
  simple: ",",
286
518
  label: ".",
287
519
  matrix: ";"
288
- }[t.style] || "&";
289
- if (t.style !== "deepObject" && t.explode === !1) {
290
- for (const c in r)
291
- n.push(c, t.allowReserved === !0 ? r[c] : encodeURIComponent(r[c]));
292
- const i = n.join(",");
293
- switch (t.style) {
520
+ }[e.style] || "&";
521
+ if (e.style !== "deepObject" && e.explode === !1) {
522
+ for (const a in r)
523
+ s.push(a, e.allowReserved === !0 ? r[a] : encodeURIComponent(r[a]));
524
+ const i = s.join(",");
525
+ switch (e.style) {
294
526
  case "form":
295
- return `${e}=${i}`;
527
+ return `${t}=${i}`;
296
528
  case "label":
297
529
  return `.${i}`;
298
530
  case "matrix":
299
- return `;${e}=${i}`;
531
+ return `;${t}=${i}`;
300
532
  default:
301
533
  return i;
302
534
  }
303
535
  }
304
536
  for (const i in r) {
305
- const c = t.style === "deepObject" ? `${e}[${i}]` : i;
306
- n.push(_(c, r[i], t));
537
+ const a = e.style === "deepObject" ? `${t}[${i}]` : i;
538
+ s.push(W(a, r[i], e));
307
539
  }
308
- const s = n.join(o);
309
- return t.style === "label" || t.style === "matrix" ? `${o}${s}` : s;
540
+ const n = s.join(o);
541
+ return e.style === "label" || e.style === "matrix" ? `${o}${n}` : n;
310
542
  }
311
- function le(e, r, t) {
543
+ function ue(t, r, e) {
312
544
  if (!Array.isArray(r))
313
545
  return "";
314
- if (t.explode === !1) {
315
- const s = { form: ",", spaceDelimited: "%20", pipeDelimited: "|" }[t.style] || ",", i = (t.allowReserved === !0 ? r : r.map((c) => encodeURIComponent(c))).join(s);
316
- switch (t.style) {
546
+ if (e.explode === !1) {
547
+ const n = { form: ",", spaceDelimited: "%20", pipeDelimited: "|" }[e.style] || ",", i = (e.allowReserved === !0 ? r : r.map((a) => encodeURIComponent(a))).join(n);
548
+ switch (e.style) {
317
549
  case "simple":
318
550
  return i;
319
551
  case "label":
320
552
  return `.${i}`;
321
553
  case "matrix":
322
- return `;${e}=${i}`;
554
+ return `;${t}=${i}`;
323
555
  // case "spaceDelimited":
324
556
  // case "pipeDelimited":
325
557
  default:
326
- return `${e}=${i}`;
558
+ return `${t}=${i}`;
327
559
  }
328
560
  }
329
- const n = { simple: ",", label: ".", matrix: ";" }[t.style] || "&", o = [];
330
- for (const s of r)
331
- t.style === "simple" || t.style === "label" ? o.push(t.allowReserved === !0 ? s : encodeURIComponent(s)) : o.push(_(e, s, t));
332
- return t.style === "label" || t.style === "matrix" ? `${n}${o.join(n)}` : o.join(n);
561
+ const s = { simple: ",", label: ".", matrix: ";" }[e.style] || "&", o = [];
562
+ for (const n of r)
563
+ e.style === "simple" || e.style === "label" ? o.push(e.allowReserved === !0 ? n : encodeURIComponent(n)) : o.push(W(t, n, e));
564
+ return e.style === "label" || e.style === "matrix" ? `${s}${o.join(s)}` : o.join(s);
333
565
  }
334
- function ne(e) {
335
- return function(t) {
336
- const n = [];
337
- if (t && typeof t == "object")
338
- for (const o in t) {
339
- const s = t[o];
340
- if (s != null) {
341
- if (Array.isArray(s)) {
342
- if (s.length === 0)
566
+ function oe(t) {
567
+ return function(e) {
568
+ const s = [];
569
+ if (e && typeof e == "object")
570
+ for (const o in e) {
571
+ const n = e[o];
572
+ if (n != null) {
573
+ if (Array.isArray(n)) {
574
+ if (n.length === 0)
343
575
  continue;
344
- n.push(
345
- le(o, s, E(b({
576
+ s.push(
577
+ ue(o, n, R(y({
346
578
  style: "form",
347
579
  explode: !0
348
- }, e == null ? void 0 : e.array), {
349
- allowReserved: (e == null ? void 0 : e.allowReserved) || !1
580
+ }, t == null ? void 0 : t.array), {
581
+ allowReserved: (t == null ? void 0 : t.allowReserved) || !1
350
582
  }))
351
583
  );
352
584
  continue;
353
585
  }
354
- if (typeof s == "object") {
355
- n.push(
356
- ce(o, s, E(b({
586
+ if (typeof n == "object") {
587
+ s.push(
588
+ le(o, n, R(y({
357
589
  style: "deepObject",
358
590
  explode: !0
359
- }, e == null ? void 0 : e.object), {
360
- allowReserved: (e == null ? void 0 : e.allowReserved) || !1
591
+ }, t == null ? void 0 : t.object), {
592
+ allowReserved: (t == null ? void 0 : t.allowReserved) || !1
361
593
  }))
362
594
  );
363
595
  continue;
364
596
  }
365
- n.push(_(o, s, e));
597
+ s.push(W(o, n, t));
366
598
  }
367
599
  }
368
- return n.join("&");
600
+ return s.join("&");
369
601
  };
370
602
  }
371
- function Ce(e, r) {
372
- var n;
373
- let t = e;
374
- for (const o of (n = e.match(ge)) != null ? n : []) {
375
- let s = o.substring(1, o.length - 1), i = !1, c = "simple";
376
- if (s.endsWith("*") && (i = !0, s = s.substring(0, s.length - 1)), s.startsWith(".") ? (c = "label", s = s.substring(1)) : s.startsWith(";") && (c = "matrix", s = s.substring(1)), !r || r[s] === void 0 || r[s] === null)
603
+ function Te(t, r) {
604
+ var s;
605
+ let e = t;
606
+ for (const o of (s = t.match($e)) != null ? s : []) {
607
+ let n = o.substring(1, o.length - 1), i = !1, a = "simple";
608
+ if (n.endsWith("*") && (i = !0, n = n.substring(0, n.length - 1)), n.startsWith(".") ? (a = "label", n = n.substring(1)) : n.startsWith(";") && (a = "matrix", n = n.substring(1)), !r || r[n] === void 0 || r[n] === null)
377
609
  continue;
378
- const f = r[s];
610
+ const f = r[n];
379
611
  if (Array.isArray(f)) {
380
- t = t.replace(o, le(s, f, { style: c, explode: i }));
612
+ e = e.replace(o, ue(n, f, { style: a, explode: i }));
381
613
  continue;
382
614
  }
383
615
  if (typeof f == "object") {
384
- t = t.replace(o, ce(s, f, { style: c, explode: i }));
616
+ e = e.replace(o, le(n, f, { style: a, explode: i }));
385
617
  continue;
386
618
  }
387
- if (c === "matrix") {
388
- t = t.replace(o, `;${_(s, f)}`);
619
+ if (a === "matrix") {
620
+ e = e.replace(o, `;${W(n, f)}`);
389
621
  continue;
390
622
  }
391
- t = t.replace(o, c === "label" ? `.${encodeURIComponent(f)}` : encodeURIComponent(f));
623
+ e = e.replace(o, a === "label" ? `.${encodeURIComponent(f)}` : encodeURIComponent(f));
392
624
  }
393
- return t;
625
+ return e;
394
626
  }
395
- function xe(e, r) {
396
- var t, n;
397
- return e instanceof FormData ? e : r && (r.get instanceof Function ? (t = r.get("Content-Type")) != null ? t : r.get("content-type") : (n = r["Content-Type"]) != null ? n : r["content-type"]) === "application/x-www-form-urlencoded" ? new URLSearchParams(e).toString() : JSON.stringify(e);
627
+ function He(t, r) {
628
+ var e, s;
629
+ return t instanceof FormData ? t : r && (r.get instanceof Function ? (e = r.get("Content-Type")) != null ? e : r.get("content-type") : (s = r["Content-Type"]) != null ? s : r["content-type"]) === "application/x-www-form-urlencoded" ? new URLSearchParams(t).toString() : JSON.stringify(t);
398
630
  }
399
- function Se(e, r) {
400
- var o, s;
401
- let t = `${r.baseUrl}${e}`;
402
- (o = r.params) != null && o.path && (t = Ce(t, r.params.path));
403
- let n = r.querySerializer((s = r.params.query) != null ? s : {});
404
- return n.startsWith("?") && (n = n.substring(1)), n && (t += `?${n}`), t;
631
+ function Ue(t, r) {
632
+ var o, n;
633
+ let e = `${r.baseUrl}${t}`;
634
+ (o = r.params) != null && o.path && (e = Te(e, r.params.path));
635
+ let s = r.querySerializer((n = r.params.query) != null ? n : {});
636
+ return s.startsWith("?") && (s = s.substring(1)), s && (e += `?${s}`), e;
405
637
  }
406
- function se(...e) {
638
+ function ie(...t) {
407
639
  const r = new Headers();
408
- for (const t of e) {
409
- if (!t || typeof t != "object")
640
+ for (const e of t) {
641
+ if (!e || typeof e != "object")
410
642
  continue;
411
- const n = t instanceof Headers ? t.entries() : Object.entries(t);
412
- for (const [o, s] of n)
413
- if (s === null)
643
+ const s = e instanceof Headers ? e.entries() : Object.entries(e);
644
+ for (const [o, n] of s)
645
+ if (n === null)
414
646
  r.delete(o);
415
- else if (Array.isArray(s))
416
- for (const i of s)
647
+ else if (Array.isArray(n))
648
+ for (const i of n)
417
649
  r.append(o, i);
418
- else s !== void 0 && r.set(o, s);
650
+ else n !== void 0 && r.set(o, n);
419
651
  }
420
652
  return r;
421
653
  }
422
- function oe(e) {
423
- return e.endsWith("/") ? e.substring(0, e.length - 1) : e;
654
+ function ae(t) {
655
+ return t.endsWith("/") ? t.substring(0, t.length - 1) : t;
424
656
  }
425
- const ie = (e, r, t) => {
426
- try {
427
- const n = `${r}_${e}`, o = localStorage.getItem(n);
428
- if (!o) return null;
429
- const { data: s, timestamp: i } = JSON.parse(o);
430
- return t && Date.now() - i > t ? (localStorage.removeItem(n), null) : s;
431
- } catch (n) {
432
- return null;
433
- }
434
- }, V = (e, r, t) => {
435
- try {
436
- const n = `${t}_${e}`, o = {
437
- data: r,
438
- timestamp: Date.now()
439
- };
440
- localStorage.setItem(n, JSON.stringify(o));
441
- } catch (n) {
442
- console.warn("Failed to cache dismissible item:", n);
443
- }
444
- }, ae = (e, r) => {
445
- try {
446
- const t = `${r}_${e}`;
447
- localStorage.removeItem(t);
448
- } catch (t) {
449
- console.warn("Failed to remove cached dismissible item:", t);
450
- }
451
- }, ue = pe(
452
- null
453
- ), Ae = () => {
454
- const e = we(ue);
455
- if (!e)
456
- throw new Error(
457
- "useDismissibleContext must be used within a DismissibleProvider"
458
- );
459
- return e;
460
- }, $e = "dismissible", je = (e, r = {}) => {
461
- const {
462
- initialData: t,
463
- enableCache: n = !0,
464
- cachePrefix: o = $e,
465
- cacheExpiration: s
466
- } = r, i = Ae(), { userId: c } = i, f = X(() => Re({
467
- baseUrl: i.baseUrl,
657
+ const qe = (t) => {
658
+ const r = Ie({
659
+ baseUrl: t,
468
660
  headers: {}
469
- }), [i.baseUrl]), d = X(() => `${c}-${e}`, [c, e]), g = W({
470
- enableCache: n,
471
- cachePrefix: o,
472
- cacheExpiration: s
473
- }), j = W(e), l = W(d), a = W(null), [R, x] = z(!1), [I, C] = z(), [v, A] = z(() => {
474
- if (t) return t;
475
- if (n) {
476
- const u = ie(
477
- d,
478
- o,
479
- s
480
- );
481
- if (u) return u;
482
- }
483
- }), S = K(() => $(null, null, function* () {
484
- var y;
485
- if (n) {
486
- const h = ie(
487
- d,
488
- o,
489
- s
490
- );
491
- if (h != null && h.dismissedAt) {
492
- A(h), x(!1);
493
- return;
494
- }
495
- }
496
- (y = a.current) == null || y.abort();
497
- const u = new AbortController();
498
- a.current = u, x(!0), C(void 0);
499
- try {
500
- const h = yield i.getAuthHeaders(), { data: H, error: P } = yield f.GET(
661
+ });
662
+ return {
663
+ getOrCreate: (e) => C(null, null, function* () {
664
+ const { userId: s, itemId: o, authHeaders: n, signal: i } = e, { data: a, error: f } = yield r.GET(
501
665
  "/v1/users/{userId}/items/{itemId}",
502
666
  {
503
667
  params: {
504
668
  path: {
505
- userId: c,
506
- itemId: e
669
+ userId: s,
670
+ itemId: o
507
671
  }
508
672
  },
509
- headers: h,
510
- signal: u.signal
673
+ headers: n,
674
+ signal: i
511
675
  }
512
676
  );
513
- if (P || !H)
677
+ if (f || !a)
514
678
  throw new Error("Failed to fetch dismissible item");
515
- A(H.data), n && V(d, H.data, o);
516
- } catch (h) {
517
- if (h instanceof Error && h.name === "AbortError")
518
- return;
519
- C(
520
- h instanceof Error ? h : new Error("Unknown error occurred")
521
- );
522
- } finally {
523
- x(!1);
524
- }
525
- }), [
526
- e,
527
- c,
528
- d,
529
- n,
530
- o,
531
- s,
532
- f,
533
- i
534
- ]);
535
- Q(() => {
536
- const u = j.current !== e, y = l.current !== d;
537
- u || y ? (j.current = e, l.current = d, S()) : t || S();
538
- }, [e, d, t, S]), Q(() => () => {
539
- var u;
540
- (u = a.current) == null || u.abort();
541
- }, []), Q(() => {
542
- const u = g.current;
543
- (u.enableCache !== n || u.cachePrefix !== o || u.cacheExpiration !== s) && (u.cachePrefix !== o && ae(d, u.cachePrefix), !n && u.enableCache && ae(d, u.cachePrefix), g.current = {
544
- enableCache: n,
545
- cachePrefix: o,
546
- cacheExpiration: s
547
- }, S());
548
- }, [n, o, s, d, S]);
549
- const k = K(() => $(null, null, function* () {
550
- C(void 0);
551
- try {
552
- const u = yield i.getAuthHeaders(), { data: y, error: h } = yield f.DELETE(
679
+ return a.data;
680
+ }),
681
+ dismiss: (e) => C(null, null, function* () {
682
+ const { userId: s, itemId: o, authHeaders: n } = e, { data: i, error: a } = yield r.DELETE(
553
683
  "/v1/users/{userId}/items/{itemId}",
554
684
  {
555
685
  params: {
556
686
  path: {
557
- userId: c,
558
- itemId: e
687
+ userId: s,
688
+ itemId: o
559
689
  }
560
690
  },
561
- headers: u
691
+ headers: n
562
692
  }
563
693
  );
564
- if (h || !y)
694
+ if (a || !i)
565
695
  throw new Error("Failed to dismiss item");
566
- A(y.data), n && V(d, y.data, o);
567
- } catch (u) {
568
- throw C(
569
- u instanceof Error ? u : new Error("Failed to dismiss item")
570
- ), u;
571
- }
572
- }), [
573
- e,
574
- c,
575
- d,
576
- n,
577
- o,
578
- f,
579
- i
580
- ]), F = K(() => $(null, null, function* () {
581
- C(void 0);
582
- try {
583
- const u = yield i.getAuthHeaders(), { data: y, error: h } = yield f.POST(
696
+ return i.data;
697
+ }),
698
+ restore: (e) => C(null, null, function* () {
699
+ const { userId: s, itemId: o, authHeaders: n } = e, { data: i, error: a } = yield r.POST(
584
700
  "/v1/users/{userId}/items/{itemId}",
585
701
  {
586
702
  params: {
587
703
  path: {
588
- userId: c,
589
- itemId: e
704
+ userId: s,
705
+ itemId: o
590
706
  }
591
707
  },
592
- headers: u
708
+ headers: n
593
709
  }
594
710
  );
595
- if (h || !y)
711
+ if (a || !i)
596
712
  throw new Error("Failed to restore item");
597
- A(y.data), n && V(d, y.data, o);
598
- } catch (u) {
599
- throw C(
600
- u instanceof Error ? u : new Error("Failed to restore item")
601
- ), u;
602
- }
603
- }), [
604
- e,
605
- c,
606
- d,
607
- n,
608
- o,
609
- f,
610
- i
611
- ]);
612
- return {
613
- dismissedAt: v == null ? void 0 : v.dismissedAt,
614
- dismiss: k,
615
- restore: F,
616
- isLoading: R,
617
- error: I,
618
- item: v
713
+ return i.data;
714
+ })
619
715
  };
620
- }, De = () => /* @__PURE__ */ U("div", { className: "dismissible-loading", "aria-live": "polite", children: "Loading..." }), Te = () => /* @__PURE__ */ U("div", { className: "dismissible-error", role: "alert", children: "Unable to load content. Please try again later." }), Ue = ({ onDismiss: e, ariaLabel: r }) => /* @__PURE__ */ U(
621
- "button",
622
- {
623
- className: "dismissible-button",
624
- onClick: e,
625
- "aria-label": r,
626
- type: "button",
627
- children: "×"
628
- }
629
- ), ke = ({
630
- itemId: e,
631
- children: r,
632
- onDismiss: t,
633
- LoadingComponent: n = De,
634
- ErrorComponent: o = Te,
635
- DismissButtonComponent: s = Ue,
636
- enableCache: i,
637
- cachePrefix: c,
638
- cacheExpiration: f,
639
- ignoreErrors: d = !1
640
- }) => {
641
- const { dismissedAt: g, isLoading: j, error: l, dismiss: a } = je(
642
- e,
643
- {
644
- enableCache: i,
645
- cachePrefix: c,
646
- cacheExpiration: f
647
- }
648
- ), [R, x] = z(!1), [I, C] = z(e);
649
- e !== I && (C(e), x(!1));
650
- const v = () => $(null, null, function* () {
651
- x(!0);
652
- try {
653
- yield a(), t == null || t();
654
- } catch (A) {
655
- x(!1);
656
- }
657
- });
658
- return j && n ? /* @__PURE__ */ U(n, { itemId: e }) : j && !n ? null : l && o && !d ? /* @__PURE__ */ U(o, { itemId: e, error: l }) : g || R ? null : /* @__PURE__ */ be("div", { className: "dismissible-container", children: [
659
- /* @__PURE__ */ U("div", { className: "dismissible-content", children: r }),
660
- s ? /* @__PURE__ */ U(
661
- s,
662
- {
663
- onDismiss: v,
664
- ariaLabel: `Dismiss ${e}`
665
- }
666
- ) : null
667
- ] });
668
- }, He = (e) => $(null, null, function* () {
669
- if (typeof e == "function")
670
- try {
671
- const r = e();
672
- return yield Promise.resolve(r);
673
- } catch (r) {
674
- console.warn("Failed to resolve JWT from function:", r);
675
- return;
676
- }
677
- return e;
678
- }), qe = (e) => $(null, null, function* () {
679
- const r = yield He(e);
680
- return r ? { Authorization: `Bearer ${r}` } : {};
681
- }), Ie = (e) => {
716
+ }, Oe = (t) => {
682
717
  try {
683
- const r = new URL(e), t = r.hostname === "localhost" || r.hostname === "127.0.0.1" || r.hostname === "[::1]", n = r.protocol === "https:";
684
- return { isSecure: n || t, isLocalhost: t, isHttps: n };
718
+ const r = new URL(t), e = r.hostname === "localhost" || r.hostname === "127.0.0.1" || r.hostname === "[::1]", s = r.protocol === "https:";
719
+ return { isSecure: s || e, isLocalhost: e, isHttps: s };
685
720
  } catch (r) {
686
721
  return { isSecure: !1, isLocalhost: !1, isHttps: !1 };
687
722
  }
688
- }, Fe = ({
689
- userId: e,
723
+ }, Le = ({
724
+ userId: t,
690
725
  jwt: r,
691
- baseUrl: t,
692
- children: n
726
+ baseUrl: e,
727
+ client: s,
728
+ children: o
693
729
  }) => {
694
- const { isSecure: o } = Ie(t);
695
- o || console.warn(
696
- `[dismissible] Insecure baseUrl "${t}". Use https:// in production (or localhost for development). JWT tokens may be exposed over insecure connections.`
730
+ const { isSecure: n } = Oe(e);
731
+ n || console.warn(
732
+ `[dismissible] Insecure baseUrl "${e}". Use https:// in production (or localhost for development). JWT tokens may be exposed over insecure connections.`
697
733
  );
698
- const s = X(
734
+ const i = re(
735
+ () => s != null ? s : qe(e),
736
+ [s, e]
737
+ ), a = re(
699
738
  () => ({
700
- userId: e,
739
+ userId: t,
701
740
  jwt: r,
702
- baseUrl: t,
703
- getAuthHeaders: () => $(null, null, function* () {
704
- return yield qe(r);
705
- })
741
+ baseUrl: e,
742
+ getAuthHeaders: () => C(null, null, function* () {
743
+ return yield Ae(r);
744
+ }),
745
+ client: i
706
746
  }),
707
- [e, r, t]
747
+ [t, r, e, i]
708
748
  );
709
- return /* @__PURE__ */ U(ue.Provider, { value: s, children: n });
749
+ return /* @__PURE__ */ H(ce.Provider, { value: a, children: o });
710
750
  };
711
751
  export {
712
- ke as Dismissible,
713
- ue as DismissibleContext,
714
- Fe as DismissibleProvider,
715
- qe as getAuthHeaders,
716
- He as resolveJwt,
717
- Ae as useDismissibleContext,
718
- je as useDismissibleItem
752
+ Fe as Dismissible,
753
+ ce as DismissibleContext,
754
+ Le as DismissibleProvider,
755
+ qe as createDefaultClient,
756
+ Ae as getAuthHeaders,
757
+ Se as resolveJwt,
758
+ ge as useDismissibleContext,
759
+ Ee as useDismissibleItem
719
760
  };