|
import { HUB_URL } from "../consts"; |
|
import { createApiError } from "../error"; |
|
import type { ApiIndexTreeEntry } from "../types/api/api-index-tree"; |
|
import type { CredentialsParams, RepoDesignation } from "../types/public"; |
|
import { checkCredentials } from "../utils/checkCredentials"; |
|
import { parseLinkHeader } from "../utils/parseLinkHeader"; |
|
import { toRepoId } from "../utils/toRepoId"; |
|
|
|
export interface ListFileEntry { |
|
type: "file" | "directory" | "unknown"; |
|
size: number; |
|
path: string; |
|
oid: string; |
|
lfs?: { |
|
oid: string; |
|
size: number; |
|
|
|
pointerSize: number; |
|
}; |
|
|
|
|
|
|
|
xetHash?: string; |
|
|
|
|
|
|
|
lastCommit?: { |
|
date: string; |
|
id: string; |
|
title: string; |
|
}; |
|
|
|
|
|
|
|
securityFileStatus?: unknown; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
export async function* listFiles( |
|
params: { |
|
repo: RepoDesignation; |
|
|
|
|
|
|
|
recursive?: boolean; |
|
|
|
|
|
|
|
|
|
path?: string; |
|
|
|
|
|
|
|
expand?: boolean; |
|
revision?: string; |
|
hubUrl?: string; |
|
|
|
|
|
|
|
fetch?: typeof fetch; |
|
} & Partial<CredentialsParams> |
|
): AsyncGenerator<ListFileEntry> { |
|
const accessToken = checkCredentials(params); |
|
const repoId = toRepoId(params.repo); |
|
let url: string | undefined = `${params.hubUrl || HUB_URL}/api/${repoId.type}s/${repoId.name}/tree/${ |
|
params.revision || "main" |
|
}${params.path ? "/" + params.path : ""}?recursive=${!!params.recursive}&expand=${!!params.expand}`; |
|
|
|
while (url) { |
|
const res: Response = await (params.fetch ?? fetch)(url, { |
|
headers: { |
|
accept: "application/json", |
|
...(accessToken ? { Authorization: `Bearer ${accessToken}` } : undefined), |
|
}, |
|
}); |
|
|
|
if (!res.ok) { |
|
throw await createApiError(res); |
|
} |
|
|
|
const items: ApiIndexTreeEntry[] = await res.json(); |
|
|
|
for (const item of items) { |
|
yield item; |
|
} |
|
|
|
const linkHeader = res.headers.get("Link"); |
|
|
|
url = linkHeader ? parseLinkHeader(linkHeader).next : undefined; |
|
} |
|
} |
|
|