@declarion/react 0.188.0 → 0.189.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,9 +1,9 @@
1
1
  import { ApiError as e, NetworkError as t, errorCodeOf as n, readProblem as r } from "../types/api.js";
2
2
  import { loginPathForCurrentLocation as i } from "./auth-redirect.js";
3
3
  import { observe as a } from "../lib/versionTracker.js";
4
- import { awaitEmbedToken as o, clearEmbedToken as s, getEmbedToken as c, isEmbedTokenMode as l } from "../embed/token.js";
5
- import { REFRESH_TIMEOUT_MS as u, requestTimeoutMs as d } from "./request-bounds.js";
6
- import { clearAccessExpiry as f, recordAccessExpiry as p } from "./access-expiry.js";
4
+ import { awaitEmbedToken as ee, clearEmbedToken as o, getEmbedToken as s, isEmbedTokenMode as c } from "../embed/token.js";
5
+ import { REFRESH_TIMEOUT_MS as l, requestTimeoutMs as u } from "./request-bounds.js";
6
+ import { clearAccessExpiry as d, clearSignOut as f, recordAccessExpiry as p } from "./access-expiry.js";
7
7
  import { sameAudience as m } from "./audience.js";
8
8
  // src/api/client.ts
9
9
  var h = (e) => JSON.stringify(e ?? {}), g = null, _ = "declarion.auth.refresh.result", v = "declarion.auth.refresh";
@@ -31,7 +31,7 @@ function x(e) {
31
31
  async function S() {
32
32
  let e = navigator.locks;
33
33
  if (!e) {
34
- await P();
34
+ await F();
35
35
  return;
36
36
  }
37
37
  let t = Date.now();
@@ -43,7 +43,7 @@ async function S() {
43
43
  } else try {
44
44
  b({
45
45
  ok: !0,
46
- access_expires_at: await P()
46
+ access_expires_at: await F()
47
47
  });
48
48
  } catch (e) {
49
49
  throw e instanceof C && b({
@@ -65,27 +65,27 @@ function T() {
65
65
  }), w;
66
66
  }
67
67
  function E(e) {
68
- return O || (F = null, l() || p(e), typeof window > "u") ? Promise.resolve() : T().then((t) => t.scheduleRenewal(e), () => void 0);
68
+ return O || (I = null, c() || p(e), typeof window > "u") ? Promise.resolve() : T().then((t) => t.scheduleRenewal(e), () => void 0);
69
69
  }
70
70
  function D() {
71
- f(), w?.then((e) => e.cancelRenewal());
71
+ d(), w?.then((e) => e.cancelRenewal());
72
72
  }
73
73
  var O = !1;
74
- function ee() {
74
+ function k() {
75
75
  O = !0, D();
76
76
  }
77
- function k() {
77
+ function A() {
78
78
  return O;
79
79
  }
80
- function A(e) {
81
- return O = !1, I += 1, E(e);
80
+ function j(e) {
81
+ return O = !1, f(), L += 1, E(e);
82
82
  }
83
83
  typeof window < "u" && window.addEventListener("storage", (e) => {
84
- if (e.key !== _ || l()) return;
84
+ if (e.key !== _ || c()) return;
85
85
  let t = y(() => e.newValue);
86
86
  t?.ok && t.access_expires_at && E(t.access_expires_at);
87
87
  });
88
- async function j(e, n, r) {
88
+ async function M(e, n, r) {
89
89
  let i = new AbortController(), a = setTimeout(() => i.abort(), r);
90
90
  try {
91
91
  return await fetch(e, {
@@ -98,145 +98,145 @@ async function j(e, n, r) {
98
98
  clearTimeout(a);
99
99
  }
100
100
  }
101
- async function M(e) {
101
+ async function N(e) {
102
102
  try {
103
103
  return (await e.json())?.result?.expires_at;
104
104
  } catch {
105
105
  return;
106
106
  }
107
107
  }
108
- function N() {
109
- return j("/api/actions/auth.refresh", {
108
+ function P() {
109
+ return M("/api/actions/auth.refresh", {
110
110
  method: "POST",
111
111
  credentials: "include",
112
112
  headers: { "Content-Type": "application/json" },
113
113
  body: h({})
114
- }, u);
114
+ }, l);
115
115
  }
116
- async function P() {
117
- let t = await N();
116
+ async function F() {
117
+ let t = await P();
118
118
  if (t.status === 429) {
119
119
  let e = parseInt(t.headers.get("Retry-After") || "1", 10);
120
- await r(t), await new Promise((t) => setTimeout(t, e * 1e3)), t = await N();
120
+ await r(t), await new Promise((t) => setTimeout(t, e * 1e3)), t = await P();
121
121
  }
122
122
  if (t.status >= 500) {
123
123
  let n = await r(t);
124
124
  throw new e(t.status, n.body, n.raw);
125
125
  }
126
126
  if (!t.ok) throw await r(t), new C(t.status);
127
- let n = await M(t);
127
+ let n = await N(t);
128
128
  return E(n), n;
129
129
  }
130
- var F = null, I = 0, L = /* @__PURE__ */ new Set(["auth.token_refresh_needed", "auth.unauthorized"]);
131
- function R(n) {
130
+ var I = null, L = 0, R = /* @__PURE__ */ new Set(["auth.token_refresh_needed", "auth.unauthorized"]);
131
+ function z(n) {
132
132
  return n instanceof t || n instanceof e;
133
133
  }
134
- async function z() {
134
+ async function B() {
135
135
  if (O) throw new C(401);
136
- if (F !== null) throw new C(F);
136
+ if (I !== null) throw new C(I);
137
137
  if (g) return g;
138
- let e = I;
138
+ let e = L;
139
139
  return g = S().catch((t) => {
140
- throw t instanceof C && e === I && (F = t.status), t;
140
+ throw t instanceof C && e === L && (I = t.status), t;
141
141
  }).finally(() => {
142
142
  g = null;
143
143
  }), g;
144
144
  }
145
- var B = () => {
145
+ var V = () => {
146
146
  if (typeof window > "u") return;
147
147
  let { pathname: e } = window.location;
148
148
  e.startsWith("/login") || e.startsWith("/signup") || e === "/reset" || e === "/forgot" || e === "/2fa" || (window.location.href = i());
149
149
  };
150
- function V(e) {
151
- let t = B;
152
- return B = e, t;
150
+ function H(e) {
151
+ let t = V;
152
+ return V = e, t;
153
153
  }
154
- function H() {
155
- return B;
154
+ function U() {
155
+ return V;
156
156
  }
157
- var U = ({ status: e, code: t, message: n }) => {
157
+ var W = ({ status: e, code: t, message: n }) => {
158
158
  console.error(`[API ${e}] ${t}: ${n}`);
159
159
  };
160
- function W(e, t, n, r) {
160
+ function G(e, t, n, r) {
161
161
  let i = new Headers(e);
162
162
  return !i.has("Content-Type") && t && i.set("Content-Type", "application/json"), n.setAccept && !i.has("Accept") && i.set("Accept", "*/*"), r && i.set("Authorization", `Bearer ${r}`), i;
163
163
  }
164
- var G = null, K;
165
- function q(e) {
166
- K = e, G && e !== void 0 && !m(G.reader, e) && (G = null);
164
+ var K = null, q;
165
+ function J(e) {
166
+ q = e, K && e !== void 0 && !m(K.reader, e) && (K = null);
167
167
  }
168
- function J(e, t) {
169
- (K === void 0 || m(e, K)) && (G = t ? {
168
+ function te(e, t) {
169
+ (q === void 0 || m(e, q)) && (K = t ? {
170
170
  language: t,
171
171
  reader: e
172
172
  } : null);
173
173
  }
174
- function te(e) {
175
- G = e ? {
174
+ function ne(e) {
175
+ K = e ? {
176
176
  language: e,
177
- reader: K ?? null
177
+ reader: q ?? null
178
178
  } : null;
179
179
  }
180
180
  function Y() {
181
- return G?.language ?? null;
181
+ return K?.language ?? null;
182
182
  }
183
183
  function X(e) {
184
184
  let t = Y();
185
185
  return !t || !e.startsWith("/api/") || /[?&]language=/.test(e) ? e : `${e}${e.includes("?") ? "&" : "?"}language=${encodeURIComponent(t)}`;
186
186
  }
187
- async function Z(t, i, u, f) {
188
- let p = X(t), m = l(), h = m ? "omit" : i.credentials ?? "include", g = m || h !== "omit", _ = W(i.headers, !!i.body, u, m ? c() : null), v = d(t, i), y = I, b = await j(p, {
187
+ async function Z(t, i, l, d) {
188
+ let f = X(t), p = c(), m = p ? "omit" : i.credentials ?? "include", h = p || m !== "omit", g = G(i.headers, !!i.body, l, p ? s() : null), _ = u(t, i), v = L, y = await M(f, {
189
189
  ...i,
190
- headers: _,
191
- credentials: h
192
- }, v);
193
- if (a(b.headers), !b.ok) {
194
- let { raw: t, body: a } = await r(b), c = n(a.type), l = a.title ?? "";
195
- if (g && b.status === 401 && L.has(c)) try {
190
+ headers: g,
191
+ credentials: m
192
+ }, _);
193
+ if (a(y.headers), !y.ok) {
194
+ let { raw: t, body: a } = await r(y), s = n(a.type), c = a.title ?? "";
195
+ if (h && y.status === 401 && R.has(s)) try {
196
196
  let t = null;
197
- m ? (s(), t = await o()) : await z();
198
- let a = W(i.headers, !!i.body, u, t), c = await j(p, {
197
+ p ? (o(), t = await ee()) : await B();
198
+ let a = G(i.headers, !!i.body, l, t), s = await M(f, {
199
199
  ...i,
200
200
  headers: a,
201
- credentials: h
202
- }, v);
203
- if (!c.ok) {
204
- let t = await r(c);
205
- throw !m && y === I && c.status === 401 && L.has(n(t.body.type)) && (F = 401), c.status === 401 && !u.isSessionProbe && B(), new e(c.status, t.body, t.raw);
201
+ credentials: m
202
+ }, _);
203
+ if (!s.ok) {
204
+ let t = await r(s);
205
+ throw !p && v === L && s.status === 401 && R.has(n(t.body.type)) && (I = 401), s.status === 401 && !l.isSessionProbe && V(), new e(s.status, t.body, t.raw);
206
206
  }
207
- return f(c);
207
+ return d(s);
208
208
  } catch (n) {
209
- throw R(n) ? n : (u.isSessionProbe || B(), new e(401, a, t));
209
+ throw z(n) ? n : (l.isSessionProbe || V(), new e(401, a, t));
210
210
  }
211
- throw g && b.status === 401 && !u.isSessionProbe && B(), U({
212
- status: b.status,
213
- code: c,
214
- message: l
215
- }), new e(b.status, a, t);
211
+ throw h && y.status === 401 && !l.isSessionProbe && V(), W({
212
+ status: y.status,
213
+ code: s,
214
+ message: c
215
+ }), new e(y.status, a, t);
216
216
  }
217
- return f(b);
217
+ return d(y);
218
218
  }
219
219
  function Q(e) {
220
220
  return e.status === 204 ? Promise.resolve(void 0) : e.json();
221
221
  }
222
- async function ne(e, t = {}, { isSessionProbe: n = !1 } = {}) {
222
+ async function re(e, t = {}, { isSessionProbe: n = !1 } = {}) {
223
223
  return Z(e, t, {
224
224
  setAccept: !1,
225
225
  isSessionProbe: n
226
226
  }, Q);
227
227
  }
228
- async function re(e) {
228
+ async function ie(e) {
229
229
  return {
230
230
  blob: await e.blob(),
231
231
  filename: $(e.headers),
232
232
  headers: e.headers
233
233
  };
234
234
  }
235
- async function ie(e, t = {}) {
235
+ async function ae(e, t = {}) {
236
236
  return Z(e, t, {
237
237
  setAccept: !0,
238
238
  isSessionProbe: !1
239
- }, re);
239
+ }, ie);
240
240
  }
241
241
  function $(e) {
242
242
  let t = e.get("Content-Disposition");
@@ -248,7 +248,7 @@ function $(e) {
248
248
  let r = t.match(/filename\s*=\s*("([^"]+)"|([^;]+))/i);
249
249
  return r ? (r[2] ?? r[3] ?? "").trim() : "";
250
250
  }
251
- async function ae(e, t = {}, { isSessionProbe: n = !1, keepalive: r = !1, credentials: i } = {}) {
251
+ async function oe(e, t = {}, { isSessionProbe: n = !1, keepalive: r = !1, credentials: i } = {}) {
252
252
  return (await Z(`/api/actions/${encodeURIComponent(e)}`, {
253
253
  method: "POST",
254
254
  body: h(t),
@@ -259,4 +259,4 @@ async function ae(e, t = {}, { isSessionProbe: n = !1, keepalive: r = !1, creden
259
259
  isSessionProbe: n
260
260
  }, Q)).result;
261
261
  }
262
- export { C as RefreshDeadError, ne as apiFetch, ie as apiFetchBlob, A as beginSessionInPage, ae as callHandler, D as cancelProactiveRefresh, Y as currentPublishedLanguage, ee as endSessionInPage, z as ensureRefresh, H as getPersistentAuthFailureHandler, k as isSignedOutInPage, R as isTransientFailure, $ as parseContentDispositionFilename, te as publishLanguage, J as publishLanguageFor, E as scheduleProactiveRefresh, q as setLanguageReader, V as setPersistentAuthFailureHandler, X as withPublishedLanguage };
262
+ export { C as RefreshDeadError, re as apiFetch, ae as apiFetchBlob, j as beginSessionInPage, oe as callHandler, D as cancelProactiveRefresh, Y as currentPublishedLanguage, k as endSessionInPage, B as ensureRefresh, U as getPersistentAuthFailureHandler, A as isSignedOutInPage, z as isTransientFailure, $ as parseContentDispositionFilename, ne as publishLanguage, te as publishLanguageFor, E as scheduleProactiveRefresh, J as setLanguageReader, H as setPersistentAuthFailureHandler, X as withPublishedLanguage };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@declarion/react",
3
- "version": "0.188.0",
3
+ "version": "0.189.0",
4
4
  "private": false,
5
5
  "license": "MIT",
6
6
  "description": "React SDK for Declarion, the schema-driven business apps platform.",