fix : correction du useApi et ajout des autres types de requête
This commit is contained in:
@@ -1,9 +1,14 @@
|
|||||||
import type { FetchOptions } from 'ofetch'
|
import type { FetchOptions } from 'ofetch'
|
||||||
import { $fetch, FetchError } from 'ofetch'
|
import { $fetch, FetchError } from 'ofetch'
|
||||||
|
|
||||||
|
export type AnyObject = Record<string, unknown>
|
||||||
|
|
||||||
export type ApiClient = {
|
export type ApiClient = {
|
||||||
get<T>(path: string, options?: FetchOptions<'json'>): Promise<T>
|
get<T>(url: string, query?: AnyObject, options?: FetchOptions<'json'>): Promise<T>
|
||||||
post<T>(path: string, body?: unknown, 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 => {
|
export const useApi = (): ApiClient => {
|
||||||
@@ -11,32 +16,29 @@ export const useApi = (): ApiClient => {
|
|||||||
const baseURL = config.public.apiBase ?? '/api'
|
const baseURL = config.public.apiBase ?? '/api'
|
||||||
const client = $fetch.create({ baseURL })
|
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 {
|
return {
|
||||||
get<T>(path: string, options?: FetchOptions<'json'>) {
|
get<T>(url: string, query: AnyObject = {}, options: FetchOptions<'json'> = {}) {
|
||||||
return client<T>(path, { ...options, method: 'GET' })
|
return request<T>('GET', url, { ...options, query })
|
||||||
},
|
},
|
||||||
post<T>(path: string, body?: unknown, options?: FetchOptions<'json'>) {
|
post<T>(url: string, body: AnyObject = {}, options: FetchOptions<'json'> = {}) {
|
||||||
return client<T>(path, { ...options, method: 'POST', body })
|
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
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user