@bitplanet/deva-sdk 0.8.1 → 0.8.2

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