Add common utilities for GitHub API requests
This commit is contained in:
parent
534b90cfe0
commit
ca2c6f9324
1 changed files with 46 additions and 0 deletions
46
src/github/common/utils.ts
Normal file
46
src/github/common/utils.ts
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
import fetch from "node-fetch";
|
||||||
|
|
||||||
|
if (!process.env.GITHUB_PERSONAL_ACCESS_TOKEN) {
|
||||||
|
console.error("GITHUB_PERSONAL_ACCESS_TOKEN environment variable is not set");
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
export const GITHUB_PERSONAL_ACCESS_TOKEN = process.env.GITHUB_PERSONAL_ACCESS_TOKEN;
|
||||||
|
|
||||||
|
interface GitHubRequestOptions {
|
||||||
|
method?: string;
|
||||||
|
body?: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function githubRequest(url: string, options: GitHubRequestOptions = {}) {
|
||||||
|
const response = await fetch(url, {
|
||||||
|
method: options.method || "GET",
|
||||||
|
headers: {
|
||||||
|
Authorization: `token ${GITHUB_PERSONAL_ACCESS_TOKEN}`,
|
||||||
|
Accept: "application/vnd.github.v3+json",
|
||||||
|
"User-Agent": "github-mcp-server",
|
||||||
|
...(options.body ? { "Content-Type": "application/json" } : {}),
|
||||||
|
},
|
||||||
|
...(options.body ? { body: JSON.stringify(options.body) } : {}),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`GitHub API error: ${response.statusText}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function buildUrl(baseUrl: string, params: Record<string, any> = {}) {
|
||||||
|
const url = new URL(baseUrl);
|
||||||
|
Object.entries(params).forEach(([key, value]) => {
|
||||||
|
if (value !== undefined && value !== null) {
|
||||||
|
if (Array.isArray(value)) {
|
||||||
|
url.searchParams.append(key, value.join(","));
|
||||||
|
} else {
|
||||||
|
url.searchParams.append(key, value.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return url.toString();
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue