update mdx integration and add ratelimit checks for octokit

This commit is contained in:
Adam Matthiesen 2024-03-01 11:14:55 -08:00
parent f4e7bef6b3
commit a27111a5aa
4 changed files with 59 additions and 7 deletions

View File

@ -0,0 +1,5 @@
---
"@matthiesenxyz/astro-gists": patch
---
update mdx integration config, as well as impliment ratelimit checks on Octokit with logs!

View File

@ -34,8 +34,8 @@ export default defineIntegration({
return {
"astro:config:setup": ({
watchIntegration, hasIntegration, addIntegration,
updateConfig, logger
watchIntegration, hasIntegration,
updateConfig, logger, config
}) => {
logger.info("Setting up Astro Gists Integration.")
const configSetup = logger.fork("astro-gists/config:setup")
@ -48,12 +48,13 @@ export default defineIntegration({
configSetup.warn("GITHUB_PERSONAL_TOKEN not found. Please add it to your .env file. Without it, you will be limited to 60 requests per hour.")
}
// CHECK FOR EXISTING INTEGRATIONS
const integrations = [...config.integrations]
// ADD ExpressiveCode INTEGRATION
if (!hasIntegration("astro-expressive-code")) {
configSetup.info("Loading Astro Gists Expressive Code Integration.")
updateConfig({
integrations: [...astroGistsExpressiveCode()]
})
integrations.push(...astroGistsExpressiveCode())
} else {
configSetup.info("Astro Expressive Code Integration already loaded.")
}
@ -64,7 +65,7 @@ export default defineIntegration({
}
if (options.MDXIntegration && !hasIntegration("@astrojs/mdx")) {
configSetup.info("Loading @astrojs/mdx Integration.")
addIntegration(mdx(), { ensureUnique: true })
integrations.push(mdx())
}
if (!options.MDXIntegration) {
configSetup.info("Internal MDX Integration Disabled. Skipping...")

View File

@ -1,5 +1,6 @@
import { Octokit } from "octokit";
import { loadEnv } from "vite";
import type { Route, RequestParameters, OctokitResponse } from "@octokit/types"
// Load environment variables
const { GITHUB_PERSONAL_TOKEN } = loadEnv("all", process.cwd(), "GITHUB_");
@ -7,9 +8,39 @@ const { GITHUB_PERSONAL_TOKEN } = loadEnv("all", process.cwd(), "GITHUB_");
// Create an Octokit instance
export const octokit = new Octokit({ auth: GITHUB_PERSONAL_TOKEN });
// Retry requests if rate limited
async function requestRetry(route: Route, parameters: RequestParameters) {
try {
const response: OctokitResponse<unknown, number> = await octokit.request(route, parameters);
return response;
} catch (error) {
/** @ts-ignore-error */
if (error.response && error.status === 403 && error.response.headers['x-ratelimit-remaining'] === '0') {
/** @ts-ignore-error */
const resetTimeEpochSeconds = error.response.headers['x-ratelimit-reset'];
const currentTimeEpochSeconds = Math.floor(new Date().getTime() / 1000);
const secondsToWait = resetTimeEpochSeconds - currentTimeEpochSeconds;
console.log(`Rate limit reached. Waiting ${secondsToWait} seconds before retrying.`);
return new Promise((resolve) => {
setTimeout(async () => {
const retryResponse = await requestRetry(route, parameters);
resolve(retryResponse);
}, secondsToWait * 1000);
});
}
// Return a rejected Promise
return Promise.reject(error);
}
}
// Get a Gist by ID
export const getGist = async (gistId: string) => {
const { data } = await octokit.request('GET /gists/{gist_id}', { gist_id: gistId });
/** @ts-ignore-error */
const { data: response } = await requestRetry('GET /gists/{gist_id}', { gist_id: gistId });
const data = response as {
// biome-ignore lint/suspicious/noExplicitAny: we don't know the shape of the data returned from the API
files: any; data: unknown
};
return data;
};

View File

@ -0,0 +1,15 @@
---
title: Hello, World
---
import { GetGist, GetGistGroup } from "@matthiesenxyz/astro-gists/components"
# Hi there! This is a MDX test page.
<GetGist
gistId="cce7f3f1d9322710be8196aa344186ba"
filename="hello.md"
/>
<GetGistGroup
gistId="84243fa11bf96a59bfb237152eb52fa7"
/>