astro-gists/package/src/utils.ts

25 lines
611 B
TypeScript
Raw Normal View History

2024-02-24 09:47:57 +00:00
import { Octokit } from "octokit";
import { loadEnv } from "vite";
const { GITHUB_PERSONAL_TOKEN } = loadEnv("all", process.cwd(), "GITHUB_");
export const octokit = new Octokit({ auth: GITHUB_PERSONAL_TOKEN });
export const getGist = async (gistId: string) => {
const { data } = await octokit.request('GET /gists/{gist_id}', { gist_id: gistId });
return data;
};
export const getGistFile = async (
gistId: string,
filename: string
) => {
const gist = await getGist(gistId);
if (gist?.files) {
const file = gist.files[filename];
return file ? file : null;
}
return null;
};