From b5efb54f71a8a8beaf7ad56e9c79277be5c1965f Mon Sep 17 00:00:00 2001 From: matthieu Date: Mon, 9 Mar 2026 22:43:07 +0100 Subject: [PATCH] feat : add Client DTO, service and Hydra utils (frontend) Co-Authored-By: Claude Opus 4.6 --- frontend/services/clients.ts | 32 ++++++++++++++++++++++++++++++++ frontend/services/dto/client.ts | 19 +++++++++++++++++++ frontend/utils/api.ts | 8 ++++++++ 3 files changed, 59 insertions(+) create mode 100644 frontend/services/clients.ts create mode 100644 frontend/services/dto/client.ts create mode 100644 frontend/utils/api.ts diff --git a/frontend/services/clients.ts b/frontend/services/clients.ts new file mode 100644 index 0000000..6a100be --- /dev/null +++ b/frontend/services/clients.ts @@ -0,0 +1,32 @@ +import type { Client, ClientWrite } from './dto/client' +import type { HydraCollection } from '~/utils/api' +import { extractHydraMembers } from '~/utils/api' + +export function useClientService() { + const api = useApi() + + async function getAll(): Promise { + const data = await api.get>('/clients') + return extractHydraMembers(data) + } + + async function create(payload: ClientWrite): Promise { + return api.post('/clients', payload as Record, { + toastSuccessKey: 'clients.created', + }) + } + + async function update(id: number, payload: Partial): Promise { + return api.patch(`/clients/${id}`, payload as Record, { + toastSuccessKey: 'clients.updated', + }) + } + + async function remove(id: number): Promise { + await api.delete(`/clients/${id}`, {}, { + toastSuccessKey: 'clients.deleted', + }) + } + + return { getAll, create, update, remove } +} diff --git a/frontend/services/dto/client.ts b/frontend/services/dto/client.ts new file mode 100644 index 0000000..191931a --- /dev/null +++ b/frontend/services/dto/client.ts @@ -0,0 +1,19 @@ +export type Client = { + id: number + '@id'?: string + name: string + email: string | null + phone: string | null + street: string | null + city: string | null + postalCode: string | null +} + +export type ClientWrite = { + name: string + email: string | null + phone: string | null + street: string | null + city: string | null + postalCode: string | null +} diff --git a/frontend/utils/api.ts b/frontend/utils/api.ts new file mode 100644 index 0000000..6a220e1 --- /dev/null +++ b/frontend/utils/api.ts @@ -0,0 +1,8 @@ +export type HydraCollection = { + 'hydra:member': T[] + 'hydra:totalItems': number +} + +export function extractHydraMembers(response: HydraCollection): T[] { + return response['hydra:member'] ?? [] +}