Update GitHub REST API integrations to support GitHub instances on alternate domains. (#2568)

This commit is contained in:
Tyler Garner
2025-11-05 07:32:17 +08:00
committed by GitHub
parent b81e6ba733
commit 117ad869eb
5 changed files with 39 additions and 4 deletions
+6
View File
@@ -0,0 +1,6 @@
---
'fumadocs-core': minor
'fumadocs-ui': minor
---
Add support for using a custom GitHub API base url
@@ -41,3 +41,20 @@ functionality.
```ts
process.env.NODE_ENV === 'development'? null : getGithubLastEdit(...)
```
### Custom GitHub Base URL
If you need to access GitHub a instance at a url other than `https://api.github.com`, you can override the base URL:
```ts
import { getGithubLastEdit } from 'fumadocs-core/content/github';
const time = await getGithubLastEdit({
owner: 'your-org',
repo: 'your-repo',
path: `content/docs/${page.path}`,
baseUrl: 'https://api.octocorp.ghe.com', // Your custom GitHub API URL
});
```
The `baseUrl` parameter defaults to `'https://api.github.com'` if not specified.
+2 -1
View File
@@ -7,13 +7,14 @@ export interface Contributor {
export async function fetchContributors(
repoOwner: string,
repoName: string,
baseUrl: string = 'https://api.github.com',
): Promise<Contributor[]> {
const headers = new Headers();
if (process.env.GITHUB_TOKEN)
headers.set('Authorization', `Bearer ${process.env.GITHUB_TOKEN}`);
const response = await fetch(
`https://api.github.com/repos/${repoOwner}/${repoName}/contributors?per_page=50`,
`${baseUrl}/repos/${repoOwner}/${repoName}/contributors?per_page=50`,
{
headers,
next: { revalidate: 1000 * 1000 },
+9 -1
View File
@@ -30,6 +30,13 @@ export interface GetGithubLastCommitOptions {
*/
sha?: string;
/**
* Base URL for GitHub API
* @default "https://api.github.com"
* @link https://docs.github.com/en/get-started/using-github-docs/about-versions-of-github-docs#determining-which-github-product-you-use
*/
baseUrl?: string;
/**
* Custom query parameters
*/
@@ -50,6 +57,7 @@ export async function getGithubLastEdit({
owner,
path,
sha,
baseUrl = 'https://api.github.com',
options = {},
params: customParams = {},
}: GetGithubLastCommitOptions): Promise<Date | null> {
@@ -70,7 +78,7 @@ export async function getGithubLastEdit({
}
const res = await fetch(
`https://api.github.com/repos/${owner}/${repo}/commits?${params.toString()}`,
`${baseUrl}/repos/${owner}/${repo}/commits?${params.toString()}`,
{
cache: 'force-cache',
...options,
+5 -2
View File
@@ -6,11 +6,12 @@ async function getRepoStarsAndForks(
owner: string,
repo: string,
token?: string,
baseUrl: string = 'https://api.github.com',
): Promise<{
stars: number;
forks: number;
}> {
const endpoint = `https://api.github.com/repos/${owner}/${repo}`;
const endpoint = `${baseUrl}/repos/${owner}/${repo}`;
const headers = new Headers({
'Content-Type': 'application/json',
});
@@ -41,13 +42,15 @@ export async function GithubInfo({
repo,
owner,
token,
baseUrl,
...props
}: AnchorHTMLAttributes<HTMLAnchorElement> & {
owner: string;
repo: string;
token?: string;
baseUrl?: string;
}) {
const { stars } = await getRepoStarsAndForks(owner, repo, token);
const { stars } = await getRepoStarsAndForks(owner, repo, token, baseUrl);
const humanizedStars = humanizeNumber(stars);
return (