diff --git a/packages/fetch/package.json b/packages/fetch/package.json index 721dc0e9..1a92dcf8 100644 --- a/packages/fetch/package.json +++ b/packages/fetch/package.json @@ -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:^", diff --git a/packages/fetch/src/core.ts b/packages/fetch/src/core.ts index b68dcec9..71514237 100644 --- a/packages/fetch/src/core.ts +++ b/packages/fetch/src/core.ts @@ -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'; @@ -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', { diff --git a/packages/fetch/src/type.ts b/packages/fetch/src/type.ts index 4ff25eb1..8291bb98 100644 --- a/packages/fetch/src/type.ts +++ b/packages/fetch/src/type.ts @@ -1,3 +1,4 @@ +import type {Duration} from '@alwatr/parse-duration'; import type {Dictionary, Json, JsonObject} from '@alwatr/type-helper'; /** @@ -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. @@ -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. diff --git a/packages/fetch/tsconfig.json b/packages/fetch/tsconfig.json index 796ac28b..000a38cf 100644 --- a/packages/fetch/tsconfig.json +++ b/packages/fetch/tsconfig.json @@ -13,6 +13,7 @@ {"path": "../logger"}, {"path": "../global-scope"}, {"path": "../delay"}, + {"path": "../parse-duration"}, {"path": "../type-helper"} ] }