mirror your GitHub repos to tangled.org automatically
1import { App } from '@octokit/app'
2
3let cachedApp: App | undefined
4
5function useApp(): App {
6 if (cachedApp) return cachedApp
7 const appId = process.env.NUXT_GITHUB_APP_ID
8 const privateKey = process.env.NUXT_GITHUB_APP_PRIVATE_KEY
9 if (!appId || !privateKey) {
10 throw new Error('NUXT_GITHUB_APP_ID and NUXT_GITHUB_APP_PRIVATE_KEY must be set')
11 }
12 cachedApp = new App({
13 appId,
14 // Vercel env vars escape newlines; restore them so PEM parsing works.
15 privateKey: privateKey.replaceAll('\\n', '\n'),
16 })
17 return cachedApp
18}
19
20export type InstallationOctokit = Awaited<ReturnType<App['getInstallationOctokit']>>
21
22/** Get an Octokit pre-authed for a specific GitHub App installation. */
23export async function installationOctokit(installationId: number): Promise<InstallationOctokit> {
24 const app = useApp()
25 return app.getInstallationOctokit(installationId)
26}
27
28/** Test hook. */
29export function clearGitHubAppCache() {
30 cachedApp = undefined
31}