| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- import type { Ref } from 'vue'
- import type { FetchContext, FetchOptions } from 'ofetch'
- import { TYPE_ALERT } from '~/types/enum/enums'
- import ApiRequestService from '~/services/data/apiRequestService'
- import { usePageStore } from '~/stores/page'
- import UnauthorizedError from '~/services/error/UnauthorizedError'
- import { useAccessProfileStore } from '~/stores/accessProfile'
- /**
- * Retourne une instance de ApiRequestService configurée pour interroger l'api Ap2i
- *
- * @see https://github.com/unjs/ohmyfetch/blob/main/README.md#%EF%B8%8F-create-fetch-with-default-options
- */
- let apiRequestServiceClass: null | ApiRequestService = null
- export const useAp2iRequestService = () => {
- const runtimeConfig = useRuntimeConfig()
- const baseURL = runtimeConfig.baseUrl ?? runtimeConfig.public.baseUrl
- const pending: Ref<boolean> = ref(false)
- /**
- * Peuple les headers avant l'envoi de la requête
- *
- * @param request
- * @param options
- */
- const onRequest = function ({ request, options }: FetchContext) {
- // @ts-expect-error options is not aware of noXaccessId
- if (options && options.noXaccessId) {
- return
- }
- const accessProfileStore = useAccessProfileStore()
- const headers = new Headers(options.headers)
- headers.set('x-accessid', String(accessProfileStore.id))
- headers.set('Authorization', 'BEARER ' + accessProfileStore.bearer)
- if (accessProfileStore.switchId) {
- headers.set('x-switch-user', String(accessProfileStore.switchId))
- }
- options.headers = headers
- pending.value = true
- console.log('Request : ' + request + ' (SSR: ' + import.meta.server + ')')
- }
- const onRequestError = function (_: FetchContext) {
- pending.value = false
- }
- /**
- * Server responded
- */
- const onResponse = function (_: FetchContext) {
- pending.value = false
- }
- /**
- * Gère les erreurs retournées par l'api
- *
- * @param request
- * @param response
- * @param error
- */
- const onResponseError = function ({ response, error }: FetchContext) {
- pending.value = false
- if (response && response.status === 401) {
- throw new UnauthorizedError('Ap2i - Unauthorized')
- } else if (response && response.status === 403) {
- console.error('! Request error: Forbidden')
- usePageStore().addAlert(TYPE_ALERT.ALERT, ['forbidden'])
- } else if (
- response &&
- (response.status === 400 || response.status >= 404)
- ) {
- // @see https://developer.mozilla.org/fr/docs/Web/HTTP/Status
- let errorMsg = []
- if (error) {
- errorMsg.push(error.message)
- }
- else if(response._data && response._data.violations){
- for (const violation of response._data.violations) {
- errorMsg.push(violation.message as string)
- }
- }
- else if (response._data && response._data.detail) {
- errorMsg.push(response._data.detail)
- } else if (response.statusText) {
- errorMsg.push(response.statusText)
- } else {
- errorMsg.push('An error occured')
- }
- console.error('! Request error: ' + errorMsg)
- usePageStore().addAlert(TYPE_ALERT.ALERT, errorMsg)
- throw ({ response, error })
- }
- }
- const config: FetchOptions = {
- baseURL,
- onRequest,
- onRequestError,
- onResponse,
- onResponseError,
- }
- // Utilise la fonction `create` d'ohmyfetch pour générer un fetcher dédié à l'interrogation de Ap2i
- const fetcher = $fetch.create(config)
- apiRequestServiceClass = new ApiRequestService(fetcher)
- return { apiRequestService: apiRequestServiceClass, pending }
- }
|