Skip to content

Commit 8671216

Browse files
authored
feat(plugins): add withRetries plugin (#67)
* test(with-caching): add test for failed responses * chore(with-retries): work in progress * test(with-retries): wip * feat(plugins): `withRetries` plugin
1 parent 9715528 commit 8671216

39 files changed

+1436
-121
lines changed

.changeset/hungry-wombats-crash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@composite-fetcher/with-caching': patch
3+
---
4+
5+
test(with-caching): add test for failed responses

.changeset/large-singers-switch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@composite-fetcher/with-retries': patch
3+
---
4+
5+
test(with-retries): wip

.changeset/polite-horses-sit.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@composite-fetcher/with-retries': patch
3+
---
4+
5+
chore(with-retries): work in progress
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"with-caching": "withCaching",
3-
"with-logging": "withLogging"
3+
"with-logging": "withLogging",
4+
"with-retries": "withRetries"
45
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# WithRetries Plugin
2+
3+
The `withRetries` plugin enhances the reliability of your fetch operations by automatically retrying failed requests. This can be particularly useful in scenarios where transient network issues might cause requests to fail temporarily.
4+
5+
## How it Works
6+
7+
The `withRetries` plugin monitors the response of each fetch request. If a request fails, the plugin will automatically retry the request for a specified number of times until it succeeds or the maximum number of retries is reached.
8+
9+
## Lifecycle:
10+
- `onPostRequest`: After receiving a response, if the response is not successful and the request does not have the `x-fetcher-no-retry` header, the plugin will retry the request.
11+
12+
## Plugin Options
13+
14+
The `withRetries` plugin offers the following options:
15+
16+
- `defaultMaxRetries`: Specifies the default maximum number of retries for each request. Default is 3.
17+
18+
You can use the following custom headers for more granular control per request:
19+
- `x-fetcher-no-retry`: If present, the plugin will not retry the request, regardless of the response.
20+
- `x-fetcher-retry-times`: Specifies the maximum number of retries for the request.
21+
22+
## Usage:
23+
24+
```js copy
25+
import { Fetcher } from '@composite-fetcher/core';
26+
import withRetriesPlugin from '@composite-fetcher/with-retries';
27+
28+
const fetcher = new Fetcher();
29+
30+
const retriesPlugin = new withRetriesPlugin(5); // retry up to 5 times by default
31+
fetcher.use([retriesPlugin]);
32+
33+
fetcher.fetch('https://example.com/api/...')
34+
.then(response => { /* handle response */ })
35+
.catch(error => { /* handle error */ });

apps/docs/tsconfig.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{
2-
"exclude": ["node_modules"],
2+
"exclude": [
3+
"node_modules"
4+
],
35
"extends": "@composite-fetcher/tsconfig/nextjs.json",
46
"compilerOptions": {
57
"outDir": "dist",
@@ -8,7 +10,13 @@
810
{
911
"name": "next"
1012
}
11-
]
13+
],
14+
"strictNullChecks": true
1215
},
13-
"include": ["src", "next-env.d.ts", ".next/types/**/*.ts", "theme.config.jsx"]
16+
"include": [
17+
"src",
18+
"next-env.d.ts",
19+
".next/types/**/*.ts",
20+
"theme.config.jsx"
21+
]
1422
}

apps/examples/.eslintrc.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
root: true,
3+
extends: ['@composite-fetcher/eslint-config/base'],
4+
};

apps/examples/.gitignore

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# next.js
12+
/.next/
13+
/out/
14+
15+
# production
16+
/build
17+
18+
# misc
19+
.DS_Store
20+
*.pem
21+
22+
# debug
23+
npm-debug.log*
24+
yarn-debug.log*
25+
yarn-error.log*
26+
27+
# local env files
28+
.env*.local
29+
30+
# vercel
31+
.vercel
32+
33+
# typescript
34+
*.tsbuildinfo
35+
next-env.d.ts

apps/examples/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
2+
3+
## Getting Started
4+
5+
First, run the development server:
6+
7+
```bash
8+
npm run dev
9+
# or
10+
yarn dev
11+
# or
12+
pnpm dev
13+
# or
14+
bun dev
15+
```
16+
17+
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
18+
19+
You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.
20+
21+
This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font.
22+
23+
## Learn More
24+
25+
To learn more about Next.js, take a look at the following resources:
26+
27+
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
28+
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
29+
30+
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!
31+
32+
## Deploy on Vercel
33+
34+
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
35+
36+
Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* eslint-disable import/prefer-default-export */
2+
export async function GET() {
3+
return Response.json(
4+
{ success: false },
5+
{
6+
status: 422,
7+
},
8+
);
9+
}

0 commit comments

Comments
 (0)