项目作者: wednesday-solutions

项目描述 :
JSONAPI middleware for redux
高级语言: JavaScript
项目地址: git://github.com/wednesday-solutions/jsonapi-redux-data.git
创建时间: 2020-01-02T21:09:37Z
项目社区:https://github.com/wednesday-solutions/jsonapi-redux-data

开源协议:MIT License

下载


JsonApi Redux Data

JsonApi Redux data is a one stop shop for all your jsonapi needs!
Provides methods to make your API call and formats and updates data in the redux store.
It joins all of your relations so that accessing it is as easy as entity.relationship

For example if you made an API call to base-url/tasks?include=list, you can access your relationships as easily as tasks[index].list

  • Provides methods to make api calls to your JSONApi compliant backend and updates the redux store as well.

  • Combines the data so it is easily accessible

  • Setup in 3 easy steps

Installation

  1. yarn add jsonapi-redux-data

OR

  1. npm i jsonapi-redux-data

Usage

  • Update the reducers / rootReducers like this

    1. ...
    2. import { jsonApiReducer } from 'jsonapi-redux-data'
    3. ...
    4. const rootReducer = combineReducers({
    5. ...,
    6. api: jsonApiReducer,
    7. ...
    8. })
    9. ...
  • Create the api client preferrably in the app.js

    1. ...
    2. import { createApiClientWithTransform } from 'jsonapi-redux-data'
    3. ...
    4. // Create redux store with history
    5. const initialState = {};
    6. const store = configureStore(initialState, history);
    7. ...
    8. createApiClientWithTransform('<base-url>', store)
    9. ...
  • Make api call easily and from anywhere

    1. ...
    2. import { getApi } from 'jsonapi-redux-data'
    3. ...
    4. getApi({ pathname: '<pathname>', include: '<include-string>' })
    5. ...

Example Usage

getApi

  1. getApi({
  2. pathname: 'tasks',
  3. include: 'lists',
  4. levelOfNesting: 3
  5. });

Invoking this method will

  • make an api call to base-url/tasks
  • include lists in the response
  • dispatch an action of type SUCCESS_API
  • update the api reducer in the redux store with the formatted response.

API Documentation

getApi

Make a get request and add api response to the redux store.

  1. /**
  2. * @param {} requestPayload: {
  3. * include: object
  4. * filter: object,
  5. * pathname: String,
  6. * levelOfNesting: number,
  7. * transformList: object,
  8. * id: String
  9. * }
  10. * @param {} api: Custom Api Client instead of the latest created api client
  11. * @param {} axios: Special axios config
  12. **/
  13. function getApi (requestPayload, api, axiosConfig)

postApi

Make a post request and add api response to the redux store

  1. /**
  2. * @param {} requestPayload: {
  3. * include: object
  4. * filter: object,
  5. * pathname: String,
  6. * levelOfNesting: number,
  7. * transformList: object,
  8. * postData: object
  9. * }
  10. * @param {} api: Custom Api Client instead of the latest created api client
  11. * @param {} axios: Special axios config
  12. */
  13. function postApi (requestPayload, api, axiosConfig)

patchApi

Make a patch request and add api response to the redux store

  1. /**
  2. *
  3. * @param {} requestPayload: {
  4. * include: object
  5. * filter: object,
  6. * pathname: String,
  7. * levelOfNesting: number,
  8. * transformList: object,
  9. * patchData: object
  10. * }
  11. * @param {} api: Custom Api Client instead of the latest created api client
  12. * @param {} axios: Special axios config
  13. *
  14. * */
  15. function patchApi (requestPayload, api, axios)

deleteApi

Make a delete request and add api response to the redux store

  1. /**
  2. *
  3. * @param {} requestPayload: {
  4. * pathname: String,
  5. * id: String
  6. * }
  7. * @param {} api: Custom Api Client instead of the latest created api client
  8. * @param {} axios: Special axios config
  9. *
  10. * */
  11. function deleteApi (requestPayload, api, axios)

getRequest

GET HTTP REQUEST, uses the apisauce.get underneath the hood.

  1. /**
  2. * @param {} pathname: the endpoint path
  3. * @param {} include: the jsonapi include string
  4. * @param {} filter: the jsonapi filter string
  5. * @param {} id: the id of the GET request.
  6. * @param {} api: default is getLatestApiClient()
  7. * @param {} axiosConfig: custom axiosConfig for this request
  8. */
  9. function getRequest (pathname, include, filter, id, api, axiosConfig)

postRequest

POST HTTP REQUEST, uses the apisauce.get underneath the hood.

  1. /**
  2. *
  3. * @param {} pathname: the endpoint path
  4. * @param {} include: the jsonapi include string
  5. * @param {} filter: the jsonapi filter string
  6. * @param {} postData: request body
  7. * @param {} api: default is getLatestApiClient()
  8. * @param {} axiosConfig: custom axiosConfig for this request
  9. */
  10. function postRequest (pathname, include, filter, postData, api, axiosConfig)

patchRequest

PATCH HTTP REQUEST, uses the apisauce.get underneath the hood.

  1. /**
  2. * @param {} pathname
  3. * @param {} include: the jsonapi include string
  4. * @param {} filter: the jsonapi filter string
  5. * @param {} id: the id of the PATCH request.
  6. * @param {} patchData: request body
  7. * @param {} api: default is getLatestApiClient()
  8. * @param {} axiosConfig: custom axiosConfig for this request
  9. */
  10. function patchRequest (pathname, include, filter, id, patchData, api,axiosConfig)

deleteRequest

DELETE HTTP REQUEST, uses the apisauce.get underneath the hood.

  1. /**
  2. * @param {} pathname
  3. * @param {} id: the id of the DELETE request.
  4. * @param {} api: default is getLatestApiClient()
  5. * @param {} axiosConfig: custom axiosConfig for this request
  6. */
  7. function deleteRequest (pathname, id, api, axiosConfig)