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