2
2
3
3
### Table of Contents
4
4
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
13
165
14
166
## LP_API
15
167
@@ -18,9 +170,9 @@ Redux Api middleware. This is implemented as a Symbol, instead of a String
18
170
to guarantee uniqueness.
19
171
20
172
The params provided as the value include anything that is supported by
21
- [ LP Redux Api Middleware] [ 2 ]
173
+ [ LP Redux Api Middleware] [ 9 ]
22
174
23
- Type: [ Symbol] [ 9 ]
175
+ Type: [ Symbol] [ 18 ]
24
176
25
177
** Examples**
26
178
@@ -38,6 +190,36 @@ function fooAction () {
38
190
}
39
191
```
40
192
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
+
41
223
## middleware
42
224
43
225
### Actions
@@ -48,7 +230,7 @@ At a high level, `lp-redux-api` actions contain the following information:
48
230
- Any extra request details
49
231
- One or many actions to dispatch based on the status of the request
50
232
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:
52
234
53
235
import { LP_API } from '@launchpadlab/lp-redux-api'
54
236
@@ -94,18 +276,18 @@ The following options can be used to configure the middleware:
94
276
- ` onUnauthorized ` (default=` null ` ): An action creator to be called and dispatched when the server rejects a request with a status of ` unauthorized ` .
95
277
- ` successDataPath ` : A path to response data that will be passed as the success action's payload
96
278
- ` 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
98
280
99
281
## reducer
100
282
101
283
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 ] .
104
286
105
287
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 ] .
107
289
108
- Type: [ Function] [ 12 ]
290
+ Type: [ Function] [ 16 ]
109
291
110
292
** Examples**
111
293
@@ -131,10 +313,10 @@ selectStatus(REQ_FETCH_USERS, state) // -> 'loading'
131
313
*
132
314
```
133
315
134
- ## requestWithKey
316
+ ## actionOpts
135
317
136
318
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 ] .
138
320
139
321
Default actions are dynamically named using the key provided, like so:
140
322
@@ -144,8 +326,8 @@ Default actions are dynamically named using the key provided, like so:
144
326
145
327
** Parameters**
146
328
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 `
149
331
150
332
** Examples**
151
333
@@ -170,7 +352,7 @@ fetchUsers()
170
352
*
171
353
```
172
354
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.
174
356
175
357
## selectors
176
358
@@ -219,7 +401,7 @@ apiSelectors.status(state, REQ_FETCH_USERS) // -> 'loading'
219
401
220
402
## setFromRequest
221
403
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 ] .
223
405
These handlers set data at a path in the state from the response(s) of a given request.
224
406
225
407
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
230
412
231
413
** Parameters**
232
414
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
235
417
236
418
** Examples**
237
419
@@ -267,32 +449,50 @@ dispatch(fetchUsers())
267
449
*
268
450
```
269
451
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
271
471
272
- [ 1 ] : #lp_api
472
+ [ 10 ] : #actions
273
473
274
- [ 2 ] : #middleware
474
+ [ 11 ] : #middleware-configuration
275
475
276
- [ 3 ] : #actions
476
+ [ 12 ] : #reducer
277
477
278
- [ 4 ] : #middleware-configuration
478
+ [ 13 ] : #actionopts
279
479
280
- [ 5 ] : #reducer
480
+ [ 14 ] : #selectors
281
481
282
- [ 6 ] : #requestwithkey
482
+ [ 15 ] : #setfromrequest
283
483
284
- [ 7 ] : #selectors
484
+ [ 16 ] : https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function
285
485
286
- [ 8 ] : #setfromrequest
486
+ [ 17 ] : https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
287
487
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
289
489
290
- [ 10 ] : https://github.com/LaunchPadLab/lp-requests/blob/master/docs.md#http
490
+ [ 19 ] : https://redux-actions.js.org/api-reference/createaction-s
291
491
292
- [ 11 ] : selectStatus
492
+ [ 20 ] : https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
293
493
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
295
495
296
- [ 13 ] : https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
496
+ [ 22 ] : requestWithKey
297
497
298
- [ 14 ] : https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
498
+ [ 23 ] : selectStatus
0 commit comments