lib/actions/api.js
function createQueryAction (endpoint, responseAction, errorAction, rewritePayload, postprocess) {
  return async function (dispatch, getState) {
    const otpState = getState().otp
    const api = otpState.config.api
    const url = `${api.host}${api.port ? ':' + api.port : ''}${api.path}/${endpoint}`
    let payload
    try {
      const response = await fetch(url)
      if (response.status >= 400) {
        const error = new Error('Received error from server')
        error.response = response
        throw error
      }
      payload = await response.json()
    } catch (err) {
      return dispatch(errorAction(err))
    }

    if (typeof rewritePayload === 'function') {
      dispatch(responseAction(rewritePayload(payload)))
    } else {
      dispatch(responseAction(payload))
    }

    if (typeof postprocess === 'function') {
      postprocess(payload, dispatch, getState)
    }
  }
}
function createQueryAction (endpoint, responseAction, errorAction, rewritePayload, postprocess) {
  return async function (dispatch, getState) {
    const otpState = getState().otp
    const api = otpState.config.api
    const url = `${api.host}${api.port ? ':' + api.port : ''}${api.path}/${endpoint}`
    let payload
    try {
      const response = await fetch(url)
      if (response.status >= 400) {
        const error = new Error('Received error from server')
        error.response = response
        throw error
      }
      payload = await response.json()
    } catch (err) {
      return dispatch(errorAction(err))
    }

    if (typeof rewritePayload === 'function') {
      dispatch(responseAction(rewritePayload(payload)))
    } else {
      dispatch(responseAction(payload))
    }

    if (typeof postprocess === 'function') {
      postprocess(payload, dispatch, getState)
    }
  }
}
function createQueryAction ( endpoint , responseAction , errorAction , rewritePayload , postprocess ) { return async function ( dispatch , getState ) { const otpState = getState ( ) . otp const api = otpState . config . api const url = ` ${ api . host } ${ api . port ? ':' + api . port : '' } ${ api . path } ${ endpoint } ` let payload try { const response = await fetch ( url ) if ( response . status >= 400 ) { const error = new Error ( 'Received error from server' ) error . response = response throw error } payload = await response . json ( ) } catch ( err ) { return dispatch ( errorAction ( err ) ) } if ( typeof rewritePayload === 'function' ) { dispatch ( responseAction ( rewritePayload ( payload ) ) ) } else { dispatch ( responseAction ( payload ) ) } if ( typeof postprocess === 'function' ) { postprocess ( payload , dispatch , getState ) } } }
generic helper for constructing API queries
generic helper for constructing API queries