Skip to content

Commit 9d164d9

Browse files
committed
feat: moved to object based model
1 parent 9411750 commit 9d164d9

File tree

12 files changed

+406
-287
lines changed

12 files changed

+406
-287
lines changed

package-lock.json

Lines changed: 9 additions & 161 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
{
22
"name": "torrust-index-api-lib",
3-
"version": "0.1.8",
3+
"version": "0.1.9",
44
"description": "Contains API functions for the Torrust project.",
55
"repository": "https://github.com/torrust/torrust-index-types-lib",
66
"license": "MIT",
77
"main": "lib/index.js",
88
"types": "lib/index.d.ts",
99
"scripts": {
1010
"build": "tsc",
11-
"prepare": "npm run build"
11+
"prepare": "npm run build",
12+
"prepublish": "tsc"
1213
},
1314
"devDependencies": {
1415
"typescript": "^4.9.3"
1516
},
1617
"dependencies": {
17-
"axios": "^1.1.3",
18-
"torrust-index-types-lib": "^0.1.1"
18+
"torrust-index-types-lib": "^0.1.3"
1919
}
2020
}

src/http-service.ts

Lines changed: 0 additions & 70 deletions
This file was deleted.

src/index.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1 @@
1-
import * as torrent from "./rest/torrent";
2-
3-
const graphql = {}
4-
5-
const rest = {
6-
torrent
7-
}
8-
9-
export { graphql, rest }
1+
export {Rest} from "./modes/rest/rest";

src/modes/rest/resources/category.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {TorrentCategory} from "torrust-index-types-lib"
2+
import {IRestResource} from "../restResource"
3+
import {Rest} from "../rest"
4+
import {fetchGet} from "../../../utils/fetch"
5+
6+
type GetCategoriesResponse = {
7+
data: Array<TorrentCategory>
8+
}
9+
10+
export class CategoryResource implements IRestResource {
11+
client: Rest
12+
13+
constructor(client: Rest) {
14+
this.client = client
15+
}
16+
17+
async getCategories(): Promise<Array<TorrentCategory>> {
18+
return await fetchGet<GetCategoriesResponse>(
19+
`${this.client.apiBaseUrl}/category`
20+
)
21+
.then((res) => {
22+
return Promise.resolve(res.data)
23+
})
24+
.catch((err) => {
25+
return Promise.reject(err)
26+
})
27+
}
28+
}
29+

src/modes/rest/resources/settings.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import {PublicSettings, Settings} from "torrust-index-types-lib"
2+
import {IRestResource} from "../restResource"
3+
import {Rest} from "../rest"
4+
import {fetchGet, fetchPost} from "../../../utils/fetch"
5+
6+
type GetSettingsResponse = {
7+
data: Settings
8+
}
9+
10+
type GetPublicSettingsResponse = {
11+
data: PublicSettings
12+
}
13+
14+
export class SettingsResource implements IRestResource {
15+
client: Rest
16+
17+
constructor(client: Rest) {
18+
this.client = client
19+
}
20+
21+
public async getSettings(): Promise<Settings> {
22+
return await fetchGet<GetSettingsResponse>(
23+
`${this.client.apiBaseUrl}/settings`,
24+
{ "Authorization": `Bearer ${this.client.authToken}` }
25+
)
26+
.then((res) => {
27+
return Promise.resolve(res.data)
28+
})
29+
.catch((err) => {
30+
return Promise.reject(err)
31+
})
32+
}
33+
34+
public async updateSettings(settings: Settings): Promise<Settings> {
35+
return await fetchPost<Settings, GetSettingsResponse>(
36+
`${this.client.apiBaseUrl}/settings`,
37+
settings,
38+
{ "Authorization": `Bearer ${this.client.authToken}` }
39+
)
40+
.then((res) => {
41+
return Promise.resolve(res.data)
42+
})
43+
.catch((err) => {
44+
return Promise.reject(err)
45+
})
46+
}
47+
48+
public async getPublicSettings(): Promise<PublicSettings> {
49+
return await fetchGet<GetPublicSettingsResponse>(
50+
`${this.client.apiBaseUrl}/settings/public`
51+
)
52+
.then((res) => {
53+
return Promise.resolve(res.data)
54+
})
55+
.catch((err) => {
56+
return Promise.reject(err)
57+
})
58+
}
59+
}

0 commit comments

Comments
 (0)