PluginProbe
Code Snippets / 3.5.0
Code Snippets v3.5.0
3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 3.0.1 All 64 releases
code-snippets / js / utils / api / axios.ts

axios.ts in Code Snippets 3.5.0, at js/utils/api/axios.ts

41 lines 1.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { useMemo } from 'react'
2 import axios, { AxiosInstance, AxiosResponse, CreateAxiosDefaults } from 'axios'
3
4 export interface AxiosAPI {
5 get: <T>(url: string) => Promise<AxiosResponse<T, never>>
6 post: <T, D>(url: string, data?: D) => Promise<AxiosResponse<T, D>>
7 del: <T>(url: string) => Promise<AxiosResponse<T, never>>
8 axiosInstance: AxiosInstance
9 }
10
11 const debugResponseHandler = <T>(response: T) => {
12 console.debug('Response', response)
13 return response
14 }
15
16 export const useAxios = (defaultConfig: CreateAxiosDefaults): AxiosAPI => {
17 const axiosInstance = useMemo(() => axios.create(defaultConfig), [defaultConfig])
18
19 return useMemo((): AxiosAPI => ({
20 get: <T>(url: string) => {
21 console.debug(`GET ${url}`)
22 return axiosInstance.get<T, AxiosResponse<T, never>, never>(url)
23 .then(debugResponseHandler)
24 },
25
26 post: <T, D>(url: string, data?: D) => {
27 console.debug(`POST ${url}`, data)
28 return axiosInstance.post<T, AxiosResponse<T, D>, D>(url, data)
29 .then(debugResponseHandler)
30 },
31
32 del: <T>(url: string) => {
33 console.debug(`DELETE ${url}`)
34 return axiosInstance.delete<T, AxiosResponse<T, never>, never>(url)
35 .then(debugResponseHandler)
36 },
37
38 axiosInstance
39 }), [axiosInstance])
40 }
41