-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
39 changed files
with
1,436 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@composite-fetcher/with-caching': patch | ||
--- | ||
|
||
test(with-caching): add test for failed responses |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@composite-fetcher/with-retries': patch | ||
--- | ||
|
||
test(with-retries): wip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@composite-fetcher/with-retries': patch | ||
--- | ||
|
||
chore(with-retries): work in progress |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"with-caching": "withCaching", | ||
"with-logging": "withLogging" | ||
"with-logging": "withLogging", | ||
"with-retries": "withRetries" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# WithRetries Plugin | ||
|
||
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. | ||
|
||
## How it Works | ||
|
||
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. | ||
|
||
## Lifecycle: | ||
- `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. | ||
|
||
## Plugin Options | ||
|
||
The `withRetries` plugin offers the following options: | ||
|
||
- `defaultMaxRetries`: Specifies the default maximum number of retries for each request. Default is 3. | ||
|
||
You can use the following custom headers for more granular control per request: | ||
- `x-fetcher-no-retry`: If present, the plugin will not retry the request, regardless of the response. | ||
- `x-fetcher-retry-times`: Specifies the maximum number of retries for the request. | ||
|
||
## Usage: | ||
|
||
```js copy | ||
import { Fetcher } from '@composite-fetcher/core'; | ||
import withRetriesPlugin from '@composite-fetcher/with-retries'; | ||
|
||
const fetcher = new Fetcher(); | ||
|
||
const retriesPlugin = new withRetriesPlugin(5); // retry up to 5 times by default | ||
fetcher.use([retriesPlugin]); | ||
|
||
fetcher.fetch('https://example.com/api/...') | ||
.then(response => { /* handle response */ }) | ||
.catch(error => { /* handle error */ }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
root: true, | ||
extends: ['@composite-fetcher/eslint-config/base'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
/node_modules | ||
/.pnp | ||
.pnp.js | ||
|
||
# testing | ||
/coverage | ||
|
||
# next.js | ||
/.next/ | ||
/out/ | ||
|
||
# production | ||
/build | ||
|
||
# misc | ||
.DS_Store | ||
*.pem | ||
|
||
# debug | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# local env files | ||
.env*.local | ||
|
||
# vercel | ||
.vercel | ||
|
||
# typescript | ||
*.tsbuildinfo | ||
next-env.d.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
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). | ||
|
||
## Getting Started | ||
|
||
First, run the development server: | ||
|
||
```bash | ||
npm run dev | ||
# or | ||
yarn dev | ||
# or | ||
pnpm dev | ||
# or | ||
bun dev | ||
``` | ||
|
||
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. | ||
|
||
You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file. | ||
|
||
This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font. | ||
|
||
## Learn More | ||
|
||
To learn more about Next.js, take a look at the following resources: | ||
|
||
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API. | ||
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial. | ||
|
||
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome! | ||
|
||
## Deploy on Vercel | ||
|
||
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. | ||
|
||
Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* eslint-disable import/prefer-default-export */ | ||
export async function GET() { | ||
return Response.json( | ||
{ success: false }, | ||
{ | ||
status: 422, | ||
}, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* eslint-disable import/prefer-default-export */ | ||
export async function GET() { | ||
return Response.json( | ||
{ success: true }, | ||
{ | ||
status: 200, | ||
}, | ||
); | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
@tailwind base; | ||
@tailwind components; | ||
@tailwind utilities; | ||
|
||
:root { | ||
--foreground-rgb: 0, 0, 0; | ||
--background-start-rgb: 214, 219, 220; | ||
--background-end-rgb: 255, 255, 255; | ||
} | ||
|
||
@media (prefers-color-scheme: dark) { | ||
:root { | ||
--foreground-rgb: 255, 255, 255; | ||
--background-start-rgb: 0, 0, 0; | ||
--background-end-rgb: 0, 0, 0; | ||
} | ||
} | ||
|
||
body { | ||
color: rgb(var(--foreground-rgb)); | ||
background: linear-gradient( | ||
to bottom, | ||
transparent, | ||
rgb(var(--background-end-rgb)) | ||
) | ||
rgb(var(--background-start-rgb)); | ||
} | ||
|
||
/* Firefox */ | ||
* { | ||
scrollbar-width: thin; | ||
scrollbar-color: var(--background-end-rgb) var(--foreground-rgb); | ||
} | ||
|
||
/* Chrome, Edge, and Safari */ | ||
*::-webkit-scrollbar { | ||
width: 15px; | ||
} | ||
|
||
*::-webkit-scrollbar-track { | ||
background: var(--foreground-rgb); | ||
border-radius: 5px; | ||
} | ||
|
||
*::-webkit-scrollbar-thumb { | ||
background-color: var(--background-end-rgb); | ||
border-radius: 14px; | ||
border: 3px solid var(--foreground-rgb); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import './globals.css'; | ||
import type { Metadata } from 'next'; | ||
import { Inter } from 'next/font/google'; | ||
import React from 'react'; | ||
|
||
const inter = Inter({ subsets: ['latin'] }); | ||
|
||
export const metadata: Metadata = { | ||
title: 'Composite Fetcher Examples', | ||
description: 'Composite Fetcher Plugin Examples ', | ||
}; | ||
|
||
export default function RootLayout({ | ||
children, | ||
}: { | ||
children: React.ReactNode; | ||
}) { | ||
return ( | ||
<html lang="en"> | ||
<body className={inter.className}>{children}</body> | ||
</html> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import RetryPluginExample from '@/components/RetryPluginExample'; | ||
import CachingPluginExample from '@/components/CachingPluginExample'; | ||
|
||
export default function Home() { | ||
return ( | ||
<> | ||
<div className="w-full p-4 text-center text-2xl font-bold"> | ||
<h1>Composite Fetcher Examples</h1> | ||
</div> | ||
<p className="text-sm font-normal text-muted max-w-xl mx-auto text-center"> | ||
When exploring these composite fetcher examples with various plugins, | ||
you might find it insightful to use the browser's inspector or developer | ||
tools. Depending on the specific example, examining network requests or | ||
caches can provide a deeper understanding. | ||
</p> | ||
<div className=" flex flex-col justify-center items-center pt-4"> | ||
<CachingPluginExample /> | ||
<RetryPluginExample /> | ||
</div> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* eslint-disable no-console */ | ||
|
||
'use client'; | ||
|
||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
// components/RetryPluginExample.tsx | ||
import React from 'react'; | ||
import { fetcherWithCaching as fetcher } from '@/lib/fetcher'; | ||
import PluginShowcaseLayout from '@/components/PluginShowcaseLayout'; | ||
import ConsoleLog from '@/components/ConsoleLog'; | ||
|
||
const CachingPluginExample: React.FC = () => { | ||
const handleFetch = async (url: string, options?: RequestInit) => { | ||
try { | ||
const res = await fetcher.fetch(url, options); | ||
const data = await res.json(); | ||
console.log('withCaching', data); | ||
} catch (error: any) { | ||
console.error('withCaching', error); | ||
} | ||
}; | ||
return ( | ||
<PluginShowcaseLayout title="withCaching Plugin"> | ||
<div className="w-full justify-evenly items-center flex pb-4"> | ||
<button | ||
onClick={() => handleFetch('https://dog.ceo/api/breeds/image/random')} | ||
className="bg-green-600 py-2 px-3 rounded-md mr-2 text-gray-200 hover:bg-green-700" | ||
> | ||
Success | ||
</button> | ||
<button | ||
onClick={() => handleFetch('/api/fail')} | ||
className="bg-red-600 py-2 px-3 rounded-md text-gray-200 hover:bg-red-700" | ||
> | ||
Failure | ||
</button> | ||
</div> | ||
<ConsoleLog filter="withCaching" /> | ||
</PluginShowcaseLayout> | ||
); | ||
}; | ||
|
||
export default CachingPluginExample; |
Oops, something went wrong.