brief-06-front/src/services/request.service.ts
Mathis c0327f9607
Refactored register services and enhanced user profile functionality
A new RequestService was introduced to standardize the HTTP requests, and the register service was refactored to use it. Improvements were made to the handling of user profile data along with error handling and display. New components were created for displaying the user profile, and changes to existing components were made to accommodate the service and data handling updates.
2024-05-22 17:03:14 +02:00

71 lines
1.9 KiB
TypeScript

'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<AxiosResponse<any, any>> {
return await axios.post(url, body, AxiosConfigs.authenticated.json())
}
async function doAuthenticatedGetReq(url: string): Promise<AxiosResponse<any, any>> {
return await axios.get(url, AxiosConfigs.authenticated.json())
}
async function doAuthenticatedPatchReq(url: string, body: object): Promise<AxiosResponse<any, any>> {
return await axios.patch(url, body, AxiosConfigs.authenticated.json())
}
async function doAuthenticatedDelReq(url: string): Promise<AxiosResponse<any, any>> {
return await axios.delete(url, AxiosConfigs.authenticated.json())
}
//TODO form/multipart req
async function doJsonPostReq(url:string, body: object): Promise<AxiosResponse<any, any>> {
return await axios.post(url, body, AxiosConfigs.standard.json())
}
async function doJsonGetReq(url: string): Promise<AxiosResponse<any, any>> {
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;