1
1
import { TorrentCategory } from "torrust-index-types-lib" ;
2
2
import { IRestResource } from "../restResource" ;
3
3
import { Rest } from "../rest" ;
4
- import { fetchGet } from "../../../utils/fetch" ;
4
+ import { fetchDelete , fetchGet , fetchPost } from "../../../utils/fetch" ;
5
5
6
6
type GetCategoriesResponse = {
7
7
data : Array < TorrentCategory >
8
8
}
9
9
10
+ type CategoryResponse = {
11
+ data : string
12
+ }
13
+
14
+ type DeleteCategoryParams = {
15
+ name : string
16
+ }
17
+
10
18
export class CategoryResource implements IRestResource {
11
19
client : Rest ;
12
20
@@ -25,5 +33,39 @@ export class CategoryResource implements IRestResource {
25
33
return Promise . reject ( err ) ;
26
34
} ) ;
27
35
}
36
+
37
+ async addCategory ( name : string ) : Promise < string > {
38
+ return await fetchPost < CategoryResponse > (
39
+ `${ this . client . apiBaseUrl } /category` ,
40
+ JSON . stringify ( { name } ) ,
41
+ {
42
+ "Authorization" : `Bearer ${ this . client . authToken } ` ,
43
+ "Content-Type" : "application/json"
44
+ }
45
+ )
46
+ . then ( ( res ) => {
47
+ return Promise . resolve ( res . data ) ;
48
+ } )
49
+ . catch ( ( err ) => {
50
+ return Promise . reject ( err ) ;
51
+ } ) ;
52
+ }
53
+
54
+ async deleteCategory ( name : string ) : Promise < string > {
55
+ return await fetchDelete < DeleteCategoryParams , CategoryResponse > (
56
+ `${ this . client . apiBaseUrl } /category` ,
57
+ { name } ,
58
+ {
59
+ "Authorization" : `Bearer ${ this . client . authToken } ` ,
60
+ "Content-Type" : "application/json"
61
+ }
62
+ )
63
+ . then ( ( res ) => {
64
+ return Promise . resolve ( res . data ) ;
65
+ } )
66
+ . catch ( ( err ) => {
67
+ return Promise . reject ( err ) ;
68
+ } ) ;
69
+ }
28
70
}
29
71
0 commit comments