tempest-react-sdk 0.83.0 → 0.84.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.
Files changed (62) hide show
  1. package/README.md +1 -0
  2. package/dist/auth/create-tempest-auth.cjs +1 -1
  3. package/dist/auth/create-tempest-auth.cjs.map +1 -1
  4. package/dist/auth/create-tempest-auth.js +150 -105
  5. package/dist/auth/create-tempest-auth.js.map +1 -1
  6. package/dist/http/resumable-upload.cjs +1 -1
  7. package/dist/http/resumable-upload.cjs.map +1 -1
  8. package/dist/http/resumable-upload.js +1 -1
  9. package/dist/http/resumable-upload.js.map +1 -1
  10. package/dist/http/upload-with-progress.cjs +1 -1
  11. package/dist/http/upload-with-progress.cjs.map +1 -1
  12. package/dist/http/upload-with-progress.js +47 -39
  13. package/dist/http/upload-with-progress.js.map +1 -1
  14. package/dist/payments/PixCheckout.cjs +2 -0
  15. package/dist/payments/PixCheckout.cjs.map +1 -0
  16. package/dist/payments/PixCheckout.js +171 -0
  17. package/dist/payments/PixCheckout.js.map +1 -0
  18. package/dist/payments/PixCheckout.module.cjs +3 -0
  19. package/dist/payments/PixCheckout.module.cjs.map +1 -0
  20. package/dist/payments/PixCheckout.module.js +22 -0
  21. package/dist/payments/PixCheckout.module.js.map +1 -0
  22. package/dist/payments/format.cjs +2 -0
  23. package/dist/payments/format.cjs.map +1 -0
  24. package/dist/payments/format.js +9 -0
  25. package/dist/payments/format.js.map +1 -0
  26. package/dist/payments/mercado-pago.cjs +2 -0
  27. package/dist/payments/mercado-pago.cjs.map +1 -0
  28. package/dist/payments/mercado-pago.js +27 -0
  29. package/dist/payments/mercado-pago.js.map +1 -0
  30. package/dist/payments/outcome.cjs +2 -0
  31. package/dist/payments/outcome.cjs.map +1 -0
  32. package/dist/payments/outcome.js +108 -0
  33. package/dist/payments/outcome.js.map +1 -0
  34. package/dist/payments/types.cjs +2 -0
  35. package/dist/payments/types.cjs.map +1 -0
  36. package/dist/payments/types.js +16 -0
  37. package/dist/payments/types.js.map +1 -0
  38. package/dist/payments/use-mercado-pago-card.cjs +2 -0
  39. package/dist/payments/use-mercado-pago-card.cjs.map +1 -0
  40. package/dist/payments/use-mercado-pago-card.js +120 -0
  41. package/dist/payments/use-mercado-pago-card.js.map +1 -0
  42. package/dist/payments/use-pix-charge.cjs +2 -0
  43. package/dist/payments/use-pix-charge.cjs.map +1 -0
  44. package/dist/payments/use-pix-charge.js +63 -0
  45. package/dist/payments/use-pix-charge.js.map +1 -0
  46. package/dist/query/offline-persistence.cjs +1 -1
  47. package/dist/query/offline-persistence.cjs.map +1 -1
  48. package/dist/query/offline-persistence.js +3 -3
  49. package/dist/query/offline-persistence.js.map +1 -1
  50. package/dist/styles/PixCheckout.css +2 -0
  51. package/dist/styles/component/PixCheckout.css +18 -0
  52. package/dist/styles/manifest.json +6 -0
  53. package/dist/styles/payments.css +18 -0
  54. package/dist/styles.css +1 -1
  55. package/dist/sw/cache.cjs +1 -1
  56. package/dist/sw/cache.cjs.map +1 -1
  57. package/dist/sw/cache.js +1 -1
  58. package/dist/sw/cache.js.map +1 -1
  59. package/dist/tempest-react-sdk.cjs +1 -1
  60. package/dist/tempest-react-sdk.d.ts +677 -23
  61. package/dist/tempest-react-sdk.js +104 -97
  62. package/package.json +1 -1
@@ -3,10 +3,11 @@ import { SessionSupersededError as t } from "../http/errors.js";
3
3
  import { createApiClient as n } from "../http/api-client.js";
4
4
  import { bindSessionIdentity as r } from "./session-identity.js";
5
5
  import { createAuthStore as i } from "./create-auth-store.js";
6
- import { createRefreshQueue as a } from "./refresh-queue.js";
6
+ import { decodeJWT as a } from "./jwt.js";
7
+ import { createRefreshQueue as o } from "./refresh-queue.js";
7
8
  //#region src/auth/create-tempest-auth.ts
8
- var o = 1e4, s = 1e4;
9
- function c(e, t) {
9
+ var s = 1e4, c = 1e4;
10
+ function l(e, t) {
10
11
  return e.length === 0 ? Promise.resolve() : new Promise((n) => {
11
12
  let r = setTimeout(n, t);
12
13
  Promise.allSettled(e).then(() => {
@@ -14,13 +15,22 @@ function c(e, t) {
14
15
  });
15
16
  });
16
17
  }
17
- function l() {
18
+ function u() {
18
19
  let e = globalThis.crypto;
19
20
  return typeof e?.randomUUID == "function" ? e.randomUUID() : `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;
20
21
  }
21
- var u = /* @__PURE__ */ new Set();
22
+ var d = /* @__PURE__ */ new Set();
22
23
  function ee(t) {
23
- e() && !u.has(t) && (u.add(t), console.warn("[tempest-react-sdk] createTempestAuth: withCredentials is set without logoutPath. logout() cannot clear the HttpOnly refresh cookie, so the next refresh() — the boot restore — signs the same account back in. Pass logoutPath (the backend's cookie logout route) so the server revokes it."));
24
+ e() && !d.has(t) && (d.add(t), console.warn("[tempest-react-sdk] createTempestAuth: withCredentials is set without logoutPath. logout() cannot clear the HttpOnly refresh cookie, so the next refresh() — the boot restore — signs the same account back in. Pass logoutPath (the backend's cookie logout route) so the server revokes it."));
25
+ }
26
+ function f(e) {
27
+ if (!e) return null;
28
+ try {
29
+ let { sub: t } = a(e).payload;
30
+ return typeof t == "string" && t !== "" ? t : null;
31
+ } catch {
32
+ return null;
33
+ }
24
34
  }
25
35
  function te(e) {
26
36
  let t = e ?? {};
@@ -29,15 +39,15 @@ function te(e) {
29
39
  refreshToken: t.refresh_token
30
40
  };
31
41
  }
32
- function d(u) {
33
- let { baseURL: d, prefix: f, loginPath: ne = "/api/auth/login", refreshPath: re = "/api/auth/refresh", logoutPath: p, onLogoutError: ie, mePath: m, storeName: h = "tempest-auth", storage: g = "local", withCredentials: _ = !1, csrf: ae, fetcher: v, parseTokens: y = te, parseUser: oe, refreshBody: b = (e) => e ? { refresh_token: e } : void 0, retry: se, redirectTo: x, onSessionEnd: S } = u;
34
- _ && !p && ee(h);
42
+ function p(a) {
43
+ let { baseURL: d, prefix: p, loginPath: ne = "/api/auth/login", refreshPath: m = "/api/auth/refresh", logoutPath: h, onLogoutError: re, mePath: ie, storeName: g = "tempest-auth", storage: _ = "local", withCredentials: v = !1, csrf: ae, fetcher: y, parseTokens: b = te, parseUser: oe, refreshBody: x = (e) => e ? { refresh_token: e } : void 0, retry: se, redirectTo: S, onSessionEnd: ce } = a;
44
+ v && !h && ee(g);
35
45
  let C = i({
36
- name: h,
37
- storage: g
38
- }), w = `${h}-refresh`;
46
+ name: g,
47
+ storage: _
48
+ }), w = `${g}-refresh`;
39
49
  function T() {
40
- return typeof window > "u" ? null : g === "session" ? window.sessionStorage : window.localStorage;
50
+ return typeof window > "u" ? null : _ === "session" ? window.sessionStorage : window.localStorage;
41
51
  }
42
52
  function E() {
43
53
  return T()?.getItem(w) ?? null;
@@ -48,33 +58,33 @@ function d(u) {
48
58
  let t = T();
49
59
  t && (e ? t.setItem(w, e) : t.removeItem(w));
50
60
  }
51
- let k = `${h}-lineage`, ce = `tempest-react-sdk:${h}:refresh`, A = T()?.getItem(k) ?? null;
61
+ let k = `${g}-lineage`, le = `tempest-react-sdk:${g}:refresh`, A = T()?.getItem(k) ?? null;
52
62
  function j(e) {
53
63
  A = e;
54
64
  let t = T();
55
65
  t && (e ? t.setItem(k, e) : t.removeItem(k));
56
66
  }
57
67
  let M = () => C.getState(), N = () => M().token;
58
- async function le() {
59
- if (!m) return M().user;
68
+ async function ue() {
69
+ if (!ie) return M().user;
60
70
  let e = P, n;
61
71
  try {
62
- n = await Q.get(m, { skipAuthRetry: !0 });
72
+ n = await Q.get(ie, { skipAuthRetry: !0 });
63
73
  } catch (n) {
64
74
  throw P === e ? n : new t();
65
75
  }
66
76
  if (P !== e || !Z().continues) throw new t();
67
77
  return M().setUser(n), n;
68
78
  }
69
- let P = 0, ue = Promise.resolve(), F = 0, I = /* @__PURE__ */ new Set(), L = /* @__PURE__ */ new Set(), R = /* @__PURE__ */ new Set(), z = /* @__PURE__ */ new Set();
79
+ let P = 0, de = Promise.resolve(), F = 0, I = /* @__PURE__ */ new Set(), L = /* @__PURE__ */ new Set(), R = /* @__PURE__ */ new Set(), z = /* @__PURE__ */ new Set();
70
80
  function B(e) {
71
81
  let t = e.then(() => void 0, () => void 0);
72
82
  return R.add(t), t.then(() => R.delete(t)), e;
73
83
  }
74
- function de(t) {
84
+ function fe(t) {
75
85
  e() && console.warn("[tempest-react-sdk] createTempestAuth: logoutPath revocation failed", t);
76
86
  try {
77
- ie?.(t);
87
+ re?.(t);
78
88
  } catch {
79
89
  return;
80
90
  }
@@ -84,116 +94,150 @@ function d(u) {
84
94
  try {
85
95
  await e();
86
96
  } catch (e) {
87
- de(e);
97
+ fe(e);
88
98
  }
89
99
  })();
90
100
  return L.add(t), t.then(() => L.delete(t)), t;
91
101
  }
92
102
  function H(e, t) {
93
- p && !_ && t && V(() => U(e).post(p, { body: b(t) }));
103
+ h && !v && t && V(() => U(e).post(h, { body: x(t) }));
94
104
  }
95
105
  function U(e) {
96
106
  return n({
97
107
  baseURL: d,
98
- prefix: f,
99
- withCredentials: _,
108
+ prefix: p,
109
+ withCredentials: v,
100
110
  csrf: ae,
101
- fetcher: v,
111
+ fetcher: y,
102
112
  getToken: () => e
103
113
  });
104
114
  }
105
115
  function W() {
106
- P += 1, K = a(G);
116
+ P += 1, q = o(G);
107
117
  }
108
118
  async function G() {
109
119
  let e = N(), t = P;
110
120
  z.add(t);
121
+ let n;
111
122
  try {
112
- await fe(() => pe(e));
123
+ n = await K(() => pe(e));
113
124
  } finally {
114
125
  z.delete(t);
115
126
  }
127
+ n && await J(Y(n));
116
128
  }
117
- async function fe(e) {
118
- let t = g === "local" && typeof navigator < "u" ? navigator.locks : void 0;
129
+ async function K(e) {
130
+ let t = _ === "local" && typeof navigator < "u" ? navigator.locks : void 0;
119
131
  if (!t) return e();
120
- let n = new AbortController(), r = setTimeout(() => n.abort(), s), i = !1;
132
+ let n = new AbortController(), r = setTimeout(() => n.abort(), c), i = !1, a;
121
133
  try {
122
- await t.request(ce, { signal: n.signal }, async () => {
123
- i = !0, clearTimeout(r), await e();
134
+ await t.request(le, { signal: n.signal }, async () => {
135
+ i = !0, clearTimeout(r), a = await e();
124
136
  });
125
137
  } catch (t) {
126
138
  if (i) throw t;
127
- clearTimeout(r), await e();
139
+ return clearTimeout(r), e();
128
140
  }
141
+ return a;
129
142
  }
130
143
  async function pe(e) {
131
144
  let n = e !== null || M().user !== null || D !== null, r = Z();
132
- if (!n && N() !== null) return;
145
+ if (!n && N() !== null) return null;
133
146
  if (!r.continues) throw new t();
134
- if (N() !== e) return;
147
+ if (N() !== e) return null;
135
148
  let i = P, a;
136
149
  try {
137
- a = await B(Q.post(re, {
138
- body: b(E()),
150
+ a = await B(Q.post(m, {
151
+ body: x(E()),
139
152
  skipAuth: !0
140
153
  }));
141
154
  } catch (e) {
142
155
  throw P === i ? e : new t();
143
156
  }
144
- let { token: o, refreshToken: s } = y(a);
157
+ let { token: o, refreshToken: s } = b(a);
145
158
  if (P !== i || !Z().continues) throw H(o ?? null, s), new t();
146
- n || j(l()), M().setToken(o), s && O(s);
159
+ let c = f(e), l = f(o);
160
+ return c !== null && l !== null && c !== l ? {
161
+ data: a,
162
+ token: o,
163
+ refreshToken: s
164
+ } : (n || j(u()), M().setToken(o), s && O(s), null);
147
165
  }
148
- let K = a(G);
149
- function q(e) {
166
+ let q = o(G);
167
+ function me(e) {
150
168
  let t = N();
151
169
  return t === null ? !1 : e.token === null || e.token !== t;
152
170
  }
153
- function J() {
171
+ function he() {
154
172
  return N() === null && E() === null;
155
173
  }
156
- async function me() {
174
+ async function ge() {
157
175
  if (!(I.size > 0 && F === 0 && (await Promise.allSettled([...I]), N() !== null))) throw Error("anonymous 401 with no session");
158
176
  }
159
- async function he(e) {
177
+ async function _e(e) {
160
178
  if (e.epoch !== null && e.epoch !== P) throw new t();
161
- if (q(e)) {
179
+ if (me(e)) {
162
180
  if (e.token === null) throw Error("anonymous 401 under a live session");
163
- } else return e.token === null && J() ? z.has(P) ? K() : me() : K();
181
+ return;
182
+ }
183
+ if (e.token === null && he()) return z.has(P) ? q() : ge();
184
+ let n = P;
185
+ if (await q(), P !== n) throw new t();
164
186
  }
165
- async function ge(e, n) {
187
+ async function ve(e, n) {
166
188
  if (n.epoch !== null && n.epoch !== P || !Z().continues) throw new t();
167
- q(n) || n.token === null && J() || await Se();
189
+ me(n) || n.token === null && he() || await De();
168
190
  }
169
- async function _e(e) {
170
- L.size > 0 && await c([...L], o);
171
- let t = ve(e);
172
- I.add(t);
191
+ async function ye(e) {
192
+ return L.size > 0 && await l([...L], s), J(Se(e));
193
+ }
194
+ async function be() {
195
+ return L.size > 0 && await l([...L], s), J(xe());
196
+ }
197
+ async function J(e) {
198
+ I.add(e);
173
199
  try {
174
- return await t;
200
+ return await e;
175
201
  } finally {
176
- I.delete(t);
202
+ I.delete(e);
177
203
  }
178
204
  }
179
- async function ve(e) {
205
+ async function xe() {
206
+ let e = P, n = await K(() => B(Q.post(m, {
207
+ body: x(E()),
208
+ skipAuth: !0
209
+ }))), { token: r, refreshToken: i } = b(n);
210
+ if (P !== e) throw H(r ?? null, i), new t();
211
+ return Y({
212
+ data: n,
213
+ token: r,
214
+ refreshToken: i
215
+ });
216
+ }
217
+ async function Se(e) {
180
218
  let n = P, r = await B(Q.post(ne, {
181
219
  body: e,
182
220
  skipAuth: !0
183
- })), { token: i, refreshToken: a } = y(r);
221
+ })), { token: i, refreshToken: a } = b(r);
184
222
  if (P !== n) throw H(i ?? null, a), new t();
185
- H(N(), E() ?? void 0);
186
- let { done: o } = Y("switch"), s = P;
187
- if (await o.catch(() => void 0), P !== s) throw new t();
188
- j(l()), M().setToken(i), O(a ?? null);
189
- let c = oe?.(r);
190
- return c == null ? le() : (M().setUser(c), c);
191
- }
192
- function Y(e) {
223
+ return H(N(), E() ?? void 0), Y({
224
+ data: r,
225
+ token: i,
226
+ refreshToken: a
227
+ });
228
+ }
229
+ async function Y(e) {
230
+ let { done: n } = X("switch"), r = P;
231
+ if (await n.catch(() => void 0), P !== r) throw new t();
232
+ j(u()), M().setToken(e.token), O(e.refreshToken ?? null);
233
+ let i = oe?.(e.data);
234
+ return i == null ? ue() : (M().setUser(i), i);
235
+ }
236
+ function X(e) {
193
237
  let { user: t, token: n } = M(), r = n !== null || t !== null || E() !== null;
194
238
  return (r || e !== "expired") && W(), M().logout(), O(null), j(null), r ? {
195
239
  live: r,
196
- done: X({
240
+ done: Ce({
197
241
  reason: e,
198
242
  user: t,
199
243
  token: n,
@@ -201,16 +245,16 @@ function d(u) {
201
245
  })
202
246
  } : {
203
247
  live: r,
204
- done: e === "switch" || F === 0 ? ue : Promise.resolve()
248
+ done: e === "switch" || F === 0 ? de : Promise.resolve()
205
249
  };
206
250
  }
207
- function X(e) {
208
- if (!S) return Promise.resolve();
251
+ function Ce(e) {
252
+ if (!ce) return Promise.resolve();
209
253
  let t = U(e.token);
210
254
  F += 1;
211
255
  let n = (async () => {
212
256
  try {
213
- await S({
257
+ await ce({
214
258
  ...e,
215
259
  api: t
216
260
  });
@@ -218,12 +262,12 @@ function d(u) {
218
262
  --F;
219
263
  }
220
264
  })();
221
- return ue = n.then(() => void 0, () => void 0), n.then(() => void 0);
265
+ return de = n.then(() => void 0, () => void 0), n.then(() => void 0);
222
266
  }
223
- function ye() {
267
+ function we() {
224
268
  let e = T();
225
269
  if (!e) return null;
226
- let t = e.getItem(w), n = e.getItem(k), r = e.getItem(h);
270
+ let t = e.getItem(w), n = e.getItem(k), r = e.getItem(g);
227
271
  if (!r) return {
228
272
  user: null,
229
273
  token: null,
@@ -248,7 +292,7 @@ function d(u) {
248
292
  }
249
293
  }
250
294
  function Z() {
251
- let e = ye(), { user: t, token: n } = M();
295
+ let e = we(), { user: t, token: n } = M();
252
296
  if (e === null) return {
253
297
  continues: !0,
254
298
  done: Promise.resolve()
@@ -267,7 +311,7 @@ function d(u) {
267
311
  continues: a,
268
312
  done: Promise.resolve()
269
313
  };
270
- let s = X({
314
+ let s = Ce({
271
315
  reason: e.token === null ? "logout" : "switch",
272
316
  user: t,
273
317
  token: n,
@@ -278,58 +322,59 @@ function d(u) {
278
322
  done: s
279
323
  };
280
324
  }
281
- function be() {
282
- if (!p) return null;
283
- let e = p, { token: t, user: n } = M(), r = E();
284
- if (!_ && t === null && n === null && r === null) return null;
285
- let i = _ ? [...R] : [];
286
- return V(async () => (await c(i, o), U(t).post(e, { body: b(r) })));
325
+ function Te() {
326
+ if (!h) return null;
327
+ let e = h, { token: t, user: n } = M(), r = E();
328
+ if (!v && t === null && n === null && r === null) return null;
329
+ let i = v ? [...R] : [];
330
+ return V(async () => (await l(i, s), U(t).post(e, { body: x(r) })));
287
331
  }
288
- function xe() {
332
+ function Ee() {
289
333
  let e = Z();
290
334
  if (!e.continues) return e.done;
291
- let t = be(), { done: n } = Y("logout");
335
+ let t = Te(), { done: n } = X("logout");
292
336
  return t ? Promise.all([t, n]).then(() => void 0) : n;
293
337
  }
294
- async function Se() {
295
- let { live: e, done: t } = Y("expired");
296
- await t.catch(() => void 0), e && x && typeof window < "u" && window.location.assign(x);
338
+ async function De() {
339
+ let { live: e, done: t } = X("expired");
340
+ await t.catch(() => void 0), e && S && typeof window < "u" && window.location.assign(S);
297
341
  }
298
342
  let Q = n({
299
343
  baseURL: d,
300
- prefix: f,
301
- withCredentials: _,
344
+ prefix: p,
345
+ withCredentials: v,
302
346
  csrf: ae,
303
- fetcher: v,
347
+ fetcher: y,
304
348
  getToken: N,
305
349
  getSessionEpoch: () => P,
306
- refresh: he,
350
+ refresh: _e,
307
351
  retry: se,
308
- onUnauthorized: ge
309
- }), Ce = () => K(), $ = null, we = () => N() === null ? null : A === null ? ($?.epoch !== P && ($ = {
352
+ onUnauthorized: ve
353
+ }), Oe = () => q(), $ = null, ke = () => N() === null ? null : A === null ? ($?.epoch !== P && ($ = {
310
354
  epoch: P,
311
- key: l()
312
- }), $.key) : A, Te = {
313
- current: we,
355
+ key: u()
356
+ }), $.key) : A, Ae = {
357
+ current: ke,
314
358
  subscribe: (e) => C.subscribe(e)
315
359
  };
316
- r(Q, Te), r(N, Te);
317
- function Ee(e) {
318
- e.storageArea === T() && (e.key === null || e.key === h || e.key === w || e.key === k) && Z();
360
+ r(Q, Ae), r(N, Ae);
361
+ function je(e) {
362
+ e.storageArea === T() && (e.key === null || e.key === g || e.key === w || e.key === k) && Z();
319
363
  }
320
- let De = typeof window > "u" ? null : window;
321
- return De?.addEventListener("storage", Ee), {
364
+ let Me = typeof window > "u" ? null : window;
365
+ return Me?.addEventListener("storage", je), {
322
366
  useAuthStore: C,
323
367
  api: Q,
324
- login: _e,
325
- logout: xe,
326
- refresh: Ce,
368
+ login: ye,
369
+ logout: Ee,
370
+ refresh: Oe,
371
+ restore: be,
327
372
  getToken: N,
328
- sessionKey: we,
329
- dispose: () => De?.removeEventListener("storage", Ee)
373
+ sessionKey: ke,
374
+ dispose: () => Me?.removeEventListener("storage", je)
330
375
  };
331
376
  }
332
377
  //#endregion
333
- export { d as createTempestAuth };
378
+ export { p as createTempestAuth };
334
379
 
335
380
  //# sourceMappingURL=create-tempest-auth.js.map