import express from "express";
const API = express.Router();

import config from "../../config.js";

API.use(express.json());
API.use(express.urlencoded({ extended: true }));

API.post("/actions/api", async (req, res, next) => {
  const endpoint = req.body.endpoint;
  const body = req.body.body;

  try {
    const apiReq = await fetch(`${config.uwuzu.host}/api${endpoint}`, {
      method: "POST",
      body: JSON.stringify(body),
    });

    const apiRes = await apiReq.json();

    res.status(200)
    .send(apiRes);
  } catch(err) {
    res.status(500)
    .send(`Error: ${err}`);
  }
});

API.get("/actions/api", (req, res) => {
  res.status(501)
  .send("POST Only");
});

export default API;
