mirror of
https://github.com/IliesAllali/parenthese.git
synced 2026-11-19 00:52:11 +00:00
Merge origin/main dans feat/immich-memories-retouches
This commit is contained in:
commit
0ff143f2c1
199 files changed
+10164
-4288
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { apiRequest } from './client'
|
||||
import { API_BASE_URL, apiRequest } from './client'
|
||||
|
||||
export async function loginUser(email, password) {
|
||||
const data = await apiRequest('/auth/login', {
|
||||
@@ -29,13 +29,26 @@ export async function logoutUser(token) {
|
||||
return data
|
||||
}
|
||||
|
||||
export async function deleteAccount(token, password) {
|
||||
// confirmation : { password } pour un compte à mot de passe, { confirmEmail } pour un compte né d'une connexion OIDC
|
||||
export async function deleteAccount(token, confirmation) {
|
||||
const data = await apiRequest('/auth/me', {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
body: JSON.stringify({ password }),
|
||||
body: JSON.stringify(confirmation),
|
||||
})
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
// Profil du compte connecté (prénom compris), pour une session restaurée sur un autre appareil
|
||||
export async function fetchAccountProfile(token) {
|
||||
const data = await apiRequest('/auth/me', {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
})
|
||||
|
||||
return data
|
||||
@@ -63,3 +76,21 @@ export async function updateLastOpenedTree(token, payload) {
|
||||
|
||||
return data?.lastTree || null
|
||||
}
|
||||
|
||||
// Options de l'instance : inscription ouverte ou fermée, connexion par mot de passe, fournisseur OIDC
|
||||
export async function fetchAuthOptions() {
|
||||
return apiRequest('/auth/options', { method: 'GET' })
|
||||
}
|
||||
|
||||
// Adresse qui part chez le fournisseur OIDC ; returnTo ramène sur l'arbre partagé ouvert (validé par l'API)
|
||||
export function oidcStartUrl(returnTo = '/') {
|
||||
return `${API_BASE_URL}/auth/oidc/start?returnTo=${encodeURIComponent(returnTo)}`
|
||||
}
|
||||
|
||||
// Jeton de passage reçu au retour du fournisseur, échangé une seule fois contre une session
|
||||
export async function exchangeOidcCode(code) {
|
||||
return apiRequest('/auth/oidc/exchange', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ code }),
|
||||
})
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
import { t } from '../i18n/index.js'
|
||||
|
||||
const rawApiBaseUrl = import.meta.env.VITE_API_BASE_URL
|
||||
const defaultApiBaseUrl = import.meta.env.DEV ? 'http://localhost:4000' : ''
|
||||
const API_BASE_URL = (rawApiBaseUrl ?? defaultApiBaseUrl).replace(/\/$/, '')
|
||||
@@ -23,7 +25,21 @@ async function safeParseJson(text) {
|
||||
}
|
||||
}
|
||||
|
||||
// Sans limite, un réseau mobile qui décroche laisse l'arbre « en chargement » pour toujours.
|
||||
// Un corps de plus de 100 Ko est un envoi de fichier : il a plus de temps sur une connexion lente.
|
||||
const DEFAULT_TIMEOUT_MS = 30_000
|
||||
const UPLOAD_TIMEOUT_MS = 5 * 60_000
|
||||
const LARGE_BODY_CHARS = 100_000
|
||||
|
||||
function timeoutSignal(options) {
|
||||
if (options.signal) return options.signal
|
||||
if (typeof AbortSignal === 'undefined' || typeof AbortSignal.timeout !== 'function') return undefined
|
||||
const isLargeBody = typeof options.body === 'string' && options.body.length > LARGE_BODY_CHARS
|
||||
return AbortSignal.timeout(options.timeoutMs ?? (isLargeBody ? UPLOAD_TIMEOUT_MS : DEFAULT_TIMEOUT_MS))
|
||||
}
|
||||
|
||||
async function apiRequest(path, options = {}) {
|
||||
const { timeoutMs: _timeoutMs, ...fetchOptions } = options
|
||||
const providedHeaders = options.headers || {}
|
||||
const hasExplicitContentType = Object.keys(providedHeaders).some((key) => key.toLowerCase() === 'content-type')
|
||||
|
||||
@@ -32,12 +48,21 @@ async function apiRequest(path, options = {}) {
|
||||
...providedHeaders,
|
||||
}
|
||||
|
||||
const response = await fetch(`${API_BASE_URL}${path}`, {
|
||||
...options,
|
||||
headers,
|
||||
})
|
||||
|
||||
const text = await response.text()
|
||||
let response
|
||||
let text
|
||||
try {
|
||||
response = await fetch(`${API_BASE_URL}${path}`, {
|
||||
...fetchOptions,
|
||||
headers,
|
||||
signal: timeoutSignal(options),
|
||||
})
|
||||
text = await response.text()
|
||||
} catch (error) {
|
||||
if (error?.name === 'TimeoutError') {
|
||||
throw new ApiError(t('Le serveur ne répond pas. Vérifiez la connexion et réessayez.'), 0, { error: 'request_timeout' })
|
||||
}
|
||||
throw error
|
||||
}
|
||||
const payload = await safeParseJson(text)
|
||||
|
||||
if (!response.ok) {
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
import { ApiError, apiRequest } from './client'
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
vi.unstubAllGlobals()
|
||||
})
|
||||
|
||||
describe('apiRequest', () => {
|
||||
it('une requête qui ne répond pas finit en erreur lisible', async () => {
|
||||
const timeout = vi.spyOn(AbortSignal, 'timeout')
|
||||
vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new DOMException('trop long', 'TimeoutError')))
|
||||
await expect(apiRequest('/trees')).rejects.toMatchObject({
|
||||
name: 'ApiError', message: 'Le serveur ne répond pas. Vérifiez la connexion et réessayez.', status: 0, payload: { error: 'request_timeout' },
|
||||
})
|
||||
expect(timeout).toHaveBeenCalledWith(30_000)
|
||||
})
|
||||
|
||||
it('un envoi de fichier a plus de temps, et un délai demandé passe devant', async () => {
|
||||
const timeout = vi.spyOn(AbortSignal, 'timeout')
|
||||
vi.stubGlobal('fetch', vi.fn().mockImplementation(async () => new Response('{"ok":true}', { status: 200 })))
|
||||
await apiRequest('/upload', { method: 'POST', body: 'x'.repeat(200_000) })
|
||||
expect(timeout).toHaveBeenLastCalledWith(5 * 60_000)
|
||||
await apiRequest('/import', { method: 'POST', body: '{}', timeoutMs: 1234 })
|
||||
expect(timeout).toHaveBeenLastCalledWith(1234)
|
||||
expect(fetch.mock.calls[1][1]).not.toHaveProperty('timeoutMs')
|
||||
})
|
||||
|
||||
it('une autre panne réseau remonte telle quelle', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new TypeError('Failed to fetch')))
|
||||
const error = await apiRequest('/trees').catch((e) => e)
|
||||
expect(error).toBeInstanceOf(TypeError)
|
||||
expect(error).not.toBeInstanceOf(ApiError)
|
||||
})
|
||||
})
|
||||
@@ -235,6 +235,19 @@ export async function reorderSiblings(treeId, order, token) {
|
||||
})
|
||||
}
|
||||
|
||||
// Lien parent-enfant modifié (type de filiation : naissance ou adoption)
|
||||
export async function updateParentChildLink(treeId, linkId, payload, token) {
|
||||
const data = await apiRequest(`/trees/${encodeURIComponent(treeId)}/links/${encodeURIComponent(linkId)}`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
body: JSON.stringify(payload),
|
||||
})
|
||||
|
||||
return data.link
|
||||
}
|
||||
|
||||
export async function deleteParentChildLink(treeId, linkId, token) {
|
||||
const data = await apiRequest(`/trees/${encodeURIComponent(treeId)}/links/${encodeURIComponent(linkId)}`, {
|
||||
method: 'DELETE',
|
||||
@@ -441,6 +454,22 @@ export async function deletePersonMedia(treeId, personId, mediaId, token) {
|
||||
return data
|
||||
}
|
||||
|
||||
// Modifier un souvenir (titre, moment de vie, texte de citation) : administrateurs seulement
|
||||
export async function updatePersonMedia(treeId, personId, mediaId, payload, token) {
|
||||
const data = await apiRequest(
|
||||
`/trees/${encodeURIComponent(treeId)}/persons/${encodeURIComponent(personId)}/media/${encodeURIComponent(mediaId)}`,
|
||||
{
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
body: JSON.stringify(payload),
|
||||
},
|
||||
)
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
export async function uploadPersonAvatar(treeId, personId, payload, token) {
|
||||
const data = await apiRequest(`/trees/${encodeURIComponent(treeId)}/persons/${encodeURIComponent(personId)}/avatar`, {
|
||||
method: 'POST',
|
||||
@@ -453,6 +482,19 @@ export async function uploadPersonAvatar(treeId, personId, payload, token) {
|
||||
return data
|
||||
}
|
||||
|
||||
// Cadrage du portrait ({ x, y, zoom }) ou null pour le cadrage par défaut
|
||||
export async function saveAvatarCrop(treeId, personId, crop, token) {
|
||||
const data = await apiRequest(`/trees/${encodeURIComponent(treeId)}/persons/${encodeURIComponent(personId)}/avatar/crop`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
body: JSON.stringify({ crop }),
|
||||
})
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
export async function deletePersonAvatar(treeId, personId, token) {
|
||||
const data = await apiRequest(`/trees/${encodeURIComponent(treeId)}/persons/${encodeURIComponent(personId)}/avatar`, {
|
||||
method: 'DELETE',
|
||||
@@ -496,6 +538,8 @@ export async function importGedcom(treeId, fileContent, token) {
|
||||
method: 'POST',
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
body: JSON.stringify({ content: fileContent }),
|
||||
// Un gros arbre se crée en une transaction d'une minute au plus côté serveur
|
||||
timeoutMs: 3 * 60_000,
|
||||
})
|
||||
return data
|
||||
}
|
||||
Reference in new issue
Block a user