createApis
createApis(axiosOptions)
createApis(axiosOptions)
Build an Axios API that implements a cancellable request caller that can be cancelled by redux sagas.
Accepts default Axios options.
Returns an object with
{ api, sagaApi }
whereapi
is the cancellable axios API, andsagaAPi
is an abstraction that usesapi
to simplify make API calls using redux sagas (see below).
Sample usage:
api
api
The base api object allows you to make regular API calls from anywhere in your app. This is basic axios stuff, with an added cancel
function with every request.
api.get(path, options)
api.get(path, options)
api.put(path, payload, options)
api.put(path, payload, options)
api.patch(path, payload, options)
api.patch(path, payload, options)
api.post(path, payload, options)
api.post(path, payload, options)
api.delete(path, payload, options)
api.delete(path, payload, options)
api.addAuthorization(token)
api.addAuthorization(token)
Sets authorization header on axios instance
api.removeAuthorization()
api.removeAuthorization()
Removes authorization header on axios instance
api.addHeader(name, value)
api.addHeader(name, value)
Adds a header value
Kind: global function
api.removeHeader(name)
api.removeHeader(name)
Removes a header value
sagaApi
sagaApi
Saga API is a helper to reduce the amount of code one writes when make an ajax call. Here is your typical ajax call:
If we were to do this inside of sagas it would look like this:
As you can tell, this can get repetitive, hence why this abstraction was built. Simply put, you pass it a path, payload, success action, fail action, and optional done action. It will handle try catching for you:
One thing to note, if you decide to add a saga for done action, you will receive a payload of { data, error }
so you can decide if you want to do anything with the data.
sagaApi.get(path, successAction, failAction, [doneAction?])
sagaApi.get(path, successAction, failAction, [doneAction?])
sagaApi.post(path, payload, successAction, failAction, [doneAction?])
sagaApi.post(path, payload, successAction, failAction, [doneAction?])
sagaApi.put(path, payload, successAction, failAction, [doneAction?])
sagaApi.put(path, payload, successAction, failAction, [doneAction?])
sagaApi.patch(path, payload, successAction, failAction, [doneAction?])
sagaApi.patch(path, payload, successAction, failAction, [doneAction?])
sagaApi.delete(path, payload, successAction, failAction, [doneAction?])
sagaApi.delete(path, payload, successAction, failAction, [doneAction?])
Last updated