|
@@ -1,8 +1,10 @@
|
|
|
-import {NuxtAxiosInstance} from '@nuxtjs/axios'
|
|
|
|
|
-import {AxiosRequestConfig, AxiosResponse} from 'axios'
|
|
|
|
|
-import {AnyJson, DataPersisterArgs, DataProviderArgs, FileArgs, UrlArgs} from '~/types/interfaces'
|
|
|
|
|
|
|
+import {AnyJson, DataPersisterArgs, DataProviderArgs, UrlArgs} from '~/types/interfaces'
|
|
|
import {HTTP_METHOD, QUERY_TYPE} from '~/types/enums'
|
|
import {HTTP_METHOD, QUERY_TYPE} from '~/types/enums'
|
|
|
import TypesTesting from "~/services/utils/typesTesting";
|
|
import TypesTesting from "~/services/utils/typesTesting";
|
|
|
|
|
+import {$fetch, FetchOptions} from "ohmyfetch";
|
|
|
|
|
+import {useProfileAccessStore} from "~/store/profile/access";
|
|
|
|
|
+import Page from "~//services/store/page";
|
|
|
|
|
+import {TYPE_ALERT} from "~//types/enums";
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* @category Services/connection
|
|
* @category Services/connection
|
|
@@ -11,15 +13,6 @@ import TypesTesting from "~/services/utils/typesTesting";
|
|
|
* Classe Wrapper du connecteur de requête (Axios dans notre cas)
|
|
* Classe Wrapper du connecteur de requête (Axios dans notre cas)
|
|
|
*/
|
|
*/
|
|
|
class Connection {
|
|
class Connection {
|
|
|
- static connector: NuxtAxiosInstance;
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * Initialisation du connecteur (Axios dans notre cas)
|
|
|
|
|
- * @param {NuxtAxiosInstance} connector
|
|
|
|
|
- */
|
|
|
|
|
- static initConnector (connector: NuxtAxiosInstance) {
|
|
|
|
|
- Connection.connector = connector
|
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Main méthode qui appellera les méthodes privées correspondantes (getItem, getCollection, put, post, delete)
|
|
* Main méthode qui appellera les méthodes privées correspondantes (getItem, getCollection, put, post, delete)
|
|
@@ -73,13 +66,12 @@ class Connection {
|
|
|
* @return {Promise<any>}
|
|
* @return {Promise<any>}
|
|
|
*/
|
|
*/
|
|
|
public static getItem (url: string, id: number, showProgress: boolean = true, params: AnyJson = {}): Promise<any> {
|
|
public static getItem (url: string, id: number, showProgress: boolean = true, params: AnyJson = {}): Promise<any> {
|
|
|
- const config: AxiosRequestConfig = {
|
|
|
|
|
- url: `${url}/${id}`,
|
|
|
|
|
|
|
+ const config: FetchOptions = {
|
|
|
method: HTTP_METHOD.GET,
|
|
method: HTTP_METHOD.GET,
|
|
|
progress: showProgress,
|
|
progress: showProgress,
|
|
|
params: params
|
|
params: params
|
|
|
}
|
|
}
|
|
|
- return Connection.request(config)
|
|
|
|
|
|
|
+ return Connection.request(`${url}/${id}`, config)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -91,8 +83,7 @@ class Connection {
|
|
|
* @return {Promise<any>}
|
|
* @return {Promise<any>}
|
|
|
*/
|
|
*/
|
|
|
public static getCollection (url: string, type: QUERY_TYPE, progress: boolean = true, params: AnyJson = {}): Promise<any> {
|
|
public static getCollection (url: string, type: QUERY_TYPE, progress: boolean = true, params: AnyJson = {}): Promise<any> {
|
|
|
- let config: AxiosRequestConfig = {
|
|
|
|
|
- url: `${url}`,
|
|
|
|
|
|
|
+ let config: FetchOptions = {
|
|
|
method: HTTP_METHOD.GET,
|
|
method: HTTP_METHOD.GET,
|
|
|
progress,
|
|
progress,
|
|
|
params: params
|
|
params: params
|
|
@@ -100,7 +91,7 @@ class Connection {
|
|
|
if(type === QUERY_TYPE.IMAGE)
|
|
if(type === QUERY_TYPE.IMAGE)
|
|
|
config = {...config, responseType: 'blob'}
|
|
config = {...config, responseType: 'blob'}
|
|
|
|
|
|
|
|
- return Connection.request(config)
|
|
|
|
|
|
|
+ return Connection.request(`${url}`, config)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -112,14 +103,13 @@ class Connection {
|
|
|
* @return {Promise<any>}
|
|
* @return {Promise<any>}
|
|
|
*/
|
|
*/
|
|
|
public static download (url: string, showProgress: boolean = true, params: AnyJson = {}): Promise<any> {
|
|
public static download (url: string, showProgress: boolean = true, params: AnyJson = {}): Promise<any> {
|
|
|
- const config: AxiosRequestConfig = {
|
|
|
|
|
- url: `${url}`,
|
|
|
|
|
|
|
+ const config: FetchOptions = {
|
|
|
method: HTTP_METHOD.GET,
|
|
method: HTTP_METHOD.GET,
|
|
|
progress: showProgress,
|
|
progress: showProgress,
|
|
|
responseType: 'blob',
|
|
responseType: 'blob',
|
|
|
params: params
|
|
params: params
|
|
|
}
|
|
}
|
|
|
- return Connection.request(config)
|
|
|
|
|
|
|
+ return Connection.request(`${url}`, config)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -131,14 +121,13 @@ class Connection {
|
|
|
* @return {Promise<any>}
|
|
* @return {Promise<any>}
|
|
|
*/
|
|
*/
|
|
|
public static post(url: string, data: AnyJson, progress: boolean = true, params: AnyJson = {}): Promise<any> {
|
|
public static post(url: string, data: AnyJson, progress: boolean = true, params: AnyJson = {}): Promise<any> {
|
|
|
- const config: AxiosRequestConfig = {
|
|
|
|
|
- url: `${url}`,
|
|
|
|
|
|
|
+ const config: FetchOptions = {
|
|
|
method: HTTP_METHOD.POST,
|
|
method: HTTP_METHOD.POST,
|
|
|
data,
|
|
data,
|
|
|
progress,
|
|
progress,
|
|
|
params: params
|
|
params: params
|
|
|
}
|
|
}
|
|
|
- return Connection.request(config)
|
|
|
|
|
|
|
+ return Connection.request(`${url}`, config)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -151,14 +140,13 @@ class Connection {
|
|
|
* @return {Promise<any>}
|
|
* @return {Promise<any>}
|
|
|
*/
|
|
*/
|
|
|
public static put (url: string, id: number, data: AnyJson, progress: boolean = true, params: AnyJson = {}): Promise<any> {
|
|
public static put (url: string, id: number, data: AnyJson, progress: boolean = true, params: AnyJson = {}): Promise<any> {
|
|
|
- const config: AxiosRequestConfig = {
|
|
|
|
|
- url: `${url}/${id}`,
|
|
|
|
|
|
|
+ const config: FetchOptions = {
|
|
|
method: HTTP_METHOD.PUT,
|
|
method: HTTP_METHOD.PUT,
|
|
|
data,
|
|
data,
|
|
|
progress,
|
|
progress,
|
|
|
params: params
|
|
params: params
|
|
|
}
|
|
}
|
|
|
- return Connection.request(config)
|
|
|
|
|
|
|
+ return Connection.request(`${url}/${id}`, config)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -170,22 +158,72 @@ class Connection {
|
|
|
* @return {Promise<any>}
|
|
* @return {Promise<any>}
|
|
|
*/
|
|
*/
|
|
|
public static deleteItem (url: string, id: number, progress: boolean = true, params: AnyJson = {}): Promise<any> {
|
|
public static deleteItem (url: string, id: number, progress: boolean = true, params: AnyJson = {}): Promise<any> {
|
|
|
- const config: AxiosRequestConfig = {
|
|
|
|
|
- url: `${url}/${id}`,
|
|
|
|
|
|
|
+ const config: FetchOptions = {
|
|
|
method: HTTP_METHOD.DELETE,
|
|
method: HTTP_METHOD.DELETE,
|
|
|
progress,
|
|
progress,
|
|
|
params: params
|
|
params: params
|
|
|
}
|
|
}
|
|
|
- return Connection.request(config)
|
|
|
|
|
|
|
+ return Connection.request(`${url}/${id}`, config)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Exécute la requete
|
|
* Exécute la requete
|
|
|
- * @param {AxiosRequestConfig} config
|
|
|
|
|
|
|
+ * @param {string} url
|
|
|
|
|
+ * @param {FetchOptions} config
|
|
|
* @return {Promise<any>}
|
|
* @return {Promise<any>}
|
|
|
*/
|
|
*/
|
|
|
- public static async request (config: AxiosRequestConfig): Promise<AxiosResponse> {
|
|
|
|
|
- return await Connection.connector.$request(config)
|
|
|
|
|
|
|
+ public static async request (url: string, config: FetchOptions): Promise<FetchResponse> {
|
|
|
|
|
+ Connection.addBaseUrl(config)
|
|
|
|
|
+ Connection.addHeader(config)
|
|
|
|
|
+ Connection.addOnResponseError(config)
|
|
|
|
|
+ return await $fetch(url, config)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * On ajoute la base URL
|
|
|
|
|
+ * @param config
|
|
|
|
|
+ */
|
|
|
|
|
+ public static addBaseUrl(config){
|
|
|
|
|
+ const runtimeConfig = useRuntimeConfig()
|
|
|
|
|
+ config['baseURL'] = runtimeConfig.baseUrl ?? runtimeConfig.public.baseUrl
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * On ajoute les headers
|
|
|
|
|
+ * @param config
|
|
|
|
|
+ */
|
|
|
|
|
+ public static addHeader(config){
|
|
|
|
|
+ if(!config.params.noXaccessId){
|
|
|
|
|
+ const profileAccessStore = useProfileAccessStore()
|
|
|
|
|
+ config['headers'] = {
|
|
|
|
|
+ 'x-accessid' : profileAccessStore.id,
|
|
|
|
|
+ 'Authorization' : 'BEARER ' + profileAccessStore.bearer
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (profileAccessStore.switchId) {
|
|
|
|
|
+ config['headers']['x-switch-user'] = profileAccessStore.switchId
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Gestion des erreurs de réponse
|
|
|
|
|
+ * @param config
|
|
|
|
|
+ */
|
|
|
|
|
+ public static async addOnResponseError(config){
|
|
|
|
|
+ config['onResponseError'] = (({ request, response, options }) => {
|
|
|
|
|
+ // In case of unauthorized, redirect to a specific page
|
|
|
|
|
+ if (response.status === 401) {
|
|
|
|
|
+ redirect('/login')
|
|
|
|
|
+ }
|
|
|
|
|
+ if (response.status === 403) {
|
|
|
|
|
+ new Page().addAlerts(TYPE_ALERT.ALERT, ['forbidden'])
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (response.status === 500) {
|
|
|
|
|
+ new Page().addAlerts(TYPE_ALERT.ALERT, [response])
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|