0.0.7 • Published 5 years ago

kiss-gcp v0.0.7

Weekly downloads
1
License
ISC
Repository
github
Last release
5 years ago

KISS-FETCH-RETRY

When fetching resources that require retry on failure ...

import fetchRetryFactory from "kiss-fetch-retry";

const fetchRetry = retryFetchFactory({
  maxRetries: 5,
  fetch: fetch, 
  shouldRetry (response) {
    return [408, 429, 500, 502, 503].indexOf(response.status) === -1
      ? testErrors(getErrorPayload(response))
      : true
  }
})

function testErrors (errors) {
  if (!errors) return false
  for (const e of errors) {
    const reason = e.reason
    if (reason && (reason === 'rateLimitExceeded' || reason === 'userRateLimitExceeded' )) return true
  }
  return false
}

function getErrorPayload (response) {
  try {
    return JSON.parse(response.body).error.errors
  } catch (e) {
  }
}


// We can now use fetchRetry anywhere that we would have used FetchAPI.

const response = await fetchRetry('http://example.com/api/getSomething')
0.0.7

5 years ago

0.0.5

5 years ago

0.0.6

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago