Skip to content

Commit 117ad86

Browse files
authored
Update GitHub REST API integrations to support GitHub instances on alternate domains. (#2568)
1 parent b81e6ba commit 117ad86

File tree

5 files changed

+39
-4
lines changed

5 files changed

+39
-4
lines changed

.changeset/old-rice-drum.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'fumadocs-core': minor
3+
'fumadocs-ui': minor
4+
---
5+
6+
Add support for using a custom GitHub API base url

apps/docs/content/docs/headless/utils/git-last-edit.mdx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,20 @@ functionality.
4141
```ts
4242
process.env.NODE_ENV === 'development'? null : getGithubLastEdit(...)
4343
```
44+
45+
### Custom GitHub Base URL
46+
47+
If you need to access GitHub a instance at a url other than `https://api.github.com`, you can override the base URL:
48+
49+
```ts
50+
import { getGithubLastEdit } from 'fumadocs-core/content/github';
51+
52+
const time = await getGithubLastEdit({
53+
owner: 'your-org',
54+
repo: 'your-repo',
55+
path: `content/docs/${page.path}`,
56+
baseUrl: 'https://api.octocorp.ghe.com', // Your custom GitHub API URL
57+
});
58+
```
59+
60+
The `baseUrl` parameter defaults to `'https://api.github.com'` if not specified.

apps/docs/lib/get-contributors.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ export interface Contributor {
77
export async function fetchContributors(
88
repoOwner: string,
99
repoName: string,
10+
baseUrl: string = 'https://api.github.com',
1011
): Promise<Contributor[]> {
1112
const headers = new Headers();
1213
if (process.env.GITHUB_TOKEN)
1314
headers.set('Authorization', `Bearer ${process.env.GITHUB_TOKEN}`);
1415

1516
const response = await fetch(
16-
`https://api.github.com/repos/${repoOwner}/${repoName}/contributors?per_page=50`,
17+
`${baseUrl}/repos/${repoOwner}/${repoName}/contributors?per_page=50`,
1718
{
1819
headers,
1920
next: { revalidate: 1000 * 1000 },

packages/core/src/content/github.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ export interface GetGithubLastCommitOptions {
3030
*/
3131
sha?: string;
3232

33+
/**
34+
* Base URL for GitHub API
35+
* @default "https://api.github.com"
36+
* @link https://docs.github.com/en/get-started/using-github-docs/about-versions-of-github-docs#determining-which-github-product-you-use
37+
*/
38+
baseUrl?: string;
39+
3340
/**
3441
* Custom query parameters
3542
*/
@@ -50,6 +57,7 @@ export async function getGithubLastEdit({
5057
owner,
5158
path,
5259
sha,
60+
baseUrl = 'https://api.github.com',
5361
options = {},
5462
params: customParams = {},
5563
}: GetGithubLastCommitOptions): Promise<Date | null> {
@@ -70,7 +78,7 @@ export async function getGithubLastEdit({
7078
}
7179

7280
const res = await fetch(
73-
`https://api.github.com/repos/${owner}/${repo}/commits?${params.toString()}`,
81+
`${baseUrl}/repos/${owner}/${repo}/commits?${params.toString()}`,
7482
{
7583
cache: 'force-cache',
7684
...options,

packages/ui/src/components/github-info.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ async function getRepoStarsAndForks(
66
owner: string,
77
repo: string,
88
token?: string,
9+
baseUrl: string = 'https://api.github.com',
910
): Promise<{
1011
stars: number;
1112
forks: number;
1213
}> {
13-
const endpoint = `https://api.github.com/repos/${owner}/${repo}`;
14+
const endpoint = `${baseUrl}/repos/${owner}/${repo}`;
1415
const headers = new Headers({
1516
'Content-Type': 'application/json',
1617
});
@@ -41,13 +42,15 @@ export async function GithubInfo({
4142
repo,
4243
owner,
4344
token,
45+
baseUrl,
4446
...props
4547
}: AnchorHTMLAttributes<HTMLAnchorElement> & {
4648
owner: string;
4749
repo: string;
4850
token?: string;
51+
baseUrl?: string;
4952
}) {
50-
const { stars } = await getRepoStarsAndForks(owner, repo, token);
53+
const { stars } = await getRepoStarsAndForks(owner, repo, token, baseUrl);
5154
const humanizedStars = humanizeNumber(stars);
5255

5356
return (

0 commit comments

Comments
 (0)