fix : correctif mr

This commit is contained in:
2026-03-13 09:47:09 +01:00
parent b3fc6f77b1
commit 00dc2daa3d
11 changed files with 61 additions and 67 deletions

View File

@@ -32,26 +32,26 @@ function getDownloadFileName(contentDisposition: string | null, fallback: string
return fallback
}
export function useApiAuthHeader() {
const runtimeConfig = useRuntimeConfig()
const token = runtimeConfig.public.apiSecretKey
if (!token) {
return {}
}
// Tous les appels frontend vers /api/* reutilisent ce header commun.
export function withApiAuth(init: RequestInit = {}) {
// Les appels frontend reutilisent les cookies httpOnly poses cote serveur.
return {
Authorization: `Bearer ${token}`
...init,
headers: {
...toHeadersObject(init.headers)
}
}
}
export const apiFetch = $fetch.create({})
export function apiRequest(input: RequestInfo | URL, init: RequestInit = {}) {
return fetch(input, withApiAuth(init))
}
export async function downloadApiFile(url: string, fileNameFallback: string) {
// Les telechargements passent aussi par fetch pour pouvoir envoyer
// le header Authorization, contrairement a un simple <a href>.
const response = await fetch(url, {
headers: useApiAuthHeader()
})
// Les telechargements passent aussi par fetch pour pouvoir recuperer
// le contenu et le nom de fichier renvoye par l'API.
const response = await apiRequest(url)
if (!response.ok) {
throw new Error(`HTTP ${response.status}`)
@@ -73,14 +73,3 @@ export async function downloadApiFile(url: string, fileNameFallback: string) {
link.remove()
URL.revokeObjectURL(objectUrl)
}
export function withApiAuth(init: RequestInit = {}) {
// Fusionne le header d'auth avec d'eventuels headers deja fournis.
return {
...init,
headers: {
...useApiAuthHeader(),
...toHeadersObject(init.headers)
}
}
}