Skip to content

Commit 949093a

Browse files
committed
Add docs
1 parent 42645c8 commit 949093a

14 files changed

+430
-75
lines changed

docs.md

Lines changed: 239 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,166 @@
22

33
### Table of Contents
44

5-
- [LP_API][1]
6-
- [middleware][2]
7-
- [Actions][3]
8-
- [Middleware configuration][4]
9-
- [reducer][5]
10-
- [requestWithKey][6]
11-
- [selectors][7]
12-
- [setFromRequest][8]
5+
- [handleSuccess][1]
6+
- [handleSuccess][2]
7+
- [handleFailure][3]
8+
- [handleFailure][4]
9+
- [handleResponse][5]
10+
- [setOnResponse][6]
11+
- [LP_API][7]
12+
- [createRequest][8]
13+
- [middleware][9]
14+
- [Actions][10]
15+
- [Middleware configuration][11]
16+
- [reducer][12]
17+
- [actionOpts][13]
18+
- [selectors][14]
19+
- [setFromRequest][15]
20+
21+
## handleSuccess
22+
23+
A function that takes an API action handler and only applies that handler when the request succeeds.
24+
25+
**Parameters**
26+
27+
- `handler` **[Function][16]** An action handler that is passed `state` and `action` params
28+
29+
**Examples**
30+
31+
```javascript
32+
handleActions({
33+
[apiActions.fetchUser]: handleSuccess((state, action) => {
34+
// This code only runs when the call was successful
35+
return set('currentUser', action.payload.data, state)
36+
})
37+
})
38+
39+
*
40+
```
41+
42+
Returns **[Function][16]** An action handler that runs when a request is successful
43+
44+
## handleSuccess
45+
46+
A function that creates an API action handler that sets a path in the state with the returned data if a request succeeds.
47+
48+
**Parameters**
49+
50+
- `path` **[String][17]** The path in the state to set with the returned data
51+
- `transform` **[Function][16]?** A function that determines the data that is set in the state. Passed `action` and `state` params.
52+
53+
**Examples**
54+
55+
```javascript
56+
handleActions({
57+
// This will do the same thing as the example for handleSuccess
58+
[apiActions.fetchUser]: setOnSuccess('currentSuccess')
59+
})
60+
61+
*
62+
```
63+
64+
Returns **[Function][16]** An action handler that runs when a request is unsuccessful
65+
66+
## handleFailure
67+
68+
A function that takes an API action handler and only applies that handler when the request fails.
69+
70+
**Parameters**
71+
72+
- `handler` **[Function][16]** An action handler that is passed `state` and `action` params
73+
74+
**Examples**
75+
76+
```javascript
77+
handleActions({
78+
[apiActions.fetchUser]: handleFailure((state, action) => {
79+
// This code only runs when the call was unsuccessful
80+
return set('userFetchError', action.payload.error, state)
81+
})
82+
})
83+
84+
*
85+
```
86+
87+
Returns **[Function][16]** An action handler that runs when a request is unsuccessful
88+
89+
## handleFailure
90+
91+
A function that creates an API action handler that sets a path in the state with the returned error if a request fails.
92+
93+
**Parameters**
94+
95+
- `path` **[String][17]** The path in the state to set with the returned error
96+
- `transform` **[Function][16]?** A function that determines the data that is set in the state. Passed `action` and `state` params.
97+
98+
**Examples**
99+
100+
```javascript
101+
handleActions({
102+
// This will do the same thing as the example for handleFailure
103+
[apiActions.fetchUser]: setOnFailure('userFetchError')
104+
})
105+
106+
*
107+
```
108+
109+
Returns **[Function][16]** An action handler that runs when a request is successful
110+
111+
## handleResponse
112+
113+
A function that takes two API action handlers, one for successful requests and one for failed requests,
114+
and applies the handlers when the responses have the correct status.
115+
116+
**Parameters**
117+
118+
- `successHandler` **[Function][16]** An action handler that is passed `state` and `action` params
119+
- `failureHandler` **[Function][16]** An action handler that is passed `state` and `action` params
120+
121+
**Examples**
122+
123+
```javascript
124+
handleActions({
125+
[apiActions.fetchUser]: handleResponse(
126+
(state, action) => {
127+
// This code runs if the call is successful
128+
return set('currentUser', action.payload.data, state)
129+
},
130+
(state, action) => {
131+
// This code runs if the call is unsuccessful
132+
return set('userFetchError', action.payload.error, state)
133+
},
134+
)
135+
})
136+
137+
*
138+
```
139+
140+
Returns **[Function][16]** An action handler runs the handler that corresponds to the request status
141+
142+
## setOnResponse
143+
144+
A function that creates an API action handler that sets one of two given paths in the state with the returned data depending on whether a request succeeds or fails.
145+
146+
**Parameters**
147+
148+
- `path` **[String][17]** The path in the state to set with the returned data on success
149+
- `path` **[String][17]** The path in the state to set with the returned error on failure
150+
- `transform` **[Function][16]?** A function that determines the success data that is set in the state. Passed `action` and `state` params.
151+
- `transform` **[Function][16]?** A function that determines the error data that is set in the state. Passed `action` and `state` params.
152+
153+
**Examples**
154+
155+
```javascript
156+
handleActions({
157+
// This will do the same thing as the example for handleResponse
158+
[apiActions.fetchUser]: setOnResponse('currentUser', 'userFetchError')
159+
})
160+
161+
*
162+
```
163+
164+
Returns **[Function][16]** An action handler
13165

14166
## LP_API
15167

@@ -18,9 +170,9 @@ Redux Api middleware. This is implemented as a Symbol, instead of a String
18170
to guarantee uniqueness.
19171

20172
The params provided as the value include anything that is supported by
21-
[LP Redux Api Middleware][2]
173+
[LP Redux Api Middleware][9]
22174

23-
Type: [Symbol][9]
175+
Type: [Symbol][18]
24176

25177
**Examples**
26178

@@ -38,6 +190,36 @@ function fooAction () {
38190
}
39191
```
40192

193+
## createRequest
194+
195+
A function that creates action creators for making API requests, much like [createAction][19] from `redux-actions`.
196+
197+
**Parameters**
198+
199+
- `type` **[String][17]** A unique key that will be used to identify the request internally inr edux
200+
- `definition` **([Object][20] \| [Function][16])** An object of `lp-request` config options, or a function that returns config options.
201+
202+
**Examples**
203+
204+
```javascript
205+
export const fetchUser = requestWithKey('FETCH_USER', (id) => ({
206+
url: '/users/' + id,
207+
}))
208+
209+
fetchUsers(5)
210+
// -> will make request to /users/5
211+
212+
// Just like in redux-action, this action can be referenced in a reducer by name:
213+
214+
handleActions({
215+
[apiActions.fetchUser]: (state, action) => ...
216+
})
217+
218+
*
219+
```
220+
221+
Returns **[Function][16]** An action creator that passes its arguments to `definition` and makes the resulting API request.
222+
41223
## middleware
42224

43225
### Actions
@@ -48,7 +230,7 @@ At a high level, `lp-redux-api` actions contain the following information:
48230
- Any extra request details
49231
- One or many actions to dispatch based on the status of the request
50232

51-
These actions are keyed using the [LP_API][1] symbol so that the middleware knows to handle them. Here's an example of a simple action creator:
233+
These actions are keyed using the [LP_API][7] symbol so that the middleware knows to handle them. Here's an example of a simple action creator:
52234

53235
import { LP_API } from '@launchpadlab/lp-redux-api'
54236

@@ -94,18 +276,18 @@ The following options can be used to configure the middleware:
94276
- `onUnauthorized` (default=`null`): An action creator to be called and dispatched when the server rejects a request with a status of `unauthorized`.
95277
- `successDataPath`: A path to response data that will be passed as the success action's payload
96278
- `failureDataPath`: A path to response data that will be passed as the failure action's payload
97-
- any options used by the lp-requests [http][10] module
279+
- any options used by the lp-requests [http][21] module
98280

99281
## reducer
100282

101283
Stores the status of API requests in your state.
102-
Statuses are stored for all requests with a `requestKey` (including those created by [requestWithKey][6]),
103-
and can be retrieved by using [selectStatus][11].
284+
Statuses are stored for all requests with a `requestKey` (including those created by [requestWithKey][22]),
285+
and can be retrieved by using [selectStatus][23].
104286

105287
To use this reducer, add it to `combineReducers()` under the `api` key. You can use a different key if you'd like,
106-
but you will need to reference it explicitly when using [selectStatus][11].
288+
but you will need to reference it explicitly when using [selectStatus][23].
107289

108-
Type: [Function][12]
290+
Type: [Function][16]
109291

110292
**Examples**
111293

@@ -131,10 +313,10 @@ selectStatus(REQ_FETCH_USERS, state) // -> 'loading'
131313
*
132314
```
133315

134-
## requestWithKey
316+
## actionOpts
135317

136318
An action creator that automatically adds a requestKey and default actions to your request.
137-
These default actions can then be picked up by [setFromRequest][8].
319+
These default actions can then be picked up by [setFromRequest][15].
138320

139321
Default actions are dynamically named using the key provided, like so:
140322

@@ -144,8 +326,8 @@ Default actions are dynamically named using the key provided, like so:
144326

145327
**Parameters**
146328

147-
- `requestKey` **[String][13]** A unique key that you can use to reference your request in [setFromRequest][8] or [selectStatus][11]
148-
- `options` **[Object][14]** Config options that you would normally include in an [LP_API] action, such as `url` and `method` (optional, default `{}`)
329+
- `requestKey` **[String][17]** A unique key that you can use to reference your request in [setFromRequest][15] or [selectStatus][23]
330+
- `options` **[Object][20]** Config options that you would normally include in an [LP_API] action, such as `url` and `method`
149331

150332
**Examples**
151333

@@ -170,7 +352,7 @@ fetchUsers()
170352
*
171353
```
172354

173-
Returns **[Object][14]** An [LP_API] action that can be handled by the lp-redux-api middleware.
355+
Returns **[Object][20]** An [LP_API] action that can be handled by the lp-redux-api middleware.
174356

175357
## selectors
176358

@@ -219,7 +401,7 @@ apiSelectors.status(state, REQ_FETCH_USERS) // -> 'loading'
219401

220402
## setFromRequest
221403

222-
A function that creates action handlers for actions generated by [requestWithKey][6].
404+
A function that creates action handlers for actions generated by [requestWithKey][22].
223405
These handlers set data at a path in the state from the response(s) of a given request.
224406

225407
If the request is successful, the data will be set at `<path>.success`.
@@ -230,8 +412,8 @@ You can override either of these handlers in your reducer by creating handlers e
230412

231413
**Parameters**
232414

233-
- `requestKey` **[String][13]** A unique key that references a request created by [requestWithKey][6]
234-
- `path` **[String][13]** A path (in dot notation) indicating where the data will be set in the state
415+
- `requestKey` **[String][17]** A unique key that references a request created by [requestWithKey][22]
416+
- `path` **[String][17]** A path (in dot notation) indicating where the data will be set in the state
235417

236418
**Examples**
237419

@@ -267,32 +449,50 @@ dispatch(fetchUsers())
267449
*
268450
```
269451

270-
Returns **[Object][14]** A hash of action handlers that can be included in a reducer by using object spread syntax
452+
Returns **[Object][20]** A hash of action handlers that can be included in a reducer by using object spread syntax
453+
454+
[1]: #handlesuccess
455+
456+
[2]: #handlesuccess-1
457+
458+
[3]: #handlefailure
459+
460+
[4]: #handlefailure-1
461+
462+
[5]: #handleresponse
463+
464+
[6]: #setonresponse
465+
466+
[7]: #lp_api
467+
468+
[8]: #createrequest
469+
470+
[9]: #middleware
271471

272-
[1]: #lp_api
472+
[10]: #actions
273473

274-
[2]: #middleware
474+
[11]: #middleware-configuration
275475

276-
[3]: #actions
476+
[12]: #reducer
277477

278-
[4]: #middleware-configuration
478+
[13]: #actionopts
279479

280-
[5]: #reducer
480+
[14]: #selectors
281481

282-
[6]: #requestwithkey
482+
[15]: #setfromrequest
283483

284-
[7]: #selectors
484+
[16]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function
285485

286-
[8]: #setfromrequest
486+
[17]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
287487

288-
[9]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol
488+
[18]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol
289489

290-
[10]: https://github.com/LaunchPadLab/lp-requests/blob/master/docs.md#http
490+
[19]: https://redux-actions.js.org/api-reference/createaction-s
291491

292-
[11]: selectStatus
492+
[20]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
293493

294-
[12]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function
494+
[21]: https://github.com/LaunchPadLab/lp-requests/blob/master/docs.md#http
295495

296-
[13]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
496+
[22]: requestWithKey
297497

298-
[14]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
498+
[23]: selectStatus

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
"es6-symbol": "^3.1.1",
5454
"humps": "^2.0.0",
5555
"isomorphic-fetch": "^2.2.1",
56-
"lodash": "^4.17.4"
56+
"lodash": "^4.17.4",
57+
"util-deprecate": "^1.0.2"
5758
},
5859
"peerDependencies": {
5960
"@launchpadlab/lp-requests": ">=2.0.0"

0 commit comments

Comments
 (0)