@@ -50,94 +50,94 @@ type UploadTorrentResponseData = {
50
50
}
51
51
52
52
export class TorrentResource implements IRestResource {
53
- client : Rest ;
54
-
55
- constructor ( client : Rest ) {
56
- this . client = client ;
57
- }
58
-
59
- async getTorrent ( torrentId : number ) : Promise < Torrent > {
60
- return await fetchGet < GetTorrentResponse > (
61
- `${ this . client . apiBaseUrl } /torrent/${ torrentId } `
62
- )
63
- . then ( ( res ) => {
64
- return Promise . resolve ( res . data ) ;
65
- } )
66
- . catch ( ( err ) => {
67
- return Promise . reject ( err ) ;
68
- } ) ;
69
- }
70
-
71
- async getTorrents ( params : GetTorrentsParams ) : Promise < GetTorrentsResponseData > {
72
- return await fetchGet < GetTorrentsResponse > (
73
- `${ this . client . apiBaseUrl } /torrents?page_size=${ params . pageSize } &page=${ params . page - 1 } &sort=${ params . sorting } ${ params . categories ? "&categories=" + params . categories . join ( "," ) : "" } ${ params . searchQuery ? "&search=" + params . searchQuery : "" } `
74
- )
75
- . then ( ( res ) => {
76
- return Promise . resolve ( res . data ) ;
77
- } )
78
- . catch ( ( err ) => {
79
- return Promise . reject ( err ) ;
80
- } ) ;
81
- }
82
-
83
- async deleteTorrent ( torrentId : number ) : Promise < boolean > {
84
- return await fetchDelete < any , DeleteTorrentResponse > (
85
- `${ this . client . apiBaseUrl } /torrent/${ torrentId } ` ,
86
- { } ,
87
- { "Authorization" : `Bearer ${ this . client . authToken } ` }
88
- )
89
- . then ( ( _res ) => {
90
- return Promise . resolve ( true ) ;
91
- } )
92
- . catch ( ( err ) => {
93
- return Promise . reject ( err ) ;
94
- } ) ;
95
- }
96
-
97
- async updateTorrent ( torrent : Torrent ) : Promise < Torrent > {
98
- return await fetchPut < Torrent , UpdateTorrentResponse > (
99
- `${ this . client . apiBaseUrl } /torrent/${ torrent . torrent_id } ` ,
100
- torrent ,
101
- { "Authorization" : `Bearer ${ this . client . authToken } ` }
102
- )
103
- . then ( ( res ) => {
104
- return Promise . resolve ( res . data ) ;
105
- } )
106
- . catch ( ( err ) => {
107
- return Promise . reject ( err ) ;
108
- } ) ;
109
- }
110
-
111
- async uploadTorrent ( params : UploadTorrentParams ) : Promise < number > {
112
- const formData = new FormData ( ) ;
113
-
114
- formData . append ( "title" , params . title ) ;
115
- formData . append ( "description" , params . description ) ;
116
- formData . append ( "category" , params . category ) ;
117
- formData . append ( "torrent" , params . file ) ;
118
-
119
- return await fetchPost < FormData , UploadTorrentResponse > (
120
- `${ this . client . apiBaseUrl } /torrent/upload` ,
121
- formData ,
122
- { "Authorization" : `Bearer ${ this . client . authToken } ` }
123
- )
124
- . then ( ( res ) => {
125
- return Promise . resolve ( res . data . torrent_id ) ;
126
- } )
127
- . catch ( ( err ) => {
128
- return Promise . reject ( err . response ?. data ?. error ?? err ) ;
129
- } ) ;
130
- }
131
-
132
- async downloadTorrent ( torrentId : number ) : Promise < Blob > {
133
- return await fetchGetBlob (
134
- `${ this . client . apiBaseUrl } /torrent/download/${ torrentId } `
135
- )
136
- . then ( ( blob ) => {
137
- return Promise . resolve ( blob ) ;
138
- } )
139
- . catch ( ( err ) => {
140
- return Promise . reject ( err ) ;
141
- } ) ;
142
- }
53
+ client : Rest ;
54
+
55
+ constructor ( client : Rest ) {
56
+ this . client = client ;
57
+ }
58
+
59
+ async getTorrent ( torrentId : number ) : Promise < Torrent > {
60
+ return await fetchGet < GetTorrentResponse > (
61
+ `${ this . client . apiBaseUrl } /torrent/${ torrentId } `
62
+ )
63
+ . then ( ( res ) => {
64
+ return Promise . resolve ( res . data ) ;
65
+ } )
66
+ . catch ( ( err ) => {
67
+ return Promise . reject ( err ) ;
68
+ } ) ;
69
+ }
70
+
71
+ async getTorrents ( params : GetTorrentsParams ) : Promise < GetTorrentsResponseData > {
72
+ return await fetchGet < GetTorrentsResponse > (
73
+ `${ this . client . apiBaseUrl } /torrents?page_size=${ params . pageSize } &page=${ params . page - 1 } &sort=${ params . sorting } ${ params . categories ? "&categories=" + params . categories . join ( "," ) : "" } ${ params . searchQuery ? "&search=" + params . searchQuery : "" } `
74
+ )
75
+ . then ( ( res ) => {
76
+ return Promise . resolve ( res . data ) ;
77
+ } )
78
+ . catch ( ( err ) => {
79
+ return Promise . reject ( err ) ;
80
+ } ) ;
81
+ }
82
+
83
+ async deleteTorrent ( torrentId : number ) : Promise < boolean > {
84
+ return await fetchDelete < any , DeleteTorrentResponse > (
85
+ `${ this . client . apiBaseUrl } /torrent/${ torrentId } ` ,
86
+ { } ,
87
+ { "Authorization" : `Bearer ${ this . client . authToken } ` }
88
+ )
89
+ . then ( ( _res ) => {
90
+ return Promise . resolve ( true ) ;
91
+ } )
92
+ . catch ( ( err ) => {
93
+ return Promise . reject ( err ) ;
94
+ } ) ;
95
+ }
96
+
97
+ async updateTorrent ( torrent : Torrent ) : Promise < Torrent > {
98
+ return await fetchPut < Torrent , UpdateTorrentResponse > (
99
+ `${ this . client . apiBaseUrl } /torrent/${ torrent . torrent_id } ` ,
100
+ torrent ,
101
+ { "Authorization" : `Bearer ${ this . client . authToken } ` }
102
+ )
103
+ . then ( ( res ) => {
104
+ return Promise . resolve ( res . data ) ;
105
+ } )
106
+ . catch ( ( err ) => {
107
+ return Promise . reject ( err ) ;
108
+ } ) ;
109
+ }
110
+
111
+ async uploadTorrent ( params : UploadTorrentParams ) : Promise < number > {
112
+ const formData = new FormData ( ) ;
113
+
114
+ formData . append ( "title" , params . title ) ;
115
+ formData . append ( "description" , params . description ) ;
116
+ formData . append ( "category" , params . category ) ;
117
+ formData . append ( "torrent" , params . file ) ;
118
+
119
+ return await fetchPost < FormData , UploadTorrentResponse > (
120
+ `${ this . client . apiBaseUrl } /torrent/upload` ,
121
+ formData ,
122
+ { "Authorization" : `Bearer ${ this . client . authToken } ` }
123
+ )
124
+ . then ( ( res ) => {
125
+ return Promise . resolve ( res . data . torrent_id ) ;
126
+ } )
127
+ . catch ( ( err ) => {
128
+ return Promise . reject ( err . response ?. data ?. error ?? err ) ;
129
+ } ) ;
130
+ }
131
+
132
+ async downloadTorrent ( torrentId : number ) : Promise < Blob > {
133
+ return await fetchGetBlob (
134
+ `${ this . client . apiBaseUrl } /torrent/download/${ torrentId } `
135
+ )
136
+ . then ( ( blob ) => {
137
+ return Promise . resolve ( blob ) ;
138
+ } )
139
+ . catch ( ( err ) => {
140
+ return Promise . reject ( err ) ;
141
+ } ) ;
142
+ }
143
143
}
0 commit comments