feat(service): add apiRequest service
This commit introduces the new 'apiRequest' service file. This service file contains functionalities for standard and authenticated network requests including POST, GET, PATCH, and DELETE actions using axios. The file has been structured to easily perform network requests with necessary headers and status validations.
This commit is contained in:
parent
74ec86c684
commit
bde56f4bb2
117
src/services/apiRequest.ts
Normal file
117
src/services/apiRequest.ts
Normal file
@ -0,0 +1,117 @@
|
||||
'use client'
|
||||
|
||||
import axios, {type AxiosResponse} from "axios";
|
||||
|
||||
const baseUrl = ""
|
||||
|
||||
const AxiosConfigs = {
|
||||
authenticated: {
|
||||
json: () => {
|
||||
return {
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
Authorization: `Bearer ${localStorage.getItem('sub')}`,
|
||||
},
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes an authenticated JSON POST request using axios.
|
||||
* @param {string} route - The route to send the request to.
|
||||
* @param {object} body - The request body.
|
||||
* @returns {Promise<AxiosResponse<ReqT, ResT>>} - The promise that resolves to the response from the server.
|
||||
*/
|
||||
async function doAuthenticatedJsonPostReq<ReqT, ResT>(route:string, body:object): Promise<AxiosResponse<ReqT, ResT>> {
|
||||
return await axios.post(baseUrl + route, body, AxiosConfigs.authenticated.json())
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes an authenticated GET request to the specified route using Axios.
|
||||
*
|
||||
* @param {string} route - The route to which the GET request is sent.
|
||||
* @returns {Promise<AxiosResponse>} - A promise that resolves to the Axios response object containing the request data and response details.
|
||||
*/
|
||||
async function doAuthenticatedGetReq<ReqT, ResT>(route:string): Promise<AxiosResponse<ReqT, ResT>> {
|
||||
return await axios.get(baseUrl + route, AxiosConfigs.authenticated.json())
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs an authenticated PATCH request to the specified route with the given body.
|
||||
*
|
||||
* @param {string} route - The route to send the PATCH request to.
|
||||
* @param {object} body - The body of the request.
|
||||
* @returns {Promise<AxiosResponse<ReqT, ResT>>} - A Promise that resolves to the AxiosResponse object containing the response data.
|
||||
*/
|
||||
async function doAuthenticatedPatchReq<ReqT, ResT>(route:string, body: object): Promise<AxiosResponse<ReqT, ResT>> {
|
||||
return await axios.patch(baseUrl + route, body, AxiosConfigs.authenticated.json())
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends an authenticated DELETE request to the specified route.
|
||||
*
|
||||
* @param {string} route - The route to send the request to.
|
||||
*
|
||||
* @return {Promise<AxiosResponse<ReqT, ResT>>} A Promise that resolves to the AxiosResponse object containing the response data.
|
||||
*/
|
||||
async function doAuthenticatedDelReq<ReqT, ResT>(route:string): Promise<AxiosResponse<ReqT, ResT>> {
|
||||
return await axios.delete(baseUrl + route, AxiosConfigs.authenticated.json())
|
||||
}
|
||||
|
||||
//TODO form/multipart req
|
||||
|
||||
|
||||
/**
|
||||
* Perform a JSON POST request.
|
||||
*
|
||||
* @param {string} route - The route to send the request to.
|
||||
* @param {object} body - The JSON object to send in the request body.
|
||||
*
|
||||
* @return {Promise<AxiosResponse<ReqT, ResT>>} - A promise that resolves with the response from the server.
|
||||
*
|
||||
* @throws {Error} - If an error occurs during the request.
|
||||
*/
|
||||
async function doJsonPostReq<ReqT, ResT>(route:string, body: object): Promise<AxiosResponse<ReqT, ResT>> {
|
||||
return await axios.post(baseUrl + route, body, AxiosConfigs.standard.json())
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform a JSON GET request using Axios.
|
||||
*
|
||||
* @param {string} route - The route URL to make the GET request to.
|
||||
* @returns {Promise<AxiosResponse<ReqT, ResT>>} - A promise that resolves to the AxiosResponse object.
|
||||
*/
|
||||
async function doJsonGetReq<ReqT, ResT>(route:string): Promise<AxiosResponse<ReqT, ResT>> {
|
||||
return await axios.get(baseUrl + route, AxiosConfigs.standard.json());
|
||||
}
|
||||
|
||||
const ApiRequest = {
|
||||
authenticated: {
|
||||
post: {json: doAuthenticatedJsonPostReq},
|
||||
patch: {json: doAuthenticatedPatchReq},
|
||||
delete: {json: doAuthenticatedDelReq},
|
||||
get: {json: doAuthenticatedGetReq}
|
||||
},
|
||||
standard: {
|
||||
post: {json: doJsonPostReq},
|
||||
get: {json: doJsonGetReq}
|
||||
}
|
||||
}
|
||||
|
||||
export default ApiRequest;
|
Loading…
x
Reference in New Issue
Block a user