You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+52-52
Original file line number
Diff line number
Diff line change
@@ -31,17 +31,17 @@ Aught to be stable but still in development while we iron out bugs.
31
31
32
32
This middleware receives [*Redux Standard API-calling Actions*](#redux-standard-api-calling-actions) (RSAAs) and dispatches [*Flux Standard Actions*](#flux-standard-actions) (FSAs) to the next middleware.
33
33
34
-
RSAAs are identified by the presence of a`[CALL_API]` property, where [`CALL_API`](#call_api) is a `Symbol` defined in, and exported by `redux-api-middleware`. They contain information describing an API call and three different types of FSAs, known as the *request*, *success* and *failure* FSAs.
34
+
RSAAs are identified by the presence of an`[RSAA]` property, where [`RSAA`](#rsaa) is a `String` constant defined in, and exported by `redux-api-middleware`. They contain information describing an API call and three different types of FSAs, known as the *request*, *success* and *failure* FSAs.
@@ -122,35 +122,35 @@ const store = configureStore(initialState);
122
122
123
123
### Defining the API call
124
124
125
-
The parameters of the API call are specified by root properties of the `[CALL_API]` property of an RSAA.
125
+
The parameters of the API call are specified by root properties of the `[RSAA]` property of an RSAA.
126
126
127
-
#### `[CALL_API].endpoint`
127
+
#### `[RSAA].endpoint`
128
128
129
129
The URL endpoint for the API call.
130
130
131
131
It is usually a string, be it a plain old one or an ES2015 template string. It may also be a function taking the state of your Redux store as its argument, and returning such a string.
132
132
133
-
#### `[CALL_API].method`
133
+
#### `[RSAA].method`
134
134
135
135
The HTTP method for the API call.
136
136
137
137
It must be one of the strings `GET`, `HEAD`, `POST`, `PUT`, `PATCH`, `DELETE` or `OPTIONS`, in any mixture of lowercase and uppercase letters.
138
138
139
-
#### `[CALL_API].body`
139
+
#### `[RSAA].body`
140
140
141
141
The body of the API call.
142
142
143
-
`redux-api-middleware` uses [`isomorphic-fetch`](https://github.com/matthew-andrews/isomorphic-fetch) to make the API call. `[CALL_API].body` should hence be a valid body according to the the [fetch specification](https://fetch.spec.whatwg.org). In most cases, this will be a JSON-encoded string or a [`FormData`](https://developer.mozilla.org/en/docs/Web/API/FormData) object.
143
+
`redux-api-middleware` uses [`isomorphic-fetch`](https://github.com/matthew-andrews/isomorphic-fetch) to make the API call. `[RSAA].body` should hence be a valid body according to the the [fetch specification](https://fetch.spec.whatwg.org). In most cases, this will be a JSON-encoded string or a [`FormData`](https://developer.mozilla.org/en/docs/Web/API/FormData) object.
@@ -160,7 +160,7 @@ It is usually an object, with the keys specifying the header names and the value
160
160
161
161
Itmayalsobeafunction taking the state of your Redux store as its argument, and returning an object of headers as above.
162
162
163
-
#### `[CALL_API].credentials`
163
+
#### `[RSAA].credentials`
164
164
165
165
Whether or not to send cookies with the API call.
166
166
@@ -174,48 +174,48 @@ It must be one of the following strings:
174
174
175
175
In some cases, the data you would like to fetch from the server may already be cached in you Redux store. Or you may decide that the current user does not have the necessary permissions to make some request.
176
176
177
-
You can tell `redux-api-middleware` to not make the API call through `[CALL_API].bailout`. If the value is `true`, the RSAA will die here, and no FSA will be passed on to the next middleware.
177
+
You can tell `redux-api-middleware` to not make the API call through `[RSAA].bailout`. If the value is `true`, the RSAA will die here, and no FSA will be passed on to the next middleware.
178
178
179
-
A more useful possibility is to give `[CALL_API].bailout` a function. At runtime, it will be passed the state of your Redux store as its only argument, if the return value of the function is `true`, the API call will not be made.
179
+
A more useful possibility is to give `[RSAA].bailout` a function. At runtime, it will be passed the state of your Redux store as its only argument, if the return value of the function is `true`, the API call will not be made.
180
180
181
181
### Lifecycle
182
182
183
-
The `[CALL_API].types` property controls the output of `redux-api-middleware`. The simplest form it can take is an array of length 3 consisting of string constants (orsymbols), as in our [example](#a-simple-example) above. This results in the default behavior we now describe.
183
+
The `[RSAA].types` property controls the output of `redux-api-middleware`. The simplest form it can take is an array of length 3 consisting of string constants (orsymbols), as in our [example](#a-simple-example) above. This results in the default behavior we now describe.
184
184
185
-
1. When `redux-api-middleware` receives an action, it first checks whether it has a `[CALL_API]` property. If it does not, it was clearly not intended for processing with `redux-api-middleware`, and so it is unceremoniously passed on to the next middleware.
185
+
1. When `redux-api-middleware` receives an action, it first checks whether it has an `[RSAA]` property. If it does not, it was clearly not intended for processing with `redux-api-middleware`, and so it is unceremoniously passed on to the next middleware.
186
186
187
187
2. It is now time to validate the action against the [RSAA definition](#redux-standard-api-calling-actions). If there are any validation errors, a *request* FSA will be dispatched (if at all possible) with the following properties:
188
-
- `type`: the string constant in the first position of the `[CALL_API].types` array;
188
+
- `type`: the string constant in the first position of the `[RSAA].types` array;
189
189
- `payload`: an [`InvalidRSAA`](#invalidrsaa) object containing a list of said validation errors;
190
190
- `error: true`.
191
191
192
192
`redux-api-middleware` will perform no further operations. In particular, no API call will be made, and the incoming RSAA will die here.
193
193
194
194
3. Now that `redux-api-middleware` is sure it has received a valid RSAA, it will try making the API call. If everything is alright, a *request* FSA will be dispatched with the following property:
195
-
- `type`: the string constant in the first position of the `[CALL_API].types` array.
195
+
- `type`: the string constant in the first position of the `[RSAA].types` array.
196
196
197
197
But errors may pop up at this stage, for several reasons:
198
-
- `redux-api-middleware` has to call those of `[CALL_API].bailout`, `[CALL_API].endpoint` and `[CALL_API].headers` that happen to be a function, which may throw an error;
199
-
- `isomorphic-fetch` may throw an error: the RSAA definition is not strong enough to preclude that from happening (you may, for example, send in a `[CALL_API].body` that is not valid according to the fetch specification — mind the SHOULDs in the [RSAA definition](#redux-standard-api-calling-actions));
198
+
- `redux-api-middleware` has to call those of `[RSAA].bailout`, `[RSAA].endpoint` and `[RSAA].headers` that happen to be a function, which may throw an error;
199
+
- `isomorphic-fetch` may throw an error: the RSAA definition is not strong enough to preclude that from happening (you may, for example, send in a `[RSAA].body` that is not valid according to the fetch specification — mind the SHOULDs in the [RSAA definition](#redux-standard-api-calling-actions));
200
200
- a network failure occurs (the network is unreachable, the server responds with an error,...).
201
201
202
202
If such an error occurs, a different *request* FSA will be dispatched (*instead* of the one described above). It will contain the following properties:
203
-
- `type`: the string constant in the first position of the `[CALL_API].types` array;
203
+
- `type`: the string constant in the first position of the `[RSAA].types` array;
204
204
- `payload`: a [`RequestError`](#requesterror) object containing an error message;
205
205
- `error: true`.
206
206
207
207
4. If `redux-api-middleware` receives a response from the server with a status code in the 200 range, a *success* FSA will be dispatched with the following properties:
208
-
- `type`: the string constant in the second position of the `[CALL_API].types` array;
208
+
- `type`: the string constant in the second position of the `[RSAA].types` array;
209
209
- `payload`: if the `Content-Type` header of the response is set to something JSONy (see [*Success* type descriptors](#success-type-descriptors) below), the parsed JSON response of the server, or undefined otherwise.
210
210
211
211
If the status code of the response falls outside that 200 range, a *failure* FSA will dispatched instead, with the following properties:
212
-
- `type`: the string constant in the third position of the `[CALL_API].types` array;
212
+
- `type`: the string constant in the third position of the `[RSAA].types` array;
213
213
- `payload`: an [`ApiError`](#apierror) object containing the message `` `${status} -${statusText}```;
214
214
- `error:true`.
215
215
216
216
### Customizing the dispatched FSAs
217
217
218
-
It is possible to customize the output of `redux-api-middleware` by replacing one or more of the string constants (or symbols) in `[CALL_API].types` by a type descriptor.
218
+
It is possible to customize the output of `redux-api-middleware` by replacing one or more of the string constants (or symbols) in `[RSAA].types` by a type descriptor.
219
219
220
220
A *type descriptor* is a plain JavaScript object that will be used as a blueprint for the dispatched FSAs. As such, type descriptors must have a `type` property, intended to house the string constant or symbol specifying the `type` of the resulting FSAs.
221
221
@@ -234,7 +234,7 @@ For example, if you want your *request* FSA to have the URL endpoint of the API
234
234
```js
235
235
// Input RSAA
236
236
{
237
-
[CALL_API]: {
237
+
[RSAA]: {
238
238
endpoint:'http://www.example.com/api/users',
239
239
method:'GET',
240
240
types: [
@@ -260,7 +260,7 @@ If you do not need access to the action itself or the state of your Redux store,
260
260
```js
261
261
// Input RSAA
262
262
{
263
-
[CALL_API]: {
263
+
[RSAA]: {
264
264
endpoint:'http://www.example.com/api/users',
265
265
method:'GET',
266
266
types: [
@@ -299,7 +299,7 @@ const userSchema = new Schema('users');
299
299
300
300
// Input RSAA
301
301
{
302
-
[CALL_API]: {
302
+
[RSAA]: {
303
303
endpoint:'http://www.example.com/api/users',
304
304
method:'GET',
305
305
types: [
@@ -360,7 +360,7 @@ For example, if you want the status code and status message of a unsuccessful AP
360
360
361
361
```js
362
362
{
363
-
[CALL_API]: {
363
+
[RSAA]: {
364
364
endpoint:'http://www.example.com/api/users/1',
365
365
method:'GET',
366
366
types: [
@@ -399,17 +399,17 @@ By default, *failure* FSAs will not contain a `meta` property, while their `payl
399
399
400
400
The following objects are exported by `redux-api-middleware`.
401
401
402
-
#### `CALL_API`
402
+
#### `RSAA`
403
403
404
-
A JavaScript `Symbol` whose presence as a key in an action signals that `redux-api-middleware` should process said action.
404
+
A JavaScript `String` whose presence as a key in an action signals that `redux-api-middleware` should process said action.
405
405
406
406
#### `apiMiddleware`
407
407
408
408
The Redux middleware itself.
409
409
410
410
#### `isRSAA(action)`
411
411
412
-
A function that returns `true` if `action` has a`[CALL_API]` property, and `false` otherwise.
412
+
A function that returns `true` if `action` has an`[RSAA]` property, and `false` otherwise.
413
413
414
414
#### `validateRSAA(action)`
415
415
@@ -511,65 +511,65 @@ The optional `meta` property MAY be any type of value. It is intended for any ex
511
511
### Redux Standard API-calling Actions
512
512
513
513
The definition of a *Redux Standard API-calling Action* below is the one used to validate RSAA actions. As explained in [Lifecycle](#lifecycle),
514
-
- actions without a`[CALL_API]` will be passed to the next middleware without any modifications;
515
-
- actions with a`[CALL_API]` property that fail validation will result in an error *request* FSA.
514
+
- actions without an`[RSAA]` property will be passed to the next middleware without any modifications;
515
+
- actions with an`[RSAA]` property that fail validation will result in an error *request* FSA.
516
516
517
517
A *Redux Standard API-calling Action* MUST
518
518
519
519
- be a plain JavaScript object,
520
-
- have a`[CALL_API]` property.
520
+
- have an`[RSAA]` property.
521
521
522
522
A *Redux Standard API-calling Action* MUST NOT
523
523
524
-
- include properties other than `[CALL_API]`.
524
+
- include properties other than `[RSAA]`.
525
525
526
-
#### `[CALL_API]`
526
+
#### `[RSAA]`
527
527
528
-
The `[CALL_API]` property MUST
528
+
The `[RSAA]` property MUST
529
529
530
530
- be a plain JavaScript Object,
531
531
- have an `endpoint` property,
532
532
- have a `method` property,
533
533
- have a `types` property.
534
534
535
-
The `[CALL_API]` property MAY
535
+
The `[RSAA]` property MAY
536
536
537
537
- have a `body` property,
538
538
- have a `headers` property,
539
539
- have a `credentials` property,
540
540
- have a `bailout` property.
541
541
542
-
The `[CALL_API]` property MUST NOT
542
+
The `[RSAA]` property MUST NOT
543
543
544
544
- include properties other than `endpoint`, `method`, `types`, `body`, `headers`, `credentials`, and `bailout`.
545
545
546
-
#### `[CALL_API].endpoint`
546
+
#### `[RSAA].endpoint`
547
547
548
-
The `[CALL_API].endpoint` property MUST be a string or a function. In the second case, the function SHOULD return a string.
548
+
The `[RSAA].endpoint` property MUST be a string or a function. In the second case, the function SHOULD return a string.
549
549
550
-
#### `[CALL_API].method`
550
+
#### `[RSAA].method`
551
551
552
-
The `[CALL_API].method` property MUST be one of the strings `GET`, `HEAD`, `POST`, `PUT`, `PATCH`, `DELETE` or `OPTIONS`, in any mixture of lowercase and uppercase letters.
552
+
The `[RSAA].method` property MUST be one of the strings `GET`, `HEAD`, `POST`, `PUT`, `PATCH`, `DELETE` or `OPTIONS`, in any mixture of lowercase and uppercase letters.
553
553
554
-
#### `[CALL_API].body`
554
+
#### `[RSAA].body`
555
555
556
-
The optional `[CALL_API].body` property SHOULD be a valid body according to the the [fetch specification](https://fetch.spec.whatwg.org).
556
+
The optional `[RSAA].body` property SHOULD be a valid body according to the the [fetch specification](https://fetch.spec.whatwg.org).
557
557
558
-
#### `[CALL_API].headers`
558
+
#### `[RSAA].headers`
559
559
560
-
The optional `[CALL_API].headers` property MUST be a plain JavaScript object or a function. In the second case, the function SHOULD return a plain JavaScript object.
560
+
The optional `[RSAA].headers` property MUST be a plain JavaScript object or a function. In the second case, the function SHOULD return a plain JavaScript object.
561
561
562
-
#### `[CALL_API].credentials`
562
+
#### `[RSAA].credentials`
563
563
564
-
The optional `[CALL_API].credentials` property MUST be one of the strings `omit`, `same-origin` or `include`.
564
+
The optional `[RSAA].credentials` property MUST be one of the strings `omit`, `same-origin` or `include`.
565
565
566
-
#### `[CALL_API].bailout`
566
+
#### `[RSAA].bailout`
567
567
568
-
The optional `[CALL_API].bailout` property MUST be a boolean or a function.
568
+
The optional `[RSAA].bailout` property MUST be a boolean or a function.
569
569
570
-
#### `[CALL_API].types`
570
+
#### `[RSAA].types`
571
571
572
-
The `[CALL_API].types` property MUST be an array of length 3. Each element of the array MUST be a string, a `Symbol`, or a type descriptor.
572
+
The `[RSAA].types` property MUST be an array of length 3. Each element of the array MUST be a string, a `Symbol`, or a type descriptor.
0 commit comments