Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make API endpoint a configuration #1

Open
mohsen1 opened this issue Jan 24, 2025 · 1 comment
Open

Make API endpoint a configuration #1

mohsen1 opened this issue Jan 24, 2025 · 1 comment

Comments

@mohsen1
Copy link
Collaborator

mohsen1 commented Jan 24, 2025

--api should allow replacing existing API endpoint with user provided API. This will enable using things like OpenRouter

@Sibghataziz
Copy link

I think this will do the work

const express = require("express");
const bodyParser = require("body-parser");
const axios = require("axios");

const app = express();
app.use(bodyParser.json()); // Middleware to parse JSON requests

// Default API endpoint
const defaultApiEndpoint = "https://default-api.com";
let currentApiEndpoint = defaultApiEndpoint; // Initialize with the default endpoint

// Function to validate the URL
function validateUrl(url) {
  try {
    new URL(url); // Throws an error if the URL is invalid
    return true;
  } catch (error) {
    throw new Error("Invalid URL provided.");
  }
}

// Function to get the current API endpoint
function getEndpoint() {
  return currentApiEndpoint;
}

// Function to set the current API endpoint
function setEndpoint(req) {
  if (!validateUrl(req.body.url)) {
    throw new Error("Invalid URL provided.");
  }
  currentApiEndpoint = req.body.url; // Update the current API endpoint
}

// Endpoint to set the user-provided API
app.post("/set-api-endpoint", (req, res) => {
  try {
    setEndpoint(req);
    res.status(200).json({ message: `API endpoint updated to: ${getEndpoint()}` });
  } catch (error) {
    res.status(400).json({ error: error.message });
  }
});

// Endpoint to fetch data using the current API endpoint, with headers in the body
app.post("/fetch-data", async (req, res) => {
  const { headers, body, path } = req.body;

  // Validate that required fields are provided
  if (!headers || !body || !path) {
    return res.status(400).json({ error: "Missing required fields: headers, body, and path" });
  }

  const url = `${getEndpoint()}/${path}`; // Get the current API endpoint dynamically

  try {
    const response = await axios.post(url, body, { headers });
    res.status(200).json(response.data);
  } catch (error) {
    const status = error.response?.status || 500;
    const message = error.response?.data || error.message;
    res.status(status).json({ error: `Failed to fetch data: ${message}` });
  }
});

// Default route to display the current API endpoint
app.get("/", (req, res) => {
  res.send(`Current API endpoint: ${getEndpoint()}`);
});

// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Server is running on http://localhost:${PORT}`);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants