From a27111a5aa4b4a587a1c598922eb1bbc94db23d3 Mon Sep 17 00:00:00 2001 From: Adam Matthiesen Date: Fri, 1 Mar 2024 11:14:55 -0800 Subject: [PATCH] update mdx integration and add ratelimit checks for octokit --- .changeset/lazy-years-refuse.md | 5 +++++ package/src/integration.ts | 13 ++++++------ package/src/utils.ts | 33 ++++++++++++++++++++++++++++++- playground/src/pages/mdx-test.mdx | 15 ++++++++++++++ 4 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 .changeset/lazy-years-refuse.md create mode 100644 playground/src/pages/mdx-test.mdx diff --git a/.changeset/lazy-years-refuse.md b/.changeset/lazy-years-refuse.md new file mode 100644 index 0000000..f2e5acc --- /dev/null +++ b/.changeset/lazy-years-refuse.md @@ -0,0 +1,5 @@ +--- +"@matthiesenxyz/astro-gists": patch +--- + +update mdx integration config, as well as impliment ratelimit checks on Octokit with logs! diff --git a/package/src/integration.ts b/package/src/integration.ts index 02b72d4..13e1ad4 100644 --- a/package/src/integration.ts +++ b/package/src/integration.ts @@ -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...") diff --git a/package/src/utils.ts b/package/src/utils.ts index a9ebaa0..3aa8bb3 100644 --- a/package/src/utils.ts +++ b/package/src/utils.ts @@ -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 = 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; }; diff --git a/playground/src/pages/mdx-test.mdx b/playground/src/pages/mdx-test.mdx new file mode 100644 index 0000000..99e762b --- /dev/null +++ b/playground/src/pages/mdx-test.mdx @@ -0,0 +1,15 @@ +--- +title: Hello, World +--- +import { GetGist, GetGistGroup } from "@matthiesenxyz/astro-gists/components" + +# Hi there! This is a MDX test page. + + + + \ No newline at end of file