@chainrails/react 0.0.11 → 0.0.13

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,19 +1,19 @@
1
- import { useState as v, useEffect as F } from "react";
2
- import { jsxs as k, jsx as g } from "react/jsx-runtime";
3
- import './index.css';function O(t) {
1
+ import { useState as M, useRef as W, useEffect as G } from "react";
2
+ import { jsxs as O, jsx as w } from "react/jsx-runtime";
3
+ import './index.css';function B(t) {
4
4
  var e, s, r = "";
5
5
  if (typeof t == "string" || typeof t == "number") r += t;
6
6
  else if (typeof t == "object") if (Array.isArray(t)) {
7
7
  var n = t.length;
8
- for (e = 0; e < n; e++) t[e] && (s = O(t[e])) && (r && (r += " "), r += s);
8
+ for (e = 0; e < n; e++) t[e] && (s = B(t[e])) && (r && (r += " "), r += s);
9
9
  } else for (s in t) t[s] && (r && (r += " "), r += s);
10
10
  return r;
11
11
  }
12
- function z() {
13
- for (var t, e, s = 0, r = "", n = arguments.length; s < n; s++) (t = arguments[s]) && (e = O(t)) && (r && (r += " "), r += e);
12
+ function X() {
13
+ for (var t, e, s = 0, r = "", n = arguments.length; s < n; s++) (t = arguments[s]) && (e = B(t)) && (r && (r += " "), r += e);
14
14
  return r;
15
15
  }
16
- class S extends Error {
16
+ class C extends Error {
17
17
  response;
18
18
  request;
19
19
  options;
@@ -22,7 +22,7 @@ class S extends Error {
22
22
  super(`Request failed with ${u}: ${s.method} ${s.url}`), this.name = "HTTPError", this.response = e, this.request = s, this.options = r;
23
23
  }
24
24
  }
25
- class L extends Error {
25
+ class D extends Error {
26
26
  name = "NonError";
27
27
  value;
28
28
  constructor(e) {
@@ -34,17 +34,17 @@ class L extends Error {
34
34
  super(s), this.value = e;
35
35
  }
36
36
  }
37
- class E extends Error {
37
+ class S extends Error {
38
38
  name = "ForceRetryError";
39
39
  customDelay;
40
40
  code;
41
41
  customRequest;
42
42
  constructor(e) {
43
- const s = e?.cause ? e.cause instanceof Error ? e.cause : new L(e.cause) : void 0;
43
+ const s = e?.cause ? e.cause instanceof Error ? e.cause : new D(e.cause) : void 0;
44
44
  super(e?.code ? `Forced retry: ${e.code}` : "Forced retry", s ? { cause: s } : void 0), this.customDelay = e?.delay, this.code = e?.code, this.customRequest = e?.request;
45
45
  }
46
46
  }
47
- const N = (() => {
47
+ const I = (() => {
48
48
  let t = !1, e = !1;
49
49
  const s = typeof globalThis.ReadableStream == "function", r = typeof globalThis.Request == "function";
50
50
  if (s && r)
@@ -63,7 +63,7 @@ const N = (() => {
63
63
  throw n;
64
64
  }
65
65
  return t && !e;
66
- })(), J = typeof globalThis.AbortController == "function", M = typeof globalThis.AbortSignal == "function" && typeof globalThis.AbortSignal.any == "function", V = typeof globalThis.ReadableStream == "function", K = typeof globalThis.FormData == "function", I = ["get", "post", "put", "patch", "head", "delete"], W = {
66
+ })(), Z = typeof globalThis.AbortController == "function", H = typeof globalThis.AbortSignal == "function" && typeof globalThis.AbortSignal.any == "function", Y = typeof globalThis.ReadableStream == "function", Q = typeof globalThis.FormData == "function", _ = ["get", "post", "put", "patch", "head", "delete"], ee = {
67
67
  json: "application/json",
68
68
  text: "text/*",
69
69
  formData: "multipart/form-data",
@@ -72,14 +72,14 @@ const N = (() => {
72
72
  // Supported in modern Fetch implementations (for example, browsers and recent Node.js/undici).
73
73
  // We still feature-check at runtime before exposing the shortcut.
74
74
  bytes: "*/*"
75
- }, A = 2147483647, X = new TextEncoder().encode("------WebKitFormBoundaryaxpyiPgbbPti10Rw").length, B = Symbol("stop");
76
- class _ {
75
+ }, A = 2147483647, te = new TextEncoder().encode("------WebKitFormBoundaryaxpyiPgbbPti10Rw").length, $ = Symbol("stop");
76
+ class F {
77
77
  options;
78
78
  constructor(e) {
79
79
  this.options = e;
80
80
  }
81
81
  }
82
- const Z = (t) => new _(t), Y = {
82
+ const se = (t) => new F(t), re = {
83
83
  json: !0,
84
84
  parseJson: !0,
85
85
  stringifyJson: !0,
@@ -93,10 +93,10 @@ const Z = (t) => new _(t), Y = {
93
93
  onUploadProgress: !0,
94
94
  fetch: !0,
95
95
  context: !0
96
- }, G = {
96
+ }, ne = {
97
97
  next: !0
98
98
  // Next.js cache revalidation (revalidate, tags)
99
- }, Q = {
99
+ }, oe = {
100
100
  method: !0,
101
101
  headers: !0,
102
102
  body: !0,
@@ -111,13 +111,13 @@ const Z = (t) => new _(t), Y = {
111
111
  signal: !0,
112
112
  window: !0,
113
113
  duplex: !0
114
- }, ee = (t) => {
114
+ }, ie = (t) => {
115
115
  if (!t)
116
116
  return 0;
117
117
  if (t instanceof FormData) {
118
118
  let e = 0;
119
119
  for (const [s, r] of t)
120
- e += X, e += new TextEncoder().encode(`Content-Disposition: form-data; name="${s}"`).length, e += typeof r == "string" ? new TextEncoder().encode(r).length : r.size;
120
+ e += te, e += new TextEncoder().encode(`Content-Disposition: form-data; name="${s}"`).length, e += typeof r == "string" ? new TextEncoder().encode(r).length : r.size;
121
121
  return e;
122
122
  }
123
123
  if (t instanceof Blob)
@@ -138,7 +138,7 @@ const Z = (t) => new _(t), Y = {
138
138
  return 0;
139
139
  }
140
140
  return 0;
141
- }, H = (t, e, s) => {
141
+ }, z = (t, e, s) => {
142
142
  let r, n = 0;
143
143
  return t.pipeThrough(new TransformStream({
144
144
  transform(o, i) {
@@ -153,7 +153,7 @@ const Z = (t) => new _(t), Y = {
153
153
  r && (n += r.byteLength, s?.({ percent: 1, totalBytes: Math.max(e, n), transferredBytes: n }, r));
154
154
  }
155
155
  }));
156
- }, te = (t, e) => {
156
+ }, ae = (t, e) => {
157
157
  if (!t.body)
158
158
  return t;
159
159
  if (t.status === 204)
@@ -163,40 +163,40 @@ const Z = (t) => new _(t), Y = {
163
163
  headers: t.headers
164
164
  });
165
165
  const s = Math.max(0, Number(t.headers.get("content-length")) || 0);
166
- return new Response(H(t.body, s, e), {
166
+ return new Response(z(t.body, s, e), {
167
167
  status: t.status,
168
168
  statusText: t.statusText,
169
169
  headers: t.headers
170
170
  });
171
- }, se = (t, e, s) => {
171
+ }, ue = (t, e, s) => {
172
172
  if (!t.body)
173
173
  return t;
174
- const r = ee(s ?? t.body);
174
+ const r = ie(s ?? t.body);
175
175
  return new Request(t, {
176
176
  // @ts-expect-error - Types are outdated.
177
177
  duplex: "half",
178
- body: H(t.body, r, e)
178
+ body: z(t.body, r, e)
179
179
  });
180
- }, f = (t) => t !== null && typeof t == "object", w = (...t) => {
180
+ }, f = (t) => t !== null && typeof t == "object", b = (...t) => {
181
181
  for (const e of t)
182
182
  if ((!f(e) || Array.isArray(e)) && e !== void 0)
183
183
  throw new TypeError("The `options` argument must be an object");
184
- return q({}, ...t);
185
- }, D = (t = {}, e = {}) => {
184
+ return j({}, ...t);
185
+ }, J = (t = {}, e = {}) => {
186
186
  const s = new globalThis.Headers(t), r = e instanceof globalThis.Headers, n = new globalThis.Headers(e);
187
187
  for (const [o, i] of n.entries())
188
188
  r && i === "undefined" || i === void 0 ? s.delete(o) : s.set(o, i);
189
189
  return s;
190
190
  };
191
- function b(t, e, s) {
192
- return Object.hasOwn(e, s) && e[s] === void 0 ? [] : q(t[s] ?? [], e[s] ?? []);
191
+ function T(t, e, s) {
192
+ return Object.hasOwn(e, s) && e[s] === void 0 ? [] : j(t[s] ?? [], e[s] ?? []);
193
193
  }
194
- const $ = (t = {}, e = {}) => ({
195
- beforeRequest: b(t, e, "beforeRequest"),
196
- beforeRetry: b(t, e, "beforeRetry"),
197
- afterResponse: b(t, e, "afterResponse"),
198
- beforeError: b(t, e, "beforeError")
199
- }), re = (t, e) => {
194
+ const K = (t = {}, e = {}) => ({
195
+ beforeRequest: T(t, e, "beforeRequest"),
196
+ beforeRetry: T(t, e, "beforeRetry"),
197
+ afterResponse: T(t, e, "afterResponse"),
198
+ beforeError: T(t, e, "beforeError")
199
+ }), ce = (t, e) => {
200
200
  const s = new URLSearchParams();
201
201
  for (const r of [t, e])
202
202
  if (r !== void 0)
@@ -218,7 +218,7 @@ const $ = (t = {}, e = {}) => ({
218
218
  s.append(o, i);
219
219
  }
220
220
  return s;
221
- }, q = (...t) => {
221
+ }, j = (...t) => {
222
222
  let e = {}, s = {}, r = {}, n;
223
223
  const o = [];
224
224
  for (const i of t)
@@ -240,28 +240,28 @@ const $ = (t = {}, e = {}) => ({
240
240
  continue;
241
241
  }
242
242
  if (u === "searchParams") {
243
- a == null ? n = void 0 : n = n === void 0 ? a : re(n, a);
243
+ a == null ? n = void 0 : n = n === void 0 ? a : ce(n, a);
244
244
  continue;
245
245
  }
246
- f(a) && u in e && (a = q(e[u], a)), e = { ...e, [u]: a };
246
+ f(a) && u in e && (a = j(e[u], a)), e = { ...e, [u]: a };
247
247
  }
248
- f(i.hooks) && (r = $(r, i.hooks), e.hooks = r), f(i.headers) && (s = D(s, i.headers), e.headers = s);
248
+ f(i.hooks) && (r = K(r, i.hooks), e.hooks = r), f(i.headers) && (s = J(s, i.headers), e.headers = s);
249
249
  }
250
- return n !== void 0 && (e.searchParams = n), o.length > 0 && (o.length === 1 ? e.signal = o[0] : M ? e.signal = AbortSignal.any(o) : e.signal = o.at(-1)), e.context === void 0 && (e.context = {}), e;
251
- }, ne = (t) => I.includes(t) ? t.toUpperCase() : t, oe = ["get", "put", "head", "delete", "options", "trace"], ie = [408, 413, 429, 500, 502, 503, 504], ae = [413, 429, 503], U = {
250
+ return n !== void 0 && (e.searchParams = n), o.length > 0 && (o.length === 1 ? e.signal = o[0] : H ? e.signal = AbortSignal.any(o) : e.signal = o.at(-1)), e.context === void 0 && (e.context = {}), e;
251
+ }, he = (t) => _.includes(t) ? t.toUpperCase() : t, le = ["get", "put", "head", "delete", "options", "trace"], fe = [408, 413, 429, 500, 502, 503, 504], de = [413, 429, 503], L = {
252
252
  limit: 2,
253
- methods: oe,
254
- statusCodes: ie,
255
- afterStatusCodes: ae,
253
+ methods: le,
254
+ statusCodes: fe,
255
+ afterStatusCodes: de,
256
256
  maxRetryAfter: Number.POSITIVE_INFINITY,
257
257
  backoffLimit: Number.POSITIVE_INFINITY,
258
258
  delay: (t) => 0.3 * 2 ** (t - 1) * 1e3,
259
259
  jitter: void 0,
260
260
  retryOnTimeout: !1
261
- }, ue = (t = {}) => {
261
+ }, pe = (t = {}) => {
262
262
  if (typeof t == "number")
263
263
  return {
264
- ...U,
264
+ ...L,
265
265
  limit: t
266
266
  };
267
267
  if (t.methods && !Array.isArray(t.methods))
@@ -269,27 +269,27 @@ const $ = (t = {}, e = {}) => ({
269
269
  if (t.statusCodes && !Array.isArray(t.statusCodes))
270
270
  throw new Error("retry.statusCodes must be an array");
271
271
  return {
272
- ...U,
272
+ ...L,
273
273
  ...t
274
274
  };
275
275
  };
276
- class C extends Error {
276
+ class P extends Error {
277
277
  request;
278
278
  constructor(e) {
279
279
  super(`Request timed out: ${e.method} ${e.url}`), this.name = "TimeoutError", this.request = e;
280
280
  }
281
281
  }
282
- async function ce(t, e, s, r) {
282
+ async function ye(t, e, s, r) {
283
283
  return new Promise((n, o) => {
284
284
  const i = setTimeout(() => {
285
- s && s.abort(), o(new C(t));
285
+ s && s.abort(), o(new P(t));
286
286
  }, r.timeout);
287
287
  r.fetch(t, e).then(n).catch(o).then(() => {
288
288
  clearTimeout(i);
289
289
  });
290
290
  });
291
291
  }
292
- async function he(t, { signal: e }) {
292
+ async function ge(t, { signal: e }) {
293
293
  return new Promise((s, r) => {
294
294
  e && (e.throwIfAborted(), e.addEventListener("abort", n, { once: !0 }));
295
295
  function n() {
@@ -300,18 +300,18 @@ async function he(t, { signal: e }) {
300
300
  }, t);
301
301
  });
302
302
  }
303
- const le = (t, e) => {
303
+ const me = (t, e) => {
304
304
  const s = {};
305
305
  for (const r in e)
306
- Object.hasOwn(e, r) && !(r in Q) && !(r in Y) && (!(r in t) || r in G) && (s[r] = e[r]);
306
+ Object.hasOwn(e, r) && !(r in oe) && !(r in re) && (!(r in t) || r in ne) && (s[r] = e[r]);
307
307
  return s;
308
- }, fe = (t) => t === void 0 ? !1 : Array.isArray(t) ? t.length > 0 : t instanceof URLSearchParams ? t.size > 0 : typeof t == "object" ? Object.keys(t).length > 0 : typeof t == "string" ? t.trim().length > 0 : !!t;
309
- function de(t) {
310
- return t instanceof S || t?.name === S.name;
311
- }
312
- function pe(t) {
308
+ }, we = (t) => t === void 0 ? !1 : Array.isArray(t) ? t.length > 0 : t instanceof URLSearchParams ? t.size > 0 : typeof t == "object" ? Object.keys(t).length > 0 : typeof t == "string" ? t.trim().length > 0 : !!t;
309
+ function be(t) {
313
310
  return t instanceof C || t?.name === C.name;
314
311
  }
312
+ function Te(t) {
313
+ return t instanceof P || t?.name === P.name;
314
+ }
315
315
  class p {
316
316
  static create(e, s) {
317
317
  const r = new p(e, s), n = async () => {
@@ -320,15 +320,15 @@ class p {
320
320
  await Promise.resolve();
321
321
  let i = await r.#y();
322
322
  for (const u of r.#e.hooks.afterResponse) {
323
- const a = r.#h(i.clone()), h = await u(r.request, r.#a(), a, { retryCount: r.#s });
324
- if (h instanceof globalThis.Response && (i = h), h instanceof _)
323
+ const a = r.#h(i.clone()), l = await u(r.request, r.#a(), a, { retryCount: r.#s });
324
+ if (l instanceof globalThis.Response && (i = l), l instanceof F)
325
325
  throw await Promise.all([
326
326
  a.body?.cancel(),
327
327
  i.body?.cancel()
328
- ]), new E(h.options);
328
+ ]), new S(l.options);
329
329
  }
330
330
  if (r.#h(i), !i.ok && (typeof r.#e.throwHttpErrors == "function" ? r.#e.throwHttpErrors(i.status) : r.#e.throwHttpErrors)) {
331
- let u = new S(i, r.request, r.#a());
331
+ let u = new C(i, r.request, r.#a());
332
332
  for (const a of r.#e.hooks.beforeError)
333
333
  u = await a(u, { retryCount: r.#s });
334
334
  throw u;
@@ -336,24 +336,24 @@ class p {
336
336
  if (r.#e.onDownloadProgress) {
337
337
  if (typeof r.#e.onDownloadProgress != "function")
338
338
  throw new TypeError("The `onDownloadProgress` option must be a function");
339
- if (!V)
339
+ if (!Y)
340
340
  throw new Error("Streams are not supported in your environment. `ReadableStream` is missing.");
341
- return te(i.clone(), r.#e.onDownloadProgress);
341
+ return ae(i.clone(), r.#e.onDownloadProgress);
342
342
  }
343
343
  return i;
344
344
  }, o = r.#l(n).finally(async () => {
345
345
  const i = r.#o, u = [];
346
346
  i && !i.bodyUsed && u.push(i.body?.cancel()), r.request.bodyUsed || u.push(r.request.body?.cancel()), await Promise.all(u);
347
347
  });
348
- for (const [i, u] of Object.entries(W))
348
+ for (const [i, u] of Object.entries(ee))
349
349
  i === "bytes" && typeof globalThis.Response?.prototype?.bytes != "function" || (o[i] = async () => {
350
350
  r.request.headers.set("accept", r.request.headers.get("accept") || u);
351
351
  const a = await o;
352
352
  if (i === "json") {
353
353
  if (a.status === 204)
354
354
  return "";
355
- const h = await a.text();
356
- return h === "" ? "" : s.parseJson ? s.parseJson(h) : JSON.parse(h);
355
+ const l = await a.text();
356
+ return l === "" ? "" : s.parseJson ? s.parseJson(l) : JSON.parse(l);
357
357
  }
358
358
  return a[i]();
359
359
  });
@@ -376,17 +376,17 @@ class p {
376
376
  constructor(e, s = {}) {
377
377
  if (this.#t = e, this.#e = {
378
378
  ...s,
379
- headers: D(this.#t.headers, s.headers),
380
- hooks: $({
379
+ headers: J(this.#t.headers, s.headers),
380
+ hooks: K({
381
381
  beforeRequest: [],
382
382
  beforeRetry: [],
383
383
  beforeError: [],
384
384
  afterResponse: []
385
385
  }, s.hooks),
386
- method: ne(s.method ?? this.#t.method ?? "GET"),
386
+ method: he(s.method ?? this.#t.method ?? "GET"),
387
387
  // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
388
388
  prefixUrl: String(s.prefixUrl || ""),
389
- retry: ue(s.retry),
389
+ retry: pe(s.retry),
390
390
  throwHttpErrors: s.throwHttpErrors ?? !0,
391
391
  timeout: s.timeout ?? 1e4,
392
392
  fetch: s.fetch ?? globalThis.fetch.bind(globalThis),
@@ -398,16 +398,16 @@ class p {
398
398
  throw new Error("`input` must not begin with a slash when using `prefixUrl`");
399
399
  this.#e.prefixUrl.endsWith("/") || (this.#e.prefixUrl += "/"), this.#t = this.#e.prefixUrl + this.#t;
400
400
  }
401
- J && M && (this.#n = this.#e.signal ?? this.#t.signal, this.#r = new globalThis.AbortController(), this.#e.signal = this.#n ? AbortSignal.any([this.#n, this.#r.signal]) : this.#r.signal), N && (this.#e.duplex = "half"), this.#e.json !== void 0 && (this.#e.body = this.#e.stringifyJson?.(this.#e.json) ?? JSON.stringify(this.#e.json), this.#e.headers.set("content-type", this.#e.headers.get("content-type") ?? "application/json"));
401
+ Z && H && (this.#n = this.#e.signal ?? this.#t.signal, this.#r = new globalThis.AbortController(), this.#e.signal = this.#n ? AbortSignal.any([this.#n, this.#r.signal]) : this.#r.signal), I && (this.#e.duplex = "half"), this.#e.json !== void 0 && (this.#e.body = this.#e.stringifyJson?.(this.#e.json) ?? JSON.stringify(this.#e.json), this.#e.headers.set("content-type", this.#e.headers.get("content-type") ?? "application/json"));
402
402
  const r = s.headers && new globalThis.Headers(s.headers).has("content-type");
403
- if (this.#t instanceof globalThis.Request && (K && this.#e.body instanceof globalThis.FormData || this.#e.body instanceof URLSearchParams) && !r && this.#e.headers.delete("content-type"), this.request = new globalThis.Request(this.#t, this.#e), fe(this.#e.searchParams)) {
403
+ if (this.#t instanceof globalThis.Request && (Q && this.#e.body instanceof globalThis.FormData || this.#e.body instanceof URLSearchParams) && !r && this.#e.headers.delete("content-type"), this.request = new globalThis.Request(this.#t, this.#e), we(this.#e.searchParams)) {
404
404
  const o = "?" + (typeof this.#e.searchParams == "string" ? this.#e.searchParams.replace(/^\?/, "") : new URLSearchParams(p.#d(this.#e.searchParams)).toString()), i = this.request.url.replace(/(?:\?.*?)?(?=#|$)/, o);
405
405
  this.request = new globalThis.Request(i, this.#e);
406
406
  }
407
407
  if (this.#e.onUploadProgress) {
408
408
  if (typeof this.#e.onUploadProgress != "function")
409
409
  throw new TypeError("The `onUploadProgress` option must be a function");
410
- if (!N)
410
+ if (!I)
411
411
  throw new Error("Request streams are not supported in your environment. The `duplex` option for `Request` is not available.");
412
412
  this.request = this.#f(this.request, this.#e.body ?? void 0);
413
413
  }
@@ -420,8 +420,8 @@ class p {
420
420
  async #p(e) {
421
421
  if (this.#s++, this.#s > this.#e.retry.limit)
422
422
  throw e;
423
- const s = e instanceof Error ? e : new L(e);
424
- if (s instanceof E)
423
+ const s = e instanceof Error ? e : new D(e);
424
+ if (s instanceof S)
425
425
  return s.customDelay ?? this.#u();
426
426
  if (!this.#e.retry.methods.includes(this.request.method.toLowerCase()))
427
427
  throw e;
@@ -432,9 +432,9 @@ class p {
432
432
  if (r === !0)
433
433
  return this.#u();
434
434
  }
435
- if (pe(e) && !this.#e.retry.retryOnTimeout)
435
+ if (Te(e) && !this.#e.retry.retryOnTimeout)
436
436
  throw e;
437
- if (de(e)) {
437
+ if (be(e)) {
438
438
  if (!this.#e.retry.statusCodes.includes(e.response.status))
439
439
  throw e;
440
440
  const r = e.response.headers.get("Retry-After") ?? e.response.headers.get("RateLimit-Reset") ?? e.response.headers.get("X-RateLimit-Retry-After") ?? e.response.headers.get("X-RateLimit-Reset") ?? e.response.headers.get("X-Rate-Limit-Reset");
@@ -459,7 +459,7 @@ class p {
459
459
  const r = Math.min(await this.#p(s), A);
460
460
  if (this.#s < 1)
461
461
  throw s;
462
- if (await he(r, this.#n ? { signal: this.#n } : {}), s instanceof E && s.customRequest) {
462
+ if (await ge(r, this.#n ? { signal: this.#n } : {}), s instanceof S && s.customRequest) {
463
463
  const n = this.#e.signal ? new globalThis.Request(s.customRequest, { signal: this.#e.signal }) : new globalThis.Request(s.customRequest);
464
464
  this.#c(n);
465
465
  }
@@ -476,7 +476,7 @@ class p {
476
476
  }
477
477
  if (o instanceof globalThis.Response)
478
478
  return o;
479
- if (o === B)
479
+ if (o === $)
480
480
  return;
481
481
  }
482
482
  return this.#l(e);
@@ -493,8 +493,8 @@ class p {
493
493
  break;
494
494
  }
495
495
  }
496
- const e = le(this.request, this.#e);
497
- return this.#o = this.request, this.request = this.#o.clone(), this.#e.timeout === !1 ? this.#e.fetch(this.#o, e) : ce(this.#o, e, this.#r, this.#e);
496
+ const e = me(this.request, this.#e);
497
+ return this.#o = this.request, this.request = this.#o.clone(), this.#e.timeout === !1 ? this.#e.fetch(this.#o, e) : ye(this.#o, e, this.#r, this.#e);
498
498
  }
499
499
  #a() {
500
500
  if (!this.#i) {
@@ -507,118 +507,159 @@ class p {
507
507
  this.#i = void 0, this.request = this.#f(e);
508
508
  }
509
509
  #f(e, s) {
510
- return !this.#e.onUploadProgress || !e.body ? e : se(e, this.#e.onUploadProgress, s ?? this.#e.body ?? void 0);
510
+ return !this.#e.onUploadProgress || !e.body ? e : ue(e, this.#e.onUploadProgress, s ?? this.#e.body ?? void 0);
511
511
  }
512
512
  }
513
513
  /*! MIT License © Sindre Sorhus */
514
514
  const x = (t) => {
515
- const e = (s, r) => p.create(s, w(t, r));
516
- for (const s of I)
517
- e[s] = (r, n) => p.create(r, w(t, n, { method: s }));
518
- return e.create = (s) => x(w(s)), e.extend = (s) => (typeof s == "function" && (s = s(t ?? {})), x(w(t, s))), e.stop = B, e.retry = Z, e;
519
- }, ye = x();
520
- var me = /* @__PURE__ */ ((t) => (t.ARBITRUM = "ARBITRUM_MAINNET", t.ARBITRUM_TESTNET = "ARBITRUM_TESTNET", t.BASE = "BASE_MAINNET", t.BASE_TESTNET = "BASE_TESTNET", t.AVALANCHE = "AVALANCHE_MAINNET", t.AVALANCHE_TESTNET = "AVALANCHE_TESTNET", t.BSC = "BSC_MAINNET", t.ETHEREUM = "ETHEREUM_MAINNET", t.ETHEREUM_TESTNET = "ETHEREUM_TESTNET", t.STARKNET = "STARKNET_MAINNET", t))(me || {}), ge = /* @__PURE__ */ ((t) => (t.USDC = "USDC", t))(ge || {});
521
- class l {
522
- constructor({ api_key: e }) {
523
- this.api_key = e;
515
+ const e = (s, r) => p.create(s, b(t, r));
516
+ for (const s of _)
517
+ e[s] = (r, n) => p.create(r, b(t, n, { method: s }));
518
+ return e.create = (s) => x(b(s)), e.extend = (s) => (typeof s == "function" && (s = s(t ?? {})), x(b(t, s))), e.stop = $, e.retry = se, e;
519
+ }, Re = x();
520
+ var Ee = /* @__PURE__ */ ((t) => (t.ARBITRUM = "ARBITRUM_MAINNET", t.ARBITRUM_TESTNET = "ARBITRUM_TESTNET", t.AVALANCHE = "AVALANCHE_MAINNET", t.AVALANCHE_TESTNET = "AVALANCHE_TESTNET", t.BASE = "BASE_MAINNET", t.BASE_TESTNET = "BASE_TESTNET", t.BSC = "BSC_MAINNET", t.ETHEREUM = "ETHEREUM_MAINNET", t.ETHEREUM_TESTNET = "ETHEREUM_TESTNET", t.STARKNET = "STARKNET_MAINNET", t))(Ee || {}), Se = /* @__PURE__ */ ((t) => (t.USDC = "USDC", t))(Se || {}), N = /* @__PURE__ */ ((t) => (t.PRODUCTION = "production", t.STAGING = "staging", t))(N || {});
521
+ class c {
522
+ constructor({ api_key: e, env: s }) {
523
+ this.api_key = e, this.jwt = "", this.env = s ?? N.PRODUCTION;
524
524
  }
525
525
  static config(e) {
526
- if (!l.app) {
526
+ if (!c.app) {
527
527
  if (!e)
528
528
  throw new Error("Please provide an api_key");
529
- l.app = new l(e);
529
+ c.app = new c(e);
530
+ }
531
+ return e && (c.app.api_key = e.api_key, e.env !== void 0 && (c.app.env = e.env), this.updateJWT(), setInterval(this.updateJWT.bind(this), 1500 * 1e3)), c.app;
532
+ }
533
+ static async updateJWT() {
534
+ try {
535
+ const e = await (await fetch(this.getBaseUrl() + "/client/api-keys/session-token", {
536
+ body: JSON.stringify({
537
+ apiKey: c.app.api_key
538
+ })
539
+ })).json();
540
+ this.app.jwt = e.token;
541
+ } catch {
530
542
  }
531
- return e && (this.app.api_key = e.api_key), l.app;
532
543
  }
533
544
  static getApiKey() {
534
- return l.app.api_key;
545
+ if (!c.app)
546
+ throw new Error("Chainrails SDK not configured. Please call Chainrails.config() first.");
547
+ return c.app.api_key;
548
+ }
549
+ static getSessionKey() {
550
+ return c.app.jwt;
551
+ }
552
+ static getEnv() {
553
+ if (!c.app)
554
+ throw new Error("Chainrails SDK not configured. Please call Chainrails.config() first.");
555
+ return c.app.env;
556
+ }
557
+ static getBaseUrl() {
558
+ if (!c.app)
559
+ throw new Error("Chainrails SDK not configured. Please call Chainrails.config() first.");
560
+ switch (c.app.env) {
561
+ case "production":
562
+ return "https://api.chainrails.io/api/v1";
563
+ case "staging":
564
+ return "https://dev.chainrails.io/api/v1";
565
+ default:
566
+ return "https://api.chainrails.io/api/v1";
567
+ }
535
568
  }
536
569
  }
537
- const c = ye.create({
538
- prefixUrl: "https://dev.chainrails.io/api/v1",
539
- headers: {
540
- "Content-Type": "application/json"
541
- },
542
- hooks: {
543
- beforeRequest: [
544
- (t) => {
545
- const e = l.getApiKey();
546
- t.headers.set("Authorization", `Bearer ${e}`);
547
- }
548
- ],
549
- afterResponse: [(t, e, s) => s]
550
- },
551
- retry: {
552
- limit: 2
553
- }
554
- });
555
- class we {
570
+ let q = null;
571
+ function Ae() {
572
+ q = Re.create({
573
+ prefixUrl: c.getBaseUrl(),
574
+ headers: {
575
+ "Content-Type": "application/json"
576
+ },
577
+ hooks: {
578
+ beforeRequest: [
579
+ (t) => {
580
+ const e = c.getSessionKey();
581
+ t.headers.set("Authorization", `Bearer ${e}`);
582
+ }
583
+ ],
584
+ afterResponse: [(t, e, s) => s]
585
+ },
586
+ retry: {
587
+ limit: 2
588
+ }
589
+ });
590
+ }
591
+ function h() {
592
+ return q || Ae(), q;
593
+ }
594
+ class Ce {
556
595
  async getById(e) {
557
- return await c.get("intents/" + e).json();
596
+ return await h().get("intents/" + e).json();
558
597
  }
559
598
  async getForSender(e) {
560
- return await c.get("intents/user/" + e).json();
599
+ return await h().get("intents/user/" + e).json();
561
600
  }
562
601
  async getForAddress(e) {
563
- return await c.get("intents/address/" + e).json();
602
+ return await h().get("intents/address/" + e).json();
564
603
  }
565
604
  async getAll(e) {
566
- return await c.get("intents", { searchParams: e }).json();
605
+ return await h().get("intents", { searchParams: e }).json();
567
606
  }
568
607
  async create(e) {
569
- return await c.post("intents", { json: e }).json();
608
+ return await h().post("intents", { json: e }).json();
570
609
  }
571
610
  async update(e, s) {
572
- return await c.post("intents", { json: s }).json();
611
+ return await h().post("intents", { json: s }).json();
612
+ }
613
+ async triggerProcessing(e) {
614
+ return await h().post(`intents/${e}/trigger-processing`).json();
573
615
  }
574
- //Calculate Destination
575
616
  }
576
- class be {
617
+ class Pe {
577
618
  async getFromSpecificBridge(e) {
578
- return await c.get("quotes/single", { searchParams: e }).json();
619
+ return await h().get("quotes/single", { searchParams: e }).json();
579
620
  }
580
621
  async getFromAllBridges(e) {
581
- return await c.get("quotes/multiple", { searchParams: e }).json();
622
+ return await h().get("quotes/multiple", { searchParams: e }).json();
582
623
  }
583
624
  async getBestAcrossBridges(e) {
584
- return await c.get("quotes/best", { searchParams: e }).json();
625
+ return await h().get("quotes/best", { searchParams: e }).json();
585
626
  }
586
627
  async getAll(e) {
587
- return await c.get("quotes/multi-source", { searchParams: e }).json();
628
+ return await h().get("quotes/multi-source", { searchParams: e }).json();
588
629
  }
589
630
  }
590
- class Te {
631
+ class xe {
591
632
  async getOptimalRoutes(e) {
592
- return await c.get("router/optimal-route", { searchParams: e }).json();
633
+ return await h().get("router/optimal-route", { searchParams: e }).json();
593
634
  }
594
635
  async getAllSupportedBridges() {
595
- return await c.get("router/supported-bridges/all").json();
636
+ return await h().get("router/supported-bridges/all").json();
596
637
  }
597
638
  async getSupportedBridges(e) {
598
- return await c.get("router/supported-bridges/route", { searchParams: e }).json();
639
+ return await h().get("router/supported-bridges/route", { searchParams: e }).json();
599
640
  }
600
641
  async getSupportedRoutes(e, s) {
601
- return await c.get("router/supported-bridges/bridge/" + e, { searchParams: s }).json();
642
+ return await h().get("router/supported-bridges/bridge/" + e, { searchParams: s }).json();
602
643
  }
603
644
  }
604
- class Re {
645
+ class qe {
605
646
  async getSupported(e) {
606
- return await c.get("chains", { searchParams: e }).json();
647
+ return await h().get("chains", { searchParams: e }).json();
607
648
  }
608
649
  }
609
- class Ee {
650
+ class je {
610
651
  async getClientInfo() {
611
- return await c.get("client/auth/client-info").json();
652
+ return await h().get("client/auth/client-info").json();
612
653
  }
613
654
  }
614
- const Ce = {
615
- router: new Te(),
616
- quotes: new be(),
617
- intents: new we(),
618
- chains: new Re(),
619
- client: new Ee()
655
+ const ve = {
656
+ router: new xe(),
657
+ quotes: new Pe(),
658
+ intents: new Ce(),
659
+ chains: new qe(),
660
+ client: new je()
620
661
  };
621
- function xe(t) {
662
+ function Ue(t) {
622
663
  const {
623
664
  to: e,
624
665
  token: s,
@@ -628,91 +669,103 @@ function xe(t) {
628
669
  close: i,
629
670
  complete: u,
630
671
  cancel: a,
631
- chainSelected: h,
672
+ chainSelected: l,
632
673
  intentCreated: y,
633
- ...T
634
- } = t, [d, R] = v(!0);
635
- return F(() => {
636
- const P = document.body.style.overflow;
637
- o ? document.body.style.overflow = "hidden" : document.body.style.overflow = P || "";
638
- const j = (m) => {
639
- m.data === "closed" && a(), m.data === "completed" && u(), m.data === "chainSelected" && h(), m.data === "intentCreated" && y();
674
+ ...R
675
+ } = t, [d, E] = M(!0), k = W(null);
676
+ G(() => {
677
+ v();
678
+ const g = document.body.style.overflow;
679
+ o ? (document.body.style.overflow = "hidden", document.body.style.overflow = "hidden") : document.body.style.overflow = g || "";
680
+ const U = (m) => {
681
+ m.data === "closed" && a(), m.data === "completed" && u(), m.data === "chainSelected" && l(), m.data === "intentCreated" && y();
682
+ };
683
+ return window.addEventListener("message", U), () => {
684
+ document.body.style.overflow = g || "", window.removeEventListener("message", U);
640
685
  };
641
- return window.addEventListener("message", j), () => {
642
- document.body.style.overflow = P || "", window.removeEventListener("message", j);
686
+ }, [o, a, u, l, y]);
687
+ function V() {
688
+ E(!1), v();
689
+ }
690
+ function v() {
691
+ const g = {
692
+ type: "sessionKey",
693
+ sessionKey: c.getSessionKey(),
694
+ environment: c.getEnv() || N.PRODUCTION
643
695
  };
644
- }, [o, a, u, h, y]), /* @__PURE__ */ k("div", {
645
- ...T,
646
- className: z("payment-modal-wrapper", d ? "loading" : "not-loading", o ? "open" : "closed"),
647
- children: [d && /* @__PURE__ */ g("div", {
696
+ k.current?.contentWindow?.postMessage(g, "https://app.chainrails.io");
697
+ }
698
+ return /* @__PURE__ */ O("div", {
699
+ ...R,
700
+ className: X("payment-modal-wrapper", d ? "loading" : "not-loading", o ? "open" : "closed"),
701
+ children: [d && /* @__PURE__ */ w("div", {
648
702
  role: "status",
649
703
  className: "payment-modal-loader",
650
- children: /* @__PURE__ */ k("svg", {
704
+ children: /* @__PURE__ */ O("svg", {
651
705
  "aria-hidden": "true",
652
706
  className: "icon",
653
707
  viewBox: "0 0 100 101",
654
708
  fill: "none",
655
709
  xmlns: "http://www.w3.org/2000/svg",
656
- children: [/* @__PURE__ */ g("path", {
710
+ children: [/* @__PURE__ */ w("path", {
657
711
  d: "M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z",
658
712
  fill: "currentColor"
659
- }), /* @__PURE__ */ g("path", {
713
+ }), /* @__PURE__ */ w("path", {
660
714
  d: "M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z",
661
715
  fill: "currentFill"
662
716
  })]
663
717
  })
664
- }), /* @__PURE__ */ g("iframe", {
718
+ }), /* @__PURE__ */ w("iframe", {
665
719
  allow: "clipboard-read *; clipboard-write *",
666
- src: `https://app.chainrails.io/pay/_chain_/${r}/_token_/${s}/_to_/${e}/_amount_/${n * 100}?id=${l.getApiKey()}`,
667
- onLoad: () => {
668
- R(!1);
669
- },
670
- className: `payment-modal ${d ? "invisible" : "visible"}`
720
+ src: `https://app.chainrails.io/pay/_chain_/${r}/_token_/${s}/_to_/${e}/_amount_/${n * 100}`,
721
+ onLoad: V,
722
+ className: `payment-modal ${d ? "invisible" : "visible"}`,
723
+ ref: k
671
724
  })]
672
725
  });
673
726
  }
674
- function qe(t) {
727
+ function Oe(t) {
675
728
  const {
676
729
  onCancel: e,
677
730
  onSuccess: s,
678
731
  onChainSelect: r,
679
732
  onIntentCreate: n,
680
733
  ...o
681
- } = t, [i, u] = v(!1);
734
+ } = t, [i, u] = M(!1);
682
735
  function a() {
683
736
  u(!0);
684
737
  }
685
- function h() {
738
+ function l() {
686
739
  u(!1);
687
740
  }
688
741
  function y() {
689
- h(), e?.();
742
+ l(), e?.();
690
743
  }
691
- function T() {
692
- window.setTimeout(h, 2e3), s?.();
744
+ function R() {
745
+ window.setTimeout(l, 2e3), s?.();
693
746
  }
694
747
  function d() {
695
748
  r?.();
696
749
  }
697
- function R() {
750
+ function E() {
698
751
  n?.();
699
752
  }
700
753
  return {
701
754
  open: a,
702
- close: h,
703
- complete: T,
755
+ close: l,
756
+ complete: R,
704
757
  cancel: y,
705
758
  chainSelected: d,
706
- intentCreated: R,
759
+ intentCreated: E,
707
760
  isOpen: i,
708
761
  ...o
709
762
  };
710
763
  }
711
764
  export {
712
- l as Chainrails,
713
- xe as PaymentModal,
714
- me as chains,
715
- Ce as crapi,
716
- ge as tokens,
717
- qe as usePaymentModal
765
+ c as Chainrails,
766
+ Ue as PaymentModal,
767
+ Ee as chains,
768
+ ve as crapi,
769
+ Se as tokens,
770
+ Oe as usePaymentModal
718
771
  };