Skip to content

Commit a1078fd

Browse files
committed
Use RSAA string instead of CALL_API symbol; update all source code & tests; modify babel runtime config (add transform plugin)
1 parent 7dc2a28 commit a1078fd

File tree

10 files changed

+213
-211
lines changed

10 files changed

+213
-211
lines changed

.babelrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2+
"plugins": ["transform-runtime"],
23
"presets": ["es2015", "stage-0"]
34
}

README.md

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ redux-api-middleware
2828

2929
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.
3030

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.
3232

3333
### A simple example
3434

3535
The following is a minimal RSAA action:
3636

3737
```js
38-
import { CALL_API } from `redux-api-middleware`;
38+
import { RSAA } from `redux-api-middleware`; // RSAA = '@@redux-api-middleware/RSAA'
3939

4040
{
41-
[CALL_API]: {
41+
[RSAA]: {
4242
endpoint: 'http://www.example.com/api/users',
4343
method: 'GET',
4444
types: ['REQUEST', 'SUCCESS', 'FAILURE']
@@ -119,35 +119,35 @@ const store = configureStore(initialState);
119119

120120
### Defining the API call
121121

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.
123123

124-
#### `[CALL_API].endpoint`
124+
#### `[RSAA].endpoint`
125125

126126
The URL endpoint for the API call.
127127

128128
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.
129129

130-
#### `[CALL_API].method`
130+
#### `[RSAA].method`
131131

132132
The HTTP method for the API call.
133133

134134
It must be one of the strings `GET`, `HEAD`, `POST`, `PUT`, `PATCH`, `DELETE` or `OPTIONS`, in any mixture of lowercase and uppercase letters.
135135

136-
#### `[CALL_API].body`
136+
#### `[RSAA].body`
137137

138138
The body of the API call.
139139

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.
141141

142-
#### `[CALL_API].headers`
142+
#### `[RSAA].headers`
143143

144144
The HTTP headers for the API call.
145145

146146
It is usually an object, with the keys specifying the header names and the values containing their content. For example, you can let the server know your call contains a JSON-encoded string body in the following way.
147147

148148
```js
149149
{
150-
[CALL_API]: {
150+
[RSAA]: {
151151
...
152152
headers: { 'Content-Type': 'application/json' }
153153
...
@@ -157,7 +157,7 @@ It is usually an object, with the keys specifying the header names and the value
157157

158158
It may also be a function taking the state of your Redux store as its argument, and returning an object of headers as above.
159159

160-
#### `[CALL_API].credentials`
160+
#### `[RSAA].credentials`
161161

162162
Whether or not to send cookies with the API call.
163163

@@ -171,48 +171,48 @@ It must be one of the following strings:
171171

172172
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.
173173

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.
175175

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.
177177

178178
### Lifecycle
179179

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 (or symbols), 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 (or symbols), as in our [example](#a-simple-example) above. This results in the default behavior we now describe.
181181

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.
183183

184184
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;
186186
- `payload`: an [`InvalidRSAA`](#invalidrsaa) object containing a list of said validation errors;
187187
- `error: true`.
188188

189189
`redux-api-middleware` will perform no further operations. In particular, no API call will be made, and the incoming RSAA will die here.
190190

191191
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.
193193

194194
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));
197197
- a network failure occurs (the network is unreachable, the server responds with an error,...).
198198

199199
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;
201201
- `payload`: a [`RequestError`](#requesterror) object containing an error message;
202202
- `error: true`.
203203

204204
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;
206206
- `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.
207207

208208
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;
210210
- `payload`: an [`ApiError`](#apierror) object containing the message `` `${status} - ${statusText}` ``;
211211
- `error: true`.
212212
213213
### Customizing the dispatched FSAs
214214
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.
216216
217217
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.
218218
@@ -231,7 +231,7 @@ For example, if you want your *request* FSA to have the URL endpoint of the API
231231
```js
232232
// Input RSAA
233233
{
234-
[CALL_API]: {
234+
[RSAA]: {
235235
endpoint: 'http://www.example.com/api/users',
236236
method: 'GET',
237237
types: [
@@ -257,7 +257,7 @@ If you do not need access to the action itself or the state of your Redux store,
257257
```js
258258
// Input RSAA
259259
{
260-
[CALL_API]: {
260+
[RSAA]: {
261261
endpoint: 'http://www.example.com/api/users',
262262
method: 'GET',
263263
types: [
@@ -296,7 +296,7 @@ const userSchema = new Schema('users');
296296

297297
// Input RSAA
298298
{
299-
[CALL_API]: {
299+
[RSAA]: {
300300
endpoint: 'http://www.example.com/api/users',
301301
method: 'GET',
302302
types: [
@@ -357,7 +357,7 @@ For example, if you want the status code and status message of a unsuccessful AP
357357
358358
```js
359359
{
360-
[CALL_API]: {
360+
[RSAA]: {
361361
endpoint: 'http://www.example.com/api/users/1',
362362
method: 'GET',
363363
types: [
@@ -396,17 +396,17 @@ By default, *failure* FSAs will not contain a `meta` property, while their `payl
396396
397397
The following objects are exported by `redux-api-middleware`.
398398
399-
#### `CALL_API`
399+
#### `RSAA`
400400
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.
402402
403403
#### `apiMiddleware`
404404
405405
The Redux middleware itself.
406406
407407
#### `isRSAA(action)`
408408
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.
410410
411411
#### `validateRSAA(action)`
412412
@@ -508,65 +508,65 @@ The optional `meta` property MAY be any type of value. It is intended for any ex
508508
### Redux Standard API-calling Actions
509509
510510
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.
513513
514514
A *Redux Standard API-calling Action* MUST
515515
516516
- be a plain JavaScript object,
517-
- have a `[CALL_API]` property.
517+
- have an `[RSAA]` property.
518518
519519
A *Redux Standard API-calling Action* MUST NOT
520520
521-
- include properties other than `[CALL_API]`.
521+
- include properties other than `[RSAA]`.
522522
523-
#### `[CALL_API]`
523+
#### `[RSAA]`
524524
525-
The `[CALL_API]` property MUST
525+
The `[RSAA]` property MUST
526526
527527
- be a plain JavaScript Object,
528528
- have an `endpoint` property,
529529
- have a `method` property,
530530
- have a `types` property.
531531
532-
The `[CALL_API]` property MAY
532+
The `[RSAA]` property MAY
533533
534534
- have a `body` property,
535535
- have a `headers` property,
536536
- have a `credentials` property,
537537
- have a `bailout` property.
538538
539-
The `[CALL_API]` property MUST NOT
539+
The `[RSAA]` property MUST NOT
540540
541541
- include properties other than `endpoint`, `method`, `types`, `body`, `headers`, `credentials`, and `bailout`.
542542
543-
#### `[CALL_API].endpoint`
543+
#### `[RSAA].endpoint`
544544
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.
546546
547-
#### `[CALL_API].method`
547+
#### `[RSAA].method`
548548
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.
550550
551-
#### `[CALL_API].body`
551+
#### `[RSAA].body`
552552
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).
554554
555-
#### `[CALL_API].headers`
555+
#### `[RSAA].headers`
556556
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.
558558
559-
#### `[CALL_API].credentials`
559+
#### `[RSAA].credentials`
560560
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`.
562562
563-
#### `[CALL_API].bailout`
563+
#### `[RSAA].bailout`
564564
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.
566566
567-
#### `[CALL_API].types`
567+
#### `[RSAA].types`
568568
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.
570570
571571
#### Type descriptors
572572

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
},
2727
"license": "MIT",
2828
"dependencies": {
29+
"babel-plugin-transform-runtime": "^6.5.2",
2930
"babel-runtime": "^6.5.0",
3031
"isomorphic-fetch": "^2.1.1",
3132
"lodash.isplainobject": "^3.2.0"

src/CALL_API.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/RSAA.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* String key that carries API call info interpreted by this Redux middleware.
3+
*
4+
* @constant {string}
5+
* @access public
6+
* @default
7+
*/
8+
const RSAA = '@@redux-api-middleware/RSAA';
9+
10+
export default RSAA;

src/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @module redux-api-middleware
44
* @requires isomorphic-fetch
55
* @requires lodash.isplainobject
6-
* @exports {symbol} CALL_API
6+
* @exports {string} RSAA
77
* @exports {function} isRSAA
88
* @exports {function} validateRSAA
99
* @exports {function} isValidRSAA
@@ -29,14 +29,14 @@
2929
* @returns undefined
3030
*/
3131

32-
import CALL_API from './CALL_API';
32+
import RSAA from './RSAA';
3333
import { isRSAA, validateRSAA, isValidRSAA } from './validation';
3434
import { InvalidRSAA, InternalError, RequestError, ApiError } from './errors';
3535
import { getJSON } from './util';
3636
import { apiMiddleware } from './middleware';
3737

3838
export {
39-
CALL_API,
39+
RSAA,
4040
isRSAA,
4141
validateRSAA,
4242
isValidRSAA,

0 commit comments

Comments
 (0)