Skip to content

Commit d0bb7c3

Browse files
Merge pull request #83 from ioBroker/feature/66-weblate-pagination
2 parents 9e06d96 + d222ad0 commit d0bb7c3

File tree

3 files changed

+37
-15
lines changed

3 files changed

+37
-15
lines changed

express/backend/src/api/weblate-proxy.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,43 @@ import axios from "axios";
22
import { Router } from "express";
33
import { env } from "../common";
44

5+
const WEBLATE_API = "https://weblate.iobroker.net/api/";
6+
const ALLOWED_PATHS = ["projects/adapters/components/"] as const;
7+
58
const router = Router();
69

710
router.get<any>("/api/weblate/*", async function (req, res) {
811
try {
9-
const result = await axios.get<any>(
10-
`https://weblate.iobroker.net/api/${req.params["0"]}`,
11-
{
12-
headers: {
13-
accept: "application/json",
14-
authorization: `Token ${env.WEBLATE_ACCESS_TOKEN}`,
15-
},
12+
const userPath = req.params["0"];
13+
if (!ALLOWED_PATHS.some((path) => userPath.startsWith(path))) {
14+
return res.status(400).send("Invalid path");
15+
}
16+
const url = new URL(`${WEBLATE_API}${userPath}`);
17+
const q = req.query;
18+
if (q.page) {
19+
url.searchParams.set("page", Number(q.page).toString());
20+
}
21+
22+
const result = await axios.get<any>(url.toString(), {
23+
headers: {
24+
accept: "application/json",
25+
authorization: `Token ${env.WEBLATE_ACCESS_TOKEN}`,
1626
},
17-
);
27+
});
28+
result.data.previous = convertLink(result.data.previous);
29+
result.data.next = convertLink(result.data.next);
1830
res.send(result.data);
1931
} catch (error: any) {
2032
console.error(error);
2133
res.status(500).send(error.message || error);
2234
}
2335
});
2436

37+
function convertLink(link: string | null): string | null {
38+
if (!link) {
39+
return null;
40+
}
41+
return link.replace(WEBLATE_API, "/api/weblate/");
42+
}
43+
2544
export default router;

express/frontend/src/components/dashboard/Dashboard.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,7 @@ async function getDiscoveryLink(adapterName: string) {
262262
async function getWeblateLink(adapterName: string) {
263263
try {
264264
const components = await getWeblateAdapterComponents();
265-
const component = components.results.find(
266-
(c: any) => c.name === adapterName,
267-
);
265+
const component = components.find((c: any) => c.name === adapterName);
268266
if (component) {
269267
return (
270268
`https://weblate.iobroker.net/projects/adapters/` +

express/frontend/src/lib/ioBroker.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,15 @@ export async function getAdapterInfos(
169169
}
170170

171171
export const getWeblateAdapterComponents = AsyncCache.of(async () => {
172-
const result = await axios.get<any>(
173-
getApiUrl("weblate/projects/adapters/components/"),
174-
);
175-
return result.data;
172+
const components: any[] = [];
173+
let url = "weblate/projects/adapters/components/";
174+
do {
175+
const result = await axios.get<any>(getApiUrl(url));
176+
components.push(...result.data.results);
177+
url = result.data.next?.replace("/api/", "");
178+
} while (url);
179+
180+
return components;
176181
});
177182

178183
export async function getStatistics(adapterName: string) {

0 commit comments

Comments
 (0)