@declarion/react 0.121.0 → 0.131.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,77 +1,77 @@
1
- import { callHandler as e, scheduleProactiveRefresh as t } from "./client.js";
1
+ import { beginSessionInPage as e, callHandler as t, scheduleProactiveRefresh as n } from "./client.js";
2
2
  //#region src/api/auth.ts
3
- async function n() {
4
- return e("auth.check_setup");
3
+ async function r() {
4
+ return t("auth.check_setup");
5
5
  }
6
- async function r(n, r, i = !0) {
7
- let a = await e("auth.login", {
6
+ async function i(n, r, i = !0) {
7
+ let a = await t("auth.login", {
8
8
  email: n,
9
9
  password: r,
10
10
  remember: i
11
11
  });
12
- return t(a.expires_at), a;
12
+ return e(a.expires_at), a;
13
13
  }
14
- async function i(n, r, i) {
15
- let a = await e("auth.signup", {
14
+ async function a(n, r, i) {
15
+ let a = await t("auth.signup", {
16
16
  email: n,
17
17
  password: r,
18
18
  display_name: i
19
19
  });
20
- return a.verification_required || t(a.expires_at), a;
20
+ return a.verification_required || e(a.expires_at), a;
21
21
  }
22
- async function a(n, r) {
23
- let i = await e("auth.confirm_signup", {
22
+ async function o(n, r) {
23
+ let i = await t("auth.confirm_signup", {
24
24
  email: n,
25
25
  code: r
26
26
  });
27
- return t(i.expires_at), i;
27
+ return e(i.expires_at), i;
28
28
  }
29
- async function o(t) {
30
- return e("auth.resend_signup_code", { email: t });
31
- }
32
- async function s() {
33
- return e("auth.me");
29
+ async function s(e) {
30
+ return t("auth.resend_signup_code", { email: e });
34
31
  }
35
32
  async function c() {
36
- return e("auth.me", {}, { isSessionProbe: !0 });
33
+ return t("auth.me");
37
34
  }
38
35
  async function l() {
39
- return (await e("auth.list_tenants")).tenants;
36
+ return t("auth.me", {}, { isSessionProbe: !0 });
37
+ }
38
+ async function u() {
39
+ return (await t("auth.list_tenants")).tenants;
40
40
  }
41
- async function u(n) {
42
- let r = await e("auth.switch_tenant", { tenant_id: n });
43
- return t(r.expires_at), r;
41
+ async function d(e) {
42
+ let r = await t("auth.switch_tenant", { tenant_id: e });
43
+ return n(r.expires_at), r;
44
44
  }
45
- async function d() {
46
- await e("auth.logout", {}, { keepalive: !0 });
45
+ async function f() {
46
+ await t("auth.logout", {}, { keepalive: !0 });
47
47
  }
48
- async function f(t) {
49
- return (await e("auth.list_identity_providers", t ? { tenant_code: t } : {})).providers;
48
+ async function p(e) {
49
+ return (await t("auth.list_identity_providers", e ? { tenant_code: e } : {})).providers;
50
50
  }
51
- function p(e, t, n) {
51
+ function m(e, t, n) {
52
52
  let r = new URLSearchParams();
53
53
  t && r.set("tenant", t), n && r.set("redirect", n);
54
54
  let i = r.toString();
55
55
  return `/api/auth/sso/authorize/${encodeURIComponent(e)}${i ? `?${i}` : ""}`;
56
56
  }
57
- async function m(t, n, r) {
57
+ async function h(e, n, r) {
58
58
  let i = {
59
- target_user_id: t,
59
+ target_user_id: e,
60
60
  reason: n
61
61
  };
62
- return r != null && (i.duration_minutes = r), e("impersonation.start", i);
62
+ return r != null && (i.duration_minutes = r), t("impersonation.start", i);
63
63
  }
64
- async function h() {
65
- return e("impersonation.stop");
64
+ async function g() {
65
+ return t("impersonation.stop");
66
66
  }
67
- async function g(t) {
68
- await e("impersonation.revoke", { session_id: t });
67
+ async function _(e) {
68
+ await t("impersonation.revoke", { session_id: e });
69
69
  }
70
- async function _(t, n) {
71
- await e("auth.change_password", {
72
- current_password: t,
70
+ async function v(e, n) {
71
+ await t("auth.change_password", {
72
+ current_password: e,
73
73
  new_password: n
74
74
  });
75
75
  }
76
76
  //#endregion
77
- export { _ as changePassword, n as checkSetup, a as confirmSignup, s as getMe, g as impersonationRevoke, m as impersonationStart, h as impersonationStop, f as listIdentityProviders, l as listTenants, r as login, d as logout, c as probeSession, o as resendSignupCode, i as signup, p as ssoAuthorizeUrl, u as switchTenant };
77
+ export { v as changePassword, r as checkSetup, o as confirmSignup, c as getMe, _ as impersonationRevoke, h as impersonationStart, g as impersonationStop, p as listIdentityProviders, u as listTenants, i as login, f as logout, l as probeSession, s as resendSignupCode, a as signup, m as ssoAuthorizeUrl, d as switchTenant };
@@ -26,12 +26,12 @@ function b(e) {
26
26
  }
27
27
  }
28
28
  function x(e) {
29
- return e.ok ? (e.access_expires_at && E(e.access_expires_at), null) : new C(e.status ?? 401);
29
+ return e.ok ? (e.access_expires_at && T(e.access_expires_at), null) : new C(e.status ?? 401);
30
30
  }
31
31
  async function S() {
32
32
  let e = navigator.locks;
33
33
  if (!e) {
34
- await j();
34
+ await P();
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 j()
46
+ access_expires_at: await P()
47
47
  });
48
48
  } catch (e) {
49
49
  throw e instanceof C && b({
@@ -59,23 +59,33 @@ var C = class extends Error {
59
59
  super(`refresh failed terminally (${e})`), this.status = e, this.name = "RefreshDeadError";
60
60
  }
61
61
  }, w = null;
62
- function T() {
62
+ function ee() {
63
63
  return w ??= import("./session-renewal.js").catch((e) => {
64
64
  throw w = null, e;
65
65
  }), w;
66
66
  }
67
- function E(e) {
68
- return M = null, c() || p(e), typeof window > "u" ? Promise.resolve() : T().then((t) => t.scheduleRenewal(e), () => void 0);
67
+ function T(e) {
68
+ return D || (F = null, c() || p(e), typeof window > "u") ? Promise.resolve() : ee().then((t) => t.scheduleRenewal(e), () => void 0);
69
69
  }
70
- function D() {
70
+ function E() {
71
71
  f(), w?.then((e) => e.cancelRenewal());
72
72
  }
73
+ var D = !1;
74
+ function O() {
75
+ D = !0, E();
76
+ }
77
+ function k() {
78
+ return D;
79
+ }
80
+ function A(e) {
81
+ return D = !1, T(e);
82
+ }
73
83
  typeof window < "u" && window.addEventListener("storage", (e) => {
74
84
  if (e.key !== _ || c()) return;
75
85
  let t = y(() => e.newValue);
76
- t?.ok && t.access_expires_at && E(t.access_expires_at);
86
+ t?.ok && t.access_expires_at && T(t.access_expires_at);
77
87
  });
78
- async function O(e, n, r) {
88
+ async function j(e, n, r) {
79
89
  let i = new AbortController(), a = setTimeout(() => i.abort(), r);
80
90
  try {
81
91
  return await fetch(e, {
@@ -88,114 +98,115 @@ async function O(e, n, r) {
88
98
  clearTimeout(a);
89
99
  }
90
100
  }
91
- async function k(e) {
101
+ async function M(e) {
92
102
  try {
93
103
  return (await e.json())?.result?.expires_at;
94
104
  } catch {
95
105
  return;
96
106
  }
97
107
  }
98
- function A() {
99
- return O("/api/actions/auth.refresh", {
108
+ function N() {
109
+ return j("/api/actions/auth.refresh", {
100
110
  method: "POST",
101
111
  credentials: "include",
102
112
  headers: { "Content-Type": "application/json" },
103
113
  body: h({})
104
114
  }, u);
105
115
  }
106
- async function j() {
107
- let t = await A();
116
+ async function P() {
117
+ let t = await N();
108
118
  if (t.status === 429) {
109
119
  let e = parseInt(t.headers.get("Retry-After") || "1", 10);
110
- await r(t), await new Promise((t) => setTimeout(t, e * 1e3)), t = await A();
120
+ await r(t), await new Promise((t) => setTimeout(t, e * 1e3)), t = await N();
111
121
  }
112
122
  if (t.status >= 500) {
113
123
  let n = await r(t);
114
124
  throw new e(t.status, n.body, n.raw);
115
125
  }
116
126
  if (!t.ok) throw await r(t), new C(t.status);
117
- let n = await k(t);
118
- return E(n), n;
127
+ let n = await M(t);
128
+ return T(n), n;
119
129
  }
120
- var M = null, N = /* @__PURE__ */ new Set(["auth.token_refresh_needed", "auth.unauthorized"]);
121
- function P(n) {
130
+ var F = null, I = /* @__PURE__ */ new Set(["auth.token_refresh_needed", "auth.unauthorized"]);
131
+ function L(n) {
122
132
  return n instanceof t || n instanceof e;
123
133
  }
124
- async function F() {
125
- if (M !== null) throw new C(M);
134
+ async function R() {
135
+ if (D) throw new C(401);
136
+ if (F !== null) throw new C(F);
126
137
  return g || (g = S().catch((e) => {
127
- throw e instanceof C && (M = e.status), e;
138
+ throw e instanceof C && (F = e.status), e;
128
139
  }).finally(() => {
129
140
  g = null;
130
141
  }), g);
131
142
  }
132
- var I = () => {
143
+ var z = () => {
133
144
  if (typeof window > "u") return;
134
145
  let { pathname: e } = window.location;
135
146
  e.startsWith("/login") || e.startsWith("/signup") || e === "/reset" || e === "/forgot" || e === "/2fa" || (window.location.href = i());
136
147
  };
137
- function L(e) {
138
- let t = I;
139
- return I = e, t;
148
+ function B(e) {
149
+ let t = z;
150
+ return z = e, t;
140
151
  }
141
- function R() {
142
- return I;
152
+ function V() {
153
+ return z;
143
154
  }
144
- var z = ({ status: e, code: t, message: n }) => {
155
+ var H = ({ status: e, code: t, message: n }) => {
145
156
  console.error(`[API ${e}] ${t}: ${n}`);
146
157
  };
147
- function B(e, t, n, r) {
158
+ function U(e, t, n, r) {
148
159
  let i = new Headers(e);
149
160
  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;
150
161
  }
151
- var V = null, H;
152
- function U(e) {
153
- H = e, V && e !== void 0 && !m(V.reader, e) && (V = null);
162
+ var W = null, G;
163
+ function K(e) {
164
+ G = e, W && e !== void 0 && !m(W.reader, e) && (W = null);
154
165
  }
155
- function W(e, t) {
156
- (H === void 0 || m(e, H)) && (V = t ? {
166
+ function q(e, t) {
167
+ (G === void 0 || m(e, G)) && (W = t ? {
157
168
  language: t,
158
169
  reader: e
159
170
  } : null);
160
171
  }
161
- function G(e) {
162
- V = e ? {
172
+ function J(e) {
173
+ W = e ? {
163
174
  language: e,
164
- reader: H ?? null
175
+ reader: G ?? null
165
176
  } : null;
166
177
  }
167
- function K() {
168
- return V?.language ?? null;
178
+ function Y() {
179
+ return W?.language ?? null;
169
180
  }
170
- function q(e) {
171
- let t = K();
181
+ function X(e) {
182
+ let t = Y();
172
183
  return !t || !e.startsWith("/api/") || /[?&]language=/.test(e) ? e : `${e}${e.includes("?") ? "&" : "?"}language=${encodeURIComponent(t)}`;
173
184
  }
174
- async function J(t, i, u, f) {
175
- let p = q(t), m = c(), h = m ? "omit" : "include", g = B(i.headers, !!i.body, u, m ? s() : null), _ = d(t, i), v = await O(p, {
185
+ async function Z(t, i, u, f) {
186
+ let p = X(t), m = c(), h = m ? "omit" : "include", g = U(i.headers, !!i.body, u, m ? s() : null), _ = d(t, i), v = await j(p, {
176
187
  ...i,
177
188
  headers: g,
178
189
  credentials: h
179
190
  }, _);
180
191
  if (l(v.headers), !v.ok) {
181
192
  let { raw: t, body: s } = await r(v), c = n(s.type), l = s.title ?? "";
182
- if (v.status === 401 && N.has(c)) try {
193
+ if (v.status === 401 && I.has(c)) try {
183
194
  let t = null;
184
- m ? (o(), t = await a()) : await F();
185
- let s = B(i.headers, !!i.body, u, t), c = await O(p, {
195
+ m ? (o(), t = await a()) : await R();
196
+ let s = U(i.headers, !!i.body, u, t), c = await j(p, {
186
197
  ...i,
187
198
  headers: s,
188
199
  credentials: h
189
200
  }, _);
190
201
  if (!c.ok) {
191
202
  let t = await r(c);
192
- throw !m && c.status === 401 && N.has(n(t.body.type)) && (M = 401), c.status === 401 && !u.isSessionProbe && I(), new e(c.status, t.body, t.raw);
203
+ throw !m && c.status === 401 && I.has(n(t.body.type)) && (F = 401), c.status === 401 && !u.isSessionProbe && z(), new e(c.status, t.body, t.raw);
193
204
  }
194
205
  return f(c);
195
206
  } catch (n) {
196
- throw P(n) ? n : (u.isSessionProbe || I(), new e(401, s, t));
207
+ throw L(n) ? n : (u.isSessionProbe || z(), new e(401, s, t));
197
208
  }
198
- throw v.status === 401 && !u.isSessionProbe && I(), z({
209
+ throw v.status === 401 && !u.isSessionProbe && z(), H({
199
210
  status: v.status,
200
211
  code: c,
201
212
  message: l
@@ -203,27 +214,27 @@ async function J(t, i, u, f) {
203
214
  }
204
215
  return f(v);
205
216
  }
206
- function Y(e) {
217
+ function Q(e) {
207
218
  return e.status === 204 ? Promise.resolve(void 0) : e.json();
208
219
  }
209
- async function X(e, t = {}, { isSessionProbe: n = !1 } = {}) {
210
- return J(e, t, {
220
+ async function te(e, t = {}, { isSessionProbe: n = !1 } = {}) {
221
+ return Z(e, t, {
211
222
  setAccept: !1,
212
223
  isSessionProbe: n
213
- }, Y);
224
+ }, Q);
214
225
  }
215
- async function Z(e) {
226
+ async function ne(e) {
216
227
  return {
217
228
  blob: await e.blob(),
218
229
  filename: $(e.headers),
219
230
  headers: e.headers
220
231
  };
221
232
  }
222
- async function Q(e, t = {}) {
223
- return J(e, t, {
233
+ async function re(e, t = {}) {
234
+ return Z(e, t, {
224
235
  setAccept: !0,
225
236
  isSessionProbe: !1
226
- }, Z);
237
+ }, ne);
227
238
  }
228
239
  function $(e) {
229
240
  let t = e.get("Content-Disposition");
@@ -235,15 +246,15 @@ function $(e) {
235
246
  let r = t.match(/filename\s*=\s*("([^"]+)"|([^;]+))/i);
236
247
  return r ? (r[2] ?? r[3] ?? "").trim() : "";
237
248
  }
238
- async function ee(e, t = {}, { isSessionProbe: n = !1, keepalive: r = !1 } = {}) {
239
- return (await J(`/api/actions/${encodeURIComponent(e)}`, {
249
+ async function ie(e, t = {}, { isSessionProbe: n = !1, keepalive: r = !1 } = {}) {
250
+ return (await Z(`/api/actions/${encodeURIComponent(e)}`, {
240
251
  method: "POST",
241
252
  body: h(t),
242
253
  keepalive: r
243
254
  }, {
244
255
  setAccept: !1,
245
256
  isSessionProbe: n
246
- }, Y)).result;
257
+ }, Q)).result;
247
258
  }
248
259
  //#endregion
249
- export { C as RefreshDeadError, X as apiFetch, Q as apiFetchBlob, ee as callHandler, D as cancelProactiveRefresh, K as currentPublishedLanguage, F as ensureRefresh, R as getPersistentAuthFailureHandler, P as isTransientFailure, $ as parseContentDispositionFilename, G as publishLanguage, W as publishLanguageFor, E as scheduleProactiveRefresh, U as setLanguageReader, L as setPersistentAuthFailureHandler, q as withPublishedLanguage };
260
+ export { C as RefreshDeadError, te as apiFetch, re as apiFetchBlob, A as beginSessionInPage, ie as callHandler, E as cancelProactiveRefresh, Y as currentPublishedLanguage, O as endSessionInPage, R as ensureRefresh, V as getPersistentAuthFailureHandler, k as isSignedOutInPage, L as isTransientFailure, $ as parseContentDispositionFilename, J as publishLanguage, q as publishLanguageFor, T as scheduleProactiveRefresh, K as setLanguageReader, B as setPersistentAuthFailureHandler, X as withPublishedLanguage };
@@ -1,33 +1,39 @@
1
1
  import { isEmbedTokenMode as e } from "../embed/token.js";
2
2
  import { startOrderNow as t } from "../api/access-expiry.js";
3
3
  import { sessionCaller as n } from "../api/audience.js";
4
- import { cancelProactiveRefresh as r, ensureRefresh as i, scheduleProactiveRefresh as a, setLanguageReader as o } from "../api/client.js";
5
- import { getMe as s, listTenants as c, logout as l, probeSession as u } from "../api/auth.js";
6
- import { warmRoutePages as d } from "../lib/capabilityPrefetch.js";
7
- import { usePreferencesStore as f } from "./preferences.js";
8
- import { create as p } from "zustand";
4
+ import { cancelProactiveRefresh as r, endSessionInPage as i, ensureRefresh as a, isSignedOutInPage as o, scheduleProactiveRefresh as s, setLanguageReader as c } from "../api/client.js";
5
+ import { getMe as l, listTenants as u, logout as d, probeSession as f } from "../api/auth.js";
6
+ import { warmRoutePages as p } from "../lib/capabilityPrefetch.js";
7
+ import { usePreferencesStore as m } from "./preferences.js";
8
+ import { create as h } from "zustand";
9
9
  //#region src/stores/auth.ts
10
- async function m(e, t, n) {
11
- try {
12
- let r = await n();
13
- a(r.access_expires_at), e({
14
- user: r,
10
+ async function g(e, t, n) {
11
+ let r = () => e({
12
+ user: null,
13
+ isAuthenticated: !1,
14
+ isLoading: !1,
15
+ sessionPresumed: !1,
16
+ identitySince: performance.now()
17
+ });
18
+ if (o()) r();
19
+ else try {
20
+ let i = await n();
21
+ if (o()) {
22
+ r();
23
+ return;
24
+ }
25
+ s(i.access_expires_at), e({
26
+ user: i,
15
27
  isAuthenticated: !0,
16
28
  isLoading: !1,
17
- activeTenant: r.tenant ?? null,
29
+ activeTenant: i.tenant ?? null,
18
30
  identitySince: performance.now()
19
31
  }), t().loadTenants();
20
32
  } catch {
21
- e({
22
- user: null,
23
- isAuthenticated: !1,
24
- isLoading: !1,
25
- sessionPresumed: !1,
26
- identitySince: performance.now()
27
- });
33
+ r();
28
34
  }
29
35
  }
30
- var h = p((e, n) => ({
36
+ var _ = h((e, n) => ({
31
37
  user: null,
32
38
  isAuthenticated: !1,
33
39
  isLoading: !0,
@@ -37,13 +43,15 @@ var h = p((e, n) => ({
37
43
  identitySince: 0,
38
44
  initialize: async () => {
39
45
  let r = t();
40
- r === "refresh-first" && (r = await i().then(() => "first-wave", () => "probe-first")), e({ sessionPresumed: r === "first-wave" }), r === "first-wave" && d(), await m(e, n, u), r === "probe-first" && n().isAuthenticated && d();
46
+ r === "refresh-first" && (r = await a().then(() => "first-wave", () => "probe-first")), e({ sessionPresumed: r === "first-wave" }), r === "first-wave" && p(), await g(e, n, f), r === "probe-first" && n().isAuthenticated && p();
41
47
  },
42
- reloadSession: () => m(e, n, s),
43
- recheckSession: () => m(e, n, u),
48
+ reloadSession: () => g(e, n, l),
49
+ recheckSession: () => g(e, n, f),
44
50
  refreshMe: async () => {
45
51
  try {
46
- let t = await s(), r = n(), i = r.user?.id !== t.id || r.activeTenant?.id !== t.tenant?.id;
52
+ let t = await l();
53
+ if (o()) return;
54
+ let r = n(), i = r.user?.id !== t.id || r.activeTenant?.id !== t.tenant?.id;
47
55
  e({
48
56
  user: t,
49
57
  activeTenant: t.tenant ?? null,
@@ -61,26 +69,28 @@ var h = p((e, n) => ({
61
69
  });
62
70
  },
63
71
  loadTenants: () => {
64
- c().then((t) => e({ tenants: t }), () => void 0);
72
+ u().then((t) => {
73
+ o() || e({ tenants: t });
74
+ }, () => void 0);
65
75
  },
66
76
  setActiveTenant: (t) => {
67
77
  e({ activeTenant: t });
68
78
  },
69
79
  logout: async () => {
70
- r(), e({
80
+ i(), e({
71
81
  user: null,
72
82
  isAuthenticated: !1,
73
83
  activeTenant: null,
74
84
  tenants: [],
75
85
  sessionPresumed: !1,
76
86
  identitySince: performance.now()
77
- }), await l(), _?.postMessage(g);
87
+ }), await d(), y?.postMessage(v);
78
88
  }
79
- })), g = "signed-out", _ = typeof BroadcastChannel == "function" ? new BroadcastChannel("declarion.auth") : null;
80
- _?.addEventListener("message", (t) => {
81
- t.data === g && !e() && h.getState().isAuthenticated && (r(), window.location.assign("/login"));
82
- }), h.subscribe((e, t) => {
83
- o(n(e)), e.user?.id !== t.user?.id && f.getState().startIdentity();
89
+ })), v = "signed-out", y = typeof BroadcastChannel == "function" ? new BroadcastChannel("declarion.auth") : null;
90
+ y?.addEventListener("message", (t) => {
91
+ t.data === v && !e() && _.getState().isAuthenticated && (r(), window.location.assign("/login"));
92
+ }), _.subscribe((e, t) => {
93
+ c(n(e)), e.user?.id !== t.user?.id && m.getState().startIdentity();
84
94
  });
85
95
  //#endregion
86
- export { h as useAuthStore };
96
+ export { _ as useAuthStore };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@declarion/react",
3
- "version": "0.121.0",
3
+ "version": "0.131.0",
4
4
  "private": false,
5
5
  "license": "MIT",
6
6
  "description": "React SDK for Declarion, the schema-driven business apps platform.",