From 4f10e04d32eb1ebe2595aa0f55918babcd85ea9a Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Fri, 23 Jun 2023 13:42:11 +0100 Subject: [PATCH] feat!: update API to follow changes in the backend --- project-words.txt | 1 + src/modes/rest/resources/category.ts | 12 ++++++++---- src/modes/rest/resources/settings.ts | 16 +--------------- src/modes/rest/resources/tag.ts | 4 ---- src/modes/rest/resources/torrent.ts | 28 +++++++++++++++++----------- src/modes/rest/resources/user.ts | 14 ++++++++++---- 6 files changed, 37 insertions(+), 38 deletions(-) diff --git a/project-words.txt b/project-words.txt index 35e930e..d48d593 100644 --- a/project-words.txt +++ b/project-words.txt @@ -1 +1,2 @@ lintfix +proxied diff --git a/src/modes/rest/resources/category.ts b/src/modes/rest/resources/category.ts index ff79d1a..6d10526 100644 --- a/src/modes/rest/resources/category.ts +++ b/src/modes/rest/resources/category.ts @@ -7,14 +7,18 @@ type GetCategoriesResponse = { data: Array } -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; @@ -35,7 +39,7 @@ export class CategoryResource implements IRestResource { } async addCategory(name: string): Promise { - return await fetchPost( + return await fetchPost( `${this.client.apiBaseUrl}/category`, JSON.stringify({ name }), { @@ -52,7 +56,7 @@ export class CategoryResource implements IRestResource { } async deleteCategory(name: string): Promise { - return await fetchDelete( + return await fetchDelete( `${this.client.apiBaseUrl}/category`, { name }, { diff --git a/src/modes/rest/resources/settings.ts b/src/modes/rest/resources/settings.ts index fcc7042..96ba599 100644 --- a/src/modes/rest/resources/settings.ts +++ b/src/modes/rest/resources/settings.ts @@ -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 @@ -31,20 +31,6 @@ export class SettingsResource implements IRestResource { }); } - public async updateSettings(settings: Settings): Promise { - return await fetchPost( - `${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 { return await fetchGet( `${this.client.apiBaseUrl}/settings/public` diff --git a/src/modes/rest/resources/tag.ts b/src/modes/rest/resources/tag.ts index 88fe110..6ac00b0 100644 --- a/src/modes/rest/resources/tag.ts +++ b/src/modes/rest/resources/tag.ts @@ -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 } diff --git a/src/modes/rest/resources/torrent.ts b/src/modes/rest/resources/torrent.ts index bed7f46..ba2d788 100644 --- a/src/modes/rest/resources/torrent.ts +++ b/src/modes/rest/resources/torrent.ts @@ -26,8 +26,13 @@ type GetTorrentsResponseData = { } type DeleteTorrentResponse = { + data: DeleteTorrentResponseData +} + +type DeleteTorrentResponseData = { data: { torrent_id: number + info_hash: string } } @@ -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 { @@ -64,7 +70,7 @@ export class TorrentResource implements IRestResource { this.client = client; } - async getTorrent(infoHash: string): Promise { + async getTorrentInfo(infoHash: string): Promise { return await fetchGet( `${this.client.apiBaseUrl}/torrent/${infoHash}` ) @@ -88,14 +94,14 @@ export class TorrentResource implements IRestResource { }); } - async deleteTorrent(infoHash: string): Promise { + async deleteTorrent(infoHash: string): Promise { return await fetchDelete( `${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); @@ -116,7 +122,7 @@ export class TorrentResource implements IRestResource { }); } - async uploadTorrent(params: UploadTorrentParams): Promise { + async uploadTorrent(params: UploadTorrentParams): Promise { const formData = new FormData(); formData.append("title", params.title); @@ -125,20 +131,20 @@ export class TorrentResource implements IRestResource { formData.append("tags", JSON.stringify(params.tags)); formData.append("torrent", params.file); - return await fetchPost( + return await fetchPost( `${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 { + async downloadTorrent(infoHash: string): Promise { return await fetchGetBlob( `${this.client.apiBaseUrl}/torrent/download/${infoHash}` ) diff --git a/src/modes/rest/resources/user.ts b/src/modes/rest/resources/user.ts index 7dbcc61..0a9b740 100644 --- a/src/modes/rest/resources/user.ts +++ b/src/modes/rest/resources/user.ts @@ -12,6 +12,10 @@ type LoginUserResponse = { data: TokenResponse } +type RenewedTokenResponse = { + data: TokenResponse +} + type RegisterUserParams = { username: string email: string @@ -24,9 +28,11 @@ type Token = { } type AddedUserResponse = { - data: { - user_id: number - } + data: NewUser +} + +type NewUser = { + user_id: number } export class UserResource implements IRestResource { @@ -68,7 +74,7 @@ export class UserResource implements IRestResource { } async renewToken(): Promise { - return await fetchPost( + return await fetchPost( `${this.client.apiBaseUrl}/user/token/renew`, JSON.stringify({ token: this.client.authToken ?? "" }), { "Content-Type": "application/json"}