@hz-solutions/restful-api-interceptor
v1.0.4
Published
restful-api-interceptor是一个axios插件,用来对API返回的结果进行反序列化。
Downloads
10
Readme
简介
restful-api-interceptor是一个axios插件,用来对API返回的结果进行反序列化。
API数据约定
{
"data": {
"type": "Order",
"attributes": {
"id": 1,
"price": "12.98",
"description": "some string data",
"created_at": "2019-03-18T03:58:19.286Z",
}
},
"meta": {
"pagination": {
"timestamp": "2019-03-18T03:58:19.286Z",
"current": 1,
"total": 10,
"count": 98
}
}
}
用法
API返回的结果会根据transformer定义的规则执行转换。
import RestfulApiInterceptor, {
date,
bigNumber,
} from '@hz-solutions/restful-api-interceptor';
const interceptor = new RestfulApiInterceptor();
interceptor.registerEntity({
type: 'Order',
transformer: {
price: bigNumber,
created_at: date,
},
});
interceptor.registerMeta({
type: 'pagination',
transformer: {
timestamp: date,
},
});
axios.interceptors.response.use(interceptor.responseInterceptor);