diff --git a/.changeset/spotty-donkeys-hear.md b/.changeset/spotty-donkeys-hear.md new file mode 100644 index 0000000..87a3da3 --- /dev/null +++ b/.changeset/spotty-donkeys-hear.md @@ -0,0 +1,5 @@ +--- +"@matthiesenxyz/astro-gists": patch +--- + +[Internal] Fix Logger and response types diff --git a/package/package.json b/package/package.json index 69b6e07..5342c1b 100644 --- a/package/package.json +++ b/package/package.json @@ -46,6 +46,7 @@ "dependencies": { "@expressive-code/plugin-line-numbers": "^0.33.4", "astro-integration-kit": "^0.5.1", + "chalk": "5.3.0", "expressive-code": "^0.33.4", "hast-util-to-html": "8.0.4", "p-retry": "6.2.0", diff --git a/package/src/lib/octokit.ts b/package/src/lib/octokit.ts index ac4629d..1dc0439 100644 --- a/package/src/lib/octokit.ts +++ b/package/src/lib/octokit.ts @@ -1,26 +1,34 @@ import { Octokit } from "octokit"; -import type { OctokitResponse } from "@octokit/types"; +import type { Endpoints } from "@octokit/types"; import { loadEnv } from "vite"; +import chalk from 'chalk'; import pRretry from 'p-retry'; import config from "virtual:astro-gists/config"; // Load config options to check if verbose logging is enabled const isVerbose = config.verbose; +// Define the GistResponse type +type GistResponse = Endpoints["GET /gists/{gist_id}"]["response"]; + // Create Gist Logger interface const gistLogger = async ( type: "info"|"warn"|"error", message: string, VerboseCheck: boolean ) => { + // simplify console.log + const log = console.log; + // Create a log banner to identify logs from this package + const logBanner = chalk.blue("[astro-gists : octokit] "); // if checkVerbose is true and isVerbose is true, log the message if (!VerboseCheck || VerboseCheck && isVerbose) { if (type === "info") { - console.log(`[astro-gists : octokit] ${message}`); + log(logBanner+message); } else if (type === "warn") { - console.log(`[WARN] [astro-gists : octokit] ${message}`); + log(chalk.yellow("[WARN] ")+logBanner+chalk.yellow(message)); } else if (type === "error") { - console.log(`[ERROR] [astro-gists : octokit] ${message}`); + log(chalk.bold.red("[ERROR] ")+logBanner+chalk.red(message)); } } }; @@ -43,20 +51,20 @@ const retry: typeof pRretry = (fn, opts) => }); // Handle the response from the Octokit API -// biome-ignore lint/suspicious/noExplicitAny: any is used to handle the response from the Octokit API -function getStatusCode(response: OctokitResponse) { - switch (response.status) { - case 200: - return response.data; - case 404: +function getStatusCode(response: GistResponse) { + if (response.status !== 200) { + if (response.status === 404) { return "E404"; - case 403: + } + if (response.status === 403) { return "E403"; - case 500: + } + if (response.status === 500) { return "E500"; - default: - return "E000"; + } + return "E000"; } + return response.data; } // Gist Grabber const gistGrabber = async (gistId: string) => { @@ -80,7 +88,7 @@ const gistGrabber = async (gistId: string) => { return null; } if (statusCode === response.data) { - gistLogger("info", `Gist ${gistId} found.`, true); + gistLogger("info", `Gist for "${response.data.description || gistId}" grabbed successfully.`, true); } return statusCode; } @@ -91,8 +99,9 @@ export const getGistFile = async ( filename: string ) => { const gist = await gistGrabber(gistId); - const file = gist.files[filename]; - return file ? file : null; + if (gist === null) return null; + if (!gist.files) return null; + return gist.files[filename]; }; // Get a Group of Gist files by ID diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a808d2e..3cf268e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -26,6 +26,9 @@ importers: astro-integration-kit: specifier: ^0.5.1 version: 0.5.1(astro@4.4.11) + chalk: + specifier: 5.3.0 + version: 5.3.0 expressive-code: specifier: ^0.33.4 version: 0.33.4