feat : add Client DTO, service and Hydra utils (frontend)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
32
frontend/services/clients.ts
Normal file
32
frontend/services/clients.ts
Normal file
@@ -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<Client[]> {
|
||||
const data = await api.get<HydraCollection<Client>>('/clients')
|
||||
return extractHydraMembers(data)
|
||||
}
|
||||
|
||||
async function create(payload: ClientWrite): Promise<Client> {
|
||||
return api.post<Client>('/clients', payload as Record<string, unknown>, {
|
||||
toastSuccessKey: 'clients.created',
|
||||
})
|
||||
}
|
||||
|
||||
async function update(id: number, payload: Partial<ClientWrite>): Promise<Client> {
|
||||
return api.patch<Client>(`/clients/${id}`, payload as Record<string, unknown>, {
|
||||
toastSuccessKey: 'clients.updated',
|
||||
})
|
||||
}
|
||||
|
||||
async function remove(id: number): Promise<void> {
|
||||
await api.delete(`/clients/${id}`, {}, {
|
||||
toastSuccessKey: 'clients.deleted',
|
||||
})
|
||||
}
|
||||
|
||||
return { getAll, create, update, remove }
|
||||
}
|
||||
19
frontend/services/dto/client.ts
Normal file
19
frontend/services/dto/client.ts
Normal file
@@ -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
|
||||
}
|
||||
8
frontend/utils/api.ts
Normal file
8
frontend/utils/api.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
export type HydraCollection<T> = {
|
||||
'hydra:member': T[]
|
||||
'hydra:totalItems': number
|
||||
}
|
||||
|
||||
export function extractHydraMembers<T>(response: HydraCollection<T>): T[] {
|
||||
return response['hydra:member'] ?? []
|
||||
}
|
||||
Reference in New Issue
Block a user