Skip to content

Commit

Permalink
feat(plugins): add withRetries plugin (#67)
Browse files Browse the repository at this point in the history
* 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
teofanis authored Oct 12, 2023
1 parent 9715528 commit 8671216
Show file tree
Hide file tree
Showing 39 changed files with 1,436 additions and 121 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-wombats-crash.md
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
5 changes: 5 additions & 0 deletions .changeset/large-singers-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@composite-fetcher/with-retries': patch
---

test(with-retries): wip
5 changes: 5 additions & 0 deletions .changeset/polite-horses-sit.md
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
3 changes: 2 additions & 1 deletion apps/docs/src/pages/plugins/_meta.json
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"
}
35 changes: 35 additions & 0 deletions apps/docs/src/pages/plugins/with-retries.mdx
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 */ });
14 changes: 11 additions & 3 deletions apps/docs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"exclude": ["node_modules"],
"exclude": [
"node_modules"
],
"extends": "@composite-fetcher/tsconfig/nextjs.json",
"compilerOptions": {
"outDir": "dist",
Expand All @@ -8,7 +10,13 @@
{
"name": "next"
}
]
],
"strictNullChecks": true
},
"include": ["src", "next-env.d.ts", ".next/types/**/*.ts", "theme.config.jsx"]
"include": [
"src",
"next-env.d.ts",
".next/types/**/*.ts",
"theme.config.jsx"
]
}
4 changes: 4 additions & 0 deletions apps/examples/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
root: true,
extends: ['@composite-fetcher/eslint-config/base'],
};
35 changes: 35 additions & 0 deletions apps/examples/.gitignore
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
36 changes: 36 additions & 0 deletions apps/examples/README.md
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.
9 changes: 9 additions & 0 deletions apps/examples/app/api/fail/route.ts
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,
},
);
}
9 changes: 9 additions & 0 deletions apps/examples/app/api/success/route.ts
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 added apps/examples/app/favicon.ico
Binary file not shown.
49 changes: 49 additions & 0 deletions apps/examples/app/globals.css
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);
}
23 changes: 23 additions & 0 deletions apps/examples/app/layout.tsx
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>
);
}
22 changes: 22 additions & 0 deletions apps/examples/app/page.tsx
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>
</>
);
}
43 changes: 43 additions & 0 deletions apps/examples/components/CachingPluginExample.tsx
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;
Loading

0 comments on commit 8671216

Please sign in to comment.