-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into 3670-fiche-sanitaire
- Loading branch information
Showing
79 changed files
with
469 additions
and
7,179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,5 +17,6 @@ node_modules | |
out/ | ||
coverage/ | ||
|
||
dump | ||
*.bson.gz | ||
*.json.gz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import axios, { AxiosError, AxiosInstance, AxiosResponse, InternalAxiosRequestConfig } from "axios"; | ||
import { toastr } from "react-redux-toastr"; | ||
|
||
import { HttpError } from "snu-lib"; | ||
|
||
import { apiv2URL } from "@/config"; | ||
import { capture } from "@/sentry"; | ||
export interface IApiV2 { | ||
get<T>(path: string): Promise<T>; | ||
post<T>(path: string, payload: unknown): Promise<T>; | ||
remove<T>(path: string): Promise<T>; | ||
put<T>(path: string, payload: unknown): Promise<T>; | ||
patch<T>(path: string, payload: unknown): Promise<T>; | ||
} | ||
class Apiv2 implements IApiV2 { | ||
private token: string; | ||
private axios: AxiosInstance; | ||
|
||
constructor() { | ||
this.axios = axios.create({ | ||
baseURL: apiv2URL, | ||
}); | ||
this.initInterceptor(); | ||
} | ||
|
||
setToken(token: string) { | ||
this.token = token; | ||
} | ||
|
||
async get<T>(path: string): Promise<T> { | ||
return this.axios.get<T, T>(path); | ||
} | ||
|
||
async post<T>(path: string, payload: unknown): Promise<T> { | ||
return this.axios.post<T, T>(path, payload); | ||
} | ||
|
||
async remove<T>(path: string): Promise<T> { | ||
return this.axios.delete<T, T>(path); | ||
} | ||
|
||
async put<T>(path: string, payload: unknown): Promise<T> { | ||
return this.axios.put<T, T>(path, payload); | ||
} | ||
|
||
async patch<T>(path: string, payload: unknown): Promise<T> { | ||
return this.axios.patch<T, T>(path, payload); | ||
} | ||
|
||
initInterceptor() { | ||
this.axios.interceptors.request.use((request: InternalAxiosRequestConfig) => { | ||
request.headers.set({ "x-user-timezone": new Date().getTimezoneOffset(), Authorization: `JWT ${this.token}` }); | ||
request.headers.setContentType("application/json"); | ||
return request; | ||
}); | ||
|
||
this.axios.interceptors.response.use( | ||
(response: AxiosResponse) => response.data, | ||
(error: AxiosError<HttpError>) => { | ||
if (error.response?.status === 401) { | ||
if (window?.location?.pathname !== "/auth") { | ||
window.location.href = "/auth?disconnected=1"; | ||
return Promise.reject(); | ||
} | ||
} else if (error.response?.status === 422) { | ||
capture(error); | ||
return Promise.reject(error.response.data); | ||
} | ||
capture(error); | ||
toastr.error("Oups, une erreur est survenue", `Code d'erreur: ${error.response?.data.correlationId}`); | ||
return Promise.reject(); | ||
}, | ||
); | ||
} | ||
} | ||
|
||
export const apiv2 = new Apiv2(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.