@bitplanet/deva-sdk 0.8.3 → 0.8.4

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 (2) hide show
  1. package/dist/index.es.js +154 -130
  2. package/package.json +1 -1
package/dist/index.es.js CHANGED
@@ -1,14 +1,14 @@
1
- import { jsx as ee } from "react/jsx-runtime";
2
- import { useState as D, useCallback as u, useEffect as p, useLayoutEffect as re, useRef as C, useContext as ne } from "react";
3
- import { f as B, D as W, u as H, e as Y } from "./config-CL6PuR8K.js";
4
- const te = () => {
1
+ import { jsx as re } from "react/jsx-runtime";
2
+ import { useState as C, useCallback as l, useEffect as y, useLayoutEffect as ne, useRef as $, useContext as te } from "react";
3
+ import { f as W, D as H, u as Y, e as G } from "./config-CL6PuR8K.js";
4
+ const oe = () => {
5
5
  const e = new Uint8Array(32);
6
- return crypto.getRandomValues(e), G(e);
7
- }, G = (e) => btoa(String.fromCharCode(...new Uint8Array(e))).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, ""), oe = async (e) => {
6
+ return crypto.getRandomValues(e), q(e);
7
+ }, q = (e) => btoa(String.fromCharCode(...new Uint8Array(e))).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, ""), se = async (e) => {
8
8
  const n = new TextEncoder().encode(e), o = await crypto.subtle.digest("SHA-256", n);
9
- return G(o);
10
- }, se = (e) => Math.round(Date.now() / 1e3 + Number(e)), ae = (e) => Math.round(Date.now()) / 1e3 + 30 >= e, P = typeof window < "u", ce = (e, r) => {
11
- if (!P)
9
+ return q(o);
10
+ }, ae = (e) => Math.round(Date.now() / 1e3 + Number(e)), B = (e) => Math.round(Date.now()) / 1e3 + 30 >= e, L = typeof window < "u", ce = (e, r) => {
11
+ if (!L)
12
12
  return r;
13
13
  try {
14
14
  const n = sessionStorage.getItem(e);
@@ -17,21 +17,21 @@ const te = () => {
17
17
  return console.error(n), r;
18
18
  }
19
19
  };
20
- function h(e, r) {
21
- const [n, o] = D(
20
+ function m(e, r) {
21
+ const [n, o] = C(
22
22
  () => ce(e, r)
23
- ), i = u(
23
+ ), i = l(
24
24
  (t) => {
25
25
  try {
26
- o(t), P && sessionStorage.setItem(e, JSON.stringify(t));
26
+ o(t), L && sessionStorage.setItem(e, JSON.stringify(t));
27
27
  } catch (s) {
28
28
  console.error(s);
29
29
  }
30
30
  },
31
31
  [e]
32
32
  );
33
- return p(() => {
34
- if (!P)
33
+ return y(() => {
34
+ if (!L)
35
35
  return;
36
36
  const t = sessionStorage.getItem(e);
37
37
  if (t)
@@ -40,8 +40,8 @@ function h(e, r) {
40
40
  } catch (s) {
41
41
  console.error(s);
42
42
  }
43
- }, []), p(() => {
44
- if (!P)
43
+ }, []), y(() => {
44
+ if (!L)
45
45
  return;
46
46
  const t = (s) => {
47
47
  if (s.storageArea === window.sessionStorage && s.key === e)
@@ -59,9 +59,9 @@ function h(e, r) {
59
59
  return window.addEventListener("storage", t, !1), () => window.removeEventListener("storage", t, !1);
60
60
  }, [e, r]), [n, i];
61
61
  }
62
- class y extends Error {
62
+ class E extends Error {
63
63
  }
64
- y.prototype.name = "InvalidTokenError";
64
+ E.prototype.name = "InvalidTokenError";
65
65
  function ie(e) {
66
66
  return decodeURIComponent(atob(e).replace(/(.)/g, (r, n) => {
67
67
  let o = n.charCodeAt(0).toString(16).toUpperCase();
@@ -90,29 +90,29 @@ function de(e) {
90
90
  }
91
91
  function ue(e, r) {
92
92
  if (typeof e != "string")
93
- throw new y("Invalid token specified: must be a string");
93
+ throw new E("Invalid token specified: must be a string");
94
94
  r || (r = {});
95
95
  const n = r.header === !0 ? 0 : 1, o = e.split(".")[n];
96
96
  if (typeof o != "string")
97
- throw new y(`Invalid token specified: missing part #${n + 1}`);
97
+ throw new E(`Invalid token specified: missing part #${n + 1}`);
98
98
  let i;
99
99
  try {
100
100
  i = de(o);
101
101
  } catch (t) {
102
- throw new y(`Invalid token specified: invalid base64 for part #${n + 1} (${t.message})`);
102
+ throw new E(`Invalid token specified: invalid base64 for part #${n + 1} (${t.message})`);
103
103
  }
104
104
  try {
105
105
  return JSON.parse(i);
106
106
  } catch (t) {
107
- throw new y(`Invalid token specified: invalid json for part #${n + 1} (${t.message})`);
107
+ throw new E(`Invalid token specified: invalid json for part #${n + 1} (${t.message})`);
108
108
  }
109
109
  }
110
- const fe = typeof window < "u" ? re : p;
110
+ const fe = typeof window < "u" ? ne : y;
111
111
  function le(e, r, n = [], o, i) {
112
- const t = C(e);
112
+ const t = $(e);
113
113
  fe(() => {
114
114
  t.current = e;
115
- }, n), p(() => {
115
+ }, n), y(() => {
116
116
  if (!r && r !== 0)
117
117
  return;
118
118
  const s = o(() => {
@@ -126,15 +126,15 @@ function le(e, r, n = [], o, i) {
126
126
  function he(e, r, n = []) {
127
127
  le(e, r, n, setInterval, clearInterval);
128
128
  }
129
- const $ = typeof window < "u" && typeof document < "u", A = (...e) => {
129
+ const U = typeof window < "u" && typeof document < "u", N = (...e) => {
130
130
  console.log("[sdk/provider]", ...e);
131
131
  }, pe = (e) => {
132
- const r = Y(e), { data: n, error: o, isLoading: i } = H(
133
- $ ? `${r}/.well-known/openid-configuration` : null
132
+ const r = G(e), { data: n, error: o, isLoading: i } = Y(
133
+ U ? `${r}/.well-known/openid-configuration` : null
134
134
  );
135
135
  return { openIdConfig: n, isOpenIdConfigLoading: i, error: o };
136
136
  }, ge = (e, r) => {
137
- const { data: n, error: o, isLoading: i } = H(
137
+ const { data: n, error: o, isLoading: i } = Y(
138
138
  r ? e : !1,
139
139
  {
140
140
  headers: {
@@ -147,12 +147,12 @@ const $ = typeof window < "u" && typeof document < "u", A = (...e) => {
147
147
  isUserInfoLoading: i,
148
148
  userInfoError: o
149
149
  };
150
- }, we = (e, r, n) => u(async () => {
150
+ }, we = (e, r, n) => l(async () => {
151
151
  if (!r || !n) {
152
- A("no refresh token or token endpoint");
152
+ N("no refresh token or token endpoint");
153
153
  return;
154
154
  }
155
- return B(n, {
155
+ return W(n, {
156
156
  method: "POST",
157
157
  headers: {
158
158
  "Content-Type": "application/x-www-form-urlencoded"
@@ -162,7 +162,7 @@ const $ = typeof window < "u" && typeof document < "u", A = (...e) => {
162
162
  refresh_token: r
163
163
  })
164
164
  });
165
- }, [r, e, n]), me = (e, r, n) => u(async () => {
165
+ }, [r, e, n]), me = (e, r, n) => l(async () => {
166
166
  !n || !e || await fetch(n, {
167
167
  method: "POST",
168
168
  headers: {
@@ -180,11 +180,11 @@ function _e({
180
180
  children: n,
181
181
  env: o
182
182
  }) {
183
- const i = Y(o), { openIdConfig: t, isOpenIdConfigLoading: s, error: U } = pe(o);
184
- U && console.error("[sdk/provider] error fetching openid info: ", U);
185
- const V = C(!1), M = C(!1);
186
- p(() => {
187
- o === "development" && !M.current && (M.current = !0, console.warn(
183
+ const i = G(o), { openIdConfig: t, isOpenIdConfigLoading: s, error: V } = pe(o);
184
+ V && console.error("[sdk/provider] error fetching openid info: ", V);
185
+ const M = $(!1), J = $(!1);
186
+ y(() => {
187
+ o === "development" && !J.current && (J.current = !0, console.warn(
188
188
  `
189
189
  ========================================
190
190
  ⚠️ WARNING: DEVELOPMENT ENVIRONMENT ⚠️
@@ -198,134 +198,158 @@ Please verify your environment settings and use 'staging' or 'production' instea
198
198
  `
199
199
  ));
200
200
  }, [o]);
201
- const [q, l] = h("code_verifier", ""), [I, g] = h(
201
+ const [K, g] = m("code_verifier", ""), [x, k] = m(
202
202
  "auth_in_progress",
203
203
  !1
204
- ), [K, k] = h("id_token", null), [f, v] = h(
204
+ ), [Q, S] = m("id_token", null), [u, T] = m(
205
205
  "access_token",
206
206
  null
207
- ), [w, _] = h(
207
+ ), [h, R] = m(
208
208
  "refresh_token",
209
209
  null
210
- ), [E, S] = h("expires_in", 0), [T, R] = D(!0), L = u(
210
+ ), [w, b] = m("expires_in", 0), [P, p] = C(!0), v = l(
211
211
  (a) => {
212
- let d = E;
213
- v(a.access_token), k(a.id_token);
212
+ let d = w;
213
+ T(a.access_token), S(a.id_token);
214
214
  try {
215
- const O = ue(a.id_token);
216
- d = Math.round(Number(O.exp) - Date.now() / 1e3);
217
- } catch (O) {
215
+ const f = ue(a.id_token);
216
+ d = Math.round(Number(f.exp) - Date.now() / 1e3);
217
+ } catch (f) {
218
218
  console.warn(
219
- `[sdk/provider]: Failed to decode idToken: ${O.message}`
219
+ `[sdk/provider]: Failed to decode idToken: ${f.message}`
220
220
  );
221
221
  }
222
222
  const c = a.expires_in ?? d;
223
- S(se(c)), _(a.refresh_token), l("");
223
+ b(ae(c)), R(a.refresh_token), g("");
224
224
  },
225
225
  [
226
- v,
227
- k,
226
+ T,
228
227
  S,
229
- _,
230
- l,
231
- E
228
+ b,
229
+ R,
230
+ g,
231
+ w
232
232
  ]
233
- ), [Q, N] = D(null), { userInfo: X } = ge(
233
+ ), [X, O] = C(null), { userInfo: Z } = ge(
234
234
  t == null ? void 0 : t.userinfo_endpoint,
235
- T ? null : f
236
- ), m = u(() => {
237
- l(""), g(!1), v(""), k(""), S(0), _("");
235
+ P ? null : u
236
+ ), _ = l(() => {
237
+ g(""), k(!1), T(""), S(""), b(0), R("");
238
238
  }, [
239
- l,
240
239
  g,
241
- v,
242
240
  k,
241
+ T,
243
242
  S,
244
- _
245
- ]), J = me(
246
- f,
243
+ b,
244
+ R
245
+ ]), j = me(
246
+ u,
247
247
  e,
248
248
  t == null ? void 0 : t.revocation_endpoint
249
- ), j = u(async () => {
250
- await J(), m(), N(null);
251
- }, [m, J]), F = u(async () => {
252
- if (m(), g(!0), s)
249
+ ), F = l(async () => {
250
+ await j(), _(), O(null);
251
+ }, [_, j]), A = we(
252
+ e,
253
+ h,
254
+ t == null ? void 0 : t.token_endpoint
255
+ ), z = l(async () => {
256
+ if (u && !B(w))
257
+ return;
258
+ if (h) {
259
+ if (s)
260
+ return;
261
+ p(!0);
262
+ try {
263
+ const f = await A();
264
+ if (f) {
265
+ v(f);
266
+ return;
267
+ }
268
+ } catch (f) {
269
+ console.error("[sdk/provider]: Error refreshing token", f);
270
+ } finally {
271
+ p(!1);
272
+ }
273
+ }
274
+ if (_(), k(!0), s)
253
275
  return;
254
- const a = te(), d = await oe(a);
255
- l(a);
276
+ const a = oe(), d = await se(a);
277
+ g(a);
256
278
  const c = new URL((t == null ? void 0 : t.authorization_endpoint) ?? "");
257
279
  c.searchParams.append("client_id", e), c.searchParams.append("redirect_uri", r), c.searchParams.append("response_type", "code"), c.searchParams.append("code_challenge", d), c.searchParams.append("code_challenge_method", "S256"), window.location.assign(c.toString());
258
280
  }, [
281
+ u,
282
+ w,
283
+ h,
284
+ A,
285
+ v,
286
+ p,
259
287
  e,
260
288
  r,
261
- l,
262
- m,
263
289
  g,
290
+ _,
291
+ k,
264
292
  s,
265
293
  t
266
- ]), z = we(
267
- e,
268
- w,
269
- t == null ? void 0 : t.token_endpoint
270
- ), x = u(
294
+ ]), D = l(
271
295
  async (a = !1) => {
272
- if (!f) {
273
- A("no access token");
296
+ if (!u) {
297
+ N("no access token");
274
298
  return;
275
299
  }
276
- if (!(!ae(E) && !a)) {
277
- if (T && !a) {
278
- A("already refreshing");
300
+ if (!(!B(w) && !a)) {
301
+ if (P && !a) {
302
+ N("already refreshing");
279
303
  return;
280
304
  }
281
- if (!w) {
282
- A("no refresh token");
305
+ if (!h) {
306
+ N("no refresh token");
283
307
  return;
284
308
  }
285
- R(!0);
309
+ p(!0);
286
310
  try {
287
- const d = await z();
288
- d && L(d);
311
+ const d = await A();
312
+ d && v(d);
289
313
  } catch (d) {
290
314
  console.error("[sdk/provider]: Error refreshing token", d);
291
315
  } finally {
292
- R(!1);
316
+ p(!1);
293
317
  }
294
318
  }
295
319
  },
296
320
  [
297
- f,
321
+ u,
322
+ h,
323
+ A,
324
+ v,
325
+ P,
298
326
  w,
299
- z,
300
- L,
301
- T,
302
- E,
303
- R
327
+ p
304
328
  ]
305
- ), Z = 1e4 * Math.random();
329
+ ), ee = 1e4 * Math.random();
306
330
  he(
307
331
  () => {
308
- x();
332
+ D();
309
333
  },
310
- $ && w ? 1e3 + Z : null,
311
- [x]
312
- ), p(() => {
334
+ U && h ? 1e3 + ee : null,
335
+ [D]
336
+ ), y(() => {
313
337
  let a = "";
314
- if ($ && !s) {
315
- if (I) {
316
- R(!1);
338
+ if (U && !s) {
339
+ if (x) {
340
+ p(!1);
317
341
  const d = new URLSearchParams(window.location.search);
318
342
  if (a = d.get("code") ?? "", !a) {
319
343
  const c = d.get("error_description") ?? "An unknown error occurred";
320
344
  console.error(
321
345
  `[sdk/provider]: ${c}
322
346
  Expected to find a '?code=' parameter in the URL but found none`
323
- ), N(c), m();
347
+ ), O(c), _();
324
348
  return;
325
349
  }
326
- if (g(!1), V.current)
350
+ if (k(!1), M.current)
327
351
  return;
328
- V.current = !0, B((t == null ? void 0 : t.token_endpoint) ?? "", {
352
+ M.current = !0, W((t == null ? void 0 : t.token_endpoint) ?? "", {
329
353
  method: "POST",
330
354
  headers: {
331
355
  "Content-Type": "application/x-www-form-urlencoded"
@@ -334,49 +358,49 @@ Expected to find a '?code=' parameter in the URL but found none`
334
358
  client_id: e,
335
359
  redirect_uri: r,
336
360
  code: a,
337
- code_verifier: q,
361
+ code_verifier: K,
338
362
  grant_type: "authorization_code"
339
363
  })
340
- }).then(L).catch((c) => {
341
- console.error("[sdk/provider]: error in fetching", c), N("An error occurred while fetching tokens");
364
+ }).then(v).catch((c) => {
365
+ console.error("[sdk/provider]: error in fetching", c), O("An error occurred while fetching tokens");
342
366
  }), window.history.replaceState({}, document.title, window.location.pathname);
343
367
  }
344
- x(!0);
368
+ D(!0);
345
369
  }
346
370
  }, [s]);
347
- const b = {
348
- isAuthenticated: !!f && !T,
349
- accessToken: f,
350
- user: X ?? null,
351
- login: F,
352
- logout: j,
353
- isReady: !s && !I
371
+ const I = {
372
+ isAuthenticated: !!u && !P,
373
+ accessToken: u,
374
+ user: Z ?? null,
375
+ login: z,
376
+ logout: F,
377
+ isReady: !s && !x
354
378
  };
355
- return /* @__PURE__ */ ee(
356
- W.Provider,
379
+ return /* @__PURE__ */ re(
380
+ H.Provider,
357
381
  {
358
382
  value: {
359
383
  clientId: e,
360
384
  redirectUri: r,
361
385
  env: o,
362
- login: F,
363
- logout: j,
364
- authInProgress: I,
365
- authError: Q,
366
- idToken: K,
367
- accessToken: f,
368
- refreshToken: w,
369
- user: b.user,
370
- isAuthenticated: b.isAuthenticated,
371
- isReady: b.isReady,
386
+ login: z,
387
+ logout: F,
388
+ authInProgress: x,
389
+ authError: X,
390
+ idToken: Q,
391
+ accessToken: u,
392
+ refreshToken: h,
393
+ user: I.user,
394
+ isAuthenticated: I.isAuthenticated,
395
+ isReady: I.isReady,
372
396
  url: i
373
397
  },
374
- children: typeof n == "function" ? n(b) : n
398
+ children: typeof n == "function" ? n(I) : n
375
399
  }
376
400
  );
377
401
  }
378
402
  const Ee = () => {
379
- const e = ne(W);
403
+ const e = te(H);
380
404
  if (!e)
381
405
  throw new Error("useDeva must be used within a DevaProvider");
382
406
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bitplanet/deva-sdk",
3
- "version": "0.8.3",
3
+ "version": "0.8.4",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/Bitplanet-L1/deva-sdk.git"