Slide 30
Slide 30 text
// action using middleware type
const action = () => ({
type: AJAX,
url: ‘http://my-api.com',
method: 'POST',
body: ({ description, title }) => ({
title, description
}),
cb: (res) => console.log('Done!', res)
})
// middleware implementation
const middleware = ({ getState }) =>
(next) => (action) => {
// if the type doesn’t match pass action to the next middleware
if(action.type !== AJAX) return next(action)
// retrieve required fields
const {cb, url, method, body } = action
// invoke remote endpoint
http.get(url, { method,
body: JSON.stringify(body(getState()))
})
// pass response to action callback
.then((response) => cb(response))
}