react-redux-axios v1.1.0
Request Methods
Request Method Initialization:
Request method can be initialized as follow. This function will return several method as a object.
Methods are GetRequest
, PostRequest
, PutRequest
and DeleteRequest
const ServerConnector = Request({
header: {},
initial: {
apiHost: "https://jsonplaceholder.typicode.com/",
contentType: "application/json"
},
interceptors: false
});
GetRequest Method:
This method will invoke a get request using axios and dispatch the result to required reducer as per configuration. Code below illustrate a sample code.
// Creating Action:
const resource = Action({
url: "/todos",
body: {},
actionName: "TODO"
});
Invoking a API call:
const { GetRequest } = resources;
const result = await GetRequest({ ...resource });
POSTRequest Method:
This method will invoke a get request using axios and dispatch the result to required reducer as per configuration. Code below illustrate a sample code.
// Creating Action:
const resource = Action({
url: "/todos",
body: {...body},
actionName: "TODO"
});
Invoking a API call:
const { PostRequest } = resources;
const result = await PostRequest({ ...resource });
PUTRequest Method:
This method will invoke a put request using axios and dispatch the result to required reducer as per configuration. Code below illustrate a sample code.
// Creating Action:
const resource = Action({
url: "/todos",
body: {...body},
actionName: "TODO"
});
Invoking a API call:
const { PutRequest } = resources;
const result = await PutRequest({ ...resource });
DeleteRequest Method:
This method will invoke a put request using axios and dispatch the result to required reducer as per configuration. Code below illustrate a sample code.
// Creating Action:
const resource = Action({
url: "/todos",
body: {...body},
actionName: "TODO"
});
Invoking a API call:
const { DeleteRequest } = resources;
const result = await DeleteRequest({ ...resource });
Reducer Generator
Reducer generator function will take a configuration object as a argument. Config object can have following valid property.
{
// `initialState` is object with initial value for reducer.
initialState: {},
// `actions` are the action constant. It can have three child node.
// They are success, error and update.
actions: { success: String, error: String, update: String },
// `payloadModifier` is a collection of function.
// Payload is passed throught this and result is updated to the store.
// This can be acted as a hook for modification.
payloadModifier: {
successModifier: Function, // if payload modifier are missing, `` (obj) => obj `` is default
errorModifier: Function, // function
updateModifer: Function
}
};
Update:
update
action will update all the existing state with payload. i.e it will return { ...payload}
.
So all the change need to be address in dispatcher function itself.
For instance, if you want to update a list or remove a item from the current list,
use getState() api from redux and make a necessary changes on the state. After that
dispatch whole state as a payload for update.
Success & Error: This event will be used by Request API to change the state of the reducer when request is complemented.
Resource Generator:
{
// endpoint to make a request.
// If you have initialize base url ie. `http:example.com/api/v1` && uel can be ` /resources/create `
url: "",
// If you want to pass any query string with the url.
// It will look like ?query1='Hello'&query2='world.
query: String,
// Its the name of the Action constant. Sufix will be added to generate Action constant.
actionName: String,
...rest
}
ServerConnector Methods
This is expose if you want to make a api request outside of the Redux.
Here is the object structure that takes as an argument by ServerConnector function.
{
header: { // extra headers},
initial: {
apihost: 'api host',
contentType: 'application/json',
},
interceptors: true,
}
Initializing default Parameter:
We can pass a object with which contain two property apiHost
and contentType
to be initialized as default paramaters for axios.
{
initial:{
apihost: "api host",
contentType:"application/json"
}
}