@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.js CHANGED
@@ -1,6 +1,6 @@
1
1
  class g extends Error {
2
- constructor(e, t, s = "unknown") {
3
- super(t), this.name = "SaaSError", this.code = e, this.domain = s;
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(e, t) {
23
- this.onUnauthorized = null, this.baseUrl = e, this.authMode = t;
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(e) {
27
- this.onUnauthorized = e;
26
+ setUnauthorizedHandler(t) {
27
+ this.onUnauthorized = t;
28
28
  }
29
- async request(e, t, s, r) {
29
+ async request(t, e, s, r) {
30
30
  try {
31
- return await this.doRequest(e, t, s, r);
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(e, t, s, {
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(e, t) {
45
- return this.request("GET", e, void 0, t);
44
+ async get(t, e) {
45
+ return this.request("GET", t, void 0, e);
46
46
  }
47
- async post(e, t, s) {
48
- return this.request("POST", e, t, s);
47
+ async post(t, e, s) {
48
+ return this.request("POST", t, e, s);
49
49
  }
50
- async patch(e, t, s) {
51
- return this.request("PATCH", e, t, s);
50
+ async patch(t, e, s) {
51
+ return this.request("PATCH", t, e, s);
52
52
  }
53
- async del(e, t) {
54
- return this.request("DELETE", e, void 0, t);
53
+ async del(t, e) {
54
+ return this.request("DELETE", t, void 0, e);
55
55
  }
56
- async uploadBinary(e, t, s) {
56
+ async uploadBinary(t, e, s) {
57
57
  try {
58
- return await this.doUploadBinary(e, t, s);
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(e, t, {
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(e, t, s) {
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}${e}`, {
76
+ }, i = await (await fetch(`${this.baseUrl}${t}`, {
77
77
  method: "POST",
78
78
  headers: r,
79
- body: t
79
+ body: e
80
80
  })).json();
81
81
  if (i.code && i.code >= 400) {
82
- const a = this.inferDomain(e);
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(e, t, s, r) {
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}${t}`, {
93
- method: e,
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(t);
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(e) {
114
- return e.startsWith("/auth") ? "auth" : e.startsWith("/billing") ? "billing" : e.startsWith("/report") ? "report" : "unknown";
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(e) {
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_${e.slice(0, 12)}`, this.refreshToken = this.loadRefreshToken(), typeof window < "u" && (this.boundHandleStorage = this.handleStorageEvent.bind(this), window.addEventListener("storage", this.boundHandleStorage));
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(e) {
122
- this.onRefreshNeeded = e;
121
+ setRefreshCallback(t) {
122
+ this.onRefreshNeeded = t;
123
123
  }
124
- setTokensChangedCallback(e) {
125
- this.onTokensChanged = e;
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(e, t) {
137
- this.accessToken = e, this.refreshToken = t, this.saveRefreshToken(t), this.scheduleRefresh(e);
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 e = this.loadRefreshToken();
161
- e && e !== this.refreshToken && (this.refreshToken = e), await this.onRefreshNeeded();
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(e) {
165
+ scheduleRefresh(t) {
166
166
  this.refreshTimer && clearTimeout(this.refreshTimer);
167
- const t = this.getTokenExpiry(e);
168
- if (!t) return;
169
- const s = t * 1e3 - Date.now() - 6e4;
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(e) {
181
- var t;
182
- if (e.key === this.storageKey) {
183
- if (e.newValue === null) {
184
- this.accessToken = null, this.refreshToken = null, this.refreshTimer && (clearTimeout(this.refreshTimer), this.refreshTimer = null), (t = this.onTokensChanged) == null || t.call(this);
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
- e.newValue !== this.refreshToken && (this.refreshToken = e.newValue, this.accessToken = null, this.refreshTimer && (clearTimeout(this.refreshTimer), this.refreshTimer = null));
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(e) {
190
+ getTokenExpiry(t) {
191
191
  try {
192
- const t = e.split(".")[1];
193
- return JSON.parse(atob(t)).exp ?? null;
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(e) {
205
+ saveRefreshToken(t) {
206
206
  try {
207
- localStorage.setItem(this.storageKey, e);
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(e, t) {
223
- return this.listeners.has(e) || this.listeners.set(e, /* @__PURE__ */ new Set()), this.listeners.get(e).add(t), () => {
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(e)) == null || s.delete(t);
225
+ (s = this.listeners.get(t)) == null || s.delete(e);
226
226
  };
227
227
  }
228
- emit(e, t) {
228
+ emit(t, e) {
229
229
  var s;
230
- (s = this.listeners.get(e)) == null || s.forEach((r) => r(t));
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(e, t, s, r) {
239
- this.cachedUser = null, this.cachedSettings = null, this.loaded = !1, this.transport = e, this.tokenManager = t, this.emitter = s, this.baseUrl = r;
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 e, t;
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 ((e = this.tokenManager) != null && e.hasRefreshToken())
252
+ if ((t = this.tokenManager) != null && t.hasRefreshToken())
253
253
  try {
254
254
  await this.performRefresh();
255
255
  } catch {
256
- (t = this.tokenManager) == null || t.clearTokens();
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(e, t) {
265
- const s = await this.transport.post("/auth/login", { email: e, password: t });
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(e, t) {
272
- const s = await this.transport.post("/auth/register", { email: e, password: t });
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 t;
277
- const e = (t = this.tokenManager) == null ? void 0 : t.getRefreshToken();
278
- if (e)
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: e });
280
+ await this.transport.post("/auth/logout", { refreshToken: t });
281
281
  } catch {
282
282
  }
283
283
  this.clearSession();
284
284
  }
285
- async signInWithOAuth(e) {
286
- const t = `${this.baseUrl}/auth/oauth/${e}/popup-callback`, { authUrl: s, state: r } = await this.transport.get(
287
- `/auth/oauth/${e}?redirect_uri=${encodeURIComponent(t)}`
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/${e}/callback`,
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(e, t) {
322
- const s = await this.transport.post("/auth/login/mfa", { mfaToken: e, code: t });
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(e, t) {
329
- await this.transport.post("/auth/magic-link/send", { email: e, redirectUrl: t });
328
+ async sendMagicLink(t, e) {
329
+ await this.transport.post("/auth/magic-link/send", { email: t, redirectUrl: e });
330
330
  }
331
- async verifyMagicLink(e) {
332
- const t = await this.transport.post("/auth/magic-link/verify", { token: e });
333
- return this.setSession(t), t;
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(e, t) {
336
- await this.transport.post("/auth/password-reset/send", { email: e, redirectUrl: t });
335
+ async sendPasswordReset(t, e) {
336
+ await this.transport.post("/auth/password-reset/send", { email: t, redirectUrl: e });
337
337
  }
338
- async resetPassword(e, t) {
339
- await this.transport.post("/auth/password-reset/verify", { token: e, newPassword: t });
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(e) {
348
- return this.transport.post("/auth/mfa/verify", { code: e }, this.authHeaders());
347
+ async verifyMfa(t) {
348
+ return this.transport.post("/auth/mfa/verify", { code: t }, this.authHeaders());
349
349
  }
350
- async disableMfa(e) {
351
- await this.transport.post("/auth/mfa/disable", { code: e }, this.authHeaders());
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 t, s, r;
358
- const e = ((t = this.tokenManager) == null ? void 0 : t.getAccessToken()) ?? null;
359
- if (e) return e;
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 e = await this.getToken();
371
- if (!e) return null;
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 ${e}` }), this.cachedUser;
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(e) {
393
- return this.emitter.on("authStateChange", e);
392
+ onAuthStateChange(t) {
393
+ return this.emitter.on("authStateChange", t);
394
394
  }
395
395
  // ---------------------------------------------------------------------------
396
396
  // Profile
397
397
  // ---------------------------------------------------------------------------
398
- async updateProfile(e) {
399
- const t = await this.transport.patch("/auth/me", e, this.authHeaders());
400
- return this.cachedUser = t, this.emitter.emit("authStateChange", t), t;
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(e) {
403
- const t = await this.transport.uploadBinary("/auth/avatar", e, this.authHeaders());
404
- return this.cachedUser && (this.cachedUser = { ...this.cachedUser, avatarUrl: t.avatarUrl }, this.emitter.emit("authStateChange", this.cachedUser)), t;
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(e, t) {
407
- await this.transport.post("/auth/change-password", { currentPassword: e, newPassword: t }, this.authHeaders());
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(e, t) {
416
- return this.transport.post("/auth/orgs", { name: e, slug: t }, this.authHeaders());
415
+ async createOrg(t, e) {
416
+ return this.transport.post("/auth/orgs", { name: t, slug: e }, this.authHeaders());
417
417
  }
418
- async getOrg(e) {
419
- return this.transport.get(`/auth/orgs/${e}`, this.authHeaders());
418
+ async getOrg(t) {
419
+ return this.transport.get(`/auth/orgs/${t}`, this.authHeaders());
420
420
  }
421
- async updateOrg(e, t) {
422
- return this.transport.patch(`/auth/orgs/${e}`, t, this.authHeaders());
421
+ async updateOrg(t, e) {
422
+ return this.transport.patch(`/auth/orgs/${t}`, e, this.authHeaders());
423
423
  }
424
- async deleteOrg(e) {
425
- await this.transport.del(`/auth/orgs/${e}`, this.authHeaders());
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(e) {
431
- return this.transport.get(`/auth/orgs/${e}/members`, this.authHeaders());
430
+ async listMembers(t) {
431
+ return this.transport.get(`/auth/orgs/${t}/members`, this.authHeaders());
432
432
  }
433
- async sendInvite(e, t, s) {
434
- return this.transport.post(`/auth/orgs/${e}/invites`, { email: t, role: s }, this.authHeaders());
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(e, t, s) {
437
- await this.transport.patch(`/auth/orgs/${e}/members/${t}`, { role: s }, this.authHeaders());
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(e, t) {
440
- await this.transport.del(`/auth/orgs/${e}/members/${t}`, this.authHeaders());
439
+ async removeMember(t, e) {
440
+ await this.transport.del(`/auth/orgs/${t}/members/${e}`, this.authHeaders());
441
441
  }
442
- async acceptInvite(e) {
443
- return this.transport.post(`/auth/invites/${e}/accept`, void 0, this.authHeaders());
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 e = (s = this.tokenManager) == null ? void 0 : s.getRefreshToken();
456
- if (!e) throw new Error("No refresh token");
457
- const t = await this.transport.post(
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: e }
468
+ { refreshToken: t }
460
469
  );
461
- if (this.tokenManager.setTokens(t.accessToken, t.refreshToken), !this.cachedUser)
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 ${t.accessToken}` }), this.emitter.emit("authStateChange", this.cachedUser);
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(e) {
468
- var t;
469
- (t = this.tokenManager) == null || t.setTokens(e.accessToken, e.refreshToken), this.cachedUser = e.user, this.emitter.emit("authStateChange", e.user);
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 e;
473
- (e = this.tokenManager) == null || e.clearTokens(), this.cachedUser = null, this.emitter.emit("authStateChange", null);
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 t;
477
- const e = (t = this.tokenManager) == null ? void 0 : t.getAccessToken();
478
- return e ? { Authorization: `Bearer ${e}` } : {};
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(e) {
483
- this.transport = e;
491
+ constructor(t) {
492
+ this.transport = t;
484
493
  }
485
494
  // --- Customer ---
486
- async createCustomer(e) {
487
- return this.transport.post("/billing/customers", e);
495
+ async createCustomer(t) {
496
+ return this.transport.post("/billing/customers", t);
488
497
  }
489
- async getCustomer(e) {
490
- return this.transport.get(`/billing/customers/${e}`);
498
+ async getCustomer(t) {
499
+ return this.transport.get(`/billing/customers/${t}`);
491
500
  }
492
- async updateCustomer(e, t) {
493
- return this.transport.patch(`/billing/customers/${e}`, t);
501
+ async updateCustomer(t, e) {
502
+ return this.transport.patch(`/billing/customers/${t}`, e);
494
503
  }
495
504
  // --- Subscription ---
496
- async subscribe(e, t) {
497
- return this.transport.post(`/billing/customers/${e}/subscribe`, { planId: t });
505
+ async subscribe(t, e) {
506
+ return this.transport.post(`/billing/customers/${t}/subscribe`, { planId: e });
498
507
  }
499
- async changePlan(e, t) {
500
- return this.transport.patch(`/billing/customers/${e}/subscription`, { planId: t });
508
+ async changePlan(t, e) {
509
+ return this.transport.patch(`/billing/customers/${t}/subscription`, { planId: e });
501
510
  }
502
- async cancelSubscription(e) {
503
- return this.transport.del(`/billing/customers/${e}/subscription`);
511
+ async cancelSubscription(t) {
512
+ return this.transport.del(`/billing/customers/${t}/subscription`);
504
513
  }
505
514
  // --- Invoices ---
506
- async getInvoices(e) {
507
- return this.transport.get(`/billing/customers/${e}/invoices`);
515
+ async getInvoices(t) {
516
+ return this.transport.get(`/billing/customers/${t}/invoices`);
508
517
  }
509
518
  // --- Usage ---
510
- async ingestUsageEvent(e) {
511
- return this.transport.post("/billing/events", e);
519
+ async ingestUsageEvent(t) {
520
+ return this.transport.post("/billing/events", t);
512
521
  }
513
- async getCurrentUsage(e) {
514
- return this.transport.get(`/billing/customers/${e}/usage`);
522
+ async getCurrentUsage(t) {
523
+ return this.transport.get(`/billing/customers/${t}/usage`);
515
524
  }
516
525
  // --- Portal ---
517
- async createPortalToken(e, t) {
518
- return this.transport.post("/billing/portal-tokens", { customerId: e, expiresIn: t });
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(e, t) {
522
- return this.transport.post(`/billing/customers/${e}/coupon`, { code: t });
530
+ async applyCoupon(t, e) {
531
+ return this.transport.post(`/billing/customers/${t}/coupon`, { code: e });
523
532
  }
524
533
  }
525
- class M {
526
- constructor(e) {
527
- this.transport = e;
534
+ class v {
535
+ constructor(t) {
536
+ this.transport = t;
528
537
  }
529
538
  // --- Query ---
530
- async executeQuery(e) {
531
- return this.transport.post("/report/query", e);
539
+ async executeQuery(t) {
540
+ return this.transport.post("/report/query", t);
532
541
  }
533
542
  // --- Saved Queries ---
534
- async listQueries(e) {
535
- const t = e ? this.toQueryString(e) : "";
536
- return this.transport.get(`/report/queries${t}`);
543
+ async listQueries(t) {
544
+ const e = t ? this.toQueryString(t) : "";
545
+ return this.transport.get(`/report/queries${e}`);
537
546
  }
538
- async saveQuery(e) {
539
- return this.transport.post("/report/queries", e);
547
+ async saveQuery(t) {
548
+ return this.transport.post("/report/queries", t);
540
549
  }
541
- async updateQuery(e, t) {
542
- return this.transport.patch(`/report/queries/${e}`, t);
550
+ async updateQuery(t, e) {
551
+ return this.transport.patch(`/report/queries/${t}`, e);
543
552
  }
544
- async deleteQuery(e) {
545
- await this.transport.del(`/report/queries/${e}`);
553
+ async deleteQuery(t) {
554
+ await this.transport.del(`/report/queries/${t}`);
546
555
  }
547
556
  // --- Dashboards ---
548
- async listDashboards(e) {
549
- const t = e ? this.toQueryString(e) : "";
550
- return this.transport.get(`/report/dashboards${t}`);
557
+ async listDashboards(t) {
558
+ const e = t ? this.toQueryString(t) : "";
559
+ return this.transport.get(`/report/dashboards${e}`);
551
560
  }
552
- async createDashboard(e) {
553
- return this.transport.post("/report/dashboards", e);
561
+ async createDashboard(t) {
562
+ return this.transport.post("/report/dashboards", t);
554
563
  }
555
- async getDashboard(e) {
556
- return this.transport.get(`/report/dashboards/${e}`);
564
+ async getDashboard(t) {
565
+ return this.transport.get(`/report/dashboards/${t}`);
557
566
  }
558
- async updateDashboard(e, t) {
559
- return this.transport.patch(`/report/dashboards/${e}`, t);
567
+ async updateDashboard(t, e) {
568
+ return this.transport.patch(`/report/dashboards/${t}`, e);
560
569
  }
561
- async deleteDashboard(e) {
562
- await this.transport.del(`/report/dashboards/${e}`);
570
+ async deleteDashboard(t) {
571
+ await this.transport.del(`/report/dashboards/${t}`);
563
572
  }
564
573
  // --- Embed Tokens ---
565
- async createEmbedToken(e) {
566
- return this.transport.post("/report/embed-tokens", e);
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(e) {
572
- await this.transport.del(`/report/embed-tokens/${e}`);
580
+ async revokeEmbedToken(t) {
581
+ await this.transport.del(`/report/embed-tokens/${t}`);
573
582
  }
574
- toQueryString(e) {
575
- const t = Object.entries(e).filter(([, s]) => s != null && s !== "");
576
- return t.length === 0 ? "" : "?" + t.map(([s, r]) => `${s}=${encodeURIComponent(String(r))}`).join("&");
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 v = "https://api.saas-support.com/v1";
588
+ const M = "https://api.saas-support.com/v1";
580
589
  class H {
581
- constructor(e) {
582
- if (this.tokenManager = null, this.loaded = !1, !e.publishableKey && !e.apiKey)
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 t = e.baseUrl ?? v;
593
+ const e = t.baseUrl ?? M;
585
594
  this.emitter = new S();
586
- const s = e.publishableKey ? new k(t, { type: "publishableKey", key: e.publishableKey }) : null, r = e.apiKey ? new k(t, { type: "apiKey", key: e.apiKey }) : null;
587
- e.publishableKey && (this.tokenManager = new b(e.publishableKey)), this.auth = new $(
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
- t
592
- ), this.billing = new R(r ?? s), this.report = new M(r ?? s), this.tokenManager && (this.tokenManager.setRefreshCallback(() => this.auth.performRefresh()), this.tokenManager.setTokensChangedCallback(() => {
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(e) {
609
- return this.emitter.on("error", e);
617
+ onError(t) {
618
+ return this.emitter.on("error", t);
610
619
  }
611
620
  destroy() {
612
- var e;
613
- (e = this.tokenManager) == null || e.destroy(), this.emitter.removeAll();
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
- M as ReportClient,
631
+ v as ReportClient,
623
632
  g as SaaSError,
624
633
  H as SaaSSupport,
625
634
  k as Transport,