mirror of
https://github.com/karl0ss/homepage.git
synced 2025-05-03 22:13:39 +01:00
39 lines
1.3 KiB
JavaScript
39 lines
1.3 KiB
JavaScript
![]() |
import getServiceWidget from "utils/config/service-helpers";
|
||
|
import { formatApiCall } from "utils/proxy/api-helpers";
|
||
|
import widgets from "widgets/widgets";
|
||
|
|
||
|
export default async function pyloadProxyHandler(req, res) {
|
||
|
const { group, service, endpoint } = req.query;
|
||
|
|
||
|
if (group && service) {
|
||
|
const widget = await getServiceWidget(group, service);
|
||
|
|
||
|
if (widget) {
|
||
|
const url = new URL(formatApiCall(widgets[widget.type].api, { endpoint, ...widget }));
|
||
|
const loginUrl = `${widget.url}/api/login`;
|
||
|
|
||
|
// Pyload api does not support argument passing as JSON.
|
||
|
const sessionId = await fetch(loginUrl, {
|
||
|
method: "POST",
|
||
|
// Empty passwords are supported.
|
||
|
body: `username=${widget.username}&password=${widget.password ?? ''}`,
|
||
|
headers: {
|
||
|
"Content-Type": "application/x-www-form-urlencoded",
|
||
|
},
|
||
|
}).then((response) => response.json());
|
||
|
|
||
|
const apiResponse = await fetch(url, {
|
||
|
method: "POST",
|
||
|
body: `session=${sessionId}`,
|
||
|
headers: {
|
||
|
"Content-Type": "application/x-www-form-urlencoded",
|
||
|
},
|
||
|
}).then((response) => response.json());
|
||
|
|
||
|
return res.send(apiResponse);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return res.status(400).json({ error: "Invalid proxy service type" });
|
||
|
}
|