Compare commits
3 Commits
882729ffc9
...
278cf844c2
Author | SHA1 | Date | |
---|---|---|---|
278cf844c2 | |||
e9048ca7eb | |||
950cb9137f |
36
src/interfaces/api.interface.ts
Normal file
36
src/interfaces/api.interface.ts
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
import {IUserData} from "@/interfaces/userdata.interface";
|
||||||
|
|
||||||
|
// ----- Request -----
|
||||||
|
|
||||||
|
export interface IApiRegisterReq {
|
||||||
|
firstName: string;
|
||||||
|
lastName: string;
|
||||||
|
pseudo: string;
|
||||||
|
city: string;
|
||||||
|
email: string;
|
||||||
|
password: string;
|
||||||
|
age: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IApiLoginReq {
|
||||||
|
email: string;
|
||||||
|
password: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ----- Response -----
|
||||||
|
|
||||||
|
export interface IAbstractApiResponse {
|
||||||
|
message?: Array<string>;
|
||||||
|
error?: string;
|
||||||
|
statusCode?: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IApiRegisterRes extends IAbstractApiResponse {
|
||||||
|
access_token?: string;
|
||||||
|
user?: IUserData
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IApiLoginRes extends IAbstractApiResponse {
|
||||||
|
access_token?: string
|
||||||
|
}
|
@ -1,3 +1,14 @@
|
|||||||
export interface IUserData {
|
export interface IUserData {
|
||||||
name: string
|
id: string;
|
||||||
|
firstName: string;
|
||||||
|
lastName: string;
|
||||||
|
pseudo: string;
|
||||||
|
email: string;
|
||||||
|
roleId: string;
|
||||||
|
isActive: boolean;
|
||||||
|
city: string;
|
||||||
|
dollarAvailables: number;
|
||||||
|
age: number;
|
||||||
|
created_at: string;
|
||||||
|
updated_at: string;
|
||||||
}
|
}
|
@ -10,7 +10,7 @@ const AxiosConfigs = {
|
|||||||
return {
|
return {
|
||||||
headers: {
|
headers: {
|
||||||
'content-type': 'application/json',
|
'content-type': 'application/json',
|
||||||
Authorization: `Bearer ${localStorage.getItem('sub')}`,
|
Authorization: `Bearer ${typeof window !== 'undefined' ? window.localStorage.getItem('sub') : "not-ssr"}`,
|
||||||
},
|
},
|
||||||
validateStatus: function (status: number) {
|
validateStatus: function (status: number) {
|
||||||
return status < 500; // Resolve only if the status code is less than 500
|
return status < 500; // Resolve only if the status code is less than 500
|
||||||
@ -32,72 +32,30 @@ const AxiosConfigs = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
async function doAuthenticatedJsonPostReq<ReqT, ResT>(route:string, body:ReqT): Promise<AxiosResponse<ResT>> {
|
||||||
* 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())
|
return await axios.post(baseUrl + route, body, AxiosConfigs.authenticated.json())
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
async function doAuthenticatedGetReq<ResT>(route:string): Promise<AxiosResponse<ResT>> {
|
||||||
* 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())
|
return await axios.get(baseUrl + route, AxiosConfigs.authenticated.json())
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
async function doAuthenticatedPatchReq<ReqT, ResT>(route:string, body: ReqT): Promise<AxiosResponse<ResT>> {
|
||||||
* 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())
|
return await axios.patch(baseUrl + route, body, AxiosConfigs.authenticated.json())
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
async function doAuthenticatedDelReq<ResT>(route:string): Promise<AxiosResponse<ResT>> {
|
||||||
* 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())
|
return await axios.delete(baseUrl + route, AxiosConfigs.authenticated.json())
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO form/multipart req
|
//TODO form/multipart req
|
||||||
|
|
||||||
|
|
||||||
/**
|
async function doJsonPostReq<ReqT, ResT>(route:string, body: ReqT): Promise<AxiosResponse<ResT>> {
|
||||||
* 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())
|
return await axios.post(baseUrl + route, body, AxiosConfigs.standard.json())
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
async function doJsonGetReq<ResT>(route:string): Promise<AxiosResponse<ResT>> {
|
||||||
* 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());
|
return await axios.get(baseUrl + route, AxiosConfigs.standard.json());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user