| 1 |
import React, { useMemo } from 'react' |
| 2 |
import axios from 'axios' |
| 3 |
import { createContextHook } from '../utils/bootstrap' |
| 4 |
import { REST_API_AXIOS_CONFIG } from '../utils/restAPI' |
| 5 |
import type { PropsWithChildren } from 'react' |
| 6 |
import type { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios' |
| 7 |
|
| 8 |
export interface RestAPIContext { |
| 9 |
api: RestAPI |
| 10 |
axiosInstance: AxiosInstance |
| 11 |
} |
| 12 |
|
| 13 |
export interface RestAPI { |
| 14 |
get: <T>(url: string) => Promise<T> |
| 15 |
getResponse: <T>(url: string) => Promise<AxiosResponse<T>> |
| 16 |
post: <T, D = never>(url: string, data?: D, config?: AxiosRequestConfig<D>) => Promise<T> |
| 17 |
put: <T>(url: string, data?: object) => Promise<T> |
| 18 |
del: <T>(url: string) => Promise<T> |
| 19 |
} |
| 20 |
|
| 21 |
const debugRequest = async <T, D = never>( |
| 22 |
method: 'GET' | 'POST' | 'PUT' | 'DELETE', |
| 23 |
url: string, |
| 24 |
doRequest: Promise<AxiosResponse<T, D>>, |
| 25 |
data?: D |
| 26 |
): Promise<AxiosResponse<T>> => { |
| 27 |
if (window.CODE_SNIPPETS?.debug) { |
| 28 |
console.debug(`${method} ${url}`, ...data ? [data] : []) |
| 29 |
const response = await doRequest |
| 30 |
console.debug('Response', response) |
| 31 |
return response |
| 32 |
} else { |
| 33 |
return await doRequest |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
const buildRestAPI = (axiosInstance: AxiosInstance): RestAPI => ({ |
| 38 |
getResponse: <T, >(url: string): Promise<AxiosResponse<T>> => |
| 39 |
debugRequest('GET', url, axiosInstance.get<T, AxiosResponse<T, never>, never>(url)), |
| 40 |
|
| 41 |
get: <T, >(url: string): Promise<T> => |
| 42 |
debugRequest('GET', url, axiosInstance.get<T, AxiosResponse<T, never>, never>(url)) |
| 43 |
.then(response => response.data), |
| 44 |
|
| 45 |
post: <T, D = never>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<T> => |
| 46 |
debugRequest('POST', url, axiosInstance.post<T, AxiosResponse<T>>(url, data, config), data) |
| 47 |
.then(response => response.data), |
| 48 |
|
| 49 |
del: <T, >(url: string): Promise<T> => |
| 50 |
debugRequest('DELETE', url, axiosInstance.delete<T, AxiosResponse<T, never>, never>(url)) |
| 51 |
.then(response => response.data), |
| 52 |
|
| 53 |
put: <T, >(url: string, data?: object): Promise<T> => |
| 54 |
debugRequest('PUT', url, axiosInstance.put<T, AxiosResponse<T>>(url, data), data) |
| 55 |
.then(response => response.data), |
| 56 |
}) |
| 57 |
|
| 58 |
const [Context, useRestAPI] = createContextHook<RestAPIContext>('useRestAPI') |
| 59 |
|
| 60 |
export const WithRestAPIContext: React.FC<PropsWithChildren> = ({ children }) => { |
| 61 |
const axiosInstance = useMemo(() => axios.create(REST_API_AXIOS_CONFIG), []) |
| 62 |
|
| 63 |
const api = useMemo(() => buildRestAPI(axiosInstance), [axiosInstance]) |
| 64 |
const value: RestAPIContext = { api, axiosInstance } |
| 65 |
|
| 66 |
return <Context.Provider value={value}>{children}</Context.Provider> |
| 67 |
} |
| 68 |
|
| 69 |
export { useRestAPI } |
| 70 |
|