Skip to content

Commit

Permalink
Merge pull request #83 from ioBroker/feature/66-weblate-pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
UncleSamSwiss authored Jan 17, 2025
2 parents 9e06d96 + d222ad0 commit d0bb7c3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
35 changes: 27 additions & 8 deletions express/backend/src/api/weblate-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,43 @@ import axios from "axios";
import { Router } from "express";
import { env } from "../common";

const WEBLATE_API = "https://weblate.iobroker.net/api/";
const ALLOWED_PATHS = ["projects/adapters/components/"] as const;

const router = Router();

router.get<any>("/api/weblate/*", async function (req, res) {
try {
const result = await axios.get<any>(
`https://weblate.iobroker.net/api/${req.params["0"]}`,
{
headers: {
accept: "application/json",
authorization: `Token ${env.WEBLATE_ACCESS_TOKEN}`,
},
const userPath = req.params["0"];
if (!ALLOWED_PATHS.some((path) => userPath.startsWith(path))) {
return res.status(400).send("Invalid path");
}
const url = new URL(`${WEBLATE_API}${userPath}`);
const q = req.query;
if (q.page) {
url.searchParams.set("page", Number(q.page).toString());
}

const result = await axios.get<any>(url.toString(), {
headers: {
accept: "application/json",
authorization: `Token ${env.WEBLATE_ACCESS_TOKEN}`,
},
);
});
result.data.previous = convertLink(result.data.previous);
result.data.next = convertLink(result.data.next);
res.send(result.data);
} catch (error: any) {
console.error(error);
res.status(500).send(error.message || error);
}
});

function convertLink(link: string | null): string | null {
if (!link) {
return null;
}
return link.replace(WEBLATE_API, "/api/weblate/");
}

export default router;
4 changes: 1 addition & 3 deletions express/frontend/src/components/dashboard/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,7 @@ async function getDiscoveryLink(adapterName: string) {
async function getWeblateLink(adapterName: string) {
try {
const components = await getWeblateAdapterComponents();
const component = components.results.find(
(c: any) => c.name === adapterName,
);
const component = components.find((c: any) => c.name === adapterName);
if (component) {
return (
`https://weblate.iobroker.net/projects/adapters/` +
Expand Down
13 changes: 9 additions & 4 deletions express/frontend/src/lib/ioBroker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,15 @@ export async function getAdapterInfos(
}

export const getWeblateAdapterComponents = AsyncCache.of(async () => {
const result = await axios.get<any>(
getApiUrl("weblate/projects/adapters/components/"),
);
return result.data;
const components: any[] = [];
let url = "weblate/projects/adapters/components/";
do {
const result = await axios.get<any>(getApiUrl(url));
components.push(...result.data.results);
url = result.data.next?.replace("/api/", "");
} while (url);

return components;
});

export async function getStatistics(adapterName: string) {
Expand Down

0 comments on commit d0bb7c3

Please sign in to comment.