'use client' import axios, {type AxiosResponse} from "axios"; const AxiosConfigs = { authenticated: { json: () => { return { headers: { 'content-type': 'application/json', Authorization: `Bearer ${localStorage.getItem('token')}`, }, validateStatus: function (status: number) { return status < 500; // Resolve only if the status code is less than 500 } } } }, standard: { json: () => { return { headers: { 'content-type': 'application/json' }, validateStatus: function (status: number) { return status < 500; // Resolve only if the status code is less than 500 } } } } } async function doAuthenticatedJsonPostReq(url:string, body:object): Promise> { return await axios.post(url, body, AxiosConfigs.authenticated.json()) } async function doAuthenticatedGetReq(url: string): Promise> { return await axios.get(url, AxiosConfigs.authenticated.json()) } async function doAuthenticatedPatchReq(url: string, body: object): Promise> { return await axios.patch(url, body, AxiosConfigs.authenticated.json()) } async function doAuthenticatedDelReq(url: string): Promise> { return await axios.delete(url, AxiosConfigs.authenticated.json()) } //TODO form/multipart req async function doJsonPostReq(url:string, body: object): Promise> { return await axios.post(url, body, AxiosConfigs.standard.json()) } async function doJsonGetReq(url: string): Promise> { return await axios.get(url, AxiosConfigs.standard.json()); } const RequestService = { authenticated: { post: {json: doAuthenticatedJsonPostReq}, patch: {json: doAuthenticatedPatchReq}, delete: {json: doAuthenticatedDelReq}, get: {json: doAuthenticatedGetReq} }, standard: { post: {json: doJsonPostReq}, get: {json: doJsonGetReq} } } export default RequestService;