Skip to content

`fetchRetry` middleware

Compare
Choose a tag to compare
@neurosnap neurosnap released this 21 Feb 03:29
· 14 commits to main since this release
8ce5257
feat(fetcher): retry middleware (#29)

A new middleware for retrying fetch requests when `response.ok` is
`false`.  This middleware was designed to work on a per-endpoint basis.
This means that it should be added immediately after the endpoint
function.

```
import { createApi, requestMonitor, fetcher, fetchRetry } from
'saga-query';
const api = createApi();
api.use(requestMonitor());
api.use(api.routes());
api.use(fetcher());

const fetchUsers = api.get('/users', [
    function* (ctx, next) {
        // ...
        yield next();
        // ...
    },
    fetchRetry(),
])
```

It also supports a backoff function as a parameter.  The function
signature is `(attempt: number) => number`, the result of which is how
long to delay the next fetch attempt.  If a negative number is provided
then we stop retrying.