|
import { HUB_URL } from "../consts"; |
|
import { createApiError } from "../error"; |
|
import type { ApiDatasetInfo } from "../types/api/api-dataset"; |
|
import type { CredentialsParams } from "../types/public"; |
|
import { checkCredentials } from "../utils/checkCredentials"; |
|
import { pick } from "../utils/pick"; |
|
import { type DATASET_EXPANDABLE_KEYS, DATASET_EXPAND_KEYS, type DatasetEntry } from "./list-datasets"; |
|
|
|
export async function datasetInfo< |
|
const T extends Exclude<(typeof DATASET_EXPANDABLE_KEYS)[number], (typeof DATASET_EXPAND_KEYS)[number]> = never, |
|
>( |
|
params: { |
|
name: string; |
|
hubUrl?: string; |
|
additionalFields?: T[]; |
|
|
|
|
|
|
|
revision?: string; |
|
|
|
|
|
|
|
fetch?: typeof fetch; |
|
} & Partial<CredentialsParams> |
|
): Promise<DatasetEntry & Pick<ApiDatasetInfo, T>> { |
|
const accessToken = params && checkCredentials(params); |
|
|
|
const search = new URLSearchParams([ |
|
...DATASET_EXPAND_KEYS.map((val) => ["expand", val] satisfies [string, string]), |
|
...(params?.additionalFields?.map((val) => ["expand", val] satisfies [string, string]) ?? []), |
|
]).toString(); |
|
|
|
const response = await (params.fetch || fetch)( |
|
`${params?.hubUrl || HUB_URL}/api/datasets/${params.name}/revision/${encodeURIComponent( |
|
params.revision ?? "HEAD" |
|
)}?${search.toString()}`, |
|
{ |
|
headers: { |
|
...(accessToken ? { Authorization: `Bearer ${accessToken}` } : {}), |
|
Accepts: "application/json", |
|
}, |
|
} |
|
); |
|
|
|
if (!response.ok) { |
|
throw await createApiError(response); |
|
} |
|
|
|
const data = await response.json(); |
|
|
|
return { |
|
...(params?.additionalFields && pick(data, params.additionalFields)), |
|
id: data._id, |
|
name: data.id, |
|
private: data.private, |
|
downloads: data.downloads, |
|
likes: data.likes, |
|
gated: data.gated, |
|
updatedAt: new Date(data.lastModified), |
|
} as DatasetEntry & Pick<ApiDatasetInfo, T>; |
|
} |
|
|