Skip to content

Commit 400ea89

Browse files
authored
💥 Simpler credentials passing around (#918)
Fix #149 Should have backwards compatibilty, just marked as deprecated
1 parent 68602cd commit 400ea89

31 files changed

+466
-452
lines changed

‎README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414

1515
await createRepo({
1616
repo: {type: "model", name: "my-user/nlp-model"},
17-
credentials: {accessToken: HF_TOKEN}
17+
accessToken: HF_TOKEN
1818
});
1919

2020
await uploadFile({
2121
repo: "my-user/nlp-model",
22-
credentials: {accessToken: HF_TOKEN},
22+
accessToken: HF_TOKEN,
2323
// Can work with native File in browsers
2424
file: {
2525
path: "pytorch_model.bin",
@@ -79,7 +79,7 @@ Then import the libraries in your code:
7979
import { HfInference } from "@huggingface/inference";
8080
import { HfAgent } from "@huggingface/agents";
8181
import { createRepo, commit, deleteRepo, listFiles } from "@huggingface/hub";
82-
import type { RepoId, Credentials } from "@huggingface/hub";
82+
import type { RepoId } from "@huggingface/hub";
8383
```
8484

8585
### From CDN or Static hosting
@@ -182,12 +182,12 @@ const HF_TOKEN = "hf_...";
182182

183183
await createRepo({
184184
repo: "my-user/nlp-model", // or {type: "model", name: "my-user/nlp-test"},
185-
credentials: {accessToken: HF_TOKEN}
185+
accessToken: HF_TOKEN
186186
});
187187

188188
await uploadFile({
189189
repo: "my-user/nlp-model",
190-
credentials: {accessToken: HF_TOKEN},
190+
accessToken: HF_TOKEN,
191191
// Can work with native File in browsers
192192
file: {
193193
path: "pytorch_model.bin",
@@ -197,7 +197,7 @@ await uploadFile({
197197

198198
await deleteFiles({
199199
repo: {type: "space", name: "my-user/my-space"}, // or "spaces/my-user/my-space"
200-
credentials: {accessToken: HF_TOKEN},
200+
accessToken: HF_TOKEN,
201201
paths: ["README.md", ".gitattributes"]
202202
});
203203
```

‎packages/hub/README.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,21 @@ Learn how to find free models using the hub package in this [interactive tutoria
3131

3232
```ts
3333
import { createRepo, uploadFiles, uploadFilesWithProgress, deleteFile, deleteRepo, listFiles, whoAmI } from "@huggingface/hub";
34-
import type { RepoDesignation, Credentials } from "@huggingface/hub";
34+
import type { RepoDesignation } from "@huggingface/hub";
3535

3636
const repo: RepoDesignation = { type: "model", name: "myname/some-model" };
37-
const credentials: Credentials = { accessToken: "hf_..." };
3837

39-
const {name: username} = await whoAmI({credentials});
38+
const {name: username} = await whoAmI({accessToken: "hf_..."});
4039

41-
for await (const model of listModels({search: {owner: username}, credentials})) {
40+
for await (const model of listModels({search: {owner: username}, accessToken: "hf_..."})) {
4241
console.log("My model:", model);
4342
}
4443

45-
await createRepo({ repo, credentials, license: "mit" });
44+
await createRepo({ repo, accessToken: "hf_...", license: "mit" });
4645

4746
await uploadFiles({
4847
repo,
49-
credentials,
48+
accessToken: "hf_...",
5049
files: [
5150
// path + blob content
5251
{
@@ -70,23 +69,23 @@ await uploadFiles({
7069

7170
for await (const progressEvent of await uploadFilesWithProgress({
7271
repo,
73-
credentials,
72+
accessToken: "hf_...",
7473
files: [
7574
...
7675
],
7776
})) {
7877
console.log(progressEvent);
7978
}
8079

81-
await deleteFile({repo, credentials, path: "myfile.bin"});
80+
await deleteFile({repo, accessToken: "hf_...", path: "myfile.bin"});
8281

8382
await (await downloadFile({ repo, path: "README.md" })).text();
8483

8584
for await (const fileInfo of listFiles({repo})) {
8685
console.log(fileInfo);
8786
}
8887

89-
await deleteRepo({ repo, credentials });
88+
await deleteRepo({ repo, accessToken: "hf_..." });
9089
```
9190

9291
## OAuth Login

‎packages/hub/src/lib/commit.spec.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ describe("commit", () => {
2525
};
2626

2727
await createRepo({
28-
credentials: {
29-
accessToken: TEST_ACCESS_TOKEN,
30-
},
28+
accessToken: TEST_ACCESS_TOKEN,
3129
hubUrl: TEST_HUB_URL,
3230
repo,
3331
license: "mit",
@@ -50,9 +48,7 @@ describe("commit", () => {
5048
await commit({
5149
repo,
5250
title: "Some commit",
53-
credentials: {
54-
accessToken: TEST_ACCESS_TOKEN,
55-
},
51+
accessToken: TEST_ACCESS_TOKEN,
5652
hubUrl: TEST_HUB_URL,
5753
operations: [
5854
{
@@ -135,9 +131,7 @@ size ${lfsContent.length}
135131
};
136132

137133
await createRepo({
138-
credentials: {
139-
accessToken: TEST_ACCESS_TOKEN,
140-
},
134+
accessToken: TEST_ACCESS_TOKEN,
141135
repo,
142136
hubUrl: TEST_HUB_URL,
143137
});
@@ -163,9 +157,7 @@ size ${lfsContent.length}
163157
);
164158
await commit({
165159
repo,
166-
credentials: {
167-
accessToken: TEST_ACCESS_TOKEN,
168-
},
160+
accessToken: TEST_ACCESS_TOKEN,
169161
hubUrl: TEST_HUB_URL,
170162
title: "upload model",
171163
operations,

‎packages/hub/src/lib/commit.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type {
1010
ApiPreuploadRequest,
1111
ApiPreuploadResponse,
1212
} from "../types/api/api-commit";
13-
import type { Credentials, RepoDesignation } from "../types/public";
13+
import type { CredentialsParams, RepoDesignation } from "../types/public";
1414
import { checkCredentials } from "../utils/checkCredentials";
1515
import { chunk } from "../utils/chunk";
1616
import { promisesQueue } from "../utils/promisesQueue";
@@ -54,12 +54,11 @@ type CommitBlob = Omit<CommitFile, "content"> & { content: Blob };
5454
export type CommitOperation = CommitDeletedEntry | CommitFile /* | CommitRenameFile */;
5555
type CommitBlobOperation = Exclude<CommitOperation, CommitFile> | CommitBlob;
5656

57-
export interface CommitParams {
57+
export type CommitParams = {
5858
title: string;
5959
description?: string;
6060
repo: RepoDesignation;
6161
operations: CommitOperation[];
62-
credentials?: Credentials;
6362
/** @default "main" */
6463
branch?: string;
6564
/**
@@ -82,7 +81,8 @@ export interface CommitParams {
8281
*/
8382
fetch?: typeof fetch;
8483
abortSignal?: AbortSignal;
85-
}
84+
// Credentials are optional due to custom fetch functions or cookie auth
85+
} & Partial<CredentialsParams>;
8686

8787
export interface CommitOutput {
8888
pullRequestUrl?: string;
@@ -121,7 +121,7 @@ export type CommitProgressEvent =
121121
* Can be exposed later to offer fine-tuned progress info
122122
*/
123123
export async function* commitIter(params: CommitParams): AsyncGenerator<CommitProgressEvent, CommitOutput> {
124-
checkCredentials(params.credentials);
124+
const accessToken = checkCredentials(params);
125125
const repoId = toRepoId(params.repo);
126126
yield { event: "phase", phase: "preuploading" };
127127

@@ -189,7 +189,7 @@ export async function* commitIter(params: CommitParams): AsyncGenerator<CommitPr
189189
{
190190
method: "POST",
191191
headers: {
192-
...(params.credentials && { Authorization: `Bearer ${params.credentials.accessToken}` }),
192+
...(accessToken && { Authorization: `Bearer ${accessToken}` }),
193193
"Content-Type": "application/json",
194194
},
195195
body: JSON.stringify(payload),
@@ -263,7 +263,7 @@ export async function* commitIter(params: CommitParams): AsyncGenerator<CommitPr
263263
{
264264
method: "POST",
265265
headers: {
266-
...(params.credentials && { Authorization: `Bearer ${params.credentials.accessToken}` }),
266+
...(accessToken && { Authorization: `Bearer ${accessToken}` }),
267267
Accept: "application/vnd.git-lfs+json",
268268
"Content-Type": "application/vnd.git-lfs+json",
269269
},
@@ -468,7 +468,7 @@ export async function* commitIter(params: CommitParams): AsyncGenerator<CommitPr
468468
{
469469
method: "POST",
470470
headers: {
471-
...(params.credentials && { Authorization: `Bearer ${params.credentials.accessToken}` }),
471+
...(accessToken && { Authorization: `Bearer ${accessToken}` }),
472472
"Content-Type": "application/x-ndjson",
473473
},
474474
body: [

‎packages/hub/src/lib/count-commits.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
import { HUB_URL } from "../consts";
22
import { createApiError } from "../error";
3-
import type { Credentials, RepoDesignation } from "../types/public";
3+
import type { CredentialsParams, RepoDesignation } from "../types/public";
44
import { checkCredentials } from "../utils/checkCredentials";
55
import { toRepoId } from "../utils/toRepoId";
66

7-
export async function countCommits(params: {
8-
credentials?: Credentials;
9-
repo: RepoDesignation;
10-
/**
11-
* Revision to list commits from. Defaults to the default branch.
12-
*/
13-
revision?: string;
14-
hubUrl?: string;
15-
fetch?: typeof fetch;
16-
}): Promise<number> {
17-
checkCredentials(params.credentials);
7+
export async function countCommits(
8+
params: {
9+
repo: RepoDesignation;
10+
/**
11+
* Revision to list commits from. Defaults to the default branch.
12+
*/
13+
revision?: string;
14+
hubUrl?: string;
15+
fetch?: typeof fetch;
16+
} & Partial<CredentialsParams>
17+
): Promise<number> {
18+
const accessToken = checkCredentials(params);
1819
const repoId = toRepoId(params.repo);
1920

2021
// Could upgrade to 1000 commits per page
@@ -23,7 +24,7 @@ export async function countCommits(params: {
2324
}?limit=1`;
2425

2526
const res: Response = await (params.fetch ?? fetch)(url, {
26-
headers: params.credentials ? { Authorization: `Bearer ${params.credentials.accessToken}` } : {},
27+
headers: accessToken ? { Authorization: `Bearer ${accessToken}` } : {},
2728
});
2829

2930
if (!res.ok) {

‎packages/hub/src/lib/create-repo.spec.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ describe("createRepo", () => {
1111
const repoName = `${TEST_USER}/TEST-${insecureRandomString()}`;
1212

1313
const result = await createRepo({
14-
credentials: {
15-
accessToken: TEST_ACCESS_TOKEN,
16-
},
14+
accessToken: TEST_ACCESS_TOKEN,
1715
repo: {
1816
name: repoName,
1917
type: "model",
@@ -62,9 +60,7 @@ describe("createRepo", () => {
6260
const repoName = `${TEST_USER}/TEST-${insecureRandomString()}`;
6361

6462
const result = await createRepo({
65-
credentials: {
66-
accessToken: TEST_ACCESS_TOKEN,
67-
},
63+
accessToken: TEST_ACCESS_TOKEN,
6864
hubUrl: TEST_HUB_URL,
6965
repo: repoName,
7066
files: [{ path: ".gitattributes", content: new Blob(["*.html filter=lfs diff=lfs merge=lfs -text"]) }],
@@ -88,9 +84,7 @@ describe("createRepo", () => {
8884
const repoName = `datasets/${TEST_USER}/TEST-${insecureRandomString()}`;
8985

9086
const result = await createRepo({
91-
credentials: {
92-
accessToken: TEST_ACCESS_TOKEN,
93-
},
87+
accessToken: TEST_ACCESS_TOKEN,
9488
hubUrl: TEST_HUB_URL,
9589
repo: repoName,
9690
files: [{ path: ".gitattributes", content: new Blob(["*.html filter=lfs diff=lfs merge=lfs -text"]) }],

‎packages/hub/src/lib/create-repo.ts

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
import { HUB_URL } from "../consts";
22
import { createApiError } from "../error";
33
import type { ApiCreateRepoPayload } from "../types/api/api-create-repo";
4-
import type { Credentials, RepoDesignation, SpaceSdk } from "../types/public";
4+
import type { CredentialsParams, RepoDesignation, SpaceSdk } from "../types/public";
55
import { base64FromBytes } from "../utils/base64FromBytes";
66
import { checkCredentials } from "../utils/checkCredentials";
77
import { toRepoId } from "../utils/toRepoId";
88

9-
export async function createRepo(params: {
10-
repo: RepoDesignation;
11-
credentials: Credentials;
12-
private?: boolean;
13-
license?: string;
14-
/**
15-
* Only a few lightweight files are supported at repo creation
16-
*/
17-
files?: Array<{ content: ArrayBuffer | Blob; path: string }>;
18-
/** @required for when {@link repo.type} === "space" */
19-
sdk?: SpaceSdk;
20-
hubUrl?: string;
21-
/**
22-
* Custom fetch function to use instead of the default one, for example to use a proxy or edit headers.
23-
*/
24-
fetch?: typeof fetch;
25-
}): Promise<{ repoUrl: string }> {
26-
checkCredentials(params.credentials);
9+
export async function createRepo(
10+
params: {
11+
repo: RepoDesignation;
12+
private?: boolean;
13+
license?: string;
14+
/**
15+
* Only a few lightweight files are supported at repo creation
16+
*/
17+
files?: Array<{ content: ArrayBuffer | Blob; path: string }>;
18+
/** @required for when {@link repo.type} === "space" */
19+
sdk?: SpaceSdk;
20+
hubUrl?: string;
21+
/**
22+
* Custom fetch function to use instead of the default one, for example to use a proxy or edit headers.
23+
*/
24+
fetch?: typeof fetch;
25+
} & CredentialsParams
26+
): Promise<{ repoUrl: string }> {
27+
const accessToken = checkCredentials(params);
2728
const repoId = toRepoId(params.repo);
2829
const [namespace, repoName] = repoId.name.split("/");
2930

@@ -61,7 +62,7 @@ export async function createRepo(params: {
6162
: undefined,
6263
} satisfies ApiCreateRepoPayload),
6364
headers: {
64-
Authorization: `Bearer ${params.credentials.accessToken}`,
65+
Authorization: `Bearer ${accessToken}`,
6566
"Content-Type": "application/json",
6667
},
6768
});

‎packages/hub/src/lib/delete-file.spec.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@ describe("deleteFile", () => {
1212
it("should delete a file", async () => {
1313
const repoName = `${TEST_USER}/TEST-${insecureRandomString()}`;
1414
const repo = { type: "model", name: repoName } satisfies RepoId;
15-
const credentials = {
16-
accessToken: TEST_ACCESS_TOKEN,
17-
};
1815

1916
try {
2017
const result = await createRepo({
21-
credentials,
18+
accessToken: TEST_ACCESS_TOKEN,
2219
hubUrl: TEST_HUB_URL,
2320
repo,
2421
files: [
@@ -39,7 +36,7 @@ describe("deleteFile", () => {
3936

4037
assert.strictEqual(await content?.text(), "file1");
4138

42-
await deleteFile({ path: "file1", repo, credentials, hubUrl: TEST_HUB_URL });
39+
await deleteFile({ path: "file1", repo, accessToken: TEST_ACCESS_TOKEN, hubUrl: TEST_HUB_URL });
4340

4441
content = await downloadFile({
4542
repo,
@@ -59,7 +56,7 @@ describe("deleteFile", () => {
5956
} finally {
6057
await deleteRepo({
6158
repo,
62-
credentials,
59+
accessToken: TEST_ACCESS_TOKEN,
6360
hubUrl: TEST_HUB_URL,
6461
});
6562
}

0 commit comments

Comments
 (0)