File size: 878 Bytes
95e3d2a
31d3580
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { DATASET } from '$lib/types';

const REF = 'main';

export type FetchOpts = { fetch?: typeof fetch; signal?: AbortSignal };

/** Build a `huggingface.co/datasets/<repo>/resolve/main/<path>` URL. */
export function resolveUrl(repoPath: string): string {
	return `https://huggingface.co/datasets/${DATASET}/resolve/${REF}/${repoPath}`;
}

export type TreeEntry = { type: 'file' | 'directory'; path: string; size?: number };

/** List entries under `<dataset>/<dirPath>` via the HF tree API. */
export async function listTree(dirPath: string, opts: FetchOpts = {}): Promise<TreeEntry[]> {
	const fetchFn = opts.fetch ?? fetch;
	const url = `https://huggingface.co/api/datasets/${DATASET}/tree/${REF}/${dirPath}`;
	const r = await fetchFn(url, { signal: opts.signal });
	if (!r.ok) throw new Error(`tree ${dirPath}: ${r.status}`);
	return (await r.json()) as TreeEntry[];
}