1
|
|
|
// Type definitions for redux-api-middleware 2.3 |
2
|
|
|
// Project: https://github.com/agraboso/redux-api-middleware |
3
|
|
|
// Definitions by: Mosen <https://github.com/mosen> |
4
|
|
|
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped |
5
|
|
|
|
6
|
|
|
declare module "redux-api-middleware" { |
7
|
|
|
import { Middleware } from "redux"; |
8
|
|
|
|
9
|
|
|
export interface StandardAction<T extends string, P, M = {}> { |
10
|
|
|
type: T; |
11
|
|
|
payload: P; |
12
|
|
|
error?: boolean; |
13
|
|
|
meta?: M; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
export interface ErrorAction<T extends string, P, M = {}> |
17
|
|
|
extends StandardAction<T, P, M> { |
18
|
|
|
error: true; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
//declare module "redux-api-middleware"; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Symbol key that carries API call info interpreted by this Redux middleware. |
25
|
|
|
* |
26
|
|
|
* @constant {string} |
27
|
|
|
* @access public |
28
|
|
|
* @default |
29
|
|
|
*/ |
30
|
|
|
export const RSAA: string; |
31
|
|
|
|
32
|
|
|
//// ERRORS |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Error class for an RSAA that does not conform to the RSAA definition |
36
|
|
|
* |
37
|
|
|
* @class InvalidRSAA |
38
|
|
|
* @access public |
39
|
|
|
* @param {array} validationErrors - an array of validation errors |
40
|
|
|
*/ |
41
|
|
|
export class InvalidRSAA { |
42
|
|
|
constructor(validationErrors: Array<string>); |
43
|
|
|
|
44
|
|
|
name: string; |
45
|
|
|
message: string; |
46
|
|
|
validationErrors: Array<string>; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Error class for a custom `payload` or `meta` function throwing |
51
|
|
|
* |
52
|
|
|
* @class InternalError |
53
|
|
|
* @access public |
54
|
|
|
* @param {string} message - the error message |
55
|
|
|
*/ |
56
|
|
|
export class InternalError { |
57
|
|
|
constructor(message: string); |
58
|
|
|
|
59
|
|
|
name: string; |
60
|
|
|
message: string; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Error class for an error raised trying to make an API call |
65
|
|
|
* |
66
|
|
|
* @class RequestError |
67
|
|
|
* @access public |
68
|
|
|
* @param {string} message - the error message |
69
|
|
|
*/ |
70
|
|
|
export class RequestError { |
71
|
|
|
constructor(message: string); |
72
|
|
|
|
73
|
|
|
name: string; |
74
|
|
|
message: string; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Error class for an API response outside the 200 range |
79
|
|
|
* |
80
|
|
|
* @class ApiError |
81
|
|
|
* @access public |
82
|
|
|
* @param {number} status - the status code of the API response |
83
|
|
|
* @param {string} statusText - the status text of the API response |
84
|
|
|
* @param {object} response - the parsed JSON response of the API server if the |
85
|
|
|
* 'Content-Type' header signals a JSON response |
86
|
|
|
*/ |
87
|
|
|
export class ApiError { |
88
|
|
|
constructor(status: number, statusText: string, response: any); |
89
|
|
|
|
90
|
|
|
name: string; |
91
|
|
|
message: string; |
92
|
|
|
status: number; |
93
|
|
|
statusText: string; |
94
|
|
|
response?: any; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
//// VALIDATION |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Is the given action a plain JavaScript object with a [RSAA] property? |
101
|
|
|
*/ |
102
|
|
|
export function isRSAA( |
103
|
|
|
action: object, |
104
|
|
|
): action is RSAAction<any, any, any, any, any, any>; |
105
|
|
|
|
106
|
|
|
export interface TypeDescriptor< |
107
|
|
|
TType extends string, |
108
|
|
|
TPayload, |
109
|
|
|
TMeta, |
110
|
|
|
TState |
111
|
|
|
> { |
112
|
|
|
type: TType; |
113
|
|
|
payload?: |
114
|
|
|
| TPayload |
115
|
|
|
| (( |
116
|
|
|
action: StandardAction<any, any, any>, |
117
|
|
|
state: TState, |
118
|
|
|
response: Response, |
119
|
|
|
) => TPayload); |
120
|
|
|
meta?: |
121
|
|
|
| TMeta |
122
|
|
|
| (( |
123
|
|
|
action: StandardAction<any, any, any>, |
124
|
|
|
state: TState, |
125
|
|
|
response: Response, |
126
|
|
|
) => TMeta); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Is the given object a valid type descriptor? |
131
|
|
|
*/ |
132
|
|
|
export function isValidTypeDescriptor( |
133
|
|
|
obj: object, |
134
|
|
|
): obj is TypeDescriptor<any, any, any, any>; |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Checks an action against the RSAA definition, returning a (possibly empty) |
138
|
|
|
* array of validation errors. |
139
|
|
|
*/ |
140
|
|
|
function validateRSAA(action: object): Array<string>; |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Is the given action a valid RSAA? |
144
|
|
|
*/ |
145
|
|
|
function isValidRSAA(action: object): boolean; |
146
|
|
|
|
147
|
|
|
//// MIDDLEWARE |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* A Redux middleware that processes RSAA actions. |
151
|
|
|
*/ |
152
|
|
|
export const apiMiddleware: Middleware; |
153
|
|
|
|
154
|
|
|
//// UTIL |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Extract JSON body from a server response |
158
|
|
|
*/ |
159
|
|
|
export function getJSON(res: Response): PromiseLike<any>; |
160
|
|
|
|
161
|
|
|
export type RSAActionTypeTuple = [ |
162
|
|
|
string | symbol, |
163
|
|
|
string | symbol, |
164
|
|
|
string | symbol, |
165
|
|
|
]; |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Blow up string or symbol types into full-fledged type descriptors, |
169
|
|
|
* and add defaults |
170
|
|
|
*/ |
171
|
|
|
export function normalizeTypeDescriptors( |
172
|
|
|
types: RSAActionTypeTuple, |
173
|
|
|
): RSAActionTypeTuple; |
174
|
|
|
|
175
|
|
|
export type HTTPVerb = |
176
|
|
|
| "GET" |
177
|
|
|
| "HEAD" |
178
|
|
|
| "POST" |
179
|
|
|
| "PUT" |
180
|
|
|
| "PATCH" |
181
|
|
|
| "DELETE" |
182
|
|
|
| "OPTIONS"; |
183
|
|
|
|
184
|
|
|
export interface RSAAction< |
185
|
|
|
TStartedType extends string, |
186
|
|
|
TSuccessType extends string, |
187
|
|
|
TFailedType extends string, |
188
|
|
|
TSuccessPayload = any, |
189
|
|
|
TFailPayload = Error, |
190
|
|
|
TStartedPayload = {}, |
191
|
|
|
TSuccessMeta = {}, |
192
|
|
|
TFailMeta = {}, |
193
|
|
|
TStartedMeta = {}, |
194
|
|
|
TState = any |
195
|
|
|
> { |
196
|
|
|
[propName: string]: { |
197
|
|
|
/** |
198
|
|
|
* Endpoint to hit, or a function to call that results in an endpoint to hit. |
199
|
|
|
*/ |
200
|
|
|
endpoint: string | ((state: TState) => string); |
201
|
|
|
method: "GET" | "HEAD" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS"; |
202
|
|
|
headers?: object | ((state: TState) => object); |
203
|
|
|
body?: object | string | ((state: TState) => object | string); |
204
|
|
|
credentials?: "omit" | "same-origin" | "include"; |
205
|
|
|
/** |
206
|
|
|
* object or function producing an object of options to pass to the Fetch API. |
207
|
|
|
* See: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch |
208
|
|
|
* for the list of available options. |
209
|
|
|
*/ |
210
|
|
|
options?: object | ((state: TState) => object); |
211
|
|
|
/** |
212
|
|
|
* Function/bool for whether or not to bail on a REST request. |
213
|
|
|
* True = do not perform network request. |
214
|
|
|
*/ |
215
|
|
|
bailout?: boolean | ((state: TState) => boolean); |
216
|
|
|
/** |
217
|
|
|
* Length 3 array of types to assign to the API request's resulting Flux Standard Action's type property for various |
218
|
|
|
* circumstances: |
219
|
|
|
* index 0: REQUEST - Request has been made (yes, two FSAs result from your request action) |
220
|
|
|
* index 1: RECEIVE - Response has been received |
221
|
|
|
* index 2: FAILURE - Response was a failure |
222
|
|
|
* TypeDescriptor objects will have their meta/payload properties merged into the resulting Flux Standard Actions |
223
|
|
|
*/ |
224
|
|
|
types: [ |
225
|
|
|
|
226
|
|
|
| TStartedType |
227
|
|
|
| TypeDescriptor<TStartedType, TStartedPayload, TStartedMeta, TState>, |
228
|
|
|
|
229
|
|
|
|
230
|
|
|
| TSuccessType |
231
|
|
|
| TypeDescriptor<TSuccessType, TSuccessPayload, TSuccessMeta, TState>, |
232
|
|
|
|
233
|
|
|
|
234
|
|
|
| TFailedType |
235
|
|
|
| TypeDescriptor<TFailedType, TFailPayload, TFailMeta, TState>, |
236
|
|
|
]; |
237
|
|
|
fetch?: typeof fetch; |
238
|
|
|
}; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
module "redux" { |
242
|
|
|
export interface Dispatch<S> { |
243
|
|
|
< |
244
|
|
|
TStartedType extends string, |
245
|
|
|
TSuccessType extends string, |
246
|
|
|
TFailedType extends string, |
247
|
|
|
TSuccessPayload, |
248
|
|
|
TFailPayload, |
249
|
|
|
TStartedPayload, |
250
|
|
|
TSuccessMeta, |
251
|
|
|
TFailMeta, |
252
|
|
|
TStartedMeta |
253
|
|
|
>( |
254
|
|
|
rsaa: RSAAction< |
255
|
|
|
TStartedType, |
256
|
|
|
TSuccessType, |
257
|
|
|
TFailedType, |
258
|
|
|
TSuccessPayload, |
259
|
|
|
TFailPayload, |
260
|
|
|
TStartedPayload, |
261
|
|
|
TSuccessMeta, |
262
|
|
|
TFailMeta, |
263
|
|
|
TStartedMeta, |
264
|
|
|
S |
265
|
|
|
>, |
266
|
|
|
): Promise< |
267
|
|
|
| StandardAction<TSuccessType, TSuccessPayload, TSuccessMeta> |
268
|
|
|
| ErrorAction<TFailedType, TFailPayload, TFailMeta> |
269
|
|
|
>; |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|