Skip to content

Commit 66cd372

Browse files
committed
feat: support authentication token for private repositories
1 parent 50b45e7 commit 66cd372

File tree

5 files changed

+20
-7
lines changed

5 files changed

+20
-7
lines changed

docs/content/docs/2.collections/1.collections.md

+2
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ type CollectionSource = {
134134
cwd?: string
135135
// Remote git repository URL (e.g., https://github.com/nuxt/content)
136136
repository?: string
137+
// Authentication token for private repositories (e.g., GitHub personal access token)
138+
authToken?: string
137139
}
138140
```
139141

docs/content/docs/2.collections/3.sources.md

+4
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,7 @@ Root directory for content matching.
5050
### `repository`
5151

5252
External source representing a remote git repository URL (e.g., https://github.com/nuxt/content)
53+
54+
### `authToken`
55+
56+
Authentication token for private repositories (e.g., GitHub personal access token).

src/types/collection.ts

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export type CollectionSource = {
1313
prefix?: string
1414
exclude?: string[]
1515
repository?: string
16+
authToken?: string
1617
cwd?: string
1718
}
1819

src/utils/git.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ export interface GitInfo {
1717
url: string
1818
}
1919

20-
export async function downloadRepository(url: string, cwd: string) {
20+
export async function downloadRepository(url: string, cwd: string, { headers }: { headers?: Record<string, string> } = {}) {
2121
const tarFile = join(cwd, '.content.clone.tar.gz')
2222
const cacheFile = join(cwd, '.content.cache.json')
2323

2424
const cache = await readFile(cacheFile, 'utf8').then(d => JSON.parse(d)).catch(() => null)
2525
if (cache) {
2626
// Directory exists, skip download
27-
const response = await fetch(url, { method: 'HEAD' })
27+
const response = await fetch(url, { method: 'HEAD', headers })
2828
const etag = response.headers.get('etag')
2929
if (etag === cache.etag) {
3030
await writeFile(cacheFile, JSON.stringify({
@@ -38,7 +38,7 @@ export async function downloadRepository(url: string, cwd: string) {
3838
await mkdir(cwd, { recursive: true })
3939

4040
try {
41-
const response = await fetch(url)
41+
const response = await fetch(url, { headers })
4242
const stream = createWriteStream(tarFile)
4343
await promisify(pipeline)(response.body as unknown as ReadableStream[], stream)
4444

src/utils/source.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,16 @@ export function defineGitHubSource(source: CollectionSource): ResolvedCollection
3131
const { org, repo, branch } = repository
3232
resolvedSource.cwd = join(nuxt.options.rootDir, '.data', 'content', `github-${org}-${repo}-${branch}`)
3333

34-
await downloadRepository(
35-
`https://github.com/${org}/${repo}/archive/refs/heads/${branch}.tar.gz`,
36-
resolvedSource.cwd!,
37-
)
34+
let headers: Record<string, string> = {}
35+
if (resolvedSource.authToken) {
36+
headers = { Authorization: `Bearer ${resolvedSource.authToken}` }
37+
}
38+
39+
const url = headers.Authorization
40+
? `https://api.github.com/repos/${org}/${repo}/tarball/${branch}`
41+
: `https://github.com/${org}/${repo}/archive/refs/heads/${branch}.tar.gz`
42+
43+
await downloadRepository(url, resolvedSource.cwd!, { headers })
3844
}
3945
},
4046
}

0 commit comments

Comments
 (0)