From 2ed2ef42e9f204d4896ada4e20b839cfabdc7284 Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Tue, 15 Nov 2022 19:59:33 +0330 Subject: [PATCH] feat(fetch): support nodejs --- demo/fetch/node.ts | 10 ++++++++++ packages/core/fetch/src/fetch.ts | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 demo/fetch/node.ts diff --git a/demo/fetch/node.ts b/demo/fetch/node.ts new file mode 100644 index 000000000..8e6be7bd9 --- /dev/null +++ b/demo/fetch/node.ts @@ -0,0 +1,10 @@ +/* eslint-disable @typescript-eslint/no-non-null-assertion */ +import {fetch} from '@alwatr/fetch'; + +const response = await fetch({ + url: 'http://httpbin.org/uuid', + timeout: 3_000, + removeDuplicate: 'auto', +}); + +console.log(await response.text()); diff --git a/packages/core/fetch/src/fetch.ts b/packages/core/fetch/src/fetch.ts index ebe6bcd29..58ca076ac 100644 --- a/packages/core/fetch/src/fetch.ts +++ b/packages/core/fetch/src/fetch.ts @@ -89,7 +89,7 @@ export interface FetchOptions extends RequestInit { } let alwatrCacheStorage: Cache; -const cacheSupported = 'caches' in self; +const cacheSupported = 'caches' in globalThis; const duplicateRequestStorage: Record> = {}; @@ -389,7 +389,7 @@ function _handleTimeout(options: FetchOptions): Promise { }); }); - window + globalThis .fetch(options.url, options) .then((response) => resolved(response)) .catch((reason) => reject(reason))