neptune-front/src/services/apiRequest.ts
Mathis a75a87f683
Add initial Neptune Frontend setup
Initial setup of the Neptune frontend project, including Dockerfile, environment files, TypeScript configuration, and essential components. Added basic page structures for dashboard and wallet, and configured Tailwind CSS and postcss.
2024-11-14 11:10:32 +01:00

84 lines
2.2 KiB
TypeScript

"use client";
import axios, { type AxiosResponse } from "axios";
const baseUrl = process.env.NEXT_PUBLIC_API_URL || "http://localhost:3333/";
const AxiosConfigs = {
authenticated: {
json: () => {
return {
headers: {
"content-type": "application/json",
Authorization: `Bearer ${typeof window !== "undefined" ? JSON.parse(window.localStorage.getItem("sub") || "not-ssr") : "not-ssr"}`,
},
validateStatus: (status: number) => {
return status < 500; // Resolve only if the status code is less than 500
},
};
},
},
standard: {
json: () => {
return {
headers: {
"content-type": "application/json",
},
validateStatus: (status: number) => {
return status < 500; // Resolve only if the status code is less than 500
},
};
},
},
};
async function doAuthenticatedJsonPostReq<ReqT, ResT>(
route: string,
body: ReqT,
): Promise<AxiosResponse<ResT>> {
return await axios.post(baseUrl + route, body, AxiosConfigs.authenticated.json());
}
async function doAuthenticatedGetReq<ResT>(route: string): Promise<AxiosResponse<ResT>> {
return await axios.get(baseUrl + route, AxiosConfigs.authenticated.json());
}
async function doAuthenticatedPatchReq<ReqT, ResT>(
route: string,
body: ReqT,
): Promise<AxiosResponse<ResT>> {
return await axios.patch(baseUrl + route, body, AxiosConfigs.authenticated.json());
}
async function doAuthenticatedDelReq<ResT>(route: string): Promise<AxiosResponse<ResT>> {
return await axios.delete(baseUrl + route, AxiosConfigs.authenticated.json());
}
//TODO form/multipart req
async function doJsonPostReq<ReqT, ResT>(
route: string,
body: ReqT,
): Promise<AxiosResponse<ResT>> {
return await axios.post(baseUrl + route, body, AxiosConfigs.standard.json());
}
async function doJsonGetReq<ResT>(route: string): Promise<AxiosResponse<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;