8000 ✨ Support specifying files when creating repo · huggingface/huggingface.js@fe190bf · GitHub
[go: up one dir, main page]

Skip to content

Commit fe190bf

Browse files
committed
✨ Support specifying files when creating repo
1 parent 64dabfd commit fe190bf

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { randomBytes } from "crypto";
22
import { TEST_ACCESS_TOKEN, TEST_USER } from "../consts";
33
import { createRepo } from "./create-repo";
44
import { deleteRepo } from "./delete-repo";
5+
import { downloadFile } from "./download-file";
6+
import * as assert from "assert";
57

68
describe("createRepo", () => {
79
it("should create a repo", async () => {
@@ -15,8 +17,20 @@ describe("createRepo", () => {
1517
name: repoName,
1618
type: "model",
1719
},
20+
files: [{ path: ".gitattributes", content: new Blob(["*.html filter=lfs diff=lfs merge=lfs -text"]) }],
1821
});
1922

23+
const content = await downloadFile({
24+
repo: {
25+
name: repoName,
26+
type: "model",
27+
},
28+
path: ".gitattributes",
29+
});
30+
31+
assert(content);
32+
assert.strictEqual(await content.text(), "*.html filter=lfs diff=lfs merge=lfs -text");
33+
2034
await deleteRepo({
2135
repo: {
2236
name: repoName,

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import { HUB_URL } from "../consts";
22
import { createApiError } from "../error";
33
import type { ApiCreateRepoPayload, Credentials, RepoId, SpaceSdk } from "../types";
4+
import { base64FromBytes } from "../utils";
45

56
export async function createRepo(params: {
67
repo: RepoId;
78
credentials: Credentials;
89
private?: boolean;
910
license?: string;
11+
/**
12+
* Only a few lightweight files are supported at repo creation
13+
*/
14+
files?: Array<{ content: ArrayBuffer | Blob; path: string }>;
1015
/** @required for when {@link repo.type} === "space" */
1116
sdk?: SpaceSdk;
1217
hubUrl?: string;
@@ -28,6 +33,17 @@ export async function createRepo(params: {
2833
: {
2934
type: params.repo.type,
3035
}),
36+
files: params.files
37+
? await Promise.all(
38+
params.files.map(async (file) => ({
39+
encoding: "base64",
40+
path: file.path,
41+
content: base64FromBytes(
42+
new Uint8Array(file.content instanceof Blob ? await file.content.arrayBuffer() : file.content)
43+
),
44+
}))
45+
)
46+
: undefined,
3147
} satisfies ApiCreateRepoPayload),
3248
headers: {
3349
Authorization: `Bearer ${params.credentials.accessToken}`,

0 commit comments

Comments
 (0)
0