Merge pull request #18 from MatthiesenXYZ/testing
update mdx integration and add ratelimit checks for octokit
This commit is contained in:
commit
12c63792a2
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
"@matthiesenxyz/astro-gists": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
update mdx integration config, as well as impliment ratelimit checks on Octokit with logs!
|
|
@ -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 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
|
### Installation
|
||||||
|
|
||||||
Install the integration **automatically** using the Astro CLI:
|
Install the integration **automatically** using the Astro CLI:
|
||||||
|
@ -55,7 +57,9 @@ import { defineConfig } from "astro/config";
|
||||||
// https://astro.build/config
|
// https://astro.build/config
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
+ integrations: [astroGist({
|
+ 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
|
||||||
+ })]
|
+ })]
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
|
@ -34,8 +34,8 @@ export default defineIntegration({
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"astro:config:setup": ({
|
"astro:config:setup": ({
|
||||||
watchIntegration, hasIntegration, addIntegration,
|
watchIntegration, hasIntegration,
|
||||||
updateConfig, logger
|
updateConfig, logger, config
|
||||||
}) => {
|
}) => {
|
||||||
logger.info("Setting up Astro Gists Integration.")
|
logger.info("Setting up Astro Gists Integration.")
|
||||||
const configSetup = logger.fork("astro-gists/config:setup")
|
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.")
|
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
|
// ADD ExpressiveCode INTEGRATION
|
||||||
if (!hasIntegration("astro-expressive-code")) {
|
if (!hasIntegration("astro-expressive-code")) {
|
||||||
configSetup.info("Loading Astro Gists Expressive Code Integration.")
|
configSetup.info("Loading Astro Gists Expressive Code Integration.")
|
||||||
updateConfig({
|
integrations.push(...astroGistsExpressiveCode())
|
||||||
integrations: [...astroGistsExpressiveCode()]
|
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
configSetup.info("Astro Expressive Code Integration already loaded.")
|
configSetup.info("Astro Expressive Code Integration already loaded.")
|
||||||
}
|
}
|
||||||
|
@ -64,7 +65,7 @@ export default defineIntegration({
|
||||||
}
|
}
|
||||||
if (options.MDXIntegration && !hasIntegration("@astrojs/mdx")) {
|
if (options.MDXIntegration && !hasIntegration("@astrojs/mdx")) {
|
||||||
configSetup.info("Loading @astrojs/mdx Integration.")
|
configSetup.info("Loading @astrojs/mdx Integration.")
|
||||||
addIntegration(mdx(), { ensureUnique: true })
|
integrations.push(mdx())
|
||||||
}
|
}
|
||||||
if (!options.MDXIntegration) {
|
if (!options.MDXIntegration) {
|
||||||
configSetup.info("Internal MDX Integration Disabled. Skipping...")
|
configSetup.info("Internal MDX Integration Disabled. Skipping...")
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import { Octokit } from "octokit";
|
import { Octokit } from "octokit";
|
||||||
import { loadEnv } from "vite";
|
import { loadEnv } from "vite";
|
||||||
|
import type { Route, RequestParameters, OctokitResponse } from "@octokit/types"
|
||||||
|
|
||||||
// Load environment variables
|
// Load environment variables
|
||||||
const { GITHUB_PERSONAL_TOKEN } = loadEnv("all", process.cwd(), "GITHUB_");
|
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
|
// Create an Octokit instance
|
||||||
export const octokit = new Octokit({ auth: GITHUB_PERSONAL_TOKEN });
|
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
|
// Get a Gist by ID
|
||||||
export const getGist = async (gistId: string) => {
|
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;
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -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"
|
||||||
|
/>
|
Loading…
Reference in New Issue