@genation/sdk 0.1.0 → 0.2.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.
- package/README.md +25 -11
- package/dist/genation.cjs.js +1 -1
- package/dist/genation.cjs.js.map +1 -1
- package/dist/genation.es.js +144 -131
- package/dist/genation.es.js.map +1 -1
- package/dist/genation.umd.js +1 -1
- package/dist/genation.umd.js.map +1 -1
- package/dist/index.d.ts +11 -32
- package/package.json +1 -1
package/dist/genation.es.js
CHANGED
|
@@ -1,43 +1,43 @@
|
|
|
1
|
-
class
|
|
1
|
+
class m extends Error {
|
|
2
2
|
code;
|
|
3
3
|
cause;
|
|
4
|
-
constructor(e, t,
|
|
5
|
-
super(e), this.name = "GenationError", this.code = t, this.cause =
|
|
4
|
+
constructor(e, t, n) {
|
|
5
|
+
super(e), this.name = "GenationError", this.code = t, this.cause = n;
|
|
6
6
|
}
|
|
7
7
|
}
|
|
8
|
-
class
|
|
9
|
-
constructor(e, t,
|
|
10
|
-
super(e, t,
|
|
8
|
+
class l extends m {
|
|
9
|
+
constructor(e, t, n) {
|
|
10
|
+
super(e, t, n), this.name = "AuthError";
|
|
11
11
|
}
|
|
12
12
|
static invalidGrant(e = "Invalid authorization code or refresh token") {
|
|
13
|
-
return new
|
|
13
|
+
return new l(e, "invalid_grant");
|
|
14
14
|
}
|
|
15
15
|
static accessDenied(e = "User denied access") {
|
|
16
|
-
return new
|
|
16
|
+
return new l(e, "access_denied");
|
|
17
17
|
}
|
|
18
18
|
static expiredToken(e = "Token has expired") {
|
|
19
|
-
return new
|
|
19
|
+
return new l(e, "expired_token");
|
|
20
20
|
}
|
|
21
21
|
static invalidState(e = "State mismatch, possible CSRF attack") {
|
|
22
|
-
return new
|
|
22
|
+
return new l(e, "invalid_state");
|
|
23
23
|
}
|
|
24
24
|
static pkceVerificationFailed(e = "PKCE verification failed") {
|
|
25
|
-
return new
|
|
25
|
+
return new l(e, "pkce_verification_failed");
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
|
-
class
|
|
28
|
+
class h extends m {
|
|
29
29
|
status;
|
|
30
|
-
constructor(e, t,
|
|
31
|
-
super(e, "network_error",
|
|
30
|
+
constructor(e, t, n) {
|
|
31
|
+
super(e, "network_error", n), this.name = "NetworkError", this.status = t;
|
|
32
32
|
}
|
|
33
33
|
static fromResponse(e) {
|
|
34
|
-
return new
|
|
34
|
+
return new h(
|
|
35
35
|
`HTTP ${e.status}: ${e.statusText}`,
|
|
36
36
|
e.status
|
|
37
37
|
);
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
|
-
class u extends
|
|
40
|
+
class u extends m {
|
|
41
41
|
constructor(e) {
|
|
42
42
|
super(e, "config_error"), this.name = "ConfigError";
|
|
43
43
|
}
|
|
@@ -55,78 +55,78 @@ class p {
|
|
|
55
55
|
* Make an HTTP request
|
|
56
56
|
*/
|
|
57
57
|
async request(e, t = {}) {
|
|
58
|
-
const { method:
|
|
59
|
-
let
|
|
60
|
-
if (
|
|
61
|
-
const
|
|
62
|
-
|
|
58
|
+
const { method: n = "GET", headers: s = {}, body: i, params: a } = t;
|
|
59
|
+
let c = `${this.baseUrl}${e}`;
|
|
60
|
+
if (a) {
|
|
61
|
+
const o = new URLSearchParams(a);
|
|
62
|
+
c += `?${o.toString()}`;
|
|
63
63
|
}
|
|
64
|
-
const
|
|
64
|
+
const y = new AbortController(), S = setTimeout(() => y.abort(), this.timeout);
|
|
65
65
|
try {
|
|
66
|
-
const
|
|
67
|
-
method:
|
|
66
|
+
const o = await fetch(c, {
|
|
67
|
+
method: n,
|
|
68
68
|
headers: {
|
|
69
69
|
"Content-Type": "application/json",
|
|
70
|
-
...
|
|
70
|
+
...s
|
|
71
71
|
},
|
|
72
|
-
body:
|
|
73
|
-
signal:
|
|
72
|
+
body: i ? JSON.stringify(i) : void 0,
|
|
73
|
+
signal: y.signal
|
|
74
74
|
});
|
|
75
|
-
if (clearTimeout(
|
|
76
|
-
throw
|
|
77
|
-
return await
|
|
78
|
-
} catch (
|
|
79
|
-
throw clearTimeout(
|
|
75
|
+
if (clearTimeout(S), !o.ok)
|
|
76
|
+
throw h.fromResponse(o);
|
|
77
|
+
return await o.json();
|
|
78
|
+
} catch (o) {
|
|
79
|
+
throw clearTimeout(S), o instanceof h ? o : o instanceof Error && o.name === "AbortError" ? new h("Request timeout", void 0, o) : new h("Network request failed", void 0, o);
|
|
80
80
|
}
|
|
81
81
|
}
|
|
82
82
|
/**
|
|
83
83
|
* POST request with form data (for OAuth token exchange)
|
|
84
84
|
*/
|
|
85
|
-
async postForm(e, t,
|
|
86
|
-
const
|
|
85
|
+
async postForm(e, t, n = {}) {
|
|
86
|
+
const s = `${this.baseUrl}${e}`, i = new AbortController(), a = setTimeout(() => i.abort(), this.timeout);
|
|
87
87
|
try {
|
|
88
|
-
const
|
|
88
|
+
const c = await fetch(s, {
|
|
89
89
|
method: "POST",
|
|
90
90
|
headers: {
|
|
91
91
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
92
|
-
...
|
|
92
|
+
...n
|
|
93
93
|
},
|
|
94
94
|
body: new URLSearchParams(t).toString(),
|
|
95
|
-
signal:
|
|
95
|
+
signal: i.signal
|
|
96
96
|
});
|
|
97
|
-
if (clearTimeout(
|
|
98
|
-
throw
|
|
99
|
-
return await
|
|
100
|
-
} catch (
|
|
101
|
-
throw clearTimeout(
|
|
97
|
+
if (clearTimeout(a), !c.ok)
|
|
98
|
+
throw h.fromResponse(c);
|
|
99
|
+
return await c.json();
|
|
100
|
+
} catch (c) {
|
|
101
|
+
throw clearTimeout(a), c instanceof h ? c : new h("Network request failed", void 0, c);
|
|
102
102
|
}
|
|
103
103
|
}
|
|
104
104
|
}
|
|
105
|
-
function k(
|
|
106
|
-
return btoa(String.fromCharCode(...
|
|
105
|
+
function k(r) {
|
|
106
|
+
return btoa(String.fromCharCode(...r)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
|
107
107
|
}
|
|
108
|
-
function
|
|
109
|
-
const
|
|
110
|
-
return crypto.getRandomValues(
|
|
108
|
+
function b() {
|
|
109
|
+
const r = new Uint8Array(32);
|
|
110
|
+
return crypto.getRandomValues(r), k(r);
|
|
111
111
|
}
|
|
112
|
-
async function
|
|
113
|
-
const t = new TextEncoder().encode(
|
|
114
|
-
return k(new Uint8Array(
|
|
112
|
+
async function C(r) {
|
|
113
|
+
const t = new TextEncoder().encode(r), n = await crypto.subtle.digest("SHA-256", t);
|
|
114
|
+
return k(new Uint8Array(n));
|
|
115
115
|
}
|
|
116
|
-
async function
|
|
117
|
-
const
|
|
116
|
+
async function _() {
|
|
117
|
+
const r = b(), e = await C(r);
|
|
118
118
|
return {
|
|
119
|
-
codeVerifier:
|
|
119
|
+
codeVerifier: r,
|
|
120
120
|
codeChallenge: e,
|
|
121
121
|
codeChallengeMethod: "S256"
|
|
122
122
|
};
|
|
123
123
|
}
|
|
124
|
-
function
|
|
125
|
-
const
|
|
126
|
-
return crypto.getRandomValues(
|
|
124
|
+
function v() {
|
|
125
|
+
const r = new Uint8Array(16);
|
|
126
|
+
return crypto.getRandomValues(r), k(r);
|
|
127
127
|
}
|
|
128
128
|
const d = "tokens", g = "pkce", f = "state";
|
|
129
|
-
class
|
|
129
|
+
class U {
|
|
130
130
|
storage;
|
|
131
131
|
constructor(e) {
|
|
132
132
|
this.storage = e;
|
|
@@ -198,7 +198,7 @@ class C {
|
|
|
198
198
|
}
|
|
199
199
|
}
|
|
200
200
|
const I = "https://mnnoheowoowbtpuoguul.supabase.co/auth/v1";
|
|
201
|
-
class
|
|
201
|
+
class x {
|
|
202
202
|
config;
|
|
203
203
|
http;
|
|
204
204
|
tokenManager;
|
|
@@ -216,9 +216,9 @@ class U {
|
|
|
216
216
|
* Stores PKCE verifier and state for later validation
|
|
217
217
|
*/
|
|
218
218
|
async getAuthorizationUrl() {
|
|
219
|
-
const e = await
|
|
219
|
+
const e = await _(), t = v();
|
|
220
220
|
await this.tokenManager.setPKCE(e.codeVerifier), await this.tokenManager.setState(t);
|
|
221
|
-
const
|
|
221
|
+
const n = new URLSearchParams({
|
|
222
222
|
response_type: "code",
|
|
223
223
|
client_id: this.config.clientId,
|
|
224
224
|
redirect_uri: this.config.redirectUri,
|
|
@@ -226,19 +226,19 @@ class U {
|
|
|
226
226
|
code_challenge: e.codeChallenge,
|
|
227
227
|
code_challenge_method: e.codeChallengeMethod
|
|
228
228
|
});
|
|
229
|
-
return this.config.scopes && this.config.scopes.length > 0 &&
|
|
229
|
+
return this.config.scopes && this.config.scopes.length > 0 && n.append("scope", this.config.scopes.join(" ")), `${this.config.authUrl}/oauth/authorize?${n.toString()}`;
|
|
230
230
|
}
|
|
231
231
|
/**
|
|
232
232
|
* Exchange authorization code for tokens
|
|
233
233
|
*/
|
|
234
234
|
async exchangeCode(e, t) {
|
|
235
|
-
const
|
|
236
|
-
if (!
|
|
237
|
-
throw
|
|
238
|
-
const
|
|
239
|
-
if (!
|
|
240
|
-
throw
|
|
241
|
-
const
|
|
235
|
+
const n = await this.tokenManager.consumeState();
|
|
236
|
+
if (!n || n !== t)
|
|
237
|
+
throw l.invalidState();
|
|
238
|
+
const s = await this.tokenManager.consumePKCE();
|
|
239
|
+
if (!s)
|
|
240
|
+
throw l.pkceVerificationFailed("Missing code verifier");
|
|
241
|
+
const i = await this.http.postForm(
|
|
242
242
|
"/oauth/token",
|
|
243
243
|
{
|
|
244
244
|
grant_type: "authorization_code",
|
|
@@ -246,10 +246,10 @@ class U {
|
|
|
246
246
|
redirect_uri: this.config.redirectUri,
|
|
247
247
|
client_id: this.config.clientId,
|
|
248
248
|
client_secret: this.config.clientSecret,
|
|
249
|
-
code_verifier:
|
|
249
|
+
code_verifier: s
|
|
250
250
|
}
|
|
251
|
-
),
|
|
252
|
-
return await this.tokenManager.setTokens(
|
|
251
|
+
), a = this.mapTokenResponse(i);
|
|
252
|
+
return await this.tokenManager.setTokens(a), a;
|
|
253
253
|
}
|
|
254
254
|
/**
|
|
255
255
|
* Refresh access token using refresh token
|
|
@@ -257,7 +257,7 @@ class U {
|
|
|
257
257
|
async refreshToken() {
|
|
258
258
|
const e = await this.tokenManager.getTokens();
|
|
259
259
|
if (!e?.refreshToken)
|
|
260
|
-
throw
|
|
260
|
+
throw l.invalidGrant("No refresh token available");
|
|
261
261
|
const t = await this.http.postForm(
|
|
262
262
|
"/oauth/token",
|
|
263
263
|
{
|
|
@@ -266,8 +266,8 @@ class U {
|
|
|
266
266
|
client_id: this.config.clientId,
|
|
267
267
|
client_secret: this.config.clientSecret
|
|
268
268
|
}
|
|
269
|
-
),
|
|
270
|
-
return await this.tokenManager.setTokens(
|
|
269
|
+
), n = this.mapTokenResponse(t);
|
|
270
|
+
return await this.tokenManager.setTokens(n), n;
|
|
271
271
|
}
|
|
272
272
|
/**
|
|
273
273
|
* Revoke current tokens
|
|
@@ -299,7 +299,7 @@ class U {
|
|
|
299
299
|
};
|
|
300
300
|
}
|
|
301
301
|
}
|
|
302
|
-
class
|
|
302
|
+
class E {
|
|
303
303
|
store = /* @__PURE__ */ new Map();
|
|
304
304
|
async get(e) {
|
|
305
305
|
return this.store.get(e) ?? null;
|
|
@@ -314,7 +314,7 @@ class x {
|
|
|
314
314
|
this.store.clear();
|
|
315
315
|
}
|
|
316
316
|
}
|
|
317
|
-
class
|
|
317
|
+
class T {
|
|
318
318
|
prefix;
|
|
319
319
|
constructor(e = "genation") {
|
|
320
320
|
this.prefix = e;
|
|
@@ -338,7 +338,7 @@ class S {
|
|
|
338
338
|
).forEach((t) => localStorage.removeItem(t));
|
|
339
339
|
}
|
|
340
340
|
}
|
|
341
|
-
class
|
|
341
|
+
class A {
|
|
342
342
|
prefix;
|
|
343
343
|
constructor(e = "genation") {
|
|
344
344
|
this.prefix = e;
|
|
@@ -362,19 +362,27 @@ class E {
|
|
|
362
362
|
).forEach((t) => sessionStorage.removeItem(t));
|
|
363
363
|
}
|
|
364
364
|
}
|
|
365
|
-
function
|
|
366
|
-
switch (
|
|
365
|
+
function M(r = "localStorage") {
|
|
366
|
+
switch (r) {
|
|
367
367
|
case "memory":
|
|
368
|
-
return new
|
|
368
|
+
return new E();
|
|
369
369
|
case "localStorage":
|
|
370
|
-
return new
|
|
370
|
+
return new T();
|
|
371
371
|
case "sessionStorage":
|
|
372
|
-
return new
|
|
372
|
+
return new A();
|
|
373
373
|
default:
|
|
374
|
-
return new
|
|
374
|
+
return new T();
|
|
375
375
|
}
|
|
376
376
|
}
|
|
377
|
-
|
|
377
|
+
function w(r) {
|
|
378
|
+
return Array.isArray(r) ? r.map(w) : typeof r == "object" && r !== null ? Object.fromEntries(
|
|
379
|
+
Object.entries(r).map(([e, t]) => [
|
|
380
|
+
e.replace(/_([a-z])/g, (n, s) => s.toUpperCase()),
|
|
381
|
+
w(t)
|
|
382
|
+
])
|
|
383
|
+
) : r;
|
|
384
|
+
}
|
|
385
|
+
class K {
|
|
378
386
|
oauth;
|
|
379
387
|
tokenManager;
|
|
380
388
|
http;
|
|
@@ -383,8 +391,8 @@ class M {
|
|
|
383
391
|
initialized = !1;
|
|
384
392
|
constructor(e) {
|
|
385
393
|
this.validateConfig(e);
|
|
386
|
-
const t = typeof e.storage == "object" ? e.storage :
|
|
387
|
-
this.tokenManager = new
|
|
394
|
+
const t = typeof e.storage == "object" ? e.storage : M(e.storage);
|
|
395
|
+
this.tokenManager = new U(t), this.oauth = new x(e, this.tokenManager), this.http = new p({
|
|
388
396
|
baseUrl: e.authUrl ?? "https://mnnoheowoowbtpuoguul.supabase.co/auth/v1"
|
|
389
397
|
}), this.httpServer = new p({
|
|
390
398
|
baseUrl: "https://ff-api.genation.ai/api/v2/client"
|
|
@@ -401,11 +409,11 @@ class M {
|
|
|
401
409
|
*/
|
|
402
410
|
async emitAuthStateChange(e) {
|
|
403
411
|
const t = await this.getSession();
|
|
404
|
-
this.listeners.forEach((
|
|
412
|
+
this.listeners.forEach((n) => {
|
|
405
413
|
try {
|
|
406
|
-
|
|
407
|
-
} catch (
|
|
408
|
-
console.error("Error in auth state change callback:",
|
|
414
|
+
n(e, t);
|
|
415
|
+
} catch (s) {
|
|
416
|
+
console.error("Error in auth state change callback:", s);
|
|
409
417
|
}
|
|
410
418
|
});
|
|
411
419
|
}
|
|
@@ -504,32 +512,33 @@ class M {
|
|
|
504
512
|
* ```
|
|
505
513
|
*/
|
|
506
514
|
async handleCallback(e, t) {
|
|
507
|
-
const
|
|
515
|
+
const n = await this.oauth.exchangeCode(e, t);
|
|
508
516
|
try {
|
|
509
|
-
const
|
|
510
|
-
console.log("Debug: handleCallback fetched user:",
|
|
511
|
-
} catch (
|
|
512
|
-
console.error("Debug: handleCallback fetchUser failed:",
|
|
517
|
+
const s = await this.fetchUser(n.accessToken);
|
|
518
|
+
console.log("Debug: handleCallback fetched user:", s);
|
|
519
|
+
} catch (s) {
|
|
520
|
+
console.error("Debug: handleCallback fetchUser failed:", s);
|
|
513
521
|
}
|
|
514
|
-
return await this.emitAuthStateChange("SIGNED_IN"),
|
|
515
|
-
}
|
|
516
|
-
/**
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
async signOut() {
|
|
531
|
-
|
|
532
|
-
|
|
522
|
+
return await this.emitAuthStateChange("SIGNED_IN"), n;
|
|
523
|
+
}
|
|
524
|
+
// /**
|
|
525
|
+
// * Sign out and revoke tokens
|
|
526
|
+
// *
|
|
527
|
+
// * Clears local session and revokes tokens on server.
|
|
528
|
+
// * Triggers `SIGNED_OUT` event for all listeners.
|
|
529
|
+
// *
|
|
530
|
+
// * @example
|
|
531
|
+
// * ```typescript
|
|
532
|
+
// * async function handleLogout() {
|
|
533
|
+
// * await client.signOut();
|
|
534
|
+
// * // onAuthStateChange will fire with SIGNED_OUT event
|
|
535
|
+
// * }
|
|
536
|
+
// * ```
|
|
537
|
+
// */
|
|
538
|
+
// async signOut(): Promise<void> {
|
|
539
|
+
// await this.oauth.revokeToken();
|
|
540
|
+
// await this.emitAuthStateChange("SIGNED_OUT");
|
|
541
|
+
// }
|
|
533
542
|
/**
|
|
534
543
|
* Get current session
|
|
535
544
|
*
|
|
@@ -560,13 +569,13 @@ class M {
|
|
|
560
569
|
}
|
|
561
570
|
const t = await this.tokenManager.getTokens();
|
|
562
571
|
if (!t) return null;
|
|
563
|
-
const
|
|
572
|
+
const n = await this.fetchUser(t.accessToken);
|
|
564
573
|
return {
|
|
565
574
|
accessToken: t.accessToken,
|
|
566
575
|
refreshToken: t.refreshToken,
|
|
567
576
|
expiresIn: t.expiresIn,
|
|
568
577
|
expiresAt: t.issuedAt + t.expiresIn * 1e3,
|
|
569
|
-
user:
|
|
578
|
+
user: n
|
|
570
579
|
};
|
|
571
580
|
}
|
|
572
581
|
/**
|
|
@@ -580,11 +589,15 @@ class M {
|
|
|
580
589
|
const t = await this.getSession();
|
|
581
590
|
if (!t)
|
|
582
591
|
return null;
|
|
583
|
-
const
|
|
584
|
-
headers: { Authorization: `Bearer ${
|
|
585
|
-
params: { expiresAfter:
|
|
592
|
+
const n = t.accessToken, { expiresAfter: s = /* @__PURE__ */ new Date() } = e, i = await this.httpServer.request("/licenses", {
|
|
593
|
+
headers: { Authorization: `Bearer ${n}` },
|
|
594
|
+
params: { expiresAfter: s.toISOString() }
|
|
586
595
|
});
|
|
587
|
-
|
|
596
|
+
if (!i.ok)
|
|
597
|
+
return console.error("GenationClient: Error fetching licenses:", i.error), null;
|
|
598
|
+
console.log("GenationClient: Response data:", i.data);
|
|
599
|
+
const a = w(i.data);
|
|
600
|
+
return console.log("GenationClient: Licenses:", a), a;
|
|
588
601
|
}
|
|
589
602
|
/**
|
|
590
603
|
* Fetch user info from auth server
|
|
@@ -608,19 +621,19 @@ class M {
|
|
|
608
621
|
}
|
|
609
622
|
}
|
|
610
623
|
}
|
|
611
|
-
function
|
|
612
|
-
return new
|
|
624
|
+
function $(r) {
|
|
625
|
+
return new K(r);
|
|
613
626
|
}
|
|
614
627
|
export {
|
|
615
|
-
|
|
628
|
+
l as AuthError,
|
|
616
629
|
u as ConfigError,
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
630
|
+
K as GenationClient,
|
|
631
|
+
m as GenationError,
|
|
632
|
+
T as LocalStorage,
|
|
633
|
+
E as MemoryStorage,
|
|
634
|
+
h as NetworkError,
|
|
635
|
+
A as SessionStorage,
|
|
636
|
+
$ as createClient,
|
|
637
|
+
M as createStorage
|
|
625
638
|
};
|
|
626
639
|
//# sourceMappingURL=genation.es.js.map
|