Skip to content

Commit 51faf34

Browse files
committed
assume the response has the correct type
1 parent 27b95e9 commit 51faf34

1 file changed

Lines changed: 7 additions & 24 deletions

File tree

src/lib/sources/gitHubSource.ts

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -72,30 +72,13 @@ async function fetchFilesList(url: DirectoryUrl, options?: { requestInit?: Reque
7272
throw new Error(`GitHub API error: ${response.status} ${response.statusText} - ${await response.text()}`)
7373
}
7474
try {
75-
const data: unknown = await response.json()
76-
const isDirectory = Array.isArray(data)
77-
if (!isDirectory) {
78-
throw new Error('Not a directory')
79-
}
80-
const files: FileMetadata[] = []
81-
for (const file of data as unknown[]) {
82-
if (typeof file !== 'object' || file === null || !('name' in file) || !('path' in file) || !('type' in file) || !('size' in file)) {
83-
throw new Error('Invalid file metadata')
84-
}
85-
if (file.type !== 'file' && file.type !== 'dir') {
86-
throw new Error('Unsupported file type')
87-
}
88-
if (typeof file.name !== 'string' || typeof file.path !== 'string' || typeof file.size !== 'number') {
89-
throw new Error('Invalid file metadata types')
90-
}
91-
files.push({
92-
name: getFileName(file.path),
93-
fileSize: file.size,
94-
sourceId: `${url.origin}/${url.repo}/${file.type === 'file' ? 'blob' : 'tree'}/${url.branch}/${file.path}`.replace(/\/$/, ''),
95-
kind: file.type === 'file' ? 'file' : 'directory', // 'unknown' is considered as a directory
96-
})
97-
}
98-
return files
75+
const data = await response.json() as {name: string, path: string, type: 'file' | 'dir', size: number}[]
76+
return data.map((file) => ({
77+
name: getFileName(file.path),
78+
fileSize: file.size,
79+
sourceId: `${url.origin}/${url.repo}/${file.type === 'file' ? 'blob' : 'tree'}/${url.branch}/${file.path}`.replace(/\/$/, ''),
80+
kind: file.type === 'file' ? 'file' : 'directory',
81+
}))
9982
} catch (error) {
10083
throw new Error(`Failed to parse GitHub API response: ${error instanceof Error ? error.message : String(error)}`)
10184
}

0 commit comments

Comments
 (0)