@@ -7,33 +7,47 @@ interface BaseUrl {
77 source : string
88 origin : string
99 repo : string
10+ }
11+
12+ interface RepoUrl extends BaseUrl {
13+ kind : 'repo'
14+ }
15+
16+ interface PathUrl extends BaseUrl {
1017 branch : string
1118 path : string
1219}
1320
14- interface DirectoryUrl extends BaseUrl {
21+ interface DirectoryUrl extends PathUrl {
1522 kind : 'directory'
1623 action : 'tree'
1724}
1825
19- interface FileUrl extends BaseUrl {
26+ interface FileUrl extends PathUrl {
2027 kind : 'file'
2128 action ?: 'blob' | 'raw' | 'raw/refs/heads'
2229 resolveUrl : string
2330}
2431
25- interface RawFileUrl extends BaseUrl {
32+ interface RawFileUrl extends PathUrl {
2633 kind : 'file'
2734 action : undefined
2835 resolveUrl : string
2936}
3037
31- type GHUrl = DirectoryUrl | FileUrl | RawFileUrl
38+ type GHUrl = RepoUrl | DirectoryUrl | FileUrl | RawFileUrl
3239
3340const baseUrl = 'https://github.com'
3441const baseRawUrl = 'https://raw.githubusercontent.com'
3542
3643function getSourceParts ( url : GHUrl ) : SourcePart [ ] {
44+ if ( url . kind === 'repo' ) {
45+ return [ {
46+ sourceId : `${ baseUrl } /${ url . repo } ` ,
47+ text : `${ baseUrl } /${ url . repo } ` ,
48+ } ]
49+ }
50+
3751 const sourceParts : SourcePart [ ] = [ {
3852 sourceId : `${ baseUrl } /${ url . repo } /tree/${ url . branch } /` ,
3953 text : `${ baseUrl } /${ url . repo } /tree/${ url . branch } /` ,
@@ -55,11 +69,10 @@ function getSourceParts(url: GHUrl): SourcePart[] {
5569 }
5670 return sourceParts
5771}
58- function getPrefix ( url : DirectoryUrl ) : string {
59- return `${ baseUrl } /${ url . repo } /tree/${ url . branch } ${ url . path } ` . replace ( / \/ $ / , '' )
60- }
61- async function fetchFilesList ( url : DirectoryUrl , options ?: { requestInit ?: RequestInit , accessToken ?: string } ) : Promise < FileMetadata [ ] > {
62- const apiURL = `https://api.github.com/repos/${ url . repo } /contents${ url . path } ?ref=${ url . branch } `
72+ async function fetchFilesList ( url : DirectoryUrl | RepoUrl , options ?: { requestInit ?: RequestInit , accessToken ?: string } ) : Promise < FileMetadata [ ] > {
73+ const path = url . kind === 'repo' ? '/' : url . path
74+ const branchParam = url . kind === 'repo' ? '' : `?ref=${ url . branch } `
75+ const apiURL = `https://api.github.com/repos/${ url . repo } /contents${ path } ${ branchParam } `
6376 const headers = new Headers ( options ?. requestInit ?. headers )
6477 headers . set ( 'Accept' , 'application/vnd.github+json' )
6578 if ( options ?. accessToken ) {
@@ -74,11 +87,11 @@ async function fetchFilesList(url: DirectoryUrl, options?: { requestInit?: Reque
7487 throw new Error ( `GitHub API error: ${ response . status } ${ response . statusText } - ${ await response . text ( ) } ` )
7588 }
7689 try {
77- const data = await response . json ( ) as { name : string , path : string , type : 'file' | 'dir' , size : number } [ ]
90+ const data = await response . json ( ) as { html_url : string , path : string , type : 'file' | 'dir' , size : number } [ ]
7891 return data . map ( ( file ) => ( {
7992 name : getFileName ( file . path ) ,
8093 fileSize : file . size ,
81- sourceId : ` ${ url . origin } / ${ url . repo } / ${ file . type === 'file' ? 'blob' : 'tree' } / ${ url . branch } / ${ file . path } ` . replace ( / \/ $ / , '' ) ,
94+ sourceId : file . html_url ,
8295 kind : file . type === 'file' ? 'file' : 'directory' ,
8396 } ) )
8497 } catch ( error ) {
@@ -88,12 +101,13 @@ async function fetchFilesList(url: DirectoryUrl, options?: { requestInit?: Reque
88101export function getGitHubSource ( sourceId : string , options ?: { requestInit ?: RequestInit , accessToken ?: string } ) : FileSource | DirSource | undefined {
89102 try {
90103 const url = parseGitHubUrl ( sourceId )
104+ const path = url . kind === 'repo' ? '/' : url . path
91105 async function fetchVersions ( ) {
92106 const branches = await fetchBranchesList ( url , options )
93107 return {
94108 label : 'Branches' ,
95109 versions : branches . map ( ( branch ) => {
96- const branchSourceId = `${ baseUrl } /${ url . repo } /${ url . kind === 'file' ? 'blob' : 'tree' } /${ branch } ${ url . path } `
110+ const branchSourceId = `${ baseUrl } /${ url . repo } /${ url . kind === 'file' ? 'blob' : 'tree' } /${ branch } ${ path } `
97111 return {
98112 label : branch ,
99113 sourceId : branchSourceId ,
@@ -116,7 +130,6 @@ export function getGitHubSource(sourceId: string, options?: {requestInit?: Reque
116130 kind : 'directory' ,
117131 sourceId,
118132 sourceParts : getSourceParts ( url ) ,
119- prefix : getPrefix ( url ) ,
120133 listFiles : ( ) => fetchFilesList ( url , options ) ,
121134 fetchVersions,
122135 }
@@ -174,13 +187,10 @@ export function parseGitHubUrl(url: string): GHUrl {
174187 ) ?. groups
175188 if ( repoGroups ?. owner !== undefined && repoGroups . repo !== undefined ) {
176189 return {
177- kind : 'directory ' ,
190+ kind : 'repo ' ,
178191 source : url ,
179192 origin : urlObject . origin ,
180193 repo : repoGroups . owner + '/' + repoGroups . repo ,
181- action : 'tree' ,
182- branch : 'main' , // hardcode the default branch
183- path : '' ,
184194 }
185195 }
186196
0 commit comments