/
file-exists.ts
40 lines (34 loc) · 1.12 KB
/
file-exists.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { HUB_URL } from "../consts";
import { createApiError } from "../error";
import type { Credentials, RepoDesignation } from "../types/public";
import { checkCredentials } from "../utils/checkCredentials";
import { toRepoId } from "../utils/toRepoId";
export async function fileExists(params: {
repo: RepoDesignation;
path: string;
revision?: string;
credentials?: Credentials;
hubUrl?: string;
/**
* Custom fetch function to use instead of the default one, for example to use a proxy or edit headers.
*/
fetch?: typeof fetch;
}): Promise<boolean> {
checkCredentials(params.credentials);
const repoId = toRepoId(params.repo);
const hubUrl = params.hubUrl ?? HUB_URL;
const url = `${hubUrl}/${repoId.type === "model" ? "" : `${repoId.type}s/`}${repoId.name}/raw/${encodeURIComponent(
params.revision ?? "main"
)}/${params.path}`;
const resp = await (params.fetch ?? fetch)(url, {
method: "HEAD",
headers: params.credentials ? { Authorization: `Bearer ${params.credentials.accessToken}` } : {},
});
if (resp.status === 404) {
return false;
}
if (!resp.ok) {
throw await createApiError(resp);
}
return true;
}