[Internal] Fix Logger and response types (#53)

Co-authored-by: Jacob Jenkins <7649031+jdtjenkins@users.noreply.github.com>
This commit is contained in:
Adam Matthiesen 2024-03-14 09:30:00 -07:00 committed by GitHub
parent c9a731aee8
commit bcc60cb0f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 35 additions and 17 deletions

View File

@ -0,0 +1,5 @@
---
"@matthiesenxyz/astro-gists": patch
---
[Internal] Fix Logger and response types

View File

@ -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",

View File

@ -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<any>) {
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

View File

@ -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