Skip to content

Commit 9512ba0

Browse files
committed
Merge pull request #52 from jtribble/topic-issue-22
[MODIFIED] Fix for issue #22, updated Babel and ESLint to latest (test script was failing after cloning fresh repo)
2 parents b31e9af + 47d1d32 commit 9512ba0

11 files changed

+224
-222
lines changed

.babelrc

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
{
2-
"stage": 0,
3-
"loose": "all",
4-
"optional": [
5-
"runtime"
6-
]
2+
"plugins": ["transform-runtime"],
3+
"presets": ["es2015", "stage-0"]
74
}

.eslintrc

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"experimentalObjectRestSpread": true,
1010
"modules": true
1111
},
12+
"root": true,
1213
"rules": {
1314
"babel/arrow-parens": [2, "always"],
1415
"no-underscore-dangle": 0,

README.md

+52-52
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ Aught to be stable but still in development while we iron out bugs.
3131

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

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

3636
### A simple example
3737

3838
The following is a minimal RSAA action:
3939

4040
```js
41-
import { CALL_API } from `redux-api-middleware`;
41+
import { RSAA } from `redux-api-middleware`; // RSAA = '@@redux-api-middleware/RSAA'
4242

4343
{
44-
[CALL_API]: {
44+
[RSAA]: {
4545
endpoint: 'http://www.example.com/api/users',
4646
method: 'GET',
4747
types: ['REQUEST', 'SUCCESS', 'FAILURE']
@@ -122,35 +122,35 @@ const store = configureStore(initialState);
122122

123123
### Defining the API call
124124

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

127-
#### `[CALL_API].endpoint`
127+
#### `[RSAA].endpoint`
128128

129129
The URL endpoint for the API call.
130130

131131
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.
132132

133-
#### `[CALL_API].method`
133+
#### `[RSAA].method`
134134

135135
The HTTP method for the API call.
136136

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

139-
#### `[CALL_API].body`
139+
#### `[RSAA].body`
140140

141141
The body of the API call.
142142

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

145-
#### `[CALL_API].headers`
145+
#### `[RSAA].headers`
146146

147147
The HTTP headers for the API call.
148148

149149
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.
150150

151151
```js
152152
{
153-
[CALL_API]: {
153+
[RSAA]: {
154154
...
155155
headers: { 'Content-Type': 'application/json' }
156156
...
@@ -160,7 +160,7 @@ It is usually an object, with the keys specifying the header names and the value
160160

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

163-
#### `[CALL_API].credentials`
163+
#### `[RSAA].credentials`
164164

165165
Whether or not to send cookies with the API call.
166166

@@ -174,48 +174,48 @@ It must be one of the following strings:
174174

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

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

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

181181
### Lifecycle
182182

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

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

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

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

194194
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.
196196

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

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

207207
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;
209209
- `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.
210210

211211
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;
213213
- `payload`: an [`ApiError`](#apierror) object containing the message `` `${status} - ${statusText}` ``;
214214
- `error: true`.
215215
216216
### Customizing the dispatched FSAs
217217
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.
219219
220220
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.
221221
@@ -234,7 +234,7 @@ For example, if you want your *request* FSA to have the URL endpoint of the API
234234
```js
235235
// Input RSAA
236236
{
237-
[CALL_API]: {
237+
[RSAA]: {
238238
endpoint: 'http://www.example.com/api/users',
239239
method: 'GET',
240240
types: [
@@ -260,7 +260,7 @@ If you do not need access to the action itself or the state of your Redux store,
260260
```js
261261
// Input RSAA
262262
{
263-
[CALL_API]: {
263+
[RSAA]: {
264264
endpoint: 'http://www.example.com/api/users',
265265
method: 'GET',
266266
types: [
@@ -299,7 +299,7 @@ const userSchema = new Schema('users');
299299

300300
// Input RSAA
301301
{
302-
[CALL_API]: {
302+
[RSAA]: {
303303
endpoint: 'http://www.example.com/api/users',
304304
method: 'GET',
305305
types: [
@@ -360,7 +360,7 @@ For example, if you want the status code and status message of a unsuccessful AP
360360
361361
```js
362362
{
363-
[CALL_API]: {
363+
[RSAA]: {
364364
endpoint: 'http://www.example.com/api/users/1',
365365
method: 'GET',
366366
types: [
@@ -399,17 +399,17 @@ By default, *failure* FSAs will not contain a `meta` property, while their `payl
399399
400400
The following objects are exported by `redux-api-middleware`.
401401
402-
#### `CALL_API`
402+
#### `RSAA`
403403
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.
405405
406406
#### `apiMiddleware`
407407
408408
The Redux middleware itself.
409409
410410
#### `isRSAA(action)`
411411
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.
413413
414414
#### `validateRSAA(action)`
415415
@@ -511,65 +511,65 @@ The optional `meta` property MAY be any type of value. It is intended for any ex
511511
### Redux Standard API-calling Actions
512512
513513
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.
516516
517517
A *Redux Standard API-calling Action* MUST
518518
519519
- be a plain JavaScript object,
520-
- have a `[CALL_API]` property.
520+
- have an `[RSAA]` property.
521521
522522
A *Redux Standard API-calling Action* MUST NOT
523523
524-
- include properties other than `[CALL_API]`.
524+
- include properties other than `[RSAA]`.
525525
526-
#### `[CALL_API]`
526+
#### `[RSAA]`
527527
528-
The `[CALL_API]` property MUST
528+
The `[RSAA]` property MUST
529529
530530
- be a plain JavaScript Object,
531531
- have an `endpoint` property,
532532
- have a `method` property,
533533
- have a `types` property.
534534
535-
The `[CALL_API]` property MAY
535+
The `[RSAA]` property MAY
536536
537537
- have a `body` property,
538538
- have a `headers` property,
539539
- have a `credentials` property,
540540
- have a `bailout` property.
541541
542-
The `[CALL_API]` property MUST NOT
542+
The `[RSAA]` property MUST NOT
543543
544544
- include properties other than `endpoint`, `method`, `types`, `body`, `headers`, `credentials`, and `bailout`.
545545
546-
#### `[CALL_API].endpoint`
546+
#### `[RSAA].endpoint`
547547
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.
549549
550-
#### `[CALL_API].method`
550+
#### `[RSAA].method`
551551
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.
553553
554-
#### `[CALL_API].body`
554+
#### `[RSAA].body`
555555
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).
557557
558-
#### `[CALL_API].headers`
558+
#### `[RSAA].headers`
559559
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.
561561
562-
#### `[CALL_API].credentials`
562+
#### `[RSAA].credentials`
563563
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`.
565565
566-
#### `[CALL_API].bailout`
566+
#### `[RSAA].bailout`
567567
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.
569569
570-
#### `[CALL_API].types`
570+
#### `[RSAA].types`
571571
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.
573573
574574
#### Type descriptors
575575

package.json

+10-6
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,21 @@
2626
},
2727
"license": "MIT",
2828
"dependencies": {
29-
"babel-runtime": "^5.8.25",
29+
"babel-plugin-transform-runtime": "^6.5.2",
30+
"babel-runtime": "^6.5.0",
3031
"isomorphic-fetch": "^2.1.1",
3132
"lodash.isplainobject": "^3.2.0"
3233
},
3334
"devDependencies": {
34-
"babel": "^5.8.23",
35-
"babel-eslint": "^4.1.3",
36-
"babel-istanbul": "^0.3.20",
35+
"babel-cli": "^6.5.1",
36+
"babel-eslint": "^4.1.8",
37+
"babel-istanbul": "^0.6.0",
38+
"babel-preset-es2015": "^6.5.0",
39+
"babel-preset-stage-0": "^6.5.0",
3740
"coveralls": "^2.11.4",
38-
"eslint": "^1.6.0",
39-
"eslint-plugin-babel": "^2.1.1",
41+
"eslint": "^2.0.0",
42+
"eslint-plugin-babel": "^3.1.0",
43+
"estraverse-fb": "^1.3.1",
4044
"nock": "^2.15.0",
4145
"normalizr": "^1.1.0",
4246
"rimraf": "^2.4.3",

src/CALL_API.js

-10
This file was deleted.

src/RSAA.js

+10
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

+3-3
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)