mirror of
https://github.com/michivonah/themepark-assistant.git
synced 2025-12-22 14:06:29 +01:00
implement general http request function & update existing usage in other files
This commit is contained in:
parent
ebb3191757
commit
6ed53835ff
4 changed files with 46 additions and 19 deletions
|
|
@ -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<AttractionImport[]>(endpoint, headers);
|
||||
const result = await httpRequest<AttractionImport[]>(endpoint, {
|
||||
headers: headers
|
||||
});
|
||||
return result;
|
||||
}
|
||||
catch(e){
|
||||
|
|
|
|||
|
|
@ -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<Park[]>(endpoint, headers);
|
||||
const result = await httpRequest<Park[]>(endpoint, {
|
||||
headers: headers
|
||||
});
|
||||
return result;
|
||||
}
|
||||
catch(e){
|
||||
|
|
|
|||
|
|
@ -1,15 +0,0 @@
|
|||
// typesafe api fetch function
|
||||
export default async function fetchData<T>(endpoint: string, headers: Record<string, string> = {}, method: string = 'GET'): Promise<T>{
|
||||
async function gatherResponse(response: Response): Promise<T>{
|
||||
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;
|
||||
}
|
||||
38
api/src/lib/http-request.ts
Normal file
38
api/src/lib/http-request.ts
Normal file
|
|
@ -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<TResponse, TBody = undefined>(
|
||||
endpoint: string,
|
||||
options: {
|
||||
method?: 'GET' | 'POST' | 'PUT' | 'DELETE',
|
||||
headers?: HeadersInit,
|
||||
body?: TBody
|
||||
}
|
||||
): Promise<TResponse>{
|
||||
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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue