@saas-support/react 0.4.0 → 0.5.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/dist/index.cjs +1 -1
- package/dist/index.d.ts +11 -0
- package/dist/index.js +218 -209
- package/dist/react.cjs +23 -3
- package/dist/react.d.ts +39 -4
- package/dist/react.js +1452 -1145
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
class g extends Error {
|
|
2
|
-
constructor(
|
|
3
|
-
super(
|
|
2
|
+
constructor(t, e, s = "unknown") {
|
|
3
|
+
super(e), this.name = "SaaSError", this.code = t, this.domain = s;
|
|
4
4
|
}
|
|
5
5
|
get isNotFound() {
|
|
6
6
|
return this.code === 404;
|
|
@@ -19,21 +19,21 @@ class g extends Error {
|
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
class k {
|
|
22
|
-
constructor(
|
|
23
|
-
this.onUnauthorized = null, this.baseUrl =
|
|
22
|
+
constructor(t, e) {
|
|
23
|
+
this.onUnauthorized = null, this.baseUrl = t, this.authMode = e;
|
|
24
24
|
}
|
|
25
25
|
/** Register a handler that refreshes tokens and returns a new access token, or null. */
|
|
26
|
-
setUnauthorizedHandler(
|
|
27
|
-
this.onUnauthorized =
|
|
26
|
+
setUnauthorizedHandler(t) {
|
|
27
|
+
this.onUnauthorized = t;
|
|
28
28
|
}
|
|
29
|
-
async request(
|
|
29
|
+
async request(t, e, s, r) {
|
|
30
30
|
try {
|
|
31
|
-
return await this.doRequest(
|
|
31
|
+
return await this.doRequest(t, e, s, r);
|
|
32
32
|
} catch (n) {
|
|
33
33
|
if (n instanceof g && n.isUnauthorized && this.onUnauthorized && (r != null && r.Authorization)) {
|
|
34
34
|
const i = await this.onUnauthorized();
|
|
35
35
|
if (i)
|
|
36
|
-
return this.doRequest(
|
|
36
|
+
return this.doRequest(t, e, s, {
|
|
37
37
|
...r,
|
|
38
38
|
Authorization: `Bearer ${i}`
|
|
39
39
|
});
|
|
@@ -41,26 +41,26 @@ class k {
|
|
|
41
41
|
throw n;
|
|
42
42
|
}
|
|
43
43
|
}
|
|
44
|
-
async get(
|
|
45
|
-
return this.request("GET",
|
|
44
|
+
async get(t, e) {
|
|
45
|
+
return this.request("GET", t, void 0, e);
|
|
46
46
|
}
|
|
47
|
-
async post(
|
|
48
|
-
return this.request("POST",
|
|
47
|
+
async post(t, e, s) {
|
|
48
|
+
return this.request("POST", t, e, s);
|
|
49
49
|
}
|
|
50
|
-
async patch(
|
|
51
|
-
return this.request("PATCH",
|
|
50
|
+
async patch(t, e, s) {
|
|
51
|
+
return this.request("PATCH", t, e, s);
|
|
52
52
|
}
|
|
53
|
-
async del(
|
|
54
|
-
return this.request("DELETE",
|
|
53
|
+
async del(t, e) {
|
|
54
|
+
return this.request("DELETE", t, void 0, e);
|
|
55
55
|
}
|
|
56
|
-
async uploadBinary(
|
|
56
|
+
async uploadBinary(t, e, s) {
|
|
57
57
|
try {
|
|
58
|
-
return await this.doUploadBinary(
|
|
58
|
+
return await this.doUploadBinary(t, e, s);
|
|
59
59
|
} catch (r) {
|
|
60
60
|
if (r instanceof g && r.isUnauthorized && this.onUnauthorized && (s != null && s.Authorization)) {
|
|
61
61
|
const n = await this.onUnauthorized();
|
|
62
62
|
if (n)
|
|
63
|
-
return this.doUploadBinary(
|
|
63
|
+
return this.doUploadBinary(t, e, {
|
|
64
64
|
...s,
|
|
65
65
|
Authorization: `Bearer ${n}`
|
|
66
66
|
});
|
|
@@ -68,34 +68,34 @@ class k {
|
|
|
68
68
|
throw r;
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
|
-
async doUploadBinary(
|
|
71
|
+
async doUploadBinary(t, e, s) {
|
|
72
72
|
const r = {
|
|
73
73
|
"Content-Type": "application/octet-stream",
|
|
74
74
|
...this.getAuthHeaders(),
|
|
75
75
|
...s
|
|
76
|
-
}, i = await (await fetch(`${this.baseUrl}${
|
|
76
|
+
}, i = await (await fetch(`${this.baseUrl}${t}`, {
|
|
77
77
|
method: "POST",
|
|
78
78
|
headers: r,
|
|
79
|
-
body:
|
|
79
|
+
body: e
|
|
80
80
|
})).json();
|
|
81
81
|
if (i.code && i.code >= 400) {
|
|
82
|
-
const a = this.inferDomain(
|
|
82
|
+
const a = this.inferDomain(t);
|
|
83
83
|
throw new g(i.code, i.message || "Upload failed", a);
|
|
84
84
|
}
|
|
85
85
|
return i.data;
|
|
86
86
|
}
|
|
87
|
-
async doRequest(
|
|
87
|
+
async doRequest(t, e, s, r) {
|
|
88
88
|
const n = {
|
|
89
89
|
"Content-Type": "application/json",
|
|
90
90
|
...this.getAuthHeaders(),
|
|
91
91
|
...r
|
|
92
|
-
}, a = await (await fetch(`${this.baseUrl}${
|
|
93
|
-
method:
|
|
92
|
+
}, a = await (await fetch(`${this.baseUrl}${e}`, {
|
|
93
|
+
method: t,
|
|
94
94
|
headers: n,
|
|
95
95
|
body: s ? JSON.stringify(s) : void 0
|
|
96
96
|
})).json();
|
|
97
97
|
if (a.code && a.code >= 400) {
|
|
98
|
-
const p = this.inferDomain(
|
|
98
|
+
const p = this.inferDomain(e);
|
|
99
99
|
throw new g(a.code, a.message || "Request failed", p);
|
|
100
100
|
}
|
|
101
101
|
return a.data;
|
|
@@ -110,19 +110,19 @@ class k {
|
|
|
110
110
|
return { Authorization: `Bearer ${this.authMode.token}` };
|
|
111
111
|
}
|
|
112
112
|
}
|
|
113
|
-
inferDomain(
|
|
114
|
-
return
|
|
113
|
+
inferDomain(t) {
|
|
114
|
+
return t.startsWith("/auth") ? "auth" : t.startsWith("/billing") ? "billing" : t.startsWith("/report") ? "report" : "unknown";
|
|
115
115
|
}
|
|
116
116
|
}
|
|
117
117
|
class b {
|
|
118
|
-
constructor(
|
|
119
|
-
this.accessToken = null, this.refreshToken = null, this.refreshTimer = null, this.refreshInFlight = null, this.onRefreshNeeded = null, this.onTokensChanged = null, this.boundHandleStorage = null, this.storageKey = `ss_rt_${
|
|
118
|
+
constructor(t) {
|
|
119
|
+
this.accessToken = null, this.refreshToken = null, this.refreshTimer = null, this.refreshInFlight = null, this.onRefreshNeeded = null, this.onTokensChanged = null, this.boundHandleStorage = null, this.storageKey = `ss_rt_${t.slice(0, 12)}`, this.refreshToken = this.loadRefreshToken(), typeof window < "u" && (this.boundHandleStorage = this.handleStorageEvent.bind(this), window.addEventListener("storage", this.boundHandleStorage));
|
|
120
120
|
}
|
|
121
|
-
setRefreshCallback(
|
|
122
|
-
this.onRefreshNeeded =
|
|
121
|
+
setRefreshCallback(t) {
|
|
122
|
+
this.onRefreshNeeded = t;
|
|
123
123
|
}
|
|
124
|
-
setTokensChangedCallback(
|
|
125
|
-
this.onTokensChanged =
|
|
124
|
+
setTokensChangedCallback(t) {
|
|
125
|
+
this.onTokensChanged = t;
|
|
126
126
|
}
|
|
127
127
|
getAccessToken() {
|
|
128
128
|
return this.accessToken;
|
|
@@ -133,8 +133,8 @@ class b {
|
|
|
133
133
|
hasRefreshToken() {
|
|
134
134
|
return this.refreshToken !== null;
|
|
135
135
|
}
|
|
136
|
-
setTokens(
|
|
137
|
-
this.accessToken =
|
|
136
|
+
setTokens(t, e) {
|
|
137
|
+
this.accessToken = t, this.refreshToken = e, this.saveRefreshToken(e), this.scheduleRefresh(t);
|
|
138
138
|
}
|
|
139
139
|
clearTokens() {
|
|
140
140
|
this.accessToken = null, this.refreshToken = null, this.removeRefreshToken(), this.refreshTimer && (clearTimeout(this.refreshTimer), this.refreshTimer = null);
|
|
@@ -157,16 +157,16 @@ class b {
|
|
|
157
157
|
typeof navigator < "u" && "locks" in navigator ? await navigator.locks.request(
|
|
158
158
|
`ss_refresh_lock_${this.storageKey}`,
|
|
159
159
|
async () => {
|
|
160
|
-
const
|
|
161
|
-
|
|
160
|
+
const t = this.loadRefreshToken();
|
|
161
|
+
t && t !== this.refreshToken && (this.refreshToken = t), await this.onRefreshNeeded();
|
|
162
162
|
}
|
|
163
163
|
) : await this.onRefreshNeeded();
|
|
164
164
|
}
|
|
165
|
-
scheduleRefresh(
|
|
165
|
+
scheduleRefresh(t) {
|
|
166
166
|
this.refreshTimer && clearTimeout(this.refreshTimer);
|
|
167
|
-
const
|
|
168
|
-
if (!
|
|
169
|
-
const s =
|
|
167
|
+
const e = this.getTokenExpiry(t);
|
|
168
|
+
if (!e) return;
|
|
169
|
+
const s = e * 1e3 - Date.now() - 6e4;
|
|
170
170
|
if (s <= 0) {
|
|
171
171
|
this.refreshOnce().catch(() => {
|
|
172
172
|
});
|
|
@@ -177,20 +177,20 @@ class b {
|
|
|
177
177
|
});
|
|
178
178
|
}, s);
|
|
179
179
|
}
|
|
180
|
-
handleStorageEvent(
|
|
181
|
-
var
|
|
182
|
-
if (
|
|
183
|
-
if (
|
|
184
|
-
this.accessToken = null, this.refreshToken = null, this.refreshTimer && (clearTimeout(this.refreshTimer), this.refreshTimer = null), (
|
|
180
|
+
handleStorageEvent(t) {
|
|
181
|
+
var e;
|
|
182
|
+
if (t.key === this.storageKey) {
|
|
183
|
+
if (t.newValue === null) {
|
|
184
|
+
this.accessToken = null, this.refreshToken = null, this.refreshTimer && (clearTimeout(this.refreshTimer), this.refreshTimer = null), (e = this.onTokensChanged) == null || e.call(this);
|
|
185
185
|
return;
|
|
186
186
|
}
|
|
187
|
-
|
|
187
|
+
t.newValue !== this.refreshToken && (this.refreshToken = t.newValue, this.accessToken = null, this.refreshTimer && (clearTimeout(this.refreshTimer), this.refreshTimer = null));
|
|
188
188
|
}
|
|
189
189
|
}
|
|
190
|
-
getTokenExpiry(
|
|
190
|
+
getTokenExpiry(t) {
|
|
191
191
|
try {
|
|
192
|
-
const
|
|
193
|
-
return JSON.parse(atob(
|
|
192
|
+
const e = t.split(".")[1];
|
|
193
|
+
return JSON.parse(atob(e)).exp ?? null;
|
|
194
194
|
} catch {
|
|
195
195
|
return null;
|
|
196
196
|
}
|
|
@@ -202,9 +202,9 @@ class b {
|
|
|
202
202
|
return null;
|
|
203
203
|
}
|
|
204
204
|
}
|
|
205
|
-
saveRefreshToken(
|
|
205
|
+
saveRefreshToken(t) {
|
|
206
206
|
try {
|
|
207
|
-
localStorage.setItem(this.storageKey,
|
|
207
|
+
localStorage.setItem(this.storageKey, t);
|
|
208
208
|
} catch {
|
|
209
209
|
}
|
|
210
210
|
}
|
|
@@ -219,15 +219,15 @@ class S {
|
|
|
219
219
|
constructor() {
|
|
220
220
|
this.listeners = /* @__PURE__ */ new Map();
|
|
221
221
|
}
|
|
222
|
-
on(
|
|
223
|
-
return this.listeners.has(
|
|
222
|
+
on(t, e) {
|
|
223
|
+
return this.listeners.has(t) || this.listeners.set(t, /* @__PURE__ */ new Set()), this.listeners.get(t).add(e), () => {
|
|
224
224
|
var s;
|
|
225
|
-
(s = this.listeners.get(
|
|
225
|
+
(s = this.listeners.get(t)) == null || s.delete(e);
|
|
226
226
|
};
|
|
227
227
|
}
|
|
228
|
-
emit(
|
|
228
|
+
emit(t, e) {
|
|
229
229
|
var s;
|
|
230
|
-
(s = this.listeners.get(
|
|
230
|
+
(s = this.listeners.get(t)) == null || s.forEach((r) => r(e));
|
|
231
231
|
}
|
|
232
232
|
removeAll() {
|
|
233
233
|
this.listeners.clear();
|
|
@@ -235,25 +235,25 @@ class S {
|
|
|
235
235
|
}
|
|
236
236
|
const w = 500, T = 600, U = 5 * 60 * 1e3;
|
|
237
237
|
class $ {
|
|
238
|
-
constructor(
|
|
239
|
-
this.cachedUser = null, this.cachedSettings = null, this.loaded = !1, this.transport =
|
|
238
|
+
constructor(t, e, s, r) {
|
|
239
|
+
this.cachedUser = null, this.cachedSettings = null, this.loaded = !1, this.transport = t, this.tokenManager = e, this.emitter = s, this.baseUrl = r;
|
|
240
240
|
}
|
|
241
241
|
// ---------------------------------------------------------------------------
|
|
242
242
|
// Lifecycle
|
|
243
243
|
// ---------------------------------------------------------------------------
|
|
244
244
|
async load() {
|
|
245
|
-
var
|
|
245
|
+
var t, e;
|
|
246
246
|
if (!this.loaded) {
|
|
247
247
|
try {
|
|
248
248
|
this.cachedSettings = await this.transport.get("/auth/settings");
|
|
249
249
|
} catch (s) {
|
|
250
250
|
console.warn("[SaaS Support] Failed to load project settings:", s);
|
|
251
251
|
}
|
|
252
|
-
if ((
|
|
252
|
+
if ((t = this.tokenManager) != null && t.hasRefreshToken())
|
|
253
253
|
try {
|
|
254
254
|
await this.performRefresh();
|
|
255
255
|
} catch {
|
|
256
|
-
(
|
|
256
|
+
(e = this.tokenManager) == null || e.clearTokens();
|
|
257
257
|
}
|
|
258
258
|
this.loaded = !0;
|
|
259
259
|
}
|
|
@@ -261,30 +261,30 @@ class $ {
|
|
|
261
261
|
// ---------------------------------------------------------------------------
|
|
262
262
|
// Core auth operations
|
|
263
263
|
// ---------------------------------------------------------------------------
|
|
264
|
-
async signIn(
|
|
265
|
-
const s = await this.transport.post("/auth/login", { email:
|
|
264
|
+
async signIn(t, e) {
|
|
265
|
+
const s = await this.transport.post("/auth/login", { email: t, password: e });
|
|
266
266
|
if ("mfaRequired" in s && s.mfaRequired)
|
|
267
267
|
return s;
|
|
268
268
|
const r = s;
|
|
269
269
|
return this.setSession(r), r;
|
|
270
270
|
}
|
|
271
|
-
async signUp(
|
|
272
|
-
const s = await this.transport.post("/auth/register", { email:
|
|
271
|
+
async signUp(t, e) {
|
|
272
|
+
const s = await this.transport.post("/auth/register", { email: t, password: e });
|
|
273
273
|
return this.setSession(s), s;
|
|
274
274
|
}
|
|
275
275
|
async signOut() {
|
|
276
|
-
var
|
|
277
|
-
const
|
|
278
|
-
if (
|
|
276
|
+
var e;
|
|
277
|
+
const t = (e = this.tokenManager) == null ? void 0 : e.getRefreshToken();
|
|
278
|
+
if (t)
|
|
279
279
|
try {
|
|
280
|
-
await this.transport.post("/auth/logout", { refreshToken:
|
|
280
|
+
await this.transport.post("/auth/logout", { refreshToken: t });
|
|
281
281
|
} catch {
|
|
282
282
|
}
|
|
283
283
|
this.clearSession();
|
|
284
284
|
}
|
|
285
|
-
async signInWithOAuth(
|
|
286
|
-
const
|
|
287
|
-
`/auth/oauth/${
|
|
285
|
+
async signInWithOAuth(t) {
|
|
286
|
+
const e = `${this.baseUrl}/auth/oauth/${t}/popup-callback`, { authUrl: s, state: r } = await this.transport.get(
|
|
287
|
+
`/auth/oauth/${t}?redirect_uri=${encodeURIComponent(e)}`
|
|
288
288
|
), n = window.screenX + (window.innerWidth - w) / 2, i = window.screenY + (window.innerHeight - T) / 2, a = window.open(
|
|
289
289
|
s,
|
|
290
290
|
"saas-support-oauth",
|
|
@@ -301,7 +301,7 @@ class $ {
|
|
|
301
301
|
}
|
|
302
302
|
try {
|
|
303
303
|
const d = await this.transport.post(
|
|
304
|
-
`/auth/oauth/${
|
|
304
|
+
`/auth/oauth/${t}/callback`,
|
|
305
305
|
{ code: u.data.code, state: u.data.state || r }
|
|
306
306
|
);
|
|
307
307
|
this.setSession(d), p(d);
|
|
@@ -318,25 +318,25 @@ class $ {
|
|
|
318
318
|
}, 500);
|
|
319
319
|
});
|
|
320
320
|
}
|
|
321
|
-
async submitMfaCode(
|
|
322
|
-
const s = await this.transport.post("/auth/login/mfa", { mfaToken:
|
|
321
|
+
async submitMfaCode(t, e) {
|
|
322
|
+
const s = await this.transport.post("/auth/login/mfa", { mfaToken: t, code: e });
|
|
323
323
|
return this.setSession(s), s;
|
|
324
324
|
}
|
|
325
325
|
// ---------------------------------------------------------------------------
|
|
326
326
|
// Magic link & password reset
|
|
327
327
|
// ---------------------------------------------------------------------------
|
|
328
|
-
async sendMagicLink(
|
|
329
|
-
await this.transport.post("/auth/magic-link/send", { email:
|
|
328
|
+
async sendMagicLink(t, e) {
|
|
329
|
+
await this.transport.post("/auth/magic-link/send", { email: t, redirectUrl: e });
|
|
330
330
|
}
|
|
331
|
-
async verifyMagicLink(
|
|
332
|
-
const
|
|
333
|
-
return this.setSession(
|
|
331
|
+
async verifyMagicLink(t) {
|
|
332
|
+
const e = await this.transport.post("/auth/magic-link/verify", { token: t });
|
|
333
|
+
return this.setSession(e), e;
|
|
334
334
|
}
|
|
335
|
-
async sendPasswordReset(
|
|
336
|
-
await this.transport.post("/auth/password-reset/send", { email:
|
|
335
|
+
async sendPasswordReset(t, e) {
|
|
336
|
+
await this.transport.post("/auth/password-reset/send", { email: t, redirectUrl: e });
|
|
337
337
|
}
|
|
338
|
-
async resetPassword(
|
|
339
|
-
await this.transport.post("/auth/password-reset/verify", { token:
|
|
338
|
+
async resetPassword(t, e) {
|
|
339
|
+
await this.transport.post("/auth/password-reset/verify", { token: t, newPassword: e });
|
|
340
340
|
}
|
|
341
341
|
// ---------------------------------------------------------------------------
|
|
342
342
|
// MFA management
|
|
@@ -344,19 +344,19 @@ class $ {
|
|
|
344
344
|
async setupMfa() {
|
|
345
345
|
return this.transport.post("/auth/mfa/setup", void 0, this.authHeaders());
|
|
346
346
|
}
|
|
347
|
-
async verifyMfa(
|
|
348
|
-
return this.transport.post("/auth/mfa/verify", { code:
|
|
347
|
+
async verifyMfa(t) {
|
|
348
|
+
return this.transport.post("/auth/mfa/verify", { code: t }, this.authHeaders());
|
|
349
349
|
}
|
|
350
|
-
async disableMfa(
|
|
351
|
-
await this.transport.post("/auth/mfa/disable", { code:
|
|
350
|
+
async disableMfa(t) {
|
|
351
|
+
await this.transport.post("/auth/mfa/disable", { code: t }, this.authHeaders());
|
|
352
352
|
}
|
|
353
353
|
// ---------------------------------------------------------------------------
|
|
354
354
|
// Token & user access
|
|
355
355
|
// ---------------------------------------------------------------------------
|
|
356
356
|
async getToken() {
|
|
357
|
-
var
|
|
358
|
-
const
|
|
359
|
-
if (
|
|
357
|
+
var e, s, r;
|
|
358
|
+
const t = ((e = this.tokenManager) == null ? void 0 : e.getAccessToken()) ?? null;
|
|
359
|
+
if (t) return t;
|
|
360
360
|
if ((s = this.tokenManager) != null && s.hasRefreshToken())
|
|
361
361
|
try {
|
|
362
362
|
return await this.tokenManager.refreshOnce(), ((r = this.tokenManager) == null ? void 0 : r.getAccessToken()) ?? null;
|
|
@@ -367,10 +367,10 @@ class $ {
|
|
|
367
367
|
}
|
|
368
368
|
async getUser() {
|
|
369
369
|
if (this.cachedUser) return this.cachedUser;
|
|
370
|
-
const
|
|
371
|
-
if (!
|
|
370
|
+
const t = await this.getToken();
|
|
371
|
+
if (!t) return null;
|
|
372
372
|
try {
|
|
373
|
-
return this.cachedUser = await this.transport.get("/auth/me", { Authorization: `Bearer ${
|
|
373
|
+
return this.cachedUser = await this.transport.get("/auth/me", { Authorization: `Bearer ${t}` }), this.cachedUser;
|
|
374
374
|
} catch {
|
|
375
375
|
return null;
|
|
376
376
|
}
|
|
@@ -389,22 +389,22 @@ class $ {
|
|
|
389
389
|
return null;
|
|
390
390
|
}
|
|
391
391
|
}
|
|
392
|
-
onAuthStateChange(
|
|
393
|
-
return this.emitter.on("authStateChange",
|
|
392
|
+
onAuthStateChange(t) {
|
|
393
|
+
return this.emitter.on("authStateChange", t);
|
|
394
394
|
}
|
|
395
395
|
// ---------------------------------------------------------------------------
|
|
396
396
|
// Profile
|
|
397
397
|
// ---------------------------------------------------------------------------
|
|
398
|
-
async updateProfile(
|
|
399
|
-
const
|
|
400
|
-
return this.cachedUser =
|
|
398
|
+
async updateProfile(t) {
|
|
399
|
+
const e = await this.transport.patch("/auth/me", t, this.authHeaders());
|
|
400
|
+
return this.cachedUser = e, this.emitter.emit("authStateChange", e), e;
|
|
401
401
|
}
|
|
402
|
-
async uploadAvatar(
|
|
403
|
-
const
|
|
404
|
-
return this.cachedUser && (this.cachedUser = { ...this.cachedUser, avatarUrl:
|
|
402
|
+
async uploadAvatar(t) {
|
|
403
|
+
const e = await this.transport.uploadBinary("/auth/avatar", t, this.authHeaders());
|
|
404
|
+
return this.cachedUser && (this.cachedUser = { ...this.cachedUser, avatarUrl: e.avatarUrl }, this.emitter.emit("authStateChange", this.cachedUser)), e;
|
|
405
405
|
}
|
|
406
|
-
async changePassword(
|
|
407
|
-
await this.transport.post("/auth/change-password", { currentPassword:
|
|
406
|
+
async changePassword(t, e) {
|
|
407
|
+
await this.transport.post("/auth/change-password", { currentPassword: t, newPassword: e }, this.authHeaders());
|
|
408
408
|
}
|
|
409
409
|
// ---------------------------------------------------------------------------
|
|
410
410
|
// Organizations
|
|
@@ -412,35 +412,44 @@ class $ {
|
|
|
412
412
|
async listOrgs() {
|
|
413
413
|
return this.transport.get("/auth/orgs", this.authHeaders());
|
|
414
414
|
}
|
|
415
|
-
async createOrg(
|
|
416
|
-
return this.transport.post("/auth/orgs", { name:
|
|
415
|
+
async createOrg(t, e) {
|
|
416
|
+
return this.transport.post("/auth/orgs", { name: t, slug: e }, this.authHeaders());
|
|
417
417
|
}
|
|
418
|
-
async getOrg(
|
|
419
|
-
return this.transport.get(`/auth/orgs/${
|
|
418
|
+
async getOrg(t) {
|
|
419
|
+
return this.transport.get(`/auth/orgs/${t}`, this.authHeaders());
|
|
420
420
|
}
|
|
421
|
-
async updateOrg(
|
|
422
|
-
return this.transport.patch(`/auth/orgs/${
|
|
421
|
+
async updateOrg(t, e) {
|
|
422
|
+
return this.transport.patch(`/auth/orgs/${t}`, e, this.authHeaders());
|
|
423
423
|
}
|
|
424
|
-
async deleteOrg(
|
|
425
|
-
await this.transport.del(`/auth/orgs/${
|
|
424
|
+
async deleteOrg(t) {
|
|
425
|
+
await this.transport.del(`/auth/orgs/${t}`, this.authHeaders());
|
|
426
426
|
}
|
|
427
427
|
// ---------------------------------------------------------------------------
|
|
428
428
|
// Members & Invites
|
|
429
429
|
// ---------------------------------------------------------------------------
|
|
430
|
-
async listMembers(
|
|
431
|
-
return this.transport.get(`/auth/orgs/${
|
|
430
|
+
async listMembers(t) {
|
|
431
|
+
return this.transport.get(`/auth/orgs/${t}/members`, this.authHeaders());
|
|
432
432
|
}
|
|
433
|
-
async sendInvite(
|
|
434
|
-
return this.transport.post(`/auth/orgs/${
|
|
433
|
+
async sendInvite(t, e, s) {
|
|
434
|
+
return this.transport.post(`/auth/orgs/${t}/invites`, { email: e, role: s }, this.authHeaders());
|
|
435
435
|
}
|
|
436
|
-
async updateMemberRole(
|
|
437
|
-
await this.transport.patch(`/auth/orgs/${
|
|
436
|
+
async updateMemberRole(t, e, s) {
|
|
437
|
+
await this.transport.patch(`/auth/orgs/${t}/members/${e}`, { role: s }, this.authHeaders());
|
|
438
438
|
}
|
|
439
|
-
async removeMember(
|
|
440
|
-
await this.transport.del(`/auth/orgs/${
|
|
439
|
+
async removeMember(t, e) {
|
|
440
|
+
await this.transport.del(`/auth/orgs/${t}/members/${e}`, this.authHeaders());
|
|
441
441
|
}
|
|
442
|
-
async acceptInvite(
|
|
443
|
-
return this.transport.post(`/auth/invites/${
|
|
442
|
+
async acceptInvite(t) {
|
|
443
|
+
return this.transport.post(`/auth/invites/${t}/accept`, void 0, this.authHeaders());
|
|
444
|
+
}
|
|
445
|
+
async listInvites(t) {
|
|
446
|
+
return this.transport.get(`/auth/orgs/${t}/invites`, this.authHeaders());
|
|
447
|
+
}
|
|
448
|
+
async revokeInvite(t, e) {
|
|
449
|
+
await this.transport.del(`/auth/orgs/${t}/invites/${e}`, this.authHeaders());
|
|
450
|
+
}
|
|
451
|
+
async deleteAccount() {
|
|
452
|
+
await this.transport.del("/auth/account", this.authHeaders()), this.clearSession();
|
|
444
453
|
}
|
|
445
454
|
// ---------------------------------------------------------------------------
|
|
446
455
|
// Internal
|
|
@@ -452,144 +461,144 @@ class $ {
|
|
|
452
461
|
/** @internal */
|
|
453
462
|
async performRefresh() {
|
|
454
463
|
var s;
|
|
455
|
-
const
|
|
456
|
-
if (!
|
|
457
|
-
const
|
|
464
|
+
const t = (s = this.tokenManager) == null ? void 0 : s.getRefreshToken();
|
|
465
|
+
if (!t) throw new Error("No refresh token");
|
|
466
|
+
const e = await this.transport.post(
|
|
458
467
|
"/auth/refresh",
|
|
459
|
-
{ refreshToken:
|
|
468
|
+
{ refreshToken: t }
|
|
460
469
|
);
|
|
461
|
-
if (this.tokenManager.setTokens(
|
|
470
|
+
if (this.tokenManager.setTokens(e.accessToken, e.refreshToken), !this.cachedUser)
|
|
462
471
|
try {
|
|
463
|
-
this.cachedUser = await this.transport.get("/auth/me", { Authorization: `Bearer ${
|
|
472
|
+
this.cachedUser = await this.transport.get("/auth/me", { Authorization: `Bearer ${e.accessToken}` }), this.emitter.emit("authStateChange", this.cachedUser);
|
|
464
473
|
} catch {
|
|
465
474
|
}
|
|
466
475
|
}
|
|
467
|
-
setSession(
|
|
468
|
-
var
|
|
469
|
-
(
|
|
476
|
+
setSession(t) {
|
|
477
|
+
var e;
|
|
478
|
+
(e = this.tokenManager) == null || e.setTokens(t.accessToken, t.refreshToken), this.cachedUser = t.user, this.emitter.emit("authStateChange", t.user);
|
|
470
479
|
}
|
|
471
480
|
clearSession() {
|
|
472
|
-
var
|
|
473
|
-
(
|
|
481
|
+
var t;
|
|
482
|
+
(t = this.tokenManager) == null || t.clearTokens(), this.cachedUser = null, this.emitter.emit("authStateChange", null);
|
|
474
483
|
}
|
|
475
484
|
authHeaders() {
|
|
476
|
-
var
|
|
477
|
-
const
|
|
478
|
-
return
|
|
485
|
+
var e;
|
|
486
|
+
const t = (e = this.tokenManager) == null ? void 0 : e.getAccessToken();
|
|
487
|
+
return t ? { Authorization: `Bearer ${t}` } : {};
|
|
479
488
|
}
|
|
480
489
|
}
|
|
481
490
|
class R {
|
|
482
|
-
constructor(
|
|
483
|
-
this.transport =
|
|
491
|
+
constructor(t) {
|
|
492
|
+
this.transport = t;
|
|
484
493
|
}
|
|
485
494
|
// --- Customer ---
|
|
486
|
-
async createCustomer(
|
|
487
|
-
return this.transport.post("/billing/customers",
|
|
495
|
+
async createCustomer(t) {
|
|
496
|
+
return this.transport.post("/billing/customers", t);
|
|
488
497
|
}
|
|
489
|
-
async getCustomer(
|
|
490
|
-
return this.transport.get(`/billing/customers/${
|
|
498
|
+
async getCustomer(t) {
|
|
499
|
+
return this.transport.get(`/billing/customers/${t}`);
|
|
491
500
|
}
|
|
492
|
-
async updateCustomer(
|
|
493
|
-
return this.transport.patch(`/billing/customers/${
|
|
501
|
+
async updateCustomer(t, e) {
|
|
502
|
+
return this.transport.patch(`/billing/customers/${t}`, e);
|
|
494
503
|
}
|
|
495
504
|
// --- Subscription ---
|
|
496
|
-
async subscribe(
|
|
497
|
-
return this.transport.post(`/billing/customers/${
|
|
505
|
+
async subscribe(t, e) {
|
|
506
|
+
return this.transport.post(`/billing/customers/${t}/subscribe`, { planId: e });
|
|
498
507
|
}
|
|
499
|
-
async changePlan(
|
|
500
|
-
return this.transport.patch(`/billing/customers/${
|
|
508
|
+
async changePlan(t, e) {
|
|
509
|
+
return this.transport.patch(`/billing/customers/${t}/subscription`, { planId: e });
|
|
501
510
|
}
|
|
502
|
-
async cancelSubscription(
|
|
503
|
-
return this.transport.del(`/billing/customers/${
|
|
511
|
+
async cancelSubscription(t) {
|
|
512
|
+
return this.transport.del(`/billing/customers/${t}/subscription`);
|
|
504
513
|
}
|
|
505
514
|
// --- Invoices ---
|
|
506
|
-
async getInvoices(
|
|
507
|
-
return this.transport.get(`/billing/customers/${
|
|
515
|
+
async getInvoices(t) {
|
|
516
|
+
return this.transport.get(`/billing/customers/${t}/invoices`);
|
|
508
517
|
}
|
|
509
518
|
// --- Usage ---
|
|
510
|
-
async ingestUsageEvent(
|
|
511
|
-
return this.transport.post("/billing/events",
|
|
519
|
+
async ingestUsageEvent(t) {
|
|
520
|
+
return this.transport.post("/billing/events", t);
|
|
512
521
|
}
|
|
513
|
-
async getCurrentUsage(
|
|
514
|
-
return this.transport.get(`/billing/customers/${
|
|
522
|
+
async getCurrentUsage(t) {
|
|
523
|
+
return this.transport.get(`/billing/customers/${t}/usage`);
|
|
515
524
|
}
|
|
516
525
|
// --- Portal ---
|
|
517
|
-
async createPortalToken(
|
|
518
|
-
return this.transport.post("/billing/portal-tokens", { customerId:
|
|
526
|
+
async createPortalToken(t, e) {
|
|
527
|
+
return this.transport.post("/billing/portal-tokens", { customerId: t, expiresIn: e });
|
|
519
528
|
}
|
|
520
529
|
// --- Coupon ---
|
|
521
|
-
async applyCoupon(
|
|
522
|
-
return this.transport.post(`/billing/customers/${
|
|
530
|
+
async applyCoupon(t, e) {
|
|
531
|
+
return this.transport.post(`/billing/customers/${t}/coupon`, { code: e });
|
|
523
532
|
}
|
|
524
533
|
}
|
|
525
|
-
class
|
|
526
|
-
constructor(
|
|
527
|
-
this.transport =
|
|
534
|
+
class v {
|
|
535
|
+
constructor(t) {
|
|
536
|
+
this.transport = t;
|
|
528
537
|
}
|
|
529
538
|
// --- Query ---
|
|
530
|
-
async executeQuery(
|
|
531
|
-
return this.transport.post("/report/query",
|
|
539
|
+
async executeQuery(t) {
|
|
540
|
+
return this.transport.post("/report/query", t);
|
|
532
541
|
}
|
|
533
542
|
// --- Saved Queries ---
|
|
534
|
-
async listQueries(
|
|
535
|
-
const
|
|
536
|
-
return this.transport.get(`/report/queries${
|
|
543
|
+
async listQueries(t) {
|
|
544
|
+
const e = t ? this.toQueryString(t) : "";
|
|
545
|
+
return this.transport.get(`/report/queries${e}`);
|
|
537
546
|
}
|
|
538
|
-
async saveQuery(
|
|
539
|
-
return this.transport.post("/report/queries",
|
|
547
|
+
async saveQuery(t) {
|
|
548
|
+
return this.transport.post("/report/queries", t);
|
|
540
549
|
}
|
|
541
|
-
async updateQuery(
|
|
542
|
-
return this.transport.patch(`/report/queries/${
|
|
550
|
+
async updateQuery(t, e) {
|
|
551
|
+
return this.transport.patch(`/report/queries/${t}`, e);
|
|
543
552
|
}
|
|
544
|
-
async deleteQuery(
|
|
545
|
-
await this.transport.del(`/report/queries/${
|
|
553
|
+
async deleteQuery(t) {
|
|
554
|
+
await this.transport.del(`/report/queries/${t}`);
|
|
546
555
|
}
|
|
547
556
|
// --- Dashboards ---
|
|
548
|
-
async listDashboards(
|
|
549
|
-
const
|
|
550
|
-
return this.transport.get(`/report/dashboards${
|
|
557
|
+
async listDashboards(t) {
|
|
558
|
+
const e = t ? this.toQueryString(t) : "";
|
|
559
|
+
return this.transport.get(`/report/dashboards${e}`);
|
|
551
560
|
}
|
|
552
|
-
async createDashboard(
|
|
553
|
-
return this.transport.post("/report/dashboards",
|
|
561
|
+
async createDashboard(t) {
|
|
562
|
+
return this.transport.post("/report/dashboards", t);
|
|
554
563
|
}
|
|
555
|
-
async getDashboard(
|
|
556
|
-
return this.transport.get(`/report/dashboards/${
|
|
564
|
+
async getDashboard(t) {
|
|
565
|
+
return this.transport.get(`/report/dashboards/${t}`);
|
|
557
566
|
}
|
|
558
|
-
async updateDashboard(
|
|
559
|
-
return this.transport.patch(`/report/dashboards/${
|
|
567
|
+
async updateDashboard(t, e) {
|
|
568
|
+
return this.transport.patch(`/report/dashboards/${t}`, e);
|
|
560
569
|
}
|
|
561
|
-
async deleteDashboard(
|
|
562
|
-
await this.transport.del(`/report/dashboards/${
|
|
570
|
+
async deleteDashboard(t) {
|
|
571
|
+
await this.transport.del(`/report/dashboards/${t}`);
|
|
563
572
|
}
|
|
564
573
|
// --- Embed Tokens ---
|
|
565
|
-
async createEmbedToken(
|
|
566
|
-
return this.transport.post("/report/embed-tokens",
|
|
574
|
+
async createEmbedToken(t) {
|
|
575
|
+
return this.transport.post("/report/embed-tokens", t);
|
|
567
576
|
}
|
|
568
577
|
async listEmbedTokens() {
|
|
569
578
|
return this.transport.get("/report/embed-tokens");
|
|
570
579
|
}
|
|
571
|
-
async revokeEmbedToken(
|
|
572
|
-
await this.transport.del(`/report/embed-tokens/${
|
|
580
|
+
async revokeEmbedToken(t) {
|
|
581
|
+
await this.transport.del(`/report/embed-tokens/${t}`);
|
|
573
582
|
}
|
|
574
|
-
toQueryString(
|
|
575
|
-
const
|
|
576
|
-
return
|
|
583
|
+
toQueryString(t) {
|
|
584
|
+
const e = Object.entries(t).filter(([, s]) => s != null && s !== "");
|
|
585
|
+
return e.length === 0 ? "" : "?" + e.map(([s, r]) => `${s}=${encodeURIComponent(String(r))}`).join("&");
|
|
577
586
|
}
|
|
578
587
|
}
|
|
579
|
-
const
|
|
588
|
+
const M = "https://api.saas-support.com/v1";
|
|
580
589
|
class H {
|
|
581
|
-
constructor(
|
|
582
|
-
if (this.tokenManager = null, this.loaded = !1, !
|
|
590
|
+
constructor(t) {
|
|
591
|
+
if (this.tokenManager = null, this.loaded = !1, !t.publishableKey && !t.apiKey)
|
|
583
592
|
throw new Error("SaaSSupport: either publishableKey or apiKey is required");
|
|
584
|
-
const
|
|
593
|
+
const e = t.baseUrl ?? M;
|
|
585
594
|
this.emitter = new S();
|
|
586
|
-
const s =
|
|
587
|
-
|
|
595
|
+
const s = t.publishableKey ? new k(e, { type: "publishableKey", key: t.publishableKey }) : null, r = t.apiKey ? new k(e, { type: "apiKey", key: t.apiKey }) : null;
|
|
596
|
+
t.publishableKey && (this.tokenManager = new b(t.publishableKey)), this.auth = new $(
|
|
588
597
|
s ?? r,
|
|
589
598
|
this.tokenManager,
|
|
590
599
|
this.emitter,
|
|
591
|
-
|
|
592
|
-
), this.billing = new R(r ?? s), this.report = new
|
|
600
|
+
e
|
|
601
|
+
), this.billing = new R(r ?? s), this.report = new v(r ?? s), this.tokenManager && (this.tokenManager.setRefreshCallback(() => this.auth.performRefresh()), this.tokenManager.setTokensChangedCallback(() => {
|
|
593
602
|
this.tokenManager.hasRefreshToken() || this.auth.handleExternalLogout();
|
|
594
603
|
})), this.tokenManager && s && s.setUnauthorizedHandler(async () => {
|
|
595
604
|
try {
|
|
@@ -605,12 +614,12 @@ class H {
|
|
|
605
614
|
isLoaded() {
|
|
606
615
|
return this.loaded;
|
|
607
616
|
}
|
|
608
|
-
onError(
|
|
609
|
-
return this.emitter.on("error",
|
|
617
|
+
onError(t) {
|
|
618
|
+
return this.emitter.on("error", t);
|
|
610
619
|
}
|
|
611
620
|
destroy() {
|
|
612
|
-
var
|
|
613
|
-
(
|
|
621
|
+
var t;
|
|
622
|
+
(t = this.tokenManager) == null || t.destroy(), this.emitter.removeAll();
|
|
614
623
|
}
|
|
615
624
|
}
|
|
616
625
|
function E(o) {
|
|
@@ -619,7 +628,7 @@ function E(o) {
|
|
|
619
628
|
export {
|
|
620
629
|
$ as AuthClient,
|
|
621
630
|
R as BillingClient,
|
|
622
|
-
|
|
631
|
+
v as ReportClient,
|
|
623
632
|
g as SaaSError,
|
|
624
633
|
H as SaaSSupport,
|
|
625
634
|
k as Transport,
|