Tweaks, working on api error handling

This commit is contained in:
Benjamin Sherriff
2023-10-23 20:19:17 -04:00
parent 3eb888b57d
commit 8c246c96e7
7 changed files with 104 additions and 47 deletions

View File

@@ -1,5 +1,3 @@
// 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}`;
@@ -18,17 +16,23 @@ export async function get(endpoint: string, params: Record<string, any> = {}): P
interface PostOptions {
headers?: Record<string, any>;
type?: 'json' | 'form';
}
export async function post(endpoint: string, body = {}, options?: PostOptions): Promise<Response> {
export async function post(endpoint: string, body: any, options?: PostOptions): Promise<Response> {
const url = `${baseURL}/${endpoint}`;
const headers = options?.headers || {};
if (!options?.type || options.type === 'json') {
body = JSON.stringify(body);
headers['Content-Type'] = 'application/json';
} else if (options.type === 'form') {
headers['Content-Type'] = 'multipart/form-data';
}
const response = await fetch(url, {
method: 'POST',
headers: options?.headers || {
'Content-Type': 'application/json'
},
headers: headers,
credentials: 'include',
body: JSON.stringify(body)
body
});
return response;
}