我想创建一个通用服务层来处理reactjs中的所有请求。
这是我到目前为止完成的代码:
这是服务层的重击动作。 postRequest方法获取调用api所需的所有参数。
bankCustomerThunkAction.js
export const fetchBankCustomers = (filterType, searchParam) => dispatch => {
apiServices.postRequest(urlBuilders.getUrlAppenderWithParams(bankCustomerApiConstant.GET_BANK_CUSTOMER_STATISTICS, "filter", filterType), searchParam).then((response) => dispatch(actions.getBankCustomerStatisticsWithDate(response))).catch(error => console.log("error", error))
}
这是请求后端api的唯一服务层
service.js
export const postRequest = (url, data) => Axios({method: 'POST' , url:url , data : data})
足以做到这一点还是有其他选择?
答案 0 :(得分:1)
我猜你做的方式是正确的。
我可以提一点建议,当您为特定请求需要不同的标头时,这将有所帮助。
即
bankCustomerThunkAction.js-> DataService.js-> service.js
bankCustomerThunkAction.js
dataService.bankCustomerThunkAction().then().catch()
DataService.js->您可以在此处输入标头代码或任何可选的参数代码
export bankCustomerThunkAction ()=>apiServices.postRequest(urlBuilders.getUrlAppenderWithParams(bankCustomerApiConstant.GET_BANK_CUSTOMER_STATISTICS, "filter", filterType), searchParam).then((response) => dispatch(actions.getBankCustomerStatisticsWithDate(response))).catch(error => console.log("error", error))