Skip to content

Commit

Permalink
refactor(fetch): update fetch package to use @alwatr/parse-duration f…
Browse files Browse the repository at this point in the history
…or timeout and retryDelay durations
  • Loading branch information
alimd committed Sep 29, 2024
1 parent cca1be2 commit 1108c54
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
3 changes: 2 additions & 1 deletion packages/fetch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@
"@alwatr/delay": "workspace:^",
"@alwatr/global-scope": "workspace:^",
"@alwatr/logger": "workspace:^",
"@alwatr/package-tracer": "workspace:^"
"@alwatr/package-tracer": "workspace:^",
"@alwatr/parse-duration": "workspace:^"
},
"devDependencies": {
"@alwatr/nano-build": "workspace:^",
Expand Down
5 changes: 3 additions & 2 deletions packages/fetch/src/core.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {delay} from '@alwatr/delay';
import {globalScope} from '@alwatr/global-scope';
import {createLogger} from '@alwatr/logger';
import {packageTracer} from '@alwatr/package-tracer';
import {delay} from '@alwatr/delay';
import {parseDuration} from '@alwatr/parse-duration';

import type {FetchOptions} from './type.js';

Expand Down Expand Up @@ -248,7 +249,7 @@ export function handleTimeout_(options: FetchOptions): Promise<Response> {
const timeoutId = setTimeout(() => {
reject(new Error('fetch_timeout'));
abortController?.abort('fetch_timeout');
}, options.timeout);
}, parseDuration(options.timeout!));

// abortController.signal.addEventListener('abort', () => {
// logger.incident('fetch', 'fetch_abort_signal', {
Expand Down
9 changes: 5 additions & 4 deletions packages/fetch/src/type.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type {Duration} from '@alwatr/parse-duration';
import type {Dictionary, Json, JsonObject} from '@alwatr/type-helper';

/**
Expand Down Expand Up @@ -59,9 +60,9 @@ export interface FetchOptions extends RequestInit {
*
* Use with caution, as it may cause memory leaks in Node.js.
*
* @default 8_000 ms
* @default '8s'
*/
timeout?: number;
timeout?: Duration;

/**
* If the fetch response is not acceptable or timed out, it will retry the request.
Expand All @@ -73,9 +74,9 @@ export interface FetchOptions extends RequestInit {
/**
* Delay before each retry.
*
* @default 1_000 ms
* @default '1s'
*/
retryDelay?: number;
retryDelay?: Duration;

/**
* Simple memory caching to remove duplicate/parallel requests.
Expand Down
1 change: 1 addition & 0 deletions packages/fetch/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
{"path": "../logger"},
{"path": "../global-scope"},
{"path": "../delay"},
{"path": "../parse-duration"},
{"path": "../type-helper"}
]
}

0 comments on commit 1108c54

Please sign in to comment.