@abramstyle/redux-api 中文文档教程
Redux API
进行 api 调用的 redux 中间件,但带有中间件。
usage
install
yarn add @abramstyle/redux-api
quick start
import {createStore, applyMiddleware } from 'redux';
import reduxApiGenerator from '@abramstyle/redux-api';
const reduxApi = reduxApiGenerator();
const middlewares = [reduxApi];
const store = createStore(reducers, applyMiddlewares(middlewares));
API
reduxApiGenerator(options = {})
它返回一个 reduxMiddleware
options.before
函数,该函数将在请求之前转换 fetchOptions。
您可以为每个请求做一些常见的事情。
options.middleware
定义全局中间件就像callAPI.middleware
options.isSuccess
定义全局成功检查器就像callAPI.isSuccess
options.success
定义全局成功回调就像callAPI.success
options.failure
定义全局失败回调就像 callAPI.failure
CALL_API Action
example
一个 api 请求操作具有 [CALL_API] 属性。 它指定了请求信息。
import {CALL_API} from '@abramstyle/redux-api';
const action = {
[CALL_API]: {
url: 'https://api.domain.com',
types: [
'REQUEST_TYPE',
'SUCCESS_TYPE',
'FAILURE_TYPE'
],
}
};
properties
callAPI.credentials
省略、同源或包含之一。 必须是一个字符串。
callAPI.headers
必须是一个对象。 指定的请求标头。
callAPI.query
它将作为查询附加到 url。
callAPI.data
数据应该发送到服务器。 如果它是一个 get 方法,它将作为查询附加到 url。
callAPI.method
请求方法,应该是一个有效的方法。
callAPI.isSuccess
检查请求是否成功。
resultPayload 将被传递到函数中。
如果结果成功,则返回 true
或 false
到标识。
注意: 它将覆盖全局 options.isSuccess
callAPI.success
此函数将在请求成功完成后调用。
结果有效负载将传递到函数中。
注意: 它将覆盖全局 options.success
callAPI.failure
此函数将在请求失败后调用。
结果有效负载将传递到函数中。
注意:它将覆盖全局 options.failure
#
Lifecycle
api 请求具有以下状态:初始、请求、成功、失败。
redux-api 将调度这些操作。
一旦调用 api 操作被调度,就会返回一个承诺。
request action:
{
type: requestType,
payload: callAPI.data || {},
meta: callAPI.meta || {}
}
success action
{
type: successType,
payload: parsedResponseData,
meta: callAPI.meta || {},
}
failure action
{
type: failureType,
payload: errorInstance,
meta: callAPI.meta,
error: true,
}
注意:有效载荷是一个错误实例。 http 状态代码将被发现为 error.status。 从服务器响应的错误消息将被发现为 error.data。 完整的响应将作为 error.response 找到。
TODO
- add tests for new features
Redux API
A redux middleware making api call, but with middlewares.
usage
install
yarn add @abramstyle/redux-api
quick start
import {createStore, applyMiddleware } from 'redux';
import reduxApiGenerator from '@abramstyle/redux-api';
const reduxApi = reduxApiGenerator();
const middlewares = [reduxApi];
const store = createStore(reducers, applyMiddlewares(middlewares));
API
reduxApiGenerator(options = {})
it returns a reduxMiddleware
options.before
an function that will transform fetchOptions before request.
you can do some common things for every request.
options.middleware
define global middleware just like callAPI.middleware
options.isSuccess
define global success checker just like callAPI.isSuccess
options.success
define global success callback just like callAPI.success
options.failure
define global failure callback just like callAPI.failure
CALL_API Action
example
a api requesting action has [CALL_API] property. it specified request info.
import {CALL_API} from '@abramstyle/redux-api';
const action = {
[CALL_API]: {
url: 'https://api.domain.com',
types: [
'REQUEST_TYPE',
'SUCCESS_TYPE',
'FAILURE_TYPE'
],
}
};
properties
callAPI.credentials
one of omit, same-origin, or include. must be a string.
callAPI.headers
must be an object. specified request headers.
callAPI.query
it will be append to url as query.
callAPI.data
data should be sent to server. if it is an get method, it will be append to url as query.
callAPI.method
request method, should be a valid method.
callAPI.isSuccess
check if a request is success.
the resultPayload will be passed into the function.
return true
or false
to identity if result is succeed.
NOTE: It will override global options.isSuccess
callAPI.success
this function will be called after request is success finished.
the result payload will be passed into the function.
NOTE: It will override global options.success
callAPI.failure
this function will be called after request is failure.
the result payload will be passed into the function.
NOTE: It will override global options.failure
#
Lifecycle
an api request has these status: initial, requesting, success, failure.
redux-api will dispatch these actions.
once a call api action is dispatched, an promise is returned.
request action:
{
type: requestType,
payload: callAPI.data || {},
meta: callAPI.meta || {}
}
success action
{
type: successType,
payload: parsedResponseData,
meta: callAPI.meta || {},
}
failure action
{
type: failureType,
payload: errorInstance,
meta: callAPI.meta,
error: true,
}
NOTE: payload is an error instance. http status code will be found as error.status. the error message responded from server will be found as error.data. the full response will be find as error.response.
TODO
- add tests for new features