FEAT-6: typed API clients for health-records, weight-records, gerbil photos (multipart upload)

This commit is contained in:
2026-06-06 00:25:00 +02:00
parent f772464401
commit 17879b1cfb
3 changed files with 183 additions and 0 deletions

View File

@@ -0,0 +1,68 @@
/** Typed API functions for the Gesundheit (HealthRecord) resource. (FEAT-6) */
import { api } from './client'
import { condition, toQueryString } from './gridify'
import type { DateOnlyString, Paged } from './types'
const RESOURCE = '/health-records'
/** HealthRecord.Type — C# enum member names (JsonStringEnumConverter). */
export type HealthRecordType =
| 'Examination'
| 'Treatment'
| 'Injury'
| 'Vaccination'
| 'Other'
export const HEALTH_RECORD_TYPES: HealthRecordType[] = [
'Examination',
'Treatment',
'Injury',
'Vaccination',
'Other',
]
export interface HealthRecord {
id: string
gerbilId: string
date: DateOnlyString
type: HealthRecordType
description: string
veterinarian: string | null
createdAt: string
}
/** Payload for POST /health-records. */
export interface CreateHealthRecord {
gerbilId: string
date: DateOnlyString
type: HealthRecordType
description: string
veterinarian?: string | null
}
/** Payload for PUT /health-records/{id}. */
export type UpdateHealthRecord = Partial<CreateHealthRecord>
/** Alle Einträge eines Tieres, neueste zuerst. */
export async function listHealthRecords(gerbilId: string): Promise<HealthRecord[]> {
const paged = await api.get<Paged<HealthRecord>>(
`${RESOURCE}${toQueryString({
filter: condition({ field: 'gerbilId', op: '==', value: gerbilId }),
orderBy: 'date desc',
page: 1,
pageSize: 500,
})}`,
)
return paged.items
}
export function createHealthRecord(body: CreateHealthRecord): Promise<HealthRecord> {
return api.post<HealthRecord>(RESOURCE, body)
}
export function updateHealthRecord(id: string, body: UpdateHealthRecord): Promise<HealthRecord> {
return api.put<HealthRecord>(`${RESOURCE}/${id}`, body)
}
export function deleteHealthRecord(id: string): Promise<void> {
return api.delete(`${RESOURCE}/${id}`)
}

View File

@@ -0,0 +1,72 @@
/**
* Typed API functions for Fotos (GerbilPhoto). (FEAT-6)
*
* Contract (FEAT-1b Phase 2, Pam):
* GET /gerbils/{id}/photos -> [{id, fileName, caption, sortOrder, url}]
* POST /gerbils/{id}/photos -> multipart, Feld 'file' (+ optional 'caption')
* DELETE /photos/{id}
* Erstes Foto nach sortOrder = Profilfoto.
*
* Der Upload geht NICHT über api.post (das setzt Content-Type: application/json);
* bei FormData muss der Browser den multipart-Boundary-Header selbst setzen.
*/
import { API_BASE_URL, ApiError, api } from './client'
import { de } from '../strings/de'
export interface GerbilPhoto {
id: string
fileName: string
caption: string | null
sortOrder: number
/** Absoluter oder API-relativer Pfad zur Bilddatei. */
url: string
}
export function listGerbilPhotos(gerbilId: string): Promise<GerbilPhoto[]> {
return api.get<GerbilPhoto[]>(`/gerbils/${gerbilId}/photos`)
}
export async function uploadGerbilPhoto(
gerbilId: string,
file: File,
caption: string | null,
): Promise<GerbilPhoto> {
const form = new FormData()
form.append('file', file)
if (caption) form.append('caption', caption)
let response: Response
try {
response = await fetch(`${API_BASE_URL}/gerbils/${gerbilId}/photos`, {
method: 'POST',
body: form,
})
} catch {
throw new ApiError(de.api.errors.network, null)
}
if (!response.ok) {
const message =
response.status === 404
? de.api.errors.notFound
: response.status >= 500
? de.api.errors.server
: de.api.errors.unknown
throw new ApiError(message, response.status)
}
return (await response.json()) as GerbilPhoto
}
export function deleteGerbilPhoto(photoId: string): Promise<void> {
return api.delete(`/photos/${photoId}`)
}
/** Bild-URL auflösen: API liefert ggf. einen API-relativen Pfad. */
export function photoSrc(photo: GerbilPhoto): string {
return photo.url.startsWith('http') ? photo.url : `${API_BASE_URL}${photo.url}`
}
/** Profilfoto = erstes Foto nach sortOrder (null, wenn keine Fotos). */
export function profilePhoto(photos: GerbilPhoto[]): GerbilPhoto | null {
if (photos.length === 0) return null
return [...photos].sort((a, b) => a.sortOrder - b.sortOrder)[0]
}

View File

@@ -0,0 +1,43 @@
/** Typed API functions for the Gewicht (WeightRecord) resource. (FEAT-6) */
import { api } from './client'
import { condition, toQueryString } from './gridify'
import type { DateOnlyString, Paged } from './types'
const RESOURCE = '/weight-records'
export interface WeightRecord {
id: string
gerbilId: string
date: DateOnlyString
weightGrams: number
notes: string | null
}
/** Payload for POST /weight-records. */
export interface CreateWeightRecord {
gerbilId: string
date: DateOnlyString
weightGrams: number
notes?: string | null
}
/** Alle Einträge eines Tieres, neueste zuerst (Chart sortiert selbst aufsteigend). */
export async function listWeightRecords(gerbilId: string): Promise<WeightRecord[]> {
const paged = await api.get<Paged<WeightRecord>>(
`${RESOURCE}${toQueryString({
filter: condition({ field: 'gerbilId', op: '==', value: gerbilId }),
orderBy: 'date desc',
page: 1,
pageSize: 1000,
})}`,
)
return paged.items
}
export function createWeightRecord(body: CreateWeightRecord): Promise<WeightRecord> {
return api.post<WeightRecord>(RESOURCE, body)
}
export function deleteWeightRecord(id: string): Promise<void> {
return api.delete(`${RESOURCE}/${id}`)
}