Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

retry revision #19

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,36 @@ const myFetch = configureFetch(
token: '...',

// Determines whether to add a header
filter: req => req.url.includes('/api/'),
})

filter: (req) => req.url.includes('/api/'),
}),
// ...or like this
jwt({
// "token" can be function that should return string or null or Promise<string | null>
token: () => getJwtFromSomewhere(),
})
}),
),
);
```

### `retry`

Returns a middleware that will retry the request until either:

- or the retries count is exceeded;
- or until a successful response is received.

```ts
import { applyMiddleware, configureFetch } from '@krutoo/fetch-tools';
import { retry } from '@krutoo/fetch-tools/middleware';

const myFetch = configureFetch(
fetch,
applyMiddleware(
retry({
count: 5,
whenNotOk: true,
whenCatch: false,
}),
),
);
```
Expand Down Expand Up @@ -310,8 +332,8 @@ his cookies in requests.

In this case you can use just `defaultHeaders` middleware:

```js
import { configureFetch, applyMiddleware } from '@krutoo/fetch-tools';
```ts
import { applyMiddleware, configureFetch } from '@krutoo/fetch-tools';
import { defaultHeaders } from '@krutoo/fetch-tools/middleware';

// example of server handler
Expand All @@ -325,9 +347,13 @@ async function handler(request: Request) {
);

// this request will contain cookies from the incoming request
const orders = await myFetch('http://something.com/api/user/orders').then(res => res.json());
const orders = await myFetch('http://something.com/api/user/orders').then(
(res) => res.json(),
);

return new Response(JSON.stringify({ orders }), { 'content-type': 'application/json' });
return new Response(JSON.stringify({ orders }), {
'content-type': 'application/json',
});
}
```

Expand Down
2 changes: 1 addition & 1 deletion src/middleware/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export {
type LogHandlerFactory,
} from './log.ts';

export { retry, type RetryConfig } from './retry.ts';
export { retry, type RetryOptions } from './retry.ts';

export { validateStatus, type ValidateStatusOptions } from './validate-status.ts';

Expand Down
7 changes: 4 additions & 3 deletions src/middleware/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Middleware } from '#fetch';
import { dump } from '#response';

/** Retry middleware config. */
export interface RetryConfig {
export interface RetryOptions {
/** Retries count. */
count?: number;

Expand All @@ -17,11 +17,12 @@ export interface RetryConfig {
* Returns a middleware that will retry the request until either:
* - or the retries count is exceeded;
* - or until a successful response is received.
* @param init Count or config.
* @param init Repeat count or options.
* @returns Middleware.
* @todo Consider AbortSignal.
*/
export function retry(
init: number | RetryConfig,
init: number | RetryOptions,
): Middleware {
const { count = 1, whenNotOk = true, whenCatch = true } = typeof init === 'number'
? { count: init }
Expand Down
7 changes: 6 additions & 1 deletion src/response/dump.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/**
* This function need for Node.js and undici.
* Calls cancel on response.body if it is present.
*
* This is need for Node.js and undici.
* Details: https://github.com/nodejs/undici/discussions/2979#discussioncomment-8865341.
*
* This is also useful in Deno when you don't read the response body.
* For example in `deno test`.
* @param response Response.
*/
export async function dump(response: Response) {
Expand Down