Skip to content

Commit

Permalink
feat!: update API to follow changes in the backend
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Jun 23, 2023
1 parent eb02a2c commit 4f10e04
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 38 deletions.
1 change: 1 addition & 0 deletions project-words.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
lintfix
proxied
12 changes: 8 additions & 4 deletions src/modes/rest/resources/category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ type GetCategoriesResponse = {
data: Array<Category>
}

type CategoryResponse = {
data: string
type AddedCategoryResponse = {
data: string // name
}

type DeleteCategoryParams = {
name: string
}

type DeletedCategoryResponse = {
data: string // name
}

export class CategoryResource implements IRestResource {
client: Rest;

Expand All @@ -35,7 +39,7 @@ export class CategoryResource implements IRestResource {
}

async addCategory(name: string): Promise<string> {
return await fetchPost<CategoryResponse>(
return await fetchPost<AddedCategoryResponse>(
`${this.client.apiBaseUrl}/category`,
JSON.stringify({ name }),
{
Expand All @@ -52,7 +56,7 @@ export class CategoryResource implements IRestResource {
}

async deleteCategory(name: string): Promise<string> {
return await fetchDelete<DeleteCategoryParams, CategoryResponse>(
return await fetchDelete<DeleteCategoryParams, DeletedCategoryResponse>(
`${this.client.apiBaseUrl}/category`,
{ name },
{
Expand Down
16 changes: 1 addition & 15 deletions src/modes/rest/resources/settings.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {PublicSettings, Settings} from "torrust-index-types-lib";
import {IRestResource} from "../restResource";
import {Rest} from "../rest";
import {fetchGet, fetchPost} from "../../../utils/fetch";
import {fetchGet} from "../../../utils/fetch";

type GetSettingsResponse = {
data: Settings
Expand Down Expand Up @@ -31,20 +31,6 @@ export class SettingsResource implements IRestResource {
});
}

public async updateSettings(settings: Settings): Promise<Settings> {
return await fetchPost<GetSettingsResponse>(
`${this.client.apiBaseUrl}/settings`,
JSON.stringify(settings),
{ "Authorization": `Bearer ${this.client.authToken}`, "Content-Type": "application/json" }
)
.then((res) => {
return Promise.resolve(res.data);
})
.catch((err) => {
return Promise.reject(err.response?.data?.error ?? err);
});
}

public async getPublicSettings(): Promise<PublicSettings> {
return await fetchGet<GetPublicSettingsResponse>(
`${this.client.apiBaseUrl}/settings/public`
Expand Down
4 changes: 0 additions & 4 deletions src/modes/rest/resources/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ import {Rest} from "../rest";
import {TorrentTag} from "torrust-index-types-lib";
import {fetchDelete, fetchGet, fetchPost} from "../../../utils/fetch";

type TagResponse = {
data: TorrentTag
}

type DeleteTagParams = {
tag_id: number
}
Expand Down
28 changes: 17 additions & 11 deletions src/modes/rest/resources/torrent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ type GetTorrentsResponseData = {
}

type DeleteTorrentResponse = {
data: DeleteTorrentResponseData
}

type DeleteTorrentResponseData = {
data: {
torrent_id: number
info_hash: string
}
}

Expand All @@ -49,12 +54,13 @@ type UploadTorrentParams = {
file: any
}

type UploadTorrentResponse = {
data: UploadTorrentResponseData
type NewTorrentResponse = {
data: NewTorrentResponseData
}

type UploadTorrentResponseData = {
type NewTorrentResponseData = {
torrent_id: number
info_hash: string
}

export class TorrentResource implements IRestResource {
Expand All @@ -64,7 +70,7 @@ export class TorrentResource implements IRestResource {
this.client = client;
}

async getTorrent(infoHash: string): Promise<TorrentResponse> {
async getTorrentInfo(infoHash: string): Promise<TorrentResponse> {
return await fetchGet<GetTorrentResponse>(
`${this.client.apiBaseUrl}/torrent/${infoHash}`
)
Expand All @@ -88,14 +94,14 @@ export class TorrentResource implements IRestResource {
});
}

async deleteTorrent(infoHash: string): Promise<boolean> {
async deleteTorrent(infoHash: string): Promise<DeleteTorrentResponseData> {
return await fetchDelete<any, DeleteTorrentResponse>(
`${this.client.apiBaseUrl}/torrent/${infoHash}`,
{},
{ "Authorization": `Bearer ${this.client.authToken}` }
)
.then((_res) => {
return Promise.resolve(true);
.then((res) => {
return Promise.resolve(res.data);
})
.catch((err) => {
return Promise.reject(err.response?.data?.error ?? err);
Expand All @@ -116,7 +122,7 @@ export class TorrentResource implements IRestResource {
});
}

async uploadTorrent(params: UploadTorrentParams): Promise<number> {
async uploadTorrent(params: UploadTorrentParams): Promise<NewTorrentResponseData> {
const formData = new FormData();

formData.append("title", params.title);
Expand All @@ -125,20 +131,20 @@ export class TorrentResource implements IRestResource {
formData.append("tags", JSON.stringify(params.tags));
formData.append("torrent", params.file);

return await fetchPost<UploadTorrentResponse>(
return await fetchPost<NewTorrentResponse>(
`${this.client.apiBaseUrl}/torrent/upload`,
formData,
{ "Authorization": `Bearer ${this.client.authToken}` }
)
.then((res) => {
return Promise.resolve(res.data.torrent_id);
return Promise.resolve(res.data);
})
.catch((err) => {
return Promise.reject(err.response?.data?.error ?? err);
});
}

async downloadTorrent(infoHash: number): Promise<Blob> {
async downloadTorrent(infoHash: string): Promise<Blob> {
return await fetchGetBlob(
`${this.client.apiBaseUrl}/torrent/download/${infoHash}`
)
Expand Down
14 changes: 10 additions & 4 deletions src/modes/rest/resources/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ type LoginUserResponse = {
data: TokenResponse
}

type RenewedTokenResponse = {
data: TokenResponse
}

type RegisterUserParams = {
username: string
email: string
Expand All @@ -24,9 +28,11 @@ type Token = {
}

type AddedUserResponse = {
data: {
user_id: number
}
data: NewUser
}

type NewUser = {
user_id: number
}

export class UserResource implements IRestResource {
Expand Down Expand Up @@ -68,7 +74,7 @@ export class UserResource implements IRestResource {
}

async renewToken(): Promise<TokenResponse> {
return await fetchPost<LoginUserResponse>(
return await fetchPost<RenewedTokenResponse>(
`${this.client.apiBaseUrl}/user/token/renew`,
JSON.stringify({ token: this.client.authToken ?? "" }),
{ "Content-Type": "application/json"}
Expand Down

0 comments on commit 4f10e04

Please sign in to comment.