Fixed url parameter issue

Fixed an issue where the url parameters weren't getting passed through to the someapi.jonasjones.dev endpoint
This commit is contained in:
Jonas_Jones 2023-12-16 05:37:47 +01:00
parent 3db9932e7a
commit cbf9bac60e

View file

@ -1,9 +1,9 @@
addEventListener('fetch', event => { addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request)) event.respondWith(handleRequest(event.request));
}) });
async function handleRequest(request) { async function handleRequest(request) {
const { pathname } = new URL(request.url); const { pathname, search } = new URL(request.url);
// List of built-in paths that should not trigger an error // List of built-in paths that should not trigger an error
const allowedPaths = ['/health', '/status']; const allowedPaths = ['/health', '/status'];
@ -13,8 +13,8 @@ addEventListener('fetch', event => {
// return fetch(request); // return fetch(request);
// } // }
// Make a request to someapi.jonasjones.dev/[PATH] // Make a request to someapi.jonasjones.dev/[PATH] including URL parameters
const apiUrl = `https://someapi.jonasjones.dev${pathname}`; const apiUrl = `https://someapi.jonasjones.dev${pathname}${search}`;
const apiRequest = new Request(apiUrl, { const apiRequest = new Request(apiUrl, {
method: request.method, method: request.method,
headers: request.headers, headers: request.headers,
@ -33,7 +33,7 @@ addEventListener('fetch', event => {
status: 420, status: 420,
statusText: 'API Backend Downtime Error' statusText: 'API Backend Downtime Error'
}); });
} else if (apiResponse.status === 530) { } else if (apiResponse.status === 530) {
// If the API request fails, return an error response // If the API request fails, return an error response
return new Response('API backend went down just now :( It should be back up in a matter of seconds!', { return new Response('API backend went down just now :( It should be back up in a matter of seconds!', {
status: 421, status: 421,