1 | # redux-api
|
2 |
|
3 | [![version][0]][1] [![downloads][2]][3]
|
4 |
|
5 | Redux-api is a library which can easily write client to communicate with backends. It generates actions and selectors for making ajax calls to API endpoint. Also, it use saga to change ajax state and save result in redux store with few config.
|
6 |
|
7 | ## Use
|
8 |
|
9 | ### Config reducer
|
10 |
|
11 | Redux-api provide two reducers: Api reducer and Entities reducer;
|
12 |
|
13 | Api reducer store api state: result, loading, request
|
14 | Entities reducer store entities of normalizr
|
15 |
|
16 | ```js
|
17 | // reducer.js
|
18 | import { combineReducers } from "redux";
|
19 |
|
20 | import entities from "./entity";
|
21 | import { apiReducers } from "@36node/redux-api";
|
22 |
|
23 | /**
|
24 | * apiReducers: {api: apiReducer, entities: entitiesReducer}
|
25 | * */
|
26 | export default combineReducers({
|
27 | ...apiReducers,
|
28 | });
|
29 | ```
|
30 |
|
31 | ### Config saga
|
32 |
|
33 | ```js
|
34 | // saga.js
|
35 | import { fork, all } from "redux-saga/effects";
|
36 | import { watchApis } from "@36node/redux-api";
|
37 |
|
38 | export default function* root() {
|
39 | yield all([fork(watchApis)]);
|
40 | }
|
41 | ```
|
42 |
|
43 | ### Write an endpoint function of an api
|
44 |
|
45 | ```js
|
46 | import fetch from "@36node/fetch";
|
47 |
|
48 | // endpoint function param is request action payload
|
49 | function listPetsEndpoint(req) {
|
50 | const { query, headers } = req;
|
51 |
|
52 | // also can user other ajax tools, should return a promise
|
53 | return fetch(`${some_url}/pets`, {
|
54 | method: "get",
|
55 | query,
|
56 | headers,
|
57 | });
|
58 | }
|
59 | ```
|
60 |
|
61 | ### Register api in actions
|
62 |
|
63 | ```js
|
64 | // actions.js
|
65 | import { createApiAction } from "@36node/redux-api";
|
66 |
|
67 | export const listPets = createApiActions("LIST_PETS", {
|
68 | // schema for normalizr
|
69 | schema: [petSchema],
|
70 | endpoint: listPetsEndpoint,
|
71 | });
|
72 | ```
|
73 |
|
74 | ### Dispatch action
|
75 |
|
76 | ```js
|
77 | import {listPets} from 'actions'
|
78 |
|
79 | ...
|
80 |
|
81 | fetchPets = () => {
|
82 | // dispatch a request action
|
83 | this.props.dispatch(listPets.request({query: {limit: 10, offset: 0}}));
|
84 | }
|
85 |
|
86 | refreshPets = () => {
|
87 | // refresh action means repeat last request action
|
88 | this.props.dispatch(listPets.refresh());
|
89 | }
|
90 |
|
91 | clearPets = () => {
|
92 | // clear action means clear api state in store
|
93 | this.props.dispatch(listPets.clear());
|
94 | }
|
95 | ...
|
96 | ```
|
97 |
|
98 | ### Select api state
|
99 |
|
100 | ```js
|
101 | import React from "react";
|
102 | import { connect } from "redux-react";
|
103 | import { apiSelector } from "@36node/redux-api";
|
104 |
|
105 | @connect(state => {
|
106 | const listPetsState = apiSelector("LIST_PETS")(state);
|
107 |
|
108 | return {
|
109 | loading: listPetsState.loading,
|
110 | pets: listPetsState.result || [],
|
111 | };
|
112 | })
|
113 | class SomeContainer extends React.Component {}
|
114 | ```
|
115 |
|
116 | [0]: https://img.shields.io/npm/v/@36node/redux-api.svg?style=flat
|
117 | [1]: https://npmjs.com/package/@36node/redux-api
|
118 | [2]: https://img.shields.io/npm/dm/@36node/redux-api.svg?style=flat
|
119 | [3]: https://npmjs.com/package/@36node/redux-api
|