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-52Lines changed: 52 additions & 52 deletions
Original file line number
Diff line number
Diff line change
@@ -28,17 +28,17 @@ redux-api-middleware
28
28
29
29
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.
30
30
31
-
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.
31
+
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.
@@ -119,35 +119,35 @@ const store = configureStore(initialState);
119
119
120
120
### Defining the API call
121
121
122
-
The parameters of the API call are specified by root properties of the `[CALL_API]` property of an RSAA.
122
+
The parameters of the API call are specified by root properties of the `[RSAA]` property of an RSAA.
123
123
124
-
#### `[CALL_API].endpoint`
124
+
#### `[RSAA].endpoint`
125
125
126
126
The URL endpoint for the API call.
127
127
128
128
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.
129
129
130
-
#### `[CALL_API].method`
130
+
#### `[RSAA].method`
131
131
132
132
The HTTP method for the API call.
133
133
134
134
It must be one of the strings `GET`, `HEAD`, `POST`, `PUT`, `PATCH`, `DELETE` or `OPTIONS`, in any mixture of lowercase and uppercase letters.
135
135
136
-
#### `[CALL_API].body`
136
+
#### `[RSAA].body`
137
137
138
138
The body of the API call.
139
139
140
-
`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.
140
+
`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.
@@ -157,7 +157,7 @@ It is usually an object, with the keys specifying the header names and the value
157
157
158
158
Itmayalsobeafunction taking the state of your Redux store as its argument, and returning an object of headers as above.
159
159
160
-
#### `[CALL_API].credentials`
160
+
#### `[RSAA].credentials`
161
161
162
162
Whether or not to send cookies with the API call.
163
163
@@ -171,48 +171,48 @@ It must be one of the following strings:
171
171
172
172
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.
173
173
174
-
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.
174
+
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.
175
175
176
-
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.
176
+
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.
177
177
178
178
### Lifecycle
179
179
180
-
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.
180
+
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.
181
181
182
-
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.
182
+
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.
183
183
184
184
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:
185
-
- `type`: the string constant in the first position of the `[CALL_API].types` array;
185
+
- `type`: the string constant in the first position of the `[RSAA].types` array;
186
186
- `payload`: an [`InvalidRSAA`](#invalidrsaa) object containing a list of said validation errors;
187
187
- `error: true`.
188
188
189
189
`redux-api-middleware` will perform no further operations. In particular, no API call will be made, and the incoming RSAA will die here.
190
190
191
191
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:
192
-
- `type`: the string constant in the first position of the `[CALL_API].types` array.
192
+
- `type`: the string constant in the first position of the `[RSAA].types` array.
193
193
194
194
But errors may pop up at this stage, for several reasons:
195
-
- `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;
196
-
- `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));
195
+
- `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;
196
+
- `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));
197
197
- a network failure occurs (the network is unreachable, the server responds with an error,...).
198
198
199
199
If such an error occurs, a different *request* FSA will be dispatched (*instead* of the one described above). It will contain the following properties:
200
-
- `type`: the string constant in the first position of the `[CALL_API].types` array;
200
+
- `type`: the string constant in the first position of the `[RSAA].types` array;
201
201
- `payload`: a [`RequestError`](#requesterror) object containing an error message;
202
202
- `error: true`.
203
203
204
204
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:
205
-
- `type`: the string constant in the second position of the `[CALL_API].types` array;
205
+
- `type`: the string constant in the second position of the `[RSAA].types` array;
206
206
- `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.
207
207
208
208
If the status code of the response falls outside that 200 range, a *failure* FSA will dispatched instead, with the following properties:
209
-
- `type`: the string constant in the third position of the `[CALL_API].types` array;
209
+
- `type`: the string constant in the third position of the `[RSAA].types` array;
210
210
- `payload`: an [`ApiError`](#apierror) object containing the message `` `${status} -${statusText}```;
211
211
- `error:true`.
212
212
213
213
### Customizing the dispatched FSAs
214
214
215
-
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.
215
+
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.
216
216
217
217
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.
218
218
@@ -231,7 +231,7 @@ For example, if you want your *request* FSA to have the URL endpoint of the API
231
231
```js
232
232
// Input RSAA
233
233
{
234
-
[CALL_API]: {
234
+
[RSAA]: {
235
235
endpoint:'http://www.example.com/api/users',
236
236
method:'GET',
237
237
types: [
@@ -257,7 +257,7 @@ If you do not need access to the action itself or the state of your Redux store,
257
257
```js
258
258
// Input RSAA
259
259
{
260
-
[CALL_API]: {
260
+
[RSAA]: {
261
261
endpoint:'http://www.example.com/api/users',
262
262
method:'GET',
263
263
types: [
@@ -296,7 +296,7 @@ const userSchema = new Schema('users');
296
296
297
297
// Input RSAA
298
298
{
299
-
[CALL_API]: {
299
+
[RSAA]: {
300
300
endpoint:'http://www.example.com/api/users',
301
301
method:'GET',
302
302
types: [
@@ -357,7 +357,7 @@ For example, if you want the status code and status message of a unsuccessful AP
357
357
358
358
```js
359
359
{
360
-
[CALL_API]: {
360
+
[RSAA]: {
361
361
endpoint:'http://www.example.com/api/users/1',
362
362
method:'GET',
363
363
types: [
@@ -396,17 +396,17 @@ By default, *failure* FSAs will not contain a `meta` property, while their `payl
396
396
397
397
The following objects are exported by `redux-api-middleware`.
398
398
399
-
#### `CALL_API`
399
+
#### `RSAA`
400
400
401
-
A JavaScript `Symbol` whose presence as a key in an action signals that `redux-api-middleware` should process said action.
401
+
A JavaScript `String` whose presence as a key in an action signals that `redux-api-middleware` should process said action.
402
402
403
403
#### `apiMiddleware`
404
404
405
405
The Redux middleware itself.
406
406
407
407
#### `isRSAA(action)`
408
408
409
-
A function that returns `true` if `action` has a`[CALL_API]` property, and `false` otherwise.
409
+
A function that returns `true` if `action` has an`[RSAA]` property, and `false` otherwise.
410
410
411
411
#### `validateRSAA(action)`
412
412
@@ -508,65 +508,65 @@ The optional `meta` property MAY be any type of value. It is intended for any ex
508
508
### Redux Standard API-calling Actions
509
509
510
510
The definition of a *Redux Standard API-calling Action* below is the one used to validate RSAA actions. As explained in [Lifecycle](#lifecycle),
511
-
- actions without a`[CALL_API]` will be passed to the next middleware without any modifications;
512
-
- actions with a`[CALL_API]` property that fail validation will result in an error *request* FSA.
511
+
- actions without an`[RSAA]` property will be passed to the next middleware without any modifications;
512
+
- actions with an`[RSAA]` property that fail validation will result in an error *request* FSA.
513
513
514
514
A *Redux Standard API-calling Action* MUST
515
515
516
516
- be a plain JavaScript object,
517
-
- have a`[CALL_API]` property.
517
+
- have an`[RSAA]` property.
518
518
519
519
A *Redux Standard API-calling Action* MUST NOT
520
520
521
-
- include properties other than `[CALL_API]`.
521
+
- include properties other than `[RSAA]`.
522
522
523
-
#### `[CALL_API]`
523
+
#### `[RSAA]`
524
524
525
-
The `[CALL_API]` property MUST
525
+
The `[RSAA]` property MUST
526
526
527
527
- be a plain JavaScript Object,
528
528
- have an `endpoint` property,
529
529
- have a `method` property,
530
530
- have a `types` property.
531
531
532
-
The `[CALL_API]` property MAY
532
+
The `[RSAA]` property MAY
533
533
534
534
- have a `body` property,
535
535
- have a `headers` property,
536
536
- have a `credentials` property,
537
537
- have a `bailout` property.
538
538
539
-
The `[CALL_API]` property MUST NOT
539
+
The `[RSAA]` property MUST NOT
540
540
541
541
- include properties other than `endpoint`, `method`, `types`, `body`, `headers`, `credentials`, and `bailout`.
542
542
543
-
#### `[CALL_API].endpoint`
543
+
#### `[RSAA].endpoint`
544
544
545
-
The `[CALL_API].endpoint` property MUST be a string or a function. In the second case, the function SHOULD return a string.
545
+
The `[RSAA].endpoint` property MUST be a string or a function. In the second case, the function SHOULD return a string.
546
546
547
-
#### `[CALL_API].method`
547
+
#### `[RSAA].method`
548
548
549
-
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.
549
+
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.
550
550
551
-
#### `[CALL_API].body`
551
+
#### `[RSAA].body`
552
552
553
-
The optional `[CALL_API].body` property SHOULD be a valid body according to the the [fetch specification](https://fetch.spec.whatwg.org).
553
+
The optional `[RSAA].body` property SHOULD be a valid body according to the the [fetch specification](https://fetch.spec.whatwg.org).
554
554
555
-
#### `[CALL_API].headers`
555
+
#### `[RSAA].headers`
556
556
557
-
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.
557
+
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.
558
558
559
-
#### `[CALL_API].credentials`
559
+
#### `[RSAA].credentials`
560
560
561
-
The optional `[CALL_API].credentials` property MUST be one of the strings `omit`, `same-origin` or `include`.
561
+
The optional `[RSAA].credentials` property MUST be one of the strings `omit`, `same-origin` or `include`.
562
562
563
-
#### `[CALL_API].bailout`
563
+
#### `[RSAA].bailout`
564
564
565
-
The optional `[CALL_API].bailout` property MUST be a boolean or a function.
565
+
The optional `[RSAA].bailout` property MUST be a boolean or a function.
566
566
567
-
#### `[CALL_API].types`
567
+
#### `[RSAA].types`
568
568
569
-
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.
569
+
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