@authorizerdev/authorizer-js 3.0.0 → 3.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.mjs CHANGED
@@ -9,8 +9,7 @@ var DEFAULT_AUTHORIZE_TIMEOUT_IN_SECONDS = 60;
9
9
  var CLEANUP_IFRAME_TIMEOUT_IN_SECONDS = 2;
10
10
 
11
11
  // src/types.ts
12
- var OAuthProviders;
13
- (function(OAuthProviders2) {
12
+ var OAuthProviders = /* @__PURE__ */ (function(OAuthProviders2) {
14
13
  OAuthProviders2["Apple"] = "apple";
15
14
  OAuthProviders2["Github"] = "github";
16
15
  OAuthProviders2["Google"] = "google";
@@ -21,20 +20,20 @@ var OAuthProviders;
21
20
  OAuthProviders2["Twitch"] = "twitch";
22
21
  OAuthProviders2["Roblox"] = "roblox";
23
22
  OAuthProviders2["Discord"] = "discord";
24
- })(OAuthProviders || (OAuthProviders = {}));
25
- var ResponseTypes;
26
- (function(ResponseTypes2) {
23
+ return OAuthProviders2;
24
+ })({});
25
+ var ResponseTypes = /* @__PURE__ */ (function(ResponseTypes2) {
27
26
  ResponseTypes2["Code"] = "code";
28
27
  ResponseTypes2["Token"] = "token";
29
- })(ResponseTypes || (ResponseTypes = {}));
28
+ return ResponseTypes2;
29
+ })({});
30
30
 
31
31
  // src/utils.ts
32
32
  var hasWindow = /* @__PURE__ */ __name(() => typeof window !== "undefined", "hasWindow");
33
33
  var trimURL = /* @__PURE__ */ __name((url) => {
34
34
  let trimmedData = url.trim();
35
35
  const lastChar = trimmedData[trimmedData.length - 1];
36
- if (lastChar === "/")
37
- trimmedData = trimmedData.slice(0, -1);
36
+ if (lastChar === "/") trimmedData = trimmedData.slice(0, -1);
38
37
  return trimmedData;
39
38
  }, "trimURL");
40
39
  var getCrypto = /* @__PURE__ */ __name(() => {
@@ -59,7 +58,9 @@ var createQueryParams = /* @__PURE__ */ __name((params) => {
59
58
  return Object.keys(params).filter((k) => typeof params[k] !== "undefined").map((k) => `${encodeURIComponent(k)}=${encodeURIComponent(params[k])}`).join("&");
60
59
  }, "createQueryParams");
61
60
  var sha256 = /* @__PURE__ */ __name(async (s) => {
62
- const digestOp = getCryptoSubtle().digest({
61
+ const subtle = getCryptoSubtle();
62
+ if (!subtle) throw new Error("Web Crypto API is not available");
63
+ const digestOp = subtle.digest({
63
64
  name: "SHA-256"
64
65
  }, new TextEncoder().encode(s));
65
66
  if (window.msCrypto) {
@@ -89,8 +90,16 @@ var bufferToBase64UrlEncoded = /* @__PURE__ */ __name((input) => {
89
90
  const ie11SafeInput = new Uint8Array(input);
90
91
  return urlEncodeB64(window.btoa(String.fromCharCode(...Array.from(ie11SafeInput))));
91
92
  }, "bufferToBase64UrlEncoded");
93
+ var originFromAuthorizerUrl = /* @__PURE__ */ __name((authorizerUrl) => {
94
+ try {
95
+ return new URL(authorizerUrl).origin;
96
+ } catch {
97
+ return authorizerUrl;
98
+ }
99
+ }, "originFromAuthorizerUrl");
92
100
  var executeIframe = /* @__PURE__ */ __name((authorizeUrl, eventOrigin, timeoutInSeconds = DEFAULT_AUTHORIZE_TIMEOUT_IN_SECONDS) => {
93
101
  return new Promise((resolve, reject) => {
102
+ const expectedOrigin = originFromAuthorizerUrl(eventOrigin);
94
103
  const iframe = window.document.createElement("iframe");
95
104
  iframe.setAttribute("id", "authorizer-iframe");
96
105
  iframe.setAttribute("width", "0");
@@ -103,17 +112,16 @@ var executeIframe = /* @__PURE__ */ __name((authorizeUrl, eventOrigin, timeoutIn
103
112
  }
104
113
  }, "removeIframe");
105
114
  const timeoutSetTimeoutId = setTimeout(() => {
115
+ reject(new Error("Authorization timeout"));
106
116
  removeIframe();
107
117
  }, timeoutInSeconds * 1e3);
108
118
  const iframeEventHandler = /* @__PURE__ */ __name(function(e) {
109
- if (e.origin !== eventOrigin)
110
- return;
111
- if (!e.data || !e.data.response)
112
- return;
119
+ if (e.origin !== expectedOrigin) return;
120
+ if (!e.data || !e.data.response) return;
113
121
  const eventSource = e.source;
114
- if (eventSource)
115
- eventSource.close();
116
- e.data.response.error ? reject(e.data.response) : resolve(e.data.response);
122
+ if (eventSource) eventSource.close();
123
+ if (e.data.response.error) reject(e.data.response);
124
+ else resolve(e.data.response);
117
125
  clearTimeout(timeoutSetTimeoutId);
118
126
  window.removeEventListener("message", iframeEventHandler, false);
119
127
  setTimeout(removeIframe, CLEANUP_IFRAME_TIMEOUT_IN_SECONDS * 1e3);
@@ -128,44 +136,72 @@ var executeIframe = /* @__PURE__ */ __name((authorizeUrl, eventOrigin, timeoutIn
128
136
  var userFragment = "id email email_verified given_name family_name middle_name nickname preferred_username picture signup_methods gender birthdate phone_number phone_number_verified roles created_at updated_at revoked_timestamp is_multi_factor_auth_enabled app_data";
129
137
  var authTokenFragment = `message access_token expires_in refresh_token id_token should_show_email_otp_screen should_show_mobile_otp_screen should_show_totp_screen authenticator_scanner_image authenticator_secret authenticator_recovery_codes user { ${userFragment} }`;
130
138
  var getFetcher = /* @__PURE__ */ __name(() => hasWindow() ? window.fetch : crossFetch, "getFetcher");
139
+ function toErrorList(errors) {
140
+ if (Array.isArray(errors)) {
141
+ return errors.map((item) => {
142
+ if (item instanceof Error) return item;
143
+ if (item && typeof item === "object" && "message" in item) return new Error(String(item.message));
144
+ return new Error(String(item));
145
+ });
146
+ }
147
+ if (errors instanceof Error) return [
148
+ errors
149
+ ];
150
+ if (errors !== null && typeof errors === "object") {
151
+ const o = errors;
152
+ if (typeof o.error_description === "string") return [
153
+ new Error(o.error_description)
154
+ ];
155
+ if (typeof o.error === "string") {
156
+ const desc = typeof o.error_description === "string" ? `: ${o.error_description}` : "";
157
+ return [
158
+ new Error(`${o.error}${desc}`)
159
+ ];
160
+ }
161
+ if (typeof o.message === "string") return [
162
+ new Error(o.message)
163
+ ];
164
+ }
165
+ if (errors === void 0 || errors === null) return [
166
+ new Error("Unknown error")
167
+ ];
168
+ return [
169
+ new Error(String(errors))
170
+ ];
171
+ }
172
+ __name(toErrorList, "toErrorList");
131
173
  var _Authorizer = class _Authorizer {
132
174
  // class variable
133
175
  config;
134
176
  codeVerifier;
135
177
  // constructor
136
178
  constructor(config) {
137
- if (!config)
138
- throw new Error("Configuration is required");
179
+ var _a, _b;
180
+ if (!config) throw new Error("Configuration is required");
139
181
  this.config = config;
140
- if (!config.authorizerURL && !config.authorizerURL.trim())
141
- throw new Error("Invalid authorizerURL");
142
- if (config.authorizerURL)
143
- this.config.authorizerURL = trimURL(config.authorizerURL);
144
- if (!config.redirectURL && !config.redirectURL.trim())
145
- throw new Error("Invalid redirectURL");
146
- else
147
- this.config.redirectURL = trimURL(config.redirectURL);
182
+ if (!((_a = config.authorizerURL) == null ? void 0 : _a.trim())) throw new Error("Invalid authorizerURL");
183
+ this.config.authorizerURL = trimURL(config.authorizerURL);
184
+ if (!((_b = config.redirectURL) == null ? void 0 : _b.trim())) throw new Error("Invalid redirectURL");
185
+ this.config.redirectURL = trimURL(config.redirectURL);
186
+ this.config.clientID = ((config == null ? void 0 : config.clientID) || "").trim();
148
187
  this.config.extraHeaders = {
149
188
  ...config.extraHeaders || {},
150
- "x-authorizer-url": this.config.authorizerURL,
151
- "x-authorizer-client-id": this.config.clientID || "",
189
+ "x-authorizer-url": config.authorizerURL,
190
+ "x-authorizer-client-id": config.clientID || "",
152
191
  "Content-Type": "application/json"
153
192
  };
154
- this.config.clientID = ((config == null ? void 0 : config.clientID) || "").trim();
155
193
  }
156
- authorize = async (data) => {
194
+ authorize = /* @__PURE__ */ __name(async (data) => {
157
195
  var _a;
158
- if (!hasWindow())
159
- return this.errorResponse([
160
- new Error("this feature is only supported in browser")
161
- ]);
196
+ if (!hasWindow()) return this.errorResponse([
197
+ new Error("this feature is only supported in browser")
198
+ ]);
162
199
  const scopes = [
163
200
  "openid",
164
201
  "profile",
165
202
  "email"
166
203
  ];
167
- if (data.use_refresh_token)
168
- scopes.push("offline_access");
204
+ if (data.use_refresh_token) scopes.push("offline_access");
169
205
  const requestData = {
170
206
  redirect_uri: this.config.redirectURL,
171
207
  response_mode: data.response_mode || "web_message",
@@ -180,6 +216,7 @@ var _Authorizer = class _Authorizer {
180
216
  const sha = await sha256(this.codeVerifier);
181
217
  const codeChallenge = bufferToBase64UrlEncoded(sha);
182
218
  requestData.code_challenge = codeChallenge;
219
+ requestData.code_challenge_method = "S256";
183
220
  }
184
221
  const authorizeURL = `${this.config.authorizerURL}/authorize?${createQueryParams(requestData)}`;
185
222
  if (requestData.response_mode !== "web_message") {
@@ -197,12 +234,16 @@ var _Authorizer = class _Authorizer {
197
234
  return this.okResponse(iframeRes);
198
235
  } catch (err) {
199
236
  if (err.error) {
200
- window.location.replace(`${this.config.authorizerURL}/app?state=${encode(JSON.stringify(this.config))}&redirect_uri=${this.config.redirectURL}`);
237
+ window.location.replace(`${this.config.authorizerURL}/app?state=${encode(JSON.stringify({
238
+ clientID: this.config.clientID,
239
+ redirectURL: this.config.redirectURL,
240
+ authorizerURL: this.config.authorizerURL
241
+ }))}&redirect_uri=${encodeURIComponent(this.config.redirectURL || "")}`);
201
242
  }
202
243
  return this.errorResponse(err);
203
244
  }
204
- };
205
- browserLogin = async () => {
245
+ }, "authorize");
246
+ browserLogin = /* @__PURE__ */ __name(async () => {
206
247
  try {
207
248
  const tokenResp = await this.getSession();
208
249
  return tokenResp.errors.length ? this.errorResponse(tokenResp.errors) : this.okResponse(tokenResp.data);
@@ -215,16 +256,18 @@ var _Authorizer = class _Authorizer {
215
256
  ]
216
257
  };
217
258
  }
218
- window.location.replace(`${this.config.authorizerURL}/app?state=${encode(JSON.stringify(this.config))}&redirect_uri=${this.config.redirectURL}`);
259
+ window.location.replace(`${this.config.authorizerURL}/app?state=${encode(JSON.stringify({
260
+ clientID: this.config.clientID,
261
+ redirectURL: this.config.redirectURL,
262
+ authorizerURL: this.config.authorizerURL
263
+ }))}&redirect_uri=${encodeURIComponent(this.config.redirectURL || "")}`);
219
264
  return this.errorResponse(err);
220
265
  }
221
- };
222
- forgotPassword = async (data) => {
223
- var _a;
224
- if (!data.state)
225
- data.state = encode(createRandomString());
226
- if (!data.redirect_uri)
227
- data.redirect_uri = this.config.redirectURL;
266
+ }, "browserLogin");
267
+ forgotPassword = /* @__PURE__ */ __name(async (data) => {
268
+ var _a, _b;
269
+ if (!data.state) data.state = encode(createRandomString());
270
+ if (!data.redirect_uri) data.redirect_uri = this.config.redirectURL;
228
271
  try {
229
272
  const forgotPasswordResp = await this.graphqlQuery({
230
273
  query: "mutation forgotPassword($data: ForgotPasswordRequest!) { forgot_password(params: $data) { message should_show_mobile_otp_screen } }",
@@ -232,14 +275,14 @@ var _Authorizer = class _Authorizer {
232
275
  data
233
276
  }
234
277
  });
235
- return ((_a = forgotPasswordResp == null ? void 0 : forgotPasswordResp.errors) == null ? void 0 : _a.length) ? this.errorResponse(forgotPasswordResp.errors) : this.okResponse(forgotPasswordResp == null ? void 0 : forgotPasswordResp.data.forgot_password);
278
+ return ((_a = forgotPasswordResp == null ? void 0 : forgotPasswordResp.errors) == null ? void 0 : _a.length) ? this.errorResponse(forgotPasswordResp.errors) : this.okResponse((_b = forgotPasswordResp == null ? void 0 : forgotPasswordResp.data) == null ? void 0 : _b.forgot_password);
236
279
  } catch (error) {
237
280
  return this.errorResponse([
238
281
  error
239
282
  ]);
240
283
  }
241
- };
242
- getMetaData = async () => {
284
+ }, "forgotPassword");
285
+ getMetaData = /* @__PURE__ */ __name(async () => {
243
286
  var _a;
244
287
  try {
245
288
  const res = await this.graphqlQuery({
@@ -251,8 +294,8 @@ var _Authorizer = class _Authorizer {
251
294
  error
252
295
  ]);
253
296
  }
254
- };
255
- getProfile = async (headers) => {
297
+ }, "getMetaData");
298
+ getProfile = /* @__PURE__ */ __name(async (headers) => {
256
299
  var _a;
257
300
  try {
258
301
  const profileRes = await this.graphqlQuery({
@@ -265,9 +308,9 @@ var _Authorizer = class _Authorizer {
265
308
  error
266
309
  ]);
267
310
  }
268
- };
311
+ }, "getProfile");
269
312
  // this is used to verify / get session using cookie by default. If using node.js pass authorization header
270
- getSession = async (headers, params) => {
313
+ getSession = /* @__PURE__ */ __name(async (headers, params) => {
271
314
  var _a, _b;
272
315
  try {
273
316
  const res = await this.graphqlQuery({
@@ -281,18 +324,16 @@ var _Authorizer = class _Authorizer {
281
324
  } catch (err) {
282
325
  return this.errorResponse(err);
283
326
  }
284
- };
285
- getToken = async (data) => {
286
- if (!data.grant_type)
287
- data.grant_type = "authorization_code";
288
- if (data.grant_type === "refresh_token" && !data.refresh_token)
289
- return this.errorResponse([
290
- new Error("Invalid refresh_token")
291
- ]);
292
- if (data.grant_type === "authorization_code" && !this.codeVerifier)
293
- return this.errorResponse([
294
- new Error("Invalid code verifier")
295
- ]);
327
+ }, "getSession");
328
+ getToken = /* @__PURE__ */ __name(async (data) => {
329
+ var _a;
330
+ if (!data.grant_type) data.grant_type = "authorization_code";
331
+ if (data.grant_type === "refresh_token" && !((_a = data.refresh_token) == null ? void 0 : _a.trim())) return this.errorResponse([
332
+ new Error("Invalid refresh_token")
333
+ ]);
334
+ if (data.grant_type === "authorization_code" && !this.codeVerifier) return this.errorResponse([
335
+ new Error("Invalid code verifier")
336
+ ]);
296
337
  const requestData = {
297
338
  client_id: this.config.clientID,
298
339
  code: data.code || "",
@@ -310,17 +351,28 @@ var _Authorizer = class _Authorizer {
310
351
  },
311
352
  credentials: "include"
312
353
  });
313
- const json = await res.json();
314
- if (res.status >= 400)
354
+ const text = await res.text();
355
+ let json = {};
356
+ if (text) {
357
+ try {
358
+ json = JSON.parse(text);
359
+ } catch {
360
+ return this.errorResponse([
361
+ new Error(res.ok ? "Invalid JSON from token endpoint" : `HTTP ${res.status}`)
362
+ ]);
363
+ }
364
+ }
365
+ if (!res.ok) {
315
366
  return this.errorResponse([
316
- new Error(json.error_description || json.error)
367
+ new Error(String(json.error_description || json.error || `HTTP ${res.status}`))
317
368
  ]);
369
+ }
318
370
  return this.okResponse(json);
319
371
  } catch (err) {
320
372
  return this.errorResponse(err);
321
373
  }
322
- };
323
- login = async (data) => {
374
+ }, "getToken");
375
+ login = /* @__PURE__ */ __name(async (data) => {
324
376
  var _a, _b;
325
377
  try {
326
378
  const res = await this.graphqlQuery({
@@ -333,32 +385,28 @@ var _Authorizer = class _Authorizer {
333
385
  });
334
386
  return ((_a = res == null ? void 0 : res.errors) == null ? void 0 : _a.length) ? this.errorResponse(res.errors) : this.okResponse((_b = res.data) == null ? void 0 : _b.login);
335
387
  } catch (err) {
336
- return this.errorResponse([
337
- new Error(err)
338
- ]);
388
+ return this.errorResponse(err);
339
389
  }
340
- };
341
- logout = async (headers) => {
390
+ }, "login");
391
+ logout = /* @__PURE__ */ __name(async (headers) => {
342
392
  var _a, _b;
343
393
  try {
344
394
  const res = await this.graphqlQuery({
345
395
  query: " mutation { logout { message } } ",
346
396
  headers
347
397
  });
348
- return ((_a = res == null ? void 0 : res.errors) == null ? void 0 : _a.length) ? this.errorResponse(res.errors) : this.okResponse((_b = res.data) == null ? void 0 : _b.response);
398
+ return ((_a = res == null ? void 0 : res.errors) == null ? void 0 : _a.length) ? this.errorResponse(res.errors) : this.okResponse((_b = res.data) == null ? void 0 : _b.logout);
349
399
  } catch (err) {
350
400
  return this.errorResponse([
351
401
  err
352
402
  ]);
353
403
  }
354
- };
355
- magicLinkLogin = async (data) => {
404
+ }, "logout");
405
+ magicLinkLogin = /* @__PURE__ */ __name(async (data) => {
356
406
  var _a, _b;
357
407
  try {
358
- if (!data.state)
359
- data.state = encode(createRandomString());
360
- if (!data.redirect_uri)
361
- data.redirect_uri = this.config.redirectURL;
408
+ if (!data.state) data.state = encode(createRandomString());
409
+ if (!data.redirect_uri) data.redirect_uri = this.config.redirectURL;
362
410
  const res = await this.graphqlQuery({
363
411
  query: `
364
412
  mutation magicLinkLogin($data: MagicLinkLoginRequest!) { magic_link_login(params: $data) { message }}
@@ -373,22 +421,21 @@ var _Authorizer = class _Authorizer {
373
421
  err
374
422
  ]);
375
423
  }
376
- };
377
- oauthLogin = async (oauthProvider, roles, redirect_uri, state) => {
424
+ }, "magicLinkLogin");
425
+ oauthLogin = /* @__PURE__ */ __name(async (oauthProvider, roles, redirect_uri, state) => {
378
426
  let urlState = state;
379
427
  if (!urlState) {
380
428
  urlState = encode(createRandomString());
381
429
  }
382
- if (!Object.values(OAuthProviders).includes(oauthProvider)) {
383
- throw new Error(`only following oauth providers are supported: ${Object.values(oauthProvider).toString()}`);
430
+ const oauthProviderIds = Object.values(OAuthProviders);
431
+ if (!oauthProviderIds.includes(oauthProvider)) {
432
+ throw new Error(`only following oauth providers are supported: ${oauthProviderIds.join(", ")}`);
384
433
  }
385
- if (!hasWindow())
386
- throw new Error("oauthLogin is only supported for browsers");
387
- if (roles && roles.length)
388
- urlState += `&roles=${roles.join(",")}`;
389
- window.location.replace(`${this.config.authorizerURL}/oauth_login/${oauthProvider}?redirect_uri=${redirect_uri || this.config.redirectURL}&state=${urlState}`);
390
- };
391
- resendOtp = async (data) => {
434
+ if (!hasWindow()) throw new Error("oauthLogin is only supported for browsers");
435
+ if (roles && roles.length) urlState += `&roles=${roles.join(",")}`;
436
+ window.location.replace(`${this.config.authorizerURL}/oauth_login/${oauthProvider}?redirect_uri=${encodeURIComponent(redirect_uri || this.config.redirectURL || "")}&state=${encodeURIComponent(urlState)}`);
437
+ }, "oauthLogin");
438
+ resendOtp = /* @__PURE__ */ __name(async (data) => {
392
439
  var _a, _b;
393
440
  try {
394
441
  const res = await this.graphqlQuery({
@@ -405,8 +452,8 @@ var _Authorizer = class _Authorizer {
405
452
  err
406
453
  ]);
407
454
  }
408
- };
409
- resetPassword = async (data) => {
455
+ }, "resendOtp");
456
+ resetPassword = /* @__PURE__ */ __name(async (data) => {
410
457
  var _a, _b;
411
458
  try {
412
459
  const resetPasswordRes = await this.graphqlQuery({
@@ -421,27 +468,47 @@ var _Authorizer = class _Authorizer {
421
468
  error
422
469
  ]);
423
470
  }
424
- };
425
- revokeToken = async (data) => {
426
- if (!data.refresh_token && !data.refresh_token.trim())
427
- return this.errorResponse([
428
- new Error("Invalid refresh_token")
429
- ]);
430
- const fetcher = getFetcher();
431
- const res = await fetcher(`${this.config.authorizerURL}/oauth/revoke`, {
432
- method: "POST",
433
- headers: {
434
- ...this.config.extraHeaders
435
- },
436
- body: JSON.stringify({
437
- refresh_token: data.refresh_token,
438
- client_id: this.config.clientID
439
- })
440
- });
441
- const responseData = await res.json();
442
- return this.okResponse(responseData);
443
- };
444
- signup = async (data) => {
471
+ }, "resetPassword");
472
+ revokeToken = /* @__PURE__ */ __name(async (data) => {
473
+ var _a;
474
+ if (!((_a = data.refresh_token) == null ? void 0 : _a.trim())) return this.errorResponse([
475
+ new Error("Invalid refresh_token")
476
+ ]);
477
+ try {
478
+ const fetcher = getFetcher();
479
+ const res = await fetcher(`${this.config.authorizerURL}/oauth/revoke`, {
480
+ method: "POST",
481
+ headers: {
482
+ ...this.config.extraHeaders
483
+ },
484
+ body: JSON.stringify({
485
+ refresh_token: data.refresh_token,
486
+ client_id: this.config.clientID
487
+ })
488
+ });
489
+ const text = await res.text();
490
+ let responseData = {};
491
+ if (text) {
492
+ try {
493
+ responseData = JSON.parse(text);
494
+ } catch {
495
+ return this.errorResponse([
496
+ new Error(res.ok ? "Invalid JSON from revoke endpoint" : `HTTP ${res.status}`)
497
+ ]);
498
+ }
499
+ }
500
+ if (!res.ok) {
501
+ const errBody = responseData;
502
+ return this.errorResponse([
503
+ new Error(String(errBody.error_description || errBody.error || `HTTP ${res.status}`))
504
+ ]);
505
+ }
506
+ return this.okResponse(responseData);
507
+ } catch (err) {
508
+ return this.errorResponse(err);
509
+ }
510
+ }, "revokeToken");
511
+ signup = /* @__PURE__ */ __name(async (data) => {
445
512
  var _a, _b;
446
513
  try {
447
514
  const res = await this.graphqlQuery({
@@ -458,8 +525,8 @@ var _Authorizer = class _Authorizer {
458
525
  err
459
526
  ]);
460
527
  }
461
- };
462
- updateProfile = async (data, headers) => {
528
+ }, "signup");
529
+ updateProfile = /* @__PURE__ */ __name(async (data, headers) => {
463
530
  var _a, _b;
464
531
  try {
465
532
  const updateProfileRes = await this.graphqlQuery({
@@ -475,8 +542,8 @@ var _Authorizer = class _Authorizer {
475
542
  error
476
543
  ]);
477
544
  }
478
- };
479
- deactivateAccount = async (headers) => {
545
+ }, "updateProfile");
546
+ deactivateAccount = /* @__PURE__ */ __name(async (headers) => {
480
547
  var _a, _b;
481
548
  try {
482
549
  const res = await this.graphqlQuery({
@@ -489,8 +556,8 @@ var _Authorizer = class _Authorizer {
489
556
  error
490
557
  ]);
491
558
  }
492
- };
493
- validateJWTToken = async (params) => {
559
+ }, "deactivateAccount");
560
+ validateJWTToken = /* @__PURE__ */ __name(async (params) => {
494
561
  var _a, _b;
495
562
  try {
496
563
  const res = await this.graphqlQuery({
@@ -505,8 +572,8 @@ var _Authorizer = class _Authorizer {
505
572
  error
506
573
  ]);
507
574
  }
508
- };
509
- validateSession = async (params) => {
575
+ }, "validateJWTToken");
576
+ validateSession = /* @__PURE__ */ __name(async (params) => {
510
577
  var _a, _b;
511
578
  try {
512
579
  const res = await this.graphqlQuery({
@@ -521,8 +588,8 @@ var _Authorizer = class _Authorizer {
521
588
  error
522
589
  ]);
523
590
  }
524
- };
525
- verifyEmail = async (data) => {
591
+ }, "validateSession");
592
+ verifyEmail = /* @__PURE__ */ __name(async (data) => {
526
593
  var _a, _b;
527
594
  try {
528
595
  const res = await this.graphqlQuery({
@@ -539,8 +606,8 @@ var _Authorizer = class _Authorizer {
539
606
  err
540
607
  ]);
541
608
  }
542
- };
543
- resendVerifyEmail = async (data) => {
609
+ }, "verifyEmail");
610
+ resendVerifyEmail = /* @__PURE__ */ __name(async (data) => {
544
611
  var _a, _b;
545
612
  try {
546
613
  const res = await this.graphqlQuery({
@@ -557,8 +624,8 @@ var _Authorizer = class _Authorizer {
557
624
  err
558
625
  ]);
559
626
  }
560
- };
561
- verifyOtp = async (data) => {
627
+ }, "resendVerifyEmail");
628
+ verifyOtp = /* @__PURE__ */ __name(async (data) => {
562
629
  var _a, _b;
563
630
  try {
564
631
  const res = await this.graphqlQuery({
@@ -575,10 +642,10 @@ var _Authorizer = class _Authorizer {
575
642
  err
576
643
  ]);
577
644
  }
578
- };
645
+ }, "verifyOtp");
579
646
  // helper to execute graphql queries
580
647
  // takes in any query or mutation string as value
581
- graphqlQuery = async (data) => {
648
+ graphqlQuery = /* @__PURE__ */ __name(async (data) => {
582
649
  var _a;
583
650
  const fetcher = getFetcher();
584
651
  const res = await fetcher(`${this.config.authorizerURL}/graphql`, {
@@ -593,30 +660,58 @@ var _Authorizer = class _Authorizer {
593
660
  },
594
661
  credentials: "include"
595
662
  });
596
- const json = await res.json();
663
+ const text = await res.text();
664
+ let json = {};
665
+ if (text) {
666
+ try {
667
+ json = JSON.parse(text);
668
+ } catch {
669
+ return {
670
+ data: void 0,
671
+ errors: [
672
+ new Error(res.ok ? "Invalid JSON from GraphQL endpoint" : `HTTP ${res.status}`)
673
+ ]
674
+ };
675
+ }
676
+ } else if (!res.ok) {
677
+ return {
678
+ data: void 0,
679
+ errors: [
680
+ new Error(`HTTP ${res.status}`)
681
+ ]
682
+ };
683
+ }
597
684
  if ((_a = json == null ? void 0 : json.errors) == null ? void 0 : _a.length) {
598
685
  return {
599
686
  data: void 0,
600
- errors: json.errors
687
+ errors: toErrorList(json.errors)
688
+ };
689
+ }
690
+ if (!res.ok) {
691
+ return {
692
+ data: void 0,
693
+ errors: [
694
+ new Error(`HTTP ${res.status}`)
695
+ ]
601
696
  };
602
697
  }
603
698
  return {
604
699
  data: json.data,
605
700
  errors: []
606
701
  };
607
- };
608
- errorResponse = (errors) => {
702
+ }, "graphqlQuery");
703
+ errorResponse = /* @__PURE__ */ __name((errors) => {
609
704
  return {
610
705
  data: void 0,
611
- errors
706
+ errors: toErrorList(errors)
612
707
  };
613
- };
614
- okResponse = (data) => {
708
+ }, "errorResponse");
709
+ okResponse = /* @__PURE__ */ __name((data) => {
615
710
  return {
616
711
  data,
617
712
  errors: []
618
713
  };
619
- };
714
+ }, "okResponse");
620
715
  };
621
716
  __name(_Authorizer, "Authorizer");
622
717
  var Authorizer = _Authorizer;