File tree Expand file tree Collapse file tree 7 files changed +131
-3
lines changed Expand file tree Collapse file tree 7 files changed +131
-3
lines changed Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ import { collectionInfo } from "./collection-info";
5
5
describe ( "collectionInfo" , ( ) => {
6
6
it ( "should return the collection info" , async ( ) => {
7
7
const collection = await collectionInfo ( {
8
- collectionSlug : "quanghuynt14/test-collection-6866ff686ca2d2e0a1931507" ,
8
+ slug : "quanghuynt14/test-collection-6866ff686ca2d2e0a1931507" ,
9
9
hubUrl : TEST_HUB_URL ,
10
10
} ) ;
11
11
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ export async function collectionInfo(
9
9
/**
10
10
* The slug of the collection.
11
11
*/
12
- collectionSlug : string ;
12
+ slug : string ;
13
13
hubUrl ?: string ;
14
14
/**
15
15
* Custom fetch function to use instead of the default one, for example to use a proxy or edit headers.
@@ -19,7 +19,7 @@ export async function collectionInfo(
19
19
) : Promise < ApiCollectionInfo & { position : number ; shareUrl : string } > {
20
20
const accessToken = checkCredentials ( params ) ;
21
21
22
- const res = await ( params . fetch ?? fetch ) ( `${ params . hubUrl ?? HUB_URL } /api/collections/${ params . collectionSlug } ` , {
22
+ const res = await ( params . fetch ?? fetch ) ( `${ params . hubUrl ?? HUB_URL } /api/collections/${ params . slug } ` , {
23
23
headers : {
24
24
"Content-Type" : "application/json" ,
25
25
...( accessToken ? { Authorization : `Bearer ${ accessToken } ` } : undefined ) ,
Original file line number Diff line number Diff line change
1
+ import { it , describe , expect } from "vitest" ;
2
+
3
+ import { TEST_HUB_URL , TEST_ACCESS_TOKEN } from "../test/consts" ;
4
+ import { createCollection } from "./create-collection" ;
5
+ import { whoAmI } from "./who-am-i" ;
6
+ import { deleteCollection } from "./delete-collection" ;
7
+
8
+ describe ( "createCollection" , ( ) => {
9
+ it ( "should create a collection" , async ( ) => {
10
+ let slug : string = "" ;
11
+
12
+ try {
13
+ const user = await whoAmI ( {
14
+ hubUrl : TEST_HUB_URL ,
15
+ accessToken : TEST_ACCESS_TOKEN ,
16
+ } ) ;
17
+
18
+ const result = await createCollection ( {
19
+ collection : {
20
+ title : "Test Collection" ,
21
+ namespace : user . name ,
22
+ description : "This is a test collection" ,
23
+ private : false ,
24
+ } ,
25
+ accessToken : TEST_ACCESS_TOKEN ,
26
+ hubUrl : TEST_HUB_URL ,
27
+ } ) ;
28
+
29
+ expect ( result . slug . startsWith ( `${ user . name } /test-collection` ) ) . toBe ( true ) ;
30
+
31
+ slug = result . slug ;
32
+ } finally {
33
+ await deleteCollection ( {
34
+ slug,
35
+ accessToken : TEST_ACCESS_TOKEN ,
36
+ hubUrl : TEST_HUB_URL ,
37
+ } ) ;
38
+ }
39
+ } ) ;
40
+ } ) ;
Original file line number Diff line number Diff line change
1
+ import { HUB_URL } from "../consts" ;
2
+ import { createApiError } from "../error" ;
3
+ import type { ApiCreateCollectionPayload } from "../types/api/api-create-collection" ;
4
+ import type { CredentialsParams } from "../types/public" ;
5
+ import { checkCredentials } from "../utils/checkCredentials" ;
6
+
7
+ export async function createCollection (
8
+ params : {
9
+ collection : ApiCreateCollectionPayload ;
10
+ hubUrl ?: string ;
11
+ /**
12
+ * Custom fetch function to use instead of the default one, for example to use a proxy or edit headers.
13
+ */
14
+ fetch ?: typeof fetch ;
15
+ } & Partial < CredentialsParams >
16
+ ) : Promise < { slug : string } > {
17
+ const accessToken = checkCredentials ( params ) ;
18
+
19
+ const res = await ( params . fetch ?? fetch ) ( `${ params . hubUrl ?? HUB_URL } /api/collections` , {
20
+ method : "POST" ,
21
+ body : JSON . stringify ( params . collection ) ,
22
+ headers : {
23
+ Authorization : `Bearer ${ accessToken } ` ,
24
+ "Content-Type" : "application/json" ,
25
+ } ,
26
+ } ) ;
27
+
28
+ if ( ! res . ok ) {
29
+ throw await createApiError ( res ) ;
30
+ }
31
+
32
+ const output = await res . json ( ) ;
33
+
34
+ return { slug : output . slug } ;
35
+ }
Original file line number Diff line number Diff line change
1
+ import { HUB_URL } from "../consts" ;
2
+ import { createApiError } from "../error" ;
3
+ import type { CredentialsParams } from "../types/public" ;
4
+ import { checkCredentials } from "../utils/checkCredentials" ;
5
+
6
+ export async function deleteCollection (
7
+ params : {
8
+ /**
9
+ * The slug of the collection to delete.
10
+ */
11
+ slug : string ;
12
+ hubUrl ?: string ;
13
+ /**
14
+ * Custom fetch function to use instead of the default one, for example to use a proxy or edit headers.
15
+ */
16
+ fetch ?: typeof fetch ;
17
+ } & Partial < CredentialsParams >
18
+ ) : Promise < void > {
19
+ const accessToken = checkCredentials ( params ) ;
20
+
21
+ const res = await ( params . fetch ?? fetch ) ( `${ params . hubUrl ?? HUB_URL } /api/collections/${ params . slug } ` , {
22
+ method : "DELETE" ,
23
+ headers : {
24
+ Authorization : `Bearer ${ accessToken } ` ,
25
+ "Content-Type" : "application/json" ,
26
+ } ,
27
+ } ) ;
28
+
29
+ if ( ! res . ok ) {
30
+ throw await createApiError ( res ) ;
31
+ }
32
+ }
Original file line number Diff line number Diff line change @@ -4,11 +4,13 @@ export * from "./commit";
4
4
export * from "./count-commits" ;
5
5
export * from "./create-repo" ;
6
6
export * from "./create-branch" ;
7
+ export * from "./create-collection" ;
7
8
export * from "./dataset-info" ;
8
9
export * from "./delete-branch" ;
9
10
export * from "./delete-file" ;
10
11
export * from "./delete-files" ;
11
12
export * from "./delete-repo" ;
13
+ export * from "./delete-collection" ;
12
14
export * from "./download-file" ;
13
15
export * from "./download-file-to-cache-dir" ;
14
16
export * from "./file-download-info" ;
Original file line number Diff line number Diff line change
1
+ export interface ApiCreateCollectionPayload {
2
+ /**
3
+ * Title of the collection to create.
4
+ */
5
+ title : string ;
6
+ /**
7
+ * Namespace of the collection to create (username or org).
8
+ */
9
+ namespace : string ;
10
+ /**
11
+ * Description of the collection to create.
12
+ */
13
+ description ?: string ;
14
+ /**
15
+ * Whether the collection should be private or not. Defaults to False (i.e. public collection).
16
+ * @default false
17
+ */
18
+ private ?: boolean ;
19
+ }
You can’t perform that action at this time.
0 commit comments