From f676a93448fc5696ed762f150003ca2abd440a5f Mon Sep 17 00:00:00 2001 From: Keyrxng <106303466+Keyrxng@users.noreply.github.com> Date: Mon, 8 Apr 2024 23:45:54 +0100 Subject: [PATCH] chore: use request error class --- package.json | 1 + src/home/fetch-github/fetch-issues-preview.ts | 11 ++-- src/home/getters/get-github-user.ts | 5 +- yarn.lock | 50 ++++++++----------- 4 files changed, 30 insertions(+), 37 deletions(-) diff --git a/package.json b/package.json index bd782b7d..8b4eaf4a 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "open-source" ], "dependencies": { + "@octokit/request-error": "^6.1.0", "@octokit/rest": "^20.0.2", "@supabase/supabase-js": "^2.39.0", "dotenv": "^16.3.1", diff --git a/src/home/fetch-github/fetch-issues-preview.ts b/src/home/fetch-github/fetch-issues-preview.ts index 3260359f..78848752 100644 --- a/src/home/fetch-github/fetch-issues-preview.ts +++ b/src/home/fetch-github/fetch-issues-preview.ts @@ -1,11 +1,10 @@ -import { Octokit, RestEndpointMethodTypes } from "@octokit/rest"; +import { Octokit } from "@octokit/rest"; import { getGitHubAccessToken, getGitHubUserName } from "../getters/get-github-access-token"; import { GitHubIssue } from "../github-types"; import { displayPopupMessage } from "../rendering/display-popup-modal"; import { TaskNoFull } from "./preview-to-full-mapping"; import { getGitHubUser } from "../getters/get-github-user"; - -type GitHubApiResponse = RestEndpointMethodTypes["issues"]["listForRepo"]; +import { RequestError } from "@octokit/request-error"; async function checkPrivateRepoAccess(): Promise { const octokit = new Octokit({ auth: await getGitHubAccessToken() }); @@ -78,7 +77,7 @@ export async function fetchIssuePreviews(): Promise { freshIssues = publicIssues; } } catch (error) { - if (error.status === 403) { + if (error instanceof RequestError && error.status === 403) { await handleRateLimit(octokit, error); } else { console.error("Error fetching issue previews:", error); @@ -104,13 +103,13 @@ type RateLimit = { user: boolean; }; -export async function handleRateLimit(octokit?: Octokit, error?: GitHubApiResponse) { +export async function handleRateLimit(octokit?: Octokit, error?: RequestError) { const rate: RateLimit = { reset: null, user: false, }; - if (error && error.response.headers["x-ratelimit-reset"]) { + if (error?.response?.headers["x-ratelimit-reset"]) { rate.reset = parseInt(error.response.headers["x-ratelimit-reset"]); } diff --git a/src/home/getters/get-github-user.ts b/src/home/getters/get-github-user.ts index 8499f8a2..e87e0621 100644 --- a/src/home/getters/get-github-user.ts +++ b/src/home/getters/get-github-user.ts @@ -3,6 +3,7 @@ import { GitHubUser, GitHubUserResponse } from "../github-types"; import { OAuthToken } from "./get-github-access-token"; import { getLocalStore } from "./get-local-store"; import { handleRateLimit } from "../fetch-github/fetch-issues-preview"; +import { RequestError } from "@octokit/request-error"; declare const SUPABASE_STORAGE_KEY: string; // @DEV: passed in at build time check build/esbuild-build.ts export async function getGitHubUser(): Promise { @@ -40,10 +41,8 @@ async function getNewGitHubUser(providerToken: string | null): Promise