| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- import {AnyJson, DataPersisterArgs, DataProviderArgs, UrlArgs} from '~/types/interfaces'
- import {HTTP_METHOD, QUERY_TYPE} from '~/types/enums'
- 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
- * @class Connection
- *
- * Classe Wrapper du connecteur de requête (Axios dans notre cas)
- */
- class Connection {
- /**
- * Main méthode qui appellera les méthodes privées correspondantes (getItem, getCollection, put, post, delete)
- * @param {HTTP_METHOD} method Mode de requêtage (GET, PUT, DELETE...)
- * @param {string} url
- * @param {DataProviderArgs|DataPersisterArgs} args
- * @return {Promise<any>}
- */
- public static invoke (method: HTTP_METHOD, url: string, args: UrlArgs): Promise<any> {
- switch (method) {
- case HTTP_METHOD.GET:
- if (args.type === QUERY_TYPE.FILE) {
- return Connection.download(url, args.showProgress, args.params)
- }
- if (args.id) {
- return Connection.getItem(url, args.id, args.showProgress, args.params)
- } else {
- return Connection.getCollection(url, args.type, args.showProgress, args.params)
- }
- case HTTP_METHOD.PUT:
- case HTTP_METHOD.POST:
- if (!TypesTesting.isDataPersisterArgs(args)) {
- throw new Error('*args* is not a dataPersisterArgs')
- }
- if (!args.data) {
- throw new Error('*args* has no data')
- }
- switch (method) {
- case HTTP_METHOD.PUT:
- return Connection.put(url, args.id, args.data, args.showProgress, args.params)
- case HTTP_METHOD.POST:
- return Connection.post(url, args.data, args.showProgress, args.params)
- }
- break;
- case HTTP_METHOD.DELETE:
- return Connection.deleteItem(url, args.id, args.showProgress, args.params)
- }
- throw new Error('Unknown connection method was invoked')
- }
- /**
- * GET Item : préparation de la config pour la récupération d'un item
- * @param {string} url
- * @param {number} id
- * @param {boolean} showProgress
- * @param {AnyJson} params
- * @return {Promise<any>}
- */
- public static getItem (url: string, id: number, showProgress: boolean = true, params: AnyJson = {}): Promise<any> {
- const config: FetchOptions = {
- method: HTTP_METHOD.GET,
- progress: showProgress,
- params: params
- }
- return Connection.request(`${url}/${id}`, config)
- }
- /**
- * Get collection : préparation de la config pour la récupération d'une collection d'items
- * @param {string} url
- * @param {boolean} progress
- * @param {QUERY_TYPE} type
- * @param {AnyJson} params
- * @return {Promise<any>}
- */
- public static getCollection (url: string, type: QUERY_TYPE, progress: boolean = true, params: AnyJson = {}): Promise<any> {
- let config: FetchOptions = {
- method: HTTP_METHOD.GET,
- progress,
- params: params
- }
- if(type === QUERY_TYPE.IMAGE)
- config = {...config, responseType: 'blob'}
- return Connection.request(`${url}`, config)
- }
- /**
- * GET dédié au téléchargement de fichiers
- * @param url
- * @param {number} id
- * @param {boolean} showProgress
- * @param {AnyJson} params
- * @return {Promise<any>}
- */
- public static download (url: string, showProgress: boolean = true, params: AnyJson = {}): Promise<any> {
- const config: FetchOptions = {
- method: HTTP_METHOD.GET,
- progress: showProgress,
- responseType: 'blob',
- params: params
- }
- return Connection.request(`${url}`, config)
- }
- /**
- * Post : préparation de la config pour la création d'un item
- * @param {string} url
- * @param {AnyJson} data
- * @param {boolean} progress
- * @param {AnyJson} params
- * @return {Promise<any>}
- */
- public static post(url: string, data: AnyJson, progress: boolean = true, params: AnyJson = {}): Promise<any> {
- const config: FetchOptions = {
- method: HTTP_METHOD.POST,
- data,
- progress,
- params: params
- }
- return Connection.request(`${url}`, config)
- }
- /**
- * Put : préparation de la config pour la mise à jour d'un item
- * @param {string} url
- * @param {number} id
- * @param {AnyJson} data
- * @param {boolean} progress
- * @param {AnyJson} params
- * @return {Promise<any>}
- */
- public static put (url: string, id: number, data: AnyJson, progress: boolean = true, params: AnyJson = {}): Promise<any> {
- const config: FetchOptions = {
- method: HTTP_METHOD.PUT,
- data,
- progress,
- params: params
- }
- return Connection.request(`${url}/${id}`, config)
- }
- /**
- * DELETE Item : préparation de la config pour la suppression d'un item
- * @param {string} url
- * @param {number} id
- * @param {boolean} progress
- * @param {AnyJson} params
- * @return {Promise<any>}
- */
- public static deleteItem (url: string, id: number, progress: boolean = true, params: AnyJson = {}): Promise<any> {
- const config: FetchOptions = {
- method: HTTP_METHOD.DELETE,
- progress,
- params: params
- }
- return Connection.request(`${url}/${id}`, config)
- }
- /**
- * Exécute la requete
- * @param {string} url
- * @param {FetchOptions} config
- * @return {Promise<any>}
- */
- 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])
- }
- })
- }
- }
- export default Connection
|