2022-09-07 16:53:24 +03:00
|
|
|
import getServiceWidget from "utils/service-helpers";
|
2022-09-04 21:58:42 +03:00
|
|
|
import { formatApiCall } from "utils/api-helpers";
|
2022-09-25 19:43:47 +03:00
|
|
|
import widgets from "widgets/widgets";
|
2022-09-04 21:58:42 +03:00
|
|
|
|
|
|
|
export default async function npmProxyHandler(req, res) {
|
|
|
|
const { group, service, endpoint } = req.query;
|
|
|
|
|
|
|
|
if (group && service) {
|
|
|
|
const widget = await getServiceWidget(group, service);
|
|
|
|
|
2022-09-25 19:43:47 +03:00
|
|
|
if (!widgets?.[widget.type]?.api) {
|
|
|
|
return res.status(403).json({ error: "Service does not support API calls" });
|
|
|
|
}
|
|
|
|
|
2022-09-04 21:58:42 +03:00
|
|
|
if (widget) {
|
2022-09-25 19:43:47 +03:00
|
|
|
const url = new URL(formatApiCall(widgets[widget.type].api, { endpoint, ...widget }));
|
2022-09-04 21:58:42 +03:00
|
|
|
|
|
|
|
const loginUrl = `${widget.url}/api/tokens`;
|
|
|
|
const body = { identity: widget.username, secret: widget.password };
|
|
|
|
|
|
|
|
const authResponse = await fetch(loginUrl, {
|
|
|
|
method: "POST",
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
}).then((response) => response.json());
|
|
|
|
|
|
|
|
const apiResponse = await fetch(url, {
|
|
|
|
method: "GET",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
2022-09-07 16:53:24 +03:00
|
|
|
Authorization: `Bearer ${authResponse.token}`,
|
2022-09-04 21:58:42 +03:00
|
|
|
},
|
|
|
|
}).then((response) => response.json());
|
|
|
|
|
|
|
|
return res.send(apiResponse);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.status(400).json({ error: "Invalid proxy service type" });
|
|
|
|
}
|