-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.d.ts
176 lines (144 loc) · 3.65 KB
/
index.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { AxiosRequestConfig, AxiosInstance, CancelTokenSource } from "axios";
export interface ApiModuleConfig {
metadatas: ApiMetadataMapper | { [namespace: string]: ApiMetadataMapper };
module?: Boolean;
console?: Boolean;
baseConfig?: AxiosRequestConfig;
}
/**
* Fore-request middleware
*/
export type ForeRequestHook = (
context: Context,
next: (error?: any) => null
) => void;
/**
* Post-request middleware
*/
export type PostRequestHook = (
context: Context,
next: (error?: any) => null
) => void;
/**
* fallback middleware
*/
export type FallbackHook = (
context: Context,
next: (error?: any) => null
) => void;
export interface ApiMetadataMapper {
[metadataName: string]: ApiMetadata;
}
export interface ApiMetadata {
method: "get" | "post" | "patch" | "delete" | "put" | "head";
url: string;
[field: string]: any
}
export interface TransformedRequestData {
query?: Object;
params?: Object;
body?: Object;
}
export type TransformedRequest = (
data?: TransformedRequestData,
opt?: AxiosRequestConfig
) => Promise<any>;
export interface TransformedRequestMapper {
[requestName: string]: TransformedRequest;
}
export interface ApiModuleOptions {
axios: AxiosInstance;
metadatas: ApiMetadataMapper | { [namespace: string]: ApiMetadataMapper };
module: boolean;
console: boolean;
baseConfig: AxiosRequestConfig;
}
export declare class ApiModule {
constructor(config: ApiModuleConfig);
options: ApiModuleOptions;
/**
* Register fore-request middleWare globally (for all instances)
*/
static globalBefore(foreRequestHook: ForeRequestHook): void;
/**
* Register post-request middleware globally (for all instances)
*/
static globalAfter(postRequestHook: PostRequestHook): void;
/**
* Register fallback middleware globally (for all instances)
*/
static globalCatch(fallbackHook: FallbackHook): void;
/**
* Registe fore-request middleware
*/
useBefore(foreRequestHook: ForeRequestHook): void;
/**
* Registe post-request middleware
*/
useAfter(postRequestHook: PostRequestHook): void;
/**
* Registe fallback-request middleware
*/
useCatch(fallbackHook: FallbackHook): void;
/**
* Get the instance of api metadatas mapper
*/
getInstance():
{
$module: ApiModule;
[requestName: string]: TransformedRequest;
}
|
{
$module: ApiModule;
[namespace: string]: TransformedRequestMapper
};
/**
* Get the instance of Axios
*/
getAxios(): AxiosInstance;
/**
* Get axios cancellation source
*/
generateCancellationSource(): CancelTokenSource;
}
export declare class Context {
/**
* Set request data
*/
setData(data: TransformedRequestData): Context;
/**
* Set response data
*/
setResponse(response: any): Context;
/**
* Set request error or response error data
*/
setError(error: string | Error): Context;
/**
* Set axios request config
*/
setAxiosOptions(options: AxiosRequestConfig): Context;
readonly metadata: ApiMetadata;
readonly metadataKeys: string[];
readonly method: string;
readonly baseURL: string;
/**
* Parsed url
*/
readonly url: string;
/**
* Request data
*/
readonly data: TransformedRequestData;
/**
* Response data
*/
readonly response: any;
/**
* Response error
*/
readonly responseError: any;
readonly axiosOptions: AxiosRequestConfig | object;
}
export default ApiModule;