From 278cf844c2e7b949465ccd548d096a8314db1057 Mon Sep 17 00:00:00 2001 From: Mathis Date: Tue, 11 Jun 2024 16:08:57 +0200 Subject: [PATCH] feat(services): update apiRequest module Updated the apiRequest module with refinements in authorization and removal of redundant comments. The changes allow handling server-side rendering and type enforcement in request body. --- src/services/apiRequest.ts | 56 +++++--------------------------------- 1 file changed, 7 insertions(+), 49 deletions(-) diff --git a/src/services/apiRequest.ts b/src/services/apiRequest.ts index a4127c9..2e9b6f8 100644 --- a/src/services/apiRequest.ts +++ b/src/services/apiRequest.ts @@ -10,7 +10,7 @@ const AxiosConfigs = { return { headers: { 'content-type': 'application/json', - Authorization: `Bearer ${localStorage.getItem('sub')}`, + Authorization: `Bearer ${typeof window !== 'undefined' ? window.localStorage.getItem('sub') : "not-ssr"}`, }, validateStatus: function (status: number) { return status < 500; // Resolve only if the status code is less than 500 @@ -32,72 +32,30 @@ const AxiosConfigs = { } } -/** - * 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>} - The promise that resolves to the response from the server. - */ -async function doAuthenticatedJsonPostReq(route:string, body:object): Promise> { +async function doAuthenticatedJsonPostReq(route:string, body:ReqT): Promise> { 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} - A promise that resolves to the Axios response object containing the request data and response details. - */ -async function doAuthenticatedGetReq(route:string): Promise> { +async function doAuthenticatedGetReq(route:string): Promise> { 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>} - A Promise that resolves to the AxiosResponse object containing the response data. - */ -async function doAuthenticatedPatchReq(route:string, body: object): Promise> { +async function doAuthenticatedPatchReq(route:string, body: ReqT): Promise> { 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>} A Promise that resolves to the AxiosResponse object containing the response data. - */ -async function doAuthenticatedDelReq(route:string): Promise> { +async function doAuthenticatedDelReq(route:string): Promise> { 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>} - A promise that resolves with the response from the server. - * - * @throws {Error} - If an error occurs during the request. - */ -async function doJsonPostReq(route:string, body: object): Promise> { +async function doJsonPostReq(route:string, body: ReqT): Promise> { 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>} - A promise that resolves to the AxiosResponse object. - */ -async function doJsonGetReq(route:string): Promise> { +async function doJsonGetReq(route:string): Promise> { return await axios.get(baseUrl + route, AxiosConfigs.standard.json()); }