Skip to content

Commit 43b74c0

Browse files
committed
feat: tags filter to getTorrents call
1 parent b8d0d97 commit 43b74c0

File tree

4 files changed

+92
-28
lines changed

4 files changed

+92
-28
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "torrust-index-api-lib",
3-
"version": "0.1.11",
3+
"version": "0.1.12",
44
"description": "Contains API functions for the Torrust project.",
55
"repository": "https://github.com/torrust/torrust-index-types-lib",
66
"license": "MIT",

src/modes/rest/resources/tag.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import {IRestResource} from "../restResource";
2+
import {Rest} from "../rest";
3+
import {TorrentTag} from "../../../../../torrust-index-types-lib";
4+
import {fetchDelete, fetchGet, fetchPost} from "../../../utils/fetch";
5+
6+
type TagResponse = {
7+
data: TorrentTag
8+
}
9+
10+
type DeleteTagPrams = {
11+
tag_id: number
12+
}
13+
14+
type GetTagsResponse = {
15+
data: Array<TorrentTag>
16+
}
17+
18+
export class TagResource implements IRestResource {
19+
client: Rest;
20+
21+
constructor(client: Rest) {
22+
this.client = client;
23+
}
24+
25+
async addTag(name: string): Promise<string> {
26+
return await fetchPost<any>(
27+
`${this.client.apiBaseUrl}/tag`,
28+
JSON.stringify({ name }),
29+
{
30+
"Authorization": `Bearer ${this.client.authToken}`,
31+
"Content-Type": "application/json"
32+
}
33+
)
34+
.then((res) => {
35+
return Promise.resolve(res.data);
36+
})
37+
.catch((err) => {
38+
return Promise.reject(err);
39+
});
40+
}
41+
42+
async deleteTag(id: number): Promise<number> {
43+
return await fetchDelete<DeleteTagPrams, any>(
44+
`${this.client.apiBaseUrl}/tag`,
45+
{ tag_id: id },
46+
{
47+
"Authorization": `Bearer ${this.client.authToken}`,
48+
"Content-Type": "application/json"
49+
}
50+
)
51+
.then((res) => {
52+
return Promise.resolve(res.data);
53+
})
54+
.catch((err) => {
55+
return Promise.reject(err);
56+
});
57+
}
58+
59+
async getTags(): Promise<Array<TorrentTag>> {
60+
return await fetchGet<GetTagsResponse>(
61+
`${this.client.apiBaseUrl}/tags`
62+
)
63+
.then((res) => {
64+
return Promise.resolve(res.data);
65+
})
66+
.catch((err) => {
67+
return Promise.reject(err);
68+
});
69+
}
70+
}

src/modes/rest/resources/torrent.ts

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type GetTorrentsParams = {
1212
page: number
1313
sorting: string
1414
categories?: Array<string>
15+
tags?: Array<string>
1516
searchQuery?: string
1617
}
1718

@@ -30,6 +31,12 @@ type DeleteTorrentResponse = {
3031
}
3132
}
3233

34+
type UpdateTorrentParams = {
35+
title?: string
36+
description?: string
37+
tags?: number[]
38+
}
39+
3340
type UpdateTorrentResponse = {
3441
data: Torrent
3542
}
@@ -50,20 +57,16 @@ type UploadTorrentResponseData = {
5057
torrent_id: number
5158
}
5259

53-
type GetTagsResponse = {
54-
data: Array<TorrentTag>
55-
}
56-
5760
export class TorrentResource implements IRestResource {
5861
client: Rest;
5962

6063
constructor(client: Rest) {
6164
this.client = client;
6265
}
6366

64-
async getTorrent(torrentId: number): Promise<Torrent> {
67+
async getTorrent(infoHash: string): Promise<Torrent> {
6568
return await fetchGet<GetTorrentResponse>(
66-
`${this.client.apiBaseUrl}/torrent/${torrentId}`
69+
`${this.client.apiBaseUrl}/torrent/${infoHash}`
6770
)
6871
.then((res) => {
6972
return Promise.resolve(res.data);
@@ -75,7 +78,7 @@ export class TorrentResource implements IRestResource {
7578

7679
async getTorrents(params: GetTorrentsParams): Promise<GetTorrentsResponseData> {
7780
return await fetchGet<GetTorrentsResponse>(
78-
`${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 : ""}`
81+
`${this.client.apiBaseUrl}/torrents?page_size=${params.pageSize}&page=${params.page - 1}&sort=${params.sorting}${ params.categories ? "&categories=" + params.categories.join(",") : ""}${ params.tags ? "&tags=" + params.tags.join(",") : ""}${params.searchQuery ? "&search=" + params.searchQuery : ""}`
7982
)
8083
.then((res) => {
8184
return Promise.resolve(res.data);
@@ -85,9 +88,9 @@ export class TorrentResource implements IRestResource {
8588
});
8689
}
8790

88-
async deleteTorrent(torrentId: number): Promise<boolean> {
91+
async deleteTorrent(infoHash: string): Promise<boolean> {
8992
return await fetchDelete<any, DeleteTorrentResponse>(
90-
`${this.client.apiBaseUrl}/torrent/${torrentId}`,
93+
`${this.client.apiBaseUrl}/torrent/${infoHash}`,
9194
{},
9295
{ "Authorization": `Bearer ${this.client.authToken}` }
9396
)
@@ -99,10 +102,10 @@ export class TorrentResource implements IRestResource {
99102
});
100103
}
101104

102-
async updateTorrent(torrent: Torrent): Promise<Torrent> {
103-
return await fetchPut<Torrent, UpdateTorrentResponse>(
104-
`${this.client.apiBaseUrl}/torrent/${torrent.torrent_id}`,
105-
torrent,
105+
async updateTorrent(infoHash: string, params: UpdateTorrentParams): Promise<Torrent> {
106+
return await fetchPut<UpdateTorrentParams, UpdateTorrentResponse>(
107+
`${this.client.apiBaseUrl}/torrent/${infoHash}`,
108+
params,
106109
{ "Authorization": `Bearer ${this.client.authToken}`, "Content-Type": "application/json" }
107110
)
108111
.then((res) => {
@@ -135,9 +138,9 @@ export class TorrentResource implements IRestResource {
135138
});
136139
}
137140

138-
async downloadTorrent(torrentId: number): Promise<Blob> {
141+
async downloadTorrent(infoHash: number): Promise<Blob> {
139142
return await fetchGetBlob(
140-
`${this.client.apiBaseUrl}/torrent/download/${torrentId}`
143+
`${this.client.apiBaseUrl}/torrent/download/${infoHash}`
141144
)
142145
.then((blob) => {
143146
return Promise.resolve(blob);
@@ -161,16 +164,4 @@ export class TorrentResource implements IRestResource {
161164
return Promise.reject(err);
162165
});
163166
}
164-
165-
async getTags(): Promise<Array<TorrentTag>> {
166-
return await fetchGet<GetTagsResponse>(
167-
`${this.client.apiBaseUrl}/tags`
168-
)
169-
.then((res) => {
170-
return Promise.resolve(res.data);
171-
})
172-
.catch((err) => {
173-
return Promise.reject(err);
174-
});
175-
}
176167
}

src/modes/rest/rest.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {CategoryResource} from "./resources/category";
22
import {SettingsResource} from "./resources/settings";
33
import {TorrentResource} from "./resources/torrent";
44
import {UserResource} from "./resources/user";
5+
import {TagResource} from "./resources/tag";
56

67
const LOCAL_STORAGE_TOKEN_KEY = "torrust_token";
78

@@ -12,6 +13,7 @@ export class Rest {
1213
public settings: SettingsResource;
1314
public torrent: TorrentResource;
1415
public user: UserResource;
16+
public tag: TagResource;
1517

1618
constructor(apiBaseUrl: string) {
1719
this.apiBaseUrl = apiBaseUrl;
@@ -22,6 +24,7 @@ export class Rest {
2224
this.settings = new SettingsResource(this);
2325
this.torrent = new TorrentResource(this);
2426
this.user = new UserResource(this);
27+
this.tag = new TagResource(this);
2528
}
2629

2730
public getToken() {

0 commit comments

Comments
 (0)