From 6ed53835ffef6734210bb0c9a31e0614c6611006 Mon Sep 17 00:00:00 2001 From: michivonah Date: Sat, 27 Sep 2025 13:11:33 +0200 Subject: [PATCH] implement general http request function & update existing usage in other files --- api/src/jobs/update-attraction-list.ts | 6 ++-- api/src/jobs/update-themepark-data.ts | 6 ++-- api/src/lib/fetch-data.ts | 15 ---------- api/src/lib/http-request.ts | 38 ++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 19 deletions(-) delete mode 100644 api/src/lib/fetch-data.ts create mode 100644 api/src/lib/http-request.ts diff --git a/api/src/jobs/update-attraction-list.ts b/api/src/jobs/update-attraction-list.ts index d1479fa..a97f8c7 100644 --- a/api/src/jobs/update-attraction-list.ts +++ b/api/src/jobs/update-attraction-list.ts @@ -1,7 +1,7 @@ import { getDbEnv } from '../db/client' import { attraction, themepark } from '../db/schema' import { inArray } from 'drizzle-orm' -import fetchData from '../lib/fetch-data' +import httpRequest from '../lib/http-request' import asyncBatchJob from '../lib/async-batch-job' interface AttractionImport { @@ -38,7 +38,9 @@ async function fetchAttractions( 'park':park }; - const result = await fetchData(endpoint, headers); + const result = await httpRequest(endpoint, { + headers: headers + }); return result; } catch(e){ diff --git a/api/src/jobs/update-themepark-data.ts b/api/src/jobs/update-themepark-data.ts index a31b21d..025641a 100644 --- a/api/src/jobs/update-themepark-data.ts +++ b/api/src/jobs/update-themepark-data.ts @@ -1,7 +1,7 @@ import { getDbEnv } from '../db/client' import { themepark } from '../db/schema' import { countryCodesDE } from '../lib/countries' -import fetchData from '../lib/fetch-data' +import httpRequest from '../lib/http-request' import asyncBatchJob from '../lib/async-batch-job' interface Park { @@ -24,7 +24,9 @@ async function fetchThemeparks( const headers = { 'language':lang } - const result = await fetchData(endpoint, headers); + const result = await httpRequest(endpoint, { + headers: headers + }); return result; } catch(e){ diff --git a/api/src/lib/fetch-data.ts b/api/src/lib/fetch-data.ts deleted file mode 100644 index c2a07bf..0000000 --- a/api/src/lib/fetch-data.ts +++ /dev/null @@ -1,15 +0,0 @@ -// typesafe api fetch function -export default async function fetchData(endpoint: string, headers: Record = {}, method: string = 'GET'): Promise{ - async function gatherResponse(response: Response): Promise{ - return await response.json(); - } - - const response: Response = await fetch(endpoint, { method: method, headers: headers}); - - if (!response.ok){ - throw new Error(`HTTP error! Status: ${response.status}`); - } - - const result: T = await gatherResponse(response); - return result; -} \ No newline at end of file diff --git a/api/src/lib/http-request.ts b/api/src/lib/http-request.ts new file mode 100644 index 0000000..47267e2 --- /dev/null +++ b/api/src/lib/http-request.ts @@ -0,0 +1,38 @@ +/** + * Executes a HTTP Request with option for custom header & body parameters + * @param endpoint Request endpoint + * @param options Object with optional keys for method, headers & body + * @returns Returns the response as JSON + */ +export default async function httpRequest( + endpoint: string, + options: { + method?: 'GET' | 'POST' | 'PUT' | 'DELETE', + headers?: HeadersInit, + body?: TBody + } +): Promise{ + const { method = 'GET', headers = {}, body } = options; + + const response = await fetch(endpoint, { + method: method, + headers: { + 'Content-Type':'application/json', + 'Accept':'application/json', + ...headers + }, + body: body !== undefined ? JSON.stringify(body) : undefined + }); + + + if (!response.ok){ + throw new Error(`HTTP error! Status: ${response.status}`); + } + + if(response.status === 204){ + return undefined as TResponse; + } + + const result: TResponse = await response.json(); + return result; +} \ No newline at end of file