forked from infinitered/apisauce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapisauce.d.ts
116 lines (98 loc) · 3.92 KB
/
apisauce.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import {AxiosInstance, AxiosRequestConfig, AxiosError, CancelTokenStatic} from 'axios';
export type HEADERS = { [key: string]: string };
export const DEFAULT_HEADERS: {
Accept: 'application/json',
'Content-Type': 'application/json'
}
export const NONE: null
export const CLIENT_ERROR: 'CLIENT_ERROR'
export const SERVER_ERROR: 'SERVER_ERROR'
export const TIMEOUT_ERROR: 'TIMEOUT_ERROR'
export const CONNECTION_ERROR: 'CONNECTION_ERROR'
export const NETWORK_ERROR: 'NETWORK_ERROR'
export const UNKNOWN_ERROR: 'UNKNOWN_ERROR'
export const CANCEL_ERROR: 'CANCEL_ERROR'
export type PROBLEM_CODE =
'CLIENT_ERROR' |
'SERVER_ERROR' |
'TIMEOUT_ERROR' |
'CONNECTION_ERROR' |
'NETWORK_ERROR' |
'UNKNOWN_ERROR' |
'CANCEL_ERROR';
export interface ApisauceConfig extends AxiosRequestConfig {
baseURL: string | undefined;
}
/**
* Creates a instance of our API using the configuration
* @param config a configuration object which must have a non-empty 'baseURL' property.
*/
export function create(config: ApisauceConfig): ApisauceInstance;
export interface ApiErrorResponse<T> {
ok: false;
problem: PROBLEM_CODE;
originalError: AxiosError;
data?: T;
status?: number;
headers?: {};
config?: AxiosRequestConfig;
duration?: number;
}
export interface ApiOkResponse<T> {
ok: true;
problem: null;
originalError: null;
data?: T;
status?: number;
headers?: {};
config?: AxiosRequestConfig;
duration?: number;
}
export type ApiResponse<T, U = T> = ApiErrorResponse<U> | ApiOkResponse<T>;
export type Monitor = (response: ApiResponse<any>) => void;
export type RequestTransform = (request: AxiosRequestConfig) => void;
export type AsyncRequestTransform = (request: AxiosRequestConfig) => (Promise<void> | ((request: AxiosRequestConfig) => Promise<void>));
export type ResponseTransform = (response: ApiResponse<any>) => void;
export interface ApisauceInstance {
axiosInstance: AxiosInstance;
monitors: Monitor;
addMonitor: (monitor: Monitor) => void;
requestTransforms: RequestTransform[];
asyncRequestTransforms: AsyncRequestTransform[];
responseTransforms: ResponseTransform[];
addRequestTransform: (transform: RequestTransform) => void;
addAsyncRequestTransform: (transform: AsyncRequestTransform) => void;
addResponseTransform: (transform: ResponseTransform) => void;
headers: HEADERS;
setHeader: (key: string, value: string) => AxiosInstance;
setHeaders: (headers: HEADERS) => AxiosInstance;
deleteHeader: (name: string) => AxiosInstance;
/** Sets a new base URL */
setBaseURL: (baseUrl: string) => AxiosInstance;
/** Gets the current base URL used by axios */
getBaseURL: () => string;
get: <T>(url: string, params?: {}, axiosConfig?: AxiosRequestConfig) => Promise<ApiResponse<T>>;
delete: <T>(url: string, params?: {}, axiosConfig?: AxiosRequestConfig) => Promise<ApiResponse<T>>;
head: <T>(url: string, params?: {}, axiosConfig?: AxiosRequestConfig) => Promise<ApiResponse<T>>;
post: <T>(url: string, data?: any, axiosConfig?: AxiosRequestConfig) => Promise<ApiResponse<T>>;
put: <T>(url: string, data?: any, axiosConfig?: AxiosRequestConfig) => Promise<ApiResponse<T>>;
patch: <T>(url: string, data?: any, axiosConfig?: AxiosRequestConfig) => Promise<ApiResponse<T>>;
link: <T>(url: string, params?: {}, axiosConfig?: AxiosRequestConfig) => Promise<ApiResponse<T>>;
unlink: <T>(url: string, params?: {}, axiosConfig?: AxiosRequestConfig) => Promise<ApiResponse<T>>;
}
export function isCancel(value: any): boolean;
export const CancelToken: CancelTokenStatic;
declare const _default: {
DEFAULT_HEADERS: typeof DEFAULT_HEADERS;
NONE: typeof NONE;
CLIENT_ERROR: typeof CLIENT_ERROR;
SERVER_ERROR: typeof SERVER_ERROR;
TIMEOUT_ERROR: typeof TIMEOUT_ERROR;
CONNECTION_ERROR: typeof CONNECTION_ERROR;
NETWORK_ERROR: typeof NETWORK_ERROR;
UNKNOWN_ERROR: typeof UNKNOWN_ERROR;
create: typeof create;
isCancel: typeof isCancel;
CancelToken: typeof CancelToken;
}
export default _default;