@bitplanet/deva-sdk 0.8.1

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.
@@ -0,0 +1,378 @@
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";
4
+ const te = () => {
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) => {
11
+ if (!P)
12
+ return r;
13
+ try {
14
+ const t = sessionStorage.getItem(e);
15
+ return t ? JSON.parse(t) : r;
16
+ } catch (t) {
17
+ return console.error(t), r;
18
+ }
19
+ };
20
+ function h(e, r) {
21
+ const [t, n] = D(
22
+ () => ae(e, r)
23
+ ), i = u(
24
+ (o) => {
25
+ try {
26
+ n(o), P && sessionStorage.setItem(e, JSON.stringify(o));
27
+ } catch (s) {
28
+ console.error(s);
29
+ }
30
+ },
31
+ [e]
32
+ );
33
+ return k(() => {
34
+ if (!P)
35
+ return;
36
+ const o = sessionStorage.getItem(e);
37
+ if (o)
38
+ try {
39
+ n(JSON.parse(o));
40
+ } catch (s) {
41
+ console.error(s);
42
+ }
43
+ }, []), k(() => {
44
+ if (!P)
45
+ return;
46
+ const o = (s) => {
47
+ if (s.storageArea === window.sessionStorage && s.key === e)
48
+ if (s.newValue === null)
49
+ n(r);
50
+ else
51
+ try {
52
+ n(JSON.parse(s.newValue));
53
+ } catch {
54
+ console.warn(
55
+ `Failed to handle storageEvent's newValue='${s.newValue}' for key '${e}'`
56
+ );
57
+ }
58
+ };
59
+ return window.addEventListener("storage", o, !1), () => window.removeEventListener("storage", o, !1);
60
+ }, [e, r]), [t, i];
61
+ }
62
+ class m extends Error {
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;
69
+ }));
70
+ }
71
+ function ie(e) {
72
+ let r = e.replace(/-/g, "+").replace(/_/g, "/");
73
+ switch (r.length % 4) {
74
+ case 0:
75
+ break;
76
+ case 2:
77
+ r += "==";
78
+ break;
79
+ case 3:
80
+ r += "=";
81
+ break;
82
+ default:
83
+ throw new Error("base64 string is not of the correct length");
84
+ }
85
+ try {
86
+ return ce(r);
87
+ } catch {
88
+ return atob(r);
89
+ }
90
+ }
91
+ function de(e, r) {
92
+ if (typeof e != "string")
93
+ throw new m("Invalid token specified: must be a string");
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}`);
98
+ let i;
99
+ try {
100
+ i = ie(n);
101
+ } catch (o) {
102
+ throw new m(`Invalid token specified: invalid base64 for part #${t + 1} (${o.message})`);
103
+ }
104
+ try {
105
+ return JSON.parse(i);
106
+ } catch (o) {
107
+ throw new m(`Invalid token specified: invalid json for part #${t + 1} (${o.message})`);
108
+ }
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(() => {
116
+ if (!r && r !== 0)
117
+ return;
118
+ const s = n(() => {
119
+ o.current();
120
+ }, r);
121
+ return () => {
122
+ i(s);
123
+ };
124
+ }, [r]);
125
+ }
126
+ function le(e, r, t = []) {
127
+ fe(e, r, t, setInterval, clearInterval);
128
+ }
129
+ const O = typeof window < "u" && typeof document < "u", x = (...e) => {
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
134
+ );
135
+ return { openIdConfig: t, isOpenIdConfigLoading: i, error: n };
136
+ }, pe = (e, r) => {
137
+ const { data: t, error: n, isLoading: i } = H(
138
+ r ? e : !1,
139
+ {
140
+ headers: {
141
+ Authorization: `Bearer ${r}`
142
+ }
143
+ }
144
+ );
145
+ return {
146
+ userInfo: t,
147
+ isUserInfoLoading: i,
148
+ userInfoError: n
149
+ };
150
+ }, ge = (e, r, t) => u(async () => {
151
+ if (!r || !t) {
152
+ x("no refresh token or token endpoint");
153
+ return;
154
+ }
155
+ return z(t, {
156
+ method: "POST",
157
+ headers: {
158
+ "Content-Type": "application/x-www-form-urlencoded"
159
+ },
160
+ body: new URLSearchParams({
161
+ client_id: e,
162
+ refresh_token: r
163
+ })
164
+ });
165
+ }, [r, e, t]), we = (e, r, t) => u(async () => {
166
+ !t || !e || await fetch(t, {
167
+ method: "POST",
168
+ headers: {
169
+ "Content-Type": "application/json"
170
+ },
171
+ body: JSON.stringify({
172
+ access_token: e,
173
+ client_id: r
174
+ })
175
+ });
176
+ }, [e, r, t]);
177
+ function _e({
178
+ clientId: e,
179
+ redirectUri: r,
180
+ children: t,
181
+ env: n
182
+ }) {
183
+ const i = W(n), { openIdConfig: o, isOpenIdConfigLoading: s, error: U } = he(n);
184
+ U && console.error("[sdk/provider] error fetching openid info: ", U);
185
+ const N = M(!1), [G, l] = h("code_verifier", ""), [A, p] = h(
186
+ "auth_in_progress",
187
+ !1
188
+ ), [K, y] = h("id_token", null), [f, _] = h(
189
+ "access_token",
190
+ null
191
+ ), [g, v] = h(
192
+ "refresh_token",
193
+ null
194
+ ), [S, R] = h("expires_in", 0), [T, b] = D(!0), I = u(
195
+ (a) => {
196
+ let d = S;
197
+ _(a.access_token), y(a.id_token);
198
+ try {
199
+ const $ = de(a.id_token);
200
+ d = Math.round(Number($.exp) - Date.now() / 1e3);
201
+ } catch ($) {
202
+ console.warn(
203
+ `[sdk/provider]: Failed to decode idToken: ${$.message}`
204
+ );
205
+ }
206
+ const c = a.expires_in ?? d;
207
+ R(ne(c)), v(a.refresh_token), l("");
208
+ },
209
+ [
210
+ _,
211
+ y,
212
+ R,
213
+ v,
214
+ l,
215
+ S
216
+ ]
217
+ ), [Q, L] = D(null), { userInfo: X } = pe(
218
+ o == null ? void 0 : o.userinfo_endpoint,
219
+ T ? null : f
220
+ ), w = u(() => {
221
+ l(""), p(!1), _(""), y(""), R(0), v("");
222
+ }, [
223
+ l,
224
+ p,
225
+ _,
226
+ y,
227
+ R,
228
+ v
229
+ ]), V = we(
230
+ f,
231
+ 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)
237
+ return;
238
+ const a = te(), d = await oe(a);
239
+ l(a);
240
+ const c = new URL((o == null ? void 0 : o.authorization_endpoint) ?? "");
241
+ 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
+ }, [
243
+ e,
244
+ r,
245
+ l,
246
+ w,
247
+ p,
248
+ s,
249
+ o
250
+ ]), F = ge(
251
+ e,
252
+ g,
253
+ o == null ? void 0 : o.token_endpoint
254
+ ), C = u(
255
+ async (a = !1) => {
256
+ if (!f) {
257
+ x("no access token");
258
+ return;
259
+ }
260
+ if (!(!se(S) && !a)) {
261
+ if (T && !a) {
262
+ x("already refreshing");
263
+ return;
264
+ }
265
+ if (!g) {
266
+ x("no refresh token");
267
+ return;
268
+ }
269
+ b(!0);
270
+ try {
271
+ const d = await F();
272
+ d && I(d);
273
+ } catch (d) {
274
+ console.error("[sdk/provider]: Error refreshing token", d);
275
+ } finally {
276
+ b(!1);
277
+ }
278
+ }
279
+ },
280
+ [
281
+ f,
282
+ g,
283
+ F,
284
+ I,
285
+ T,
286
+ S,
287
+ b
288
+ ]
289
+ ), Y = 1e4 * Math.random();
290
+ le(
291
+ () => {
292
+ C();
293
+ },
294
+ O && g ? 1e3 + Y : null,
295
+ [C]
296
+ ), k(() => {
297
+ let a = "";
298
+ if (O && !s) {
299
+ if (A) {
300
+ b(!1);
301
+ const d = new URLSearchParams(window.location.search);
302
+ if (a = d.get("code") ?? "", !a) {
303
+ const c = d.get("error_description") ?? "An unknown error occurred";
304
+ console.error(
305
+ `[sdk/provider]: ${c}
306
+ Expected to find a '?code=' parameter in the URL but found none`
307
+ ), L(c), w();
308
+ return;
309
+ }
310
+ if (p(!1), N.current)
311
+ return;
312
+ N.current = !0, z((o == null ? void 0 : o.token_endpoint) ?? "", {
313
+ method: "POST",
314
+ headers: {
315
+ "Content-Type": "application/x-www-form-urlencoded"
316
+ },
317
+ body: new URLSearchParams({
318
+ client_id: e,
319
+ redirect_uri: r,
320
+ code: a,
321
+ code_verifier: G,
322
+ grant_type: "authorization_code"
323
+ })
324
+ }).then(I).catch((c) => {
325
+ console.error("[sdk/provider]: error in fetching", c), L("An error occurred while fetching tokens");
326
+ }), window.history.replaceState({}, document.title, window.location.pathname);
327
+ }
328
+ C(!0);
329
+ }
330
+ }, [s]);
331
+ const E = {
332
+ isAuthenticated: !!f && !T,
333
+ accessToken: f,
334
+ user: X ?? null,
335
+ login: j,
336
+ logout: J,
337
+ isReady: !s && !A
338
+ };
339
+ return /* @__PURE__ */ Z(
340
+ B.Provider,
341
+ {
342
+ value: {
343
+ clientId: e,
344
+ redirectUri: r,
345
+ env: n,
346
+ login: j,
347
+ logout: J,
348
+ authInProgress: A,
349
+ authError: Q,
350
+ idToken: K,
351
+ accessToken: f,
352
+ refreshToken: g,
353
+ user: E.user,
354
+ isAuthenticated: E.isAuthenticated,
355
+ isReady: E.isReady,
356
+ url: i
357
+ },
358
+ children: typeof t == "function" ? t(E) : t
359
+ }
360
+ );
361
+ }
362
+ const ve = () => {
363
+ const e = re(B);
364
+ if (!e)
365
+ throw new Error("useDeva must be used within a DevaProvider");
366
+ return {
367
+ isAuthenticated: !!e.accessToken,
368
+ isReady: e.isReady,
369
+ accessToken: e.accessToken,
370
+ user: e.user,
371
+ login: e.login,
372
+ logout: e.logout
373
+ };
374
+ };
375
+ export {
376
+ _e as DevaProvider,
377
+ ve as useDeva
378
+ };
package/package.json ADDED
@@ -0,0 +1,119 @@
1
+ {
2
+ "name": "@bitplanet/deva-sdk",
3
+ "version": "0.8.1",
4
+ "repository": {
5
+ "type": "git",
6
+ "url": "https://github.com/Bitplanet-L1/deva-sdk.git"
7
+ },
8
+ "type": "module",
9
+ "main": "dist/index.es.js",
10
+ "module": "dist/index.es.js",
11
+ "types": "dist/index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/index.es.js",
16
+ "require": "./dist/index.es.js"
17
+ },
18
+ "./package.json": "./package.json",
19
+ "./style.css": "./dist/assets/deva-sdk.css",
20
+ "./components": {
21
+ "types": "./dist/components.d.ts",
22
+ "import": "./dist/components.es.js",
23
+ "require": "./dist/components.es.js"
24
+ }
25
+ },
26
+ "files": [
27
+ "dist/index.es.js",
28
+ "dist/index.d.ts",
29
+ "dist/components.es.js",
30
+ "dist/components.d.ts",
31
+ "dist/config-*.js",
32
+ "dist/assets/deva-sdk.css",
33
+ "README.md"
34
+ ],
35
+ "dependencies": {
36
+ "@microsoft/fetch-event-source": "^2.0.1",
37
+ "@radix-ui/react-avatar": "^1.1.3",
38
+ "@radix-ui/react-dialog": "^1.1.5",
39
+ "@radix-ui/react-dropdown-menu": "^2.1.6",
40
+ "@radix-ui/react-label": "^2.1.1",
41
+ "@radix-ui/react-popover": "^1.1.6",
42
+ "@radix-ui/react-portal": "^1.1.5",
43
+ "@radix-ui/react-scroll-area": "^1.2.3",
44
+ "@radix-ui/react-slot": "^1.1.1",
45
+ "@radix-ui/react-toast": "^1.2.5",
46
+ "@radix-ui/react-tooltip": "^1.1.8",
47
+ "@tanstack/react-query": "^5.50.0",
48
+ "@tiptap/core": "^2.12.0",
49
+ "@tiptap/extension-placeholder": "^2.12.0",
50
+ "@tiptap/react": "^2.12.0",
51
+ "@tiptap/starter-kit": "^2.12.0",
52
+ "@tiptap/suggestion": "^2.12.0",
53
+ "class-variance-authority": "^0.7.1",
54
+ "clsx": "^2.1.1",
55
+ "cmdk": "1.0.0",
56
+ "framer-motion": "^12.5.0",
57
+ "geist": "^1.3.0",
58
+ "jwt-decode": "^4.0.0",
59
+ "lucide-react": "^0.474.0",
60
+ "openapi-fetch": "^0.13.4",
61
+ "prosemirror-model": "^1.25.1",
62
+ "prosemirror-state": "^1.4.3",
63
+ "react": "^19.2.0",
64
+ "react-dom": "^19.2.0",
65
+ "react-markdown": "^10.0.0",
66
+ "rehype-raw": "^7.0.0",
67
+ "remark-gfm": "^4.0.1",
68
+ "superjson": "^2.2.1",
69
+ "swr": "^2.3.0",
70
+ "swr-openapi": "^5.1.3",
71
+ "tailwind-merge": "^2.6.0",
72
+ "tailwindcss-animate": "^1.0.7",
73
+ "tippy.js": "^6.3.7",
74
+ "uuid": "^11.1.0",
75
+ "validator": "^13.12.0",
76
+ "zod": "^3.23.3"
77
+ },
78
+ "devDependencies": {
79
+ "@types/eslint": "^8.56.10",
80
+ "@types/node": "^20.14.10",
81
+ "@types/react": "^19.2.4",
82
+ "@types/react-dom": "^19.2.3",
83
+ "@types/validator": "^13.12.2",
84
+ "@typescript-eslint/eslint-plugin": "^8.1.0",
85
+ "@typescript-eslint/parser": "^8.1.0",
86
+ "@vitejs/plugin-react": "^4.3.4",
87
+ "eslint": "^8.57.0",
88
+ "eslint-plugin-react": "^7.37.5",
89
+ "eslint-plugin-react-hooks": "^5.2.0",
90
+ "globals": "^16.2.0",
91
+ "postcss": "^8.4.39",
92
+ "prettier": "^3.3.2",
93
+ "prettier-plugin-tailwindcss": "^0.6.5",
94
+ "tailwindcss": "^3.4.3",
95
+ "typescript": "^5.5.3",
96
+ "typescript-eslint": "^8.33.1",
97
+ "vite": "^6.0.11",
98
+ "vite-plugin-dts": "^4.5.0"
99
+ },
100
+ "ct3aMetadata": {
101
+ "initVersion": "7.38.1"
102
+ },
103
+ "scripts": {
104
+ "build": "vite build",
105
+ "dev:install": "pnpm install --frozen-lockfile && cd example && pnpm install --frozen-lockfile",
106
+ "dev:example": "cd example && pnpm run dev",
107
+ "dev": "NODE_ENV=development vite build --watch",
108
+ "example": "cd example && pnpm run dev",
109
+ "example2": "cd example2 && pnpm run dev",
110
+ "example:link": "cd example && pnpm link ../",
111
+ "format:check": "prettier --check \"**/*.{ts,tsx,js,jsx,mdx}\" --cache",
112
+ "format:write": "prettier --write \"**/*.{ts,tsx,js,jsx,mdx}\" --cache",
113
+ "lint": "eslint .",
114
+ "pretypecheck": "npm run lint",
115
+ "start": "next start",
116
+ "tc": "pnpm run typecheck",
117
+ "typecheck": "tsc --noEmit --project tsconfig.typecheck.json"
118
+ }
119
+ }