[#203] Réceptions — Parcours de pesée multi-étapes #3

Merged
tristan merged 10 commits from feat/203-reception-parcours-pesee-multi-etapas into develop 2026-01-14 07:17:34 +00:00
Showing only changes of commit 03638d988b - Show all commits

View File

@@ -1,9 +1,14 @@
import type { FetchOptions } from 'ofetch'
import { $fetch, FetchError } from 'ofetch'
export type AnyObject = Record<string, unknown>
export type ApiClient = {
get<T>(path: string, options?: FetchOptions<'json'>): Promise<T>
post<T>(path: string, body?: unknown, options?: FetchOptions<'json'>): Promise<T>
get<T>(url: string, query?: AnyObject, options?: FetchOptions<'json'>): Promise<T>
post<T>(url: string, body?: AnyObject, options?: FetchOptions<'json'>): Promise<T>
put<T>(url: string, body?: AnyObject, options?: FetchOptions<'json'>): Promise<T>
patch<T>(url: string, body?: AnyObject, options?: FetchOptions<'json'>): Promise<T>
delete<T>(url: string, query?: AnyObject, options?: FetchOptions<'json'>): Promise<T>
}
export const useApi = (): ApiClient => {
@@ -11,32 +16,29 @@ export const useApi = (): ApiClient => {
const baseURL = config.public.apiBase ?? '/api'
const client = $fetch.create({ baseURL })
const request = <T>(
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE',
url: string,
options: FetchOptions<'json'> = {}
) => {
return client<T>(url, { ...options, method })
}
return {
get<T>(path: string, options?: FetchOptions<'json'>) {
return client<T>(path, { ...options, method: 'GET' })
get<T>(url: string, query: AnyObject = {}, options: FetchOptions<'json'> = {}) {
return request<T>('GET', url, { ...options, query })
},
post<T>(path: string, body?: unknown, options?: FetchOptions<'json'>) {
return client<T>(path, { ...options, method: 'POST', body })
post<T>(url: string, body: AnyObject = {}, options: FetchOptions<'json'> = {}) {
return request<T>('POST', url, { ...options, body })
},
put<T>(url: string, body: AnyObject = {}, options: FetchOptions<'json'> = {}) {
return request<T>('PUT', url, { ...options, body })
},
patch<T>(url: string, body: AnyObject = {}, options: FetchOptions<'json'> = {}) {
return request<T>('PATCH', url, { ...options, body })
},
delete<T>(url: string, query: AnyObject = {}, options: FetchOptions<'json'> = {}) {
return request<T>('DELETE', url, { ...options, query })
}
}
}
export const getApiStatus = (error: unknown): number | null => {
if (error && typeof error === 'object') {
if (error instanceof FetchError) {
return error.status ?? error.response?.status ?? null
}
const maybeResponse = (error as { response?: { status?: number } }).response
if (typeof maybeResponse?.status === 'number') {
return maybeResponse.status
}
const maybeStatus = (error as { status?: number }).status
if (typeof maybeStatus === 'number') {
return maybeStatus
}
}
return null
}