mirror of
https://github.com/DI0IK/homepage-plus.git
synced 2025-07-08 06:38:46 +00:00
refactor widget api design
this passes all widget API calls through the backend, with a pluggable design and reusable API handlers
This commit is contained in:
parent
975f79f6cc
commit
97bf174b78
27 changed files with 370 additions and 252 deletions
39
src/utils/proxies/nzbget.js
Normal file
39
src/utils/proxies/nzbget.js
Normal file
|
@ -0,0 +1,39 @@
|
|||
import { JSONRPCClient } from "json-rpc-2.0";
|
||||
import { getServiceWidget } from "utils/service-helpers";
|
||||
|
||||
export default async function nzbgetProxyHandler(req, res) {
|
||||
const { group, service, endpoint } = req.query;
|
||||
|
||||
if (group && service) {
|
||||
const widget = await getServiceWidget(group, service);
|
||||
|
||||
if (widget) {
|
||||
const constructedUrl = new URL(widget.url);
|
||||
constructedUrl.pathname = "jsonrpc";
|
||||
|
||||
const authorization = Buffer.from(`${widget.username}:${widget.password}`).toString("base64");
|
||||
|
||||
const client = new JSONRPCClient((jsonRPCRequest) =>
|
||||
fetch(constructedUrl.toString(), {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
authorization: `Basic ${authorization}`,
|
||||
},
|
||||
body: JSON.stringify(jsonRPCRequest),
|
||||
}).then(async (response) => {
|
||||
if (response.status === 200) {
|
||||
const jsonRPCResponse = await response.json();
|
||||
return client.receive(jsonRPCResponse);
|
||||
} else if (jsonRPCRequest.id !== undefined) {
|
||||
return Promise.reject(new Error(response.statusText));
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
return res.send(await client.request(endpoint));
|
||||
}
|
||||
}
|
||||
|
||||
return res.status(400).json({ error: "Invalid proxy service type" });
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue