Tweaks, working on api error handling
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user