| Numéro du ticket | Titre du ticket | |------------------|-----------------| | | | ## Description de la PR ## Modification du .env ## Check list - [ ] Pas de régression - [ ] TU/TI/TF rédigée - [ ] TU/TI/TF OK - [ ] CHANGELOG modifié Reviewed-on: #2 Reviewed-by: THOLOT DECHENE Matthieu <matthieu@yuno.malio.fr> Co-authored-by: AUTIN Tristan <tristan@yuno.malio.fr> Co-committed-by: AUTIN Tristan <tristan@yuno.malio.fr>
45 lines
1.7 KiB
TypeScript
45 lines
1.7 KiB
TypeScript
import type { FetchOptions } from 'ofetch'
|
|
import { $fetch, FetchError } from 'ofetch'
|
|
|
|
export type AnyObject = Record<string, unknown>
|
|
|
|
export type ApiClient = {
|
|
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 => {
|
|
const config = useRuntimeConfig()
|
|
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>(url: string, query: AnyObject = {}, options: FetchOptions<'json'> = {}) {
|
|
return request<T>('GET', url, { ...options, query })
|
|
},
|
|
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 })
|
|
}
|
|
}
|
|
}
|