Migrated code to fit adapter

This commit is contained in:
Jonas_Jones 2023-12-13 04:05:32 +01:00
parent 4c42b36460
commit f138451053
4 changed files with 47 additions and 105 deletions

View file

@ -1,19 +1,48 @@
import { handleV1Requests } from "./v1/v1RequestHandler.js";
import { notFoundError } from "./stdErrorResponses.js";
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});
function stripUrl(url) {
const port = url.port;
const domain = url.hostname;
return url.toString().replace(domain, "").replace("https://", "").replace("http://", "").replace(":" + port, "");
}
async function handleRequest(request) {
let pathname = stripUrl(new URL(request.url));
if (pathname.startsWith("/v1/")) {
return handleV1Requests(pathname, request);
} else {
return notFoundError();
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const { pathname } = new URL(request.url);
// List of built-in paths that should not trigger an error
const allowedPaths = ['/health', '/status'];
// if (allowedPaths.includes(pathname)) {
// // Allow requests to built-in paths
// return fetch(request);
// }
// Make a request to someapi.jonasjones.dev/[PATH]
const apiUrl = `https://someapi.jonasjones.dev${pathname}`;
const apiRequest = new Request(apiUrl, {
method: request.method,
headers: request.headers,
body: request.body
});
try {
const apiResponse = await fetch(apiRequest);
if (apiResponse.ok) {
// If the API request is successful, return the response
return apiResponse;
} else if (apiResponse.status === 502) {
// If the API request fails, return an error response
return new Response('API backend is down temporarily', {
status: 420,
statusText: 'API Backend Downtime Error'
});
} else {
// If the API request fails, return the API response
return apiResponse;
}
} catch (error) {
// If an error occurs during the API request, return an error response
return new Response('API backend is down temporarily', {
status: 420,
statusText: 'API Backend Downtime Error'
});
}
}
}