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/README.md b/package/README.md index b494c9e..8488c01 100644 --- a/package/README.md +++ b/package/README.md @@ -14,6 +14,8 @@ The Only Requirement to install this package is a **Github account with a Verifi This Integration uses [`Octokit`](http://octokit.github.io/) by `GitHub` to Generate custom gists using [`ExpressiveCode`](https://expressive-code.com/) within your Astro project! +This Integration currently includes `@astrojs/mdx` and is enabled by default. + ### Installation Install the integration **automatically** using the Astro CLI: @@ -55,7 +57,9 @@ import { defineConfig } from "astro/config"; // https://astro.build/config export default defineConfig({ + integrations: [astroGist({ -+ MDXIntegration: true // This is the default option and will enable or disable @astrojs/mdx + // OPTIONAL CONFIG OPTIONS + // Enable the Astrojs/MDX Integration - Default: true + MDXIntegration: true + })] }); ``` 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