基于响应的条件API调用-重构可维护性

时间:2019-04-02 18:48:55

标签: javascript ecmascript-6 refactoring

以下函数需要首先进行API调用,如果该API调用的响应不满足某些条件,则需要再次调用相同的API,这次的查询略有不同(上一年而不是当前年份)年)。该函数的目标是调度具有某个month(字符串)值的操作。响应数据如下所示:

[  
    {  
       "month":2,
       "year":2019,
       "status":"product is not ready to go yet"
    },
    {  
       "month":10,
       "year":2019,
       "status":"product is not ready to go yet"
    },
    {  
       "month":1,
       "year":2018,
       "status":"product is not ready to go yet"
    },
    {  
       "month":5,
       "year":2018,
       "status":"product is ready to go"
    }
 ]

这是我的功能:

const months = [{key: '1', value: 'January'}] //and so on
export function getProductStatus(productId) {
  const year = moment().format('YYYY');
  const previousYear = moment().subtract(1, 'year').format('YYYY');
  let targetURL = `www.someAPI.com/api/data/${productId}?year=${year}`;
  let targetMonth = '';
  return async dispatch => {
    try{
      let productResponse = await makeRequest(targetURL);
      const currentYearWithMonth = productResponse.find(res => res.status.toLowerCase() === 'product is ready to go')
      if(currentYearWithMonth) {
        targetMonth = months.find(month => month.key === currentYearWithMonth.month.toString())
      }
      else {
        targetURL = `www.someAPI.com/api/data/${productId}?year=${previousYear}`;
        productResponse = await makeRequest(targetURL);
        const prevYearWithMonth = productResponse.find(res => res.status.toLowerCase() === 'product is ready to go')
        if(prevYearWithMonth) {
          targetMonth = months.find(month => month.key === prevYearWithMonth.month.toString())
        }
      }
      dispatch({ type: GET_PRODUCT_STATUS, payload: targetMonth ? targetMonth.value : null }) // better way to handle this too?
    } catch (error) {
      console.log('error => ', error)
    }
  }
}

此逻辑的问题在于,它执行了该函数所期望的操作,因为它返回了正确的月份值,但该逻辑看起来非常重复。一方面,该函数确实需要首先调用API,检查值,但另一方面,如果第一次调用时未获取值,则还需要再次调用API。重构这段代码的好方法是什么?

0 个答案:

没有答案