Files
siren/ui/src/api/index.ts
Benjamin Sherriff 00a6629e60 Updated fetch cookie
2023-10-19 19:48:24 -04:00

38 lines
1.1 KiB
TypeScript

// import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
const serviceHost = process.env.SERVICE_HOST || 'http://localhost';
const servicePort = process.env.SERVICE_PORT || 5000;
const baseURL = `${serviceHost}:${servicePort}`;
export async function get(endpoint: string, params: Record<string, any> = {}): Promise<Response> {
// Remove undefined params
Object.keys(params).forEach((key) => params[key] === undefined && delete params[key]);
const urlParams = new URLSearchParams(params);
const url = urlParams ? `${baseURL}/${endpoint}?${urlParams}` : `${baseURL}/${endpoint}`;
const response = await fetch(url, {
method: 'GET',
credentials: 'include'
});
return response;
}
export async function post(endpoint: string, body = {}): Promise<Response> {
const url = `${baseURL}/${endpoint}`;
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
credentials: 'include',
body: JSON.stringify(body)
});
return response;
}
export interface Metadata {
limit: number;
page: number;
pages: number;
total: number;
}