1 | import { AxiosInstance, AxiosRequestConfig, AxiosError, CancelTokenStatic } from 'axios'
|
2 |
|
3 | export type HEADERS = { [key: string]: string }
|
4 | export const DEFAULT_HEADERS: {
|
5 | Accept: 'application/json'
|
6 | 'Content-Type': 'application/json'
|
7 | }
|
8 |
|
9 | export const NONE: null
|
10 | export const CLIENT_ERROR: 'CLIENT_ERROR'
|
11 | export const SERVER_ERROR: 'SERVER_ERROR'
|
12 | export const TIMEOUT_ERROR: 'TIMEOUT_ERROR'
|
13 | export const CONNECTION_ERROR: 'CONNECTION_ERROR'
|
14 | export const NETWORK_ERROR: 'NETWORK_ERROR'
|
15 | export const UNKNOWN_ERROR: 'UNKNOWN_ERROR'
|
16 | export const CANCEL_ERROR: 'CANCEL_ERROR'
|
17 | export type PROBLEM_CODE =
|
18 | | 'CLIENT_ERROR'
|
19 | | 'SERVER_ERROR'
|
20 | | 'TIMEOUT_ERROR'
|
21 | | 'CONNECTION_ERROR'
|
22 | | 'NETWORK_ERROR'
|
23 | | 'UNKNOWN_ERROR'
|
24 | | 'CANCEL_ERROR'
|
25 |
|
26 | export interface ApisauceConfig extends AxiosRequestConfig {
|
27 | baseURL: string | undefined
|
28 | axiosInstance?: AxiosInstance
|
29 | }
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | export function create(config: ApisauceConfig): ApisauceInstance
|
36 |
|
37 | export interface ApiErrorResponse<T> {
|
38 | ok: false
|
39 | problem: PROBLEM_CODE
|
40 | originalError: AxiosError
|
41 |
|
42 | data?: T
|
43 | status?: number
|
44 | headers?: HEADERS
|
45 | config?: AxiosRequestConfig
|
46 | duration?: number
|
47 | }
|
48 | export interface ApiOkResponse<T> {
|
49 | ok: true
|
50 | problem: null
|
51 | originalError: null
|
52 |
|
53 | data?: T
|
54 | status?: number
|
55 | headers?: HEADERS
|
56 | config?: AxiosRequestConfig
|
57 | duration?: number
|
58 | }
|
59 | export type ApiResponse<T, U = T> = ApiErrorResponse<U> | ApiOkResponse<T>
|
60 |
|
61 | export type Monitor = (response: ApiResponse<any>) => void
|
62 | export type RequestTransform = (request: AxiosRequestConfig) => void
|
63 | export type AsyncRequestTransform = (
|
64 | request: AxiosRequestConfig,
|
65 | ) => Promise<void> | ((request: AxiosRequestConfig) => Promise<void>)
|
66 | export type ResponseTransform = (response: ApiResponse<any>) => void
|
67 | export type AsyncResponseTransform = (
|
68 | response: ApiResponse<any>,
|
69 | ) => Promise<void> | ((response: ApiResponse<any>) => Promise<void>)
|
70 |
|
71 | export interface ApisauceInstance {
|
72 | axiosInstance: AxiosInstance
|
73 |
|
74 | monitors: Monitor
|
75 | addMonitor: (monitor: Monitor) => void
|
76 |
|
77 | requestTransforms: RequestTransform[]
|
78 | asyncRequestTransforms: AsyncRequestTransform[]
|
79 | responseTransforms: ResponseTransform[]
|
80 | asyncResponseTransforms: AsyncResponseTransform[]
|
81 | addRequestTransform: (transform: RequestTransform) => void
|
82 | addAsyncRequestTransform: (transform: AsyncRequestTransform) => void
|
83 | addResponseTransform: (transform: ResponseTransform) => void
|
84 | addAsyncResponseTransform: (transform: AsyncResponseTransform) => void
|
85 |
|
86 | headers: HEADERS
|
87 | setHeader: (key: string, value: string) => AxiosInstance
|
88 | setHeaders: (headers: HEADERS) => AxiosInstance
|
89 | deleteHeader: (name: string) => AxiosInstance
|
90 |
|
91 |
|
92 | setBaseURL: (baseUrl: string) => AxiosInstance
|
93 |
|
94 | getBaseURL: () => string
|
95 |
|
96 | any: <T, U = T>(config: AxiosRequestConfig) => Promise<ApiResponse<T, U>>
|
97 | get: <T, U = T>(url: string, params?: {}, axiosConfig?: AxiosRequestConfig) => Promise<ApiResponse<T, U>>
|
98 | delete: <T, U = T>(url: string, params?: {}, axiosConfig?: AxiosRequestConfig) => Promise<ApiResponse<T, U>>
|
99 | head: <T, U = T>(url: string, params?: {}, axiosConfig?: AxiosRequestConfig) => Promise<ApiResponse<T, U>>
|
100 | post: <T, U = T>(url: string, data?: any, axiosConfig?: AxiosRequestConfig) => Promise<ApiResponse<T, U>>
|
101 | put: <T, U = T>(url: string, data?: any, axiosConfig?: AxiosRequestConfig) => Promise<ApiResponse<T, U>>
|
102 | patch: <T, U = T>(url: string, data?: any, axiosConfig?: AxiosRequestConfig) => Promise<ApiResponse<T, U>>
|
103 | link: <T, U = T>(url: string, params?: {}, axiosConfig?: AxiosRequestConfig) => Promise<ApiResponse<T, U>>
|
104 | unlink: <T, U = T>(url: string, params?: {}, axiosConfig?: AxiosRequestConfig) => Promise<ApiResponse<T, U>>
|
105 | }
|
106 |
|
107 | export function isCancel(value: any): boolean
|
108 |
|
109 | export const CancelToken: CancelTokenStatic
|
110 |
|
111 | declare const _default: {
|
112 | DEFAULT_HEADERS: typeof DEFAULT_HEADERS
|
113 | NONE: typeof NONE
|
114 | CLIENT_ERROR: typeof CLIENT_ERROR
|
115 | SERVER_ERROR: typeof SERVER_ERROR
|
116 | TIMEOUT_ERROR: typeof TIMEOUT_ERROR
|
117 | CONNECTION_ERROR: typeof CONNECTION_ERROR
|
118 | NETWORK_ERROR: typeof NETWORK_ERROR
|
119 | UNKNOWN_ERROR: typeof UNKNOWN_ERROR
|
120 | create: typeof create
|
121 | isCancel: typeof isCancel
|
122 | CancelToken: typeof CancelToken
|
123 | }
|
124 |
|
125 | export default _default
|