Merge pull request #18 from MatthiesenXYZ/testing

update mdx integration and add ratelimit checks for octokit
This commit is contained in:
Adam Matthiesen 2024-03-02 02:01:57 -08:00 committed by GitHub
commit 12c63792a2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 64 additions and 8 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

@ -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
+ })]
});
```

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"
/>