Swagger middleware for redux
This is an attempt to add asynchronous swagger api calls to redux. It works by dispatching an action that includes the field swagger
that takes a function and passes the swagger client element to that function. If the swagger spec has not yet been parsed, the action will get queued.
note: This module requires redux-thunk Github:
npm install --save github:noh4ck/redux-swagger-client
Package pending:
npm install --save redux-swagger-client
To enable Redux Swagger-client, use applyMiddleware()
:
import { createStore, applyMiddleware } from 'redux'
import swaggerClient from 'redux-swagger-client'
import thunk from 'redux-thunk'
const store = createStore(
rootReducer,
applyMiddleware([
thunk,
swaggerClient({url:'http://petstore.swagger.io/v2/swagger.json'})
])
);
function fetchPet() {
return {
types: ["FETCH_PETS", "FETCH_PETS_SUCCESS", "FETCH_PETS_FAILED"],
swagger: api => api.pet.findPetsByStatus({status: 'available'})
}
}
store.dispatch(fetchPets())
Note, it's also possible to dispatch functions:
function fetchPets_request(json) {
return {
type: 'FETCH_PETS',
receivedAt: Date.now()
}
}
function fetchPets_success(json) {
return {
type: 'FETCH_PETS_SUCCESS',
pets: json.result.body,
receivedAt: Date.now()
}
}
function fetchPets_failure(json) {
return {
type: 'FETCH_PETS_FAILED',
pets: null,
error,
receivedAt: Date.now()
}
}
function fetchPet() {
return {
actions: [fetchPets_request, fetchPets_success, fetchPets_failure],
swagger: api => api.pet.findPetsByStatus({status: 'available'})
}
}
store.dispatch(fetchPets())