Files
Lorenzo Lewis d31158d660 GitHub Sponsors and Open Collective contribution graph (#1649)
Signed-off-by: Lorenzo Lewis <lorenzo_lewis@icloud.com>
Co-authored-by: Vitor Ayres <gitkey@virtuaires.com.br>
2025-08-10 14:34:45 -03:00

42 lines
1.0 KiB
TypeScript

import fs from 'node:fs';
export async function q(query: string, url: string, name: string, headers?: any) {
const res = await fetch(url, {
method: 'POST',
body: JSON.stringify({ query }),
headers: {
'Content-Type': 'application/json',
...headers,
},
});
if (!res.ok) {
throw Error(
`${name} query failed: ${res.status} ${res.statusText} \n ${JSON.stringify(await res.json(), null, 2)}, `
);
}
const data = (await res.json()).data;
return data;
}
export async function saveToFile(filePath: string, fetcher: any) {
try {
const data = await fetcher();
fs.writeFileSync(filePath, JSON.stringify(data, null, 2));
} catch (error) {
console.error(`Failed to fetch or write ${filePath}:`, error);
}
}
export async function GITHUB_TOKEN(): Promise<string> {
if (process.env.NODE_ENV !== 'production') {
await import('dotenv/config');
}
const token = process.env.GITHUB_TOKEN;
if (!token) {
throw new Error('GITHUB_TOKEN environment variable is not set');
}
return token;
}