@sentry/junior-github 0.74.1 → 0.75.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.
Files changed (3) hide show
  1. package/index.d.ts +2 -4
  2. package/index.js +33 -14
  3. package/package.json +2 -2
package/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { JuniorPluginRegistration } from "@sentry/junior-plugin-api";
1
+ import type { PluginRegistration } from "@sentry/junior-plugin-api";
2
2
 
3
3
  export type GitHubAppPermissionLevel = "read" | "write" | "admin";
4
4
 
@@ -46,6 +46,4 @@ export interface GitHubPluginOptions {
46
46
  }
47
47
 
48
48
  /** Register GitHub manifest content and runtime hooks. */
49
- export function githubPlugin(
50
- options?: GitHubPluginOptions,
51
- ): JuniorPluginRegistration;
49
+ export function githubPlugin(options?: GitHubPluginOptions): PluginRegistration;
package/index.js CHANGED
@@ -290,20 +290,27 @@ function buildOAuthTokenRequest(input) {
290
290
  };
291
291
  }
292
292
 
293
- function parseOAuthError(text) {
294
- if (!text.trim()) {
293
+ function parseOAuthResponseJson(responseText) {
294
+ if (!responseText.trim()) {
295
295
  return undefined;
296
296
  }
297
297
  try {
298
- const parsed = JSON.parse(text);
299
- return isRecord(parsed) && typeof parsed.error === "string"
300
- ? parsed.error
301
- : undefined;
298
+ return JSON.parse(responseText);
302
299
  } catch {
303
300
  return undefined;
304
301
  }
305
302
  }
306
303
 
304
+ function oauthErrorCode(data) {
305
+ return isRecord(data) && typeof data.error === "string"
306
+ ? data.error
307
+ : undefined;
308
+ }
309
+
310
+ function isRejectedRefreshError(errorCode) {
311
+ return errorCode === "bad_refresh_token" || errorCode === "invalid_grant";
312
+ }
313
+
307
314
  function parseOAuthTokenResponse(data, requestedScope) {
308
315
  if (!isRecord(data)) {
309
316
  throw new Error("OAuth token response is invalid");
@@ -358,18 +365,30 @@ async function refreshUserAccessToken(input) {
358
365
  headers: request.headers,
359
366
  body: request.body,
360
367
  });
361
- if (!response.ok) {
362
- const errorCode = parseOAuthError(await response.text());
363
- if (errorCode === "bad_refresh_token" || errorCode === "invalid_grant") {
364
- throw new GitHubUserRefreshRejectedError(
365
- `GitHub user token refresh rejected: ${errorCode}`,
366
- );
367
- }
368
+ const responseText = await response.text();
369
+ const responseData = parseOAuthResponseJson(responseText);
370
+ const errorCode = oauthErrorCode(responseData);
371
+ if (isRejectedRefreshError(errorCode)) {
372
+ throw new GitHubUserRefreshRejectedError(
373
+ `GitHub user token refresh rejected: ${errorCode}`,
374
+ );
375
+ }
376
+ if (!response.ok || errorCode) {
368
377
  throw new Error(
369
378
  `GitHub user token refresh failed: ${response.status}${errorCode ? ` ${errorCode}` : ""}`,
370
379
  );
371
380
  }
372
- return parseOAuthTokenResponse(await response.json(), input.requestedScope);
381
+ try {
382
+ return parseOAuthTokenResponse(responseData, input.requestedScope);
383
+ } catch (error) {
384
+ if (
385
+ error instanceof Error &&
386
+ error.message === "OAuth token response missing access_token"
387
+ ) {
388
+ throw new GitHubUserRefreshRejectedError(error.message);
389
+ }
390
+ throw error;
391
+ }
373
392
  }
374
393
 
375
394
  function leaseExpiry(expiresAt) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sentry/junior-github",
3
- "version": "0.74.1",
3
+ "version": "0.75.0",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -25,6 +25,6 @@
25
25
  "SETUP.md"
26
26
  ],
27
27
  "dependencies": {
28
- "@sentry/junior-plugin-api": "0.74.1"
28
+ "@sentry/junior-plugin-api": "0.75.0"
29
29
  }
30
30
  }