Skip to content

Commit 42645c8

Browse files
committed
Add more handlers
1 parent e0d5de9 commit 42645c8

11 files changed

+83
-16
lines changed

src/create-request.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import LP_API from './LP_API'
22

3+
function createActionOptions (definition, args) {
4+
if (typeof definition === 'object') return definition
5+
if (typeof definition === 'function') return definition(...args) || {}
6+
throw new Error('Request definition must be an object or a function.')
7+
}
8+
39
function createRequest (requestKey, definition) {
4-
if (!requestKey) throw new Error('Must include a key for requestWithKey() request.')
10+
if (!requestKey) throw new Error('Must include a request key.')
511
function actionCreator (...args) {
612
return {
713
[LP_API]: {
8-
...definition(...args),
14+
...createActionOptions(definition, args),
915
requestKey,
10-
// requestAction: `${requestKey}_REQUEST`,
11-
// successAction: `${requestKey}_SUCCESS`,
12-
// failureAction: `${requestKey}_FAILURE`,
1316
}
1417
}
1518
}

src/handlers/handle-failure.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { isFailureAction } from './helpers'
2+
3+
function handleSuccess (handler) {
4+
return (state, action) => isFailureAction(action) ? handler(state, action) : state
5+
}
6+
7+
export default handleSuccess

src/handlers/handle-response.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
function handleReponse (handler) {
3+
return handler
4+
}
5+
6+
export default handleReponse

src/handlers/handle-success.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { isSuccessAction } from './helpers'
2+
3+
function handleSuccess (handler) {
4+
return (state, action) => isSuccessAction(action) ? handler(state, action) : state
5+
}
6+
7+
export default handleSuccess

src/handlers/helpers.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { get } from '../utils'
2+
import { LP_API_STATUS_SUCCESS, LP_API_STATUS_FAILURE } from '../actions'
3+
4+
export function isSuccessAction (action) {
5+
return get('payload.status', action) === LP_API_STATUS_SUCCESS
6+
}
7+
8+
export function isFailureAction (action) {
9+
return get('payload.status', action) === LP_API_STATUS_FAILURE
10+
}
11+
12+
export function defaultTransform (action) {
13+
return get('payload.data', action)
14+
}

src/handlers/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export handleSuccess from './handle-success'
2+
// export handleFailure from './handle-failure'
3+
// export handleResponse from './handle-response'
4+
export setOnSuccess from './set-on-success'

src/handlers/set-on-failure.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { defaultTransform } from './helpers'
2+
import handleFailure from './handle-failure'
3+
import { set } from '../utils'
4+
5+
function setOnFailure (path, transform=defaultTransform) {
6+
return handleFailure((state, action) => set(path, transform(action), state))
7+
}
8+
9+
export default setOnFailure

src/handlers/set-on-response.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { defaultTransform, isSuccessAction, isFailureAction } from './helpers'
2+
import handleResponse from './handle-response'
3+
import { set } from '../utils'
4+
5+
function setOnResponse (
6+
successPath,
7+
failurePath,
8+
transformSuccess=defaultTransform,
9+
transformFailure=defaultTransform,
10+
) {
11+
return handleResponse((state, action) => {
12+
if (isSuccessAction(action)) return set(successPath, transformSuccess(action), state)
13+
if (isFailureAction(action)) return set(failurePath, transformFailure(action), state)
14+
return state
15+
})
16+
}
17+
18+
export default setOnResponse

src/handlers/set-on-success.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { defaultTransform } from './helpers'
2+
import handleSuccess from './handle-success'
3+
import { set } from '../utils'
4+
5+
function setOnSuccess (path, transform=defaultTransform) {
6+
return handleSuccess((state, action) => set(path, transform(action), state))
7+
}
8+
9+
export default setOnSuccess

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ export {
33
LP_API_STATUS_SUCCESS,
44
LP_API_STATUS_FAILURE
55
} from './actions'
6+
export * from './handlers'
67
export { default as LP_API } from './LP_API'
78
export { default as createRequest } from './create-request'
8-
export { default as setOnSuccess } from './set-on-success'
99
export { default as middleware } from './middleware'
1010
export { default as reducer } from './reducer'
1111
export { default as requestWithKey } from './request-with-key'

src/set-on-success.js

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

0 commit comments

Comments
 (0)