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'
});
}
}
}

View file

@ -1,29 +0,0 @@
import { internalServerError } from '../stdErrorResponses.js';
import { version } from '../../package.json';
export const builtinRequests = ["ping", "version", "help"];
export function isBuiltinRequest(request) {
return builtinRequests.includes(request);
}
export function returnVersion() {
return new Response(JSON.stringify([version]), {
headers: {
"Content-Type": "application/json"
}
});
}
export function handleBuiltinRequest(request) {
switch (request) {
case "ping":
return new Response("pong");
case "version":
return returnVersion();
case "help":
return new Response("Please refer to the wiki at https://wiki.jonasjones.dev/Api/");
default:
return internalServerError();
}
}

View file

@ -1,40 +0,0 @@
const lastfmRequests = ['gettrackinfo', 'getrecent']
export function isLastfmRequest(pathname) {
return pathname.startsWith("lastfm/")
}
export function handleLastfmRequest(pathname) {
requestUrl = new URL(pathname, "https://api.jonasjones.dev/v1/" + pathname)
requestType = pathname.replace("lastfm/", "").split("?")[0];
switch (requestType) {
case "gettrackinfo":
return getTrackInfoRequestHandler(requestUrl);
case "getrecent":
return getRecentRequestHandler(requestUrl);
default:
return notFoundError();
}
}
function getRecentRequestHandler(requestUrl) {
const urlParams = new URLSearchParams(requestUrl.search);
const offset = urlParams.get("offset");
const limit = urlParams.get("limit");
return new Response(JSON.stringify([offset, limit]), {
headers: {
"Content-Type": "application/json"
}
});
}
function getTrackInfoRequestHandler(requestUrl) {
const urlParams = new URLSearchParams(requestUrl.search);
const track = urlParams.get("track");
const artist = urlParams.get("artist");
return new Response(JSON.stringify([track, artist]), {
headers: {
"Content-Type": "application/json"
}
});
}

View file

@ -1,18 +0,0 @@
import { isBuiltinRequest, handleBuiltinRequest } from './builtInRequests.js';
import { methodNotAllowedError, notFoundError } from '../stdErrorResponses.js';
import { handleLastfmRequest, isLastfmRequest } from './lastfm/Requests.js';
export async function handleV1Requests(pathname, request) {
if (request.method !== "GET") {
return methodNotAllowedError();
} else {
const requestPath = pathname.replace("/v1/", "");
if (isBuiltinRequest(requestPath)) {
return handleBuiltinRequest(requestPath);
} else if (isLastfmRequest(requestPath)) {
return handleLastfmRequest(requestPath);
} else {
return notFoundError();
}
}
}