UNPKG

4.42 kBTypeScriptView Raw
1import { AxiosInstance, AxiosRequestConfig, AxiosError, CancelTokenStatic } from 'axios'
2
3export type HEADERS = { [key: string]: string }
4export const DEFAULT_HEADERS: {
5 Accept: 'application/json'
6 'Content-Type': 'application/json'
7}
8
9export const NONE: null
10export const CLIENT_ERROR: 'CLIENT_ERROR'
11export const SERVER_ERROR: 'SERVER_ERROR'
12export const TIMEOUT_ERROR: 'TIMEOUT_ERROR'
13export const CONNECTION_ERROR: 'CONNECTION_ERROR'
14export const NETWORK_ERROR: 'NETWORK_ERROR'
15export const UNKNOWN_ERROR: 'UNKNOWN_ERROR'
16export const CANCEL_ERROR: 'CANCEL_ERROR'
17export 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
26export interface ApisauceConfig extends AxiosRequestConfig {
27 baseURL: string | undefined
28 axiosInstance?: AxiosInstance
29}
30
31/**
32 * Creates a instance of our API using the configuration
33 * @param config a configuration object which must have a non-empty 'baseURL' property.
34 */
35export function create(config: ApisauceConfig): ApisauceInstance
36
37export 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}
48export 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}
59export type ApiResponse<T, U = T> = ApiErrorResponse<U> | ApiOkResponse<T>
60
61export type Monitor = (response: ApiResponse<any>) => void
62export type RequestTransform = (request: AxiosRequestConfig) => void
63export type AsyncRequestTransform = (
64 request: AxiosRequestConfig,
65) => Promise<void> | ((request: AxiosRequestConfig) => Promise<void>)
66export type ResponseTransform = (response: ApiResponse<any>) => void
67export type AsyncResponseTransform = (
68 response: ApiResponse<any>,
69) => Promise<void> | ((response: ApiResponse<any>) => Promise<void>)
70
71export 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 /** Sets a new base URL */
92 setBaseURL: (baseUrl: string) => AxiosInstance
93 /** Gets the current base URL used by axios */
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
107export function isCancel(value: any): boolean
108
109export const CancelToken: CancelTokenStatic
110
111declare 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
125export default _default