react-sprucebot
Version:
React components for your Sprucebot Skill 💪🏼
50 lines (45 loc) • 869 B
JavaScript
export default function clientMiddleware(client) {
return ({dispatch, getState}) => { // eslint-disable-line
return next => action => {
const { promise, types, ...rest } = action
if (!promise) {
return next(action)
}
const [REQUEST, SUCCESS, FAILURE] = types
next({
...rest,
type: REQUEST
})
const { auth } = getState()
if (auth && auth.jwt) {
client.setJwt(auth.jwt)
}
const actionPromise = promise(client, auth)
actionPromise
.then(
result => {
return next({
...rest,
result,
type: SUCCESS
})
},
error =>
next({
...rest,
error,
type: FAILURE
})
)
.catch(error => {
console.log('MIDDLEWARE ERROR:', error)
next({
...rest,
error,
type: FAILURE
})
})
return actionPromise
}
}
}