mirror of
https://github.com/JonasunderscoreJones/api-worker.git
synced 2025-10-22 18:09:19 +02:00
Migrated code to fit adapter
This commit is contained in:
parent
4c42b36460
commit
f138451053
4 changed files with 47 additions and 105 deletions
65
src/index.js
65
src/index.js
|
@ -1,19 +1,48 @@
|
||||||
import { handleV1Requests } from "./v1/v1RequestHandler.js";
|
addEventListener('fetch', event => {
|
||||||
import { notFoundError } from "./stdErrorResponses.js";
|
event.respondWith(handleRequest(event.request))
|
||||||
|
})
|
||||||
addEventListener("fetch", (event) => {
|
|
||||||
event.respondWith(handleRequest(event.request));
|
async function handleRequest(request) {
|
||||||
});
|
const { pathname } = new URL(request.url);
|
||||||
function stripUrl(url) {
|
|
||||||
const port = url.port;
|
// List of built-in paths that should not trigger an error
|
||||||
const domain = url.hostname;
|
const allowedPaths = ['/health', '/status'];
|
||||||
return url.toString().replace(domain, "").replace("https://", "").replace("http://", "").replace(":" + port, "");
|
|
||||||
}
|
// if (allowedPaths.includes(pathname)) {
|
||||||
async function handleRequest(request) {
|
// // Allow requests to built-in paths
|
||||||
let pathname = stripUrl(new URL(request.url));
|
// return fetch(request);
|
||||||
if (pathname.startsWith("/v1/")) {
|
// }
|
||||||
return handleV1Requests(pathname, request);
|
|
||||||
} else {
|
// Make a request to someapi.jonasjones.dev/[PATH]
|
||||||
return notFoundError();
|
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'
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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"
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
|
@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Add table
Add a link
Reference in a new issue