From 8ec27254f3e3ae44b41fe7f005114d2133b5e651 Mon Sep 17 00:00:00 2001 From: Denis Gursky Date: Thu, 9 Mar 2023 13:21:52 -0500 Subject: [PATCH] fix fetch wrapping --- fetch.ts => src/fetch.node.ts | 8 ++++++-- src/fetch.web.ts | 21 +++++++++++++++++++++ src/rest.ts | 6 ++++-- webpack.config.js | 4 ++-- 4 files changed, 33 insertions(+), 6 deletions(-) rename fetch.ts => src/fetch.node.ts (80%) create mode 100644 src/fetch.web.ts diff --git a/fetch.ts b/src/fetch.node.ts similarity index 80% rename from fetch.ts rename to src/fetch.node.ts index d19d4d7..024787e 100644 --- a/fetch.ts +++ b/src/fetch.node.ts @@ -14,5 +14,9 @@ * under the License. */ -// Using browser's fetch in the browser env -export default globalThis.fetch; +import nodeFetch from 'node-fetch-commonjs'; +export type { Response } from 'node-fetch-commonjs'; + +export function getFetch() { + return nodeFetch; +} diff --git a/src/fetch.web.ts b/src/fetch.web.ts new file mode 100644 index 0000000..446bc08 --- /dev/null +++ b/src/fetch.web.ts @@ -0,0 +1,21 @@ +/** + * Copyright 2021 RelationalAI, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy + * of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +// Always using fetch from the global object in the browser +// This allows to wrap fetch in the app if necessary +export function getFetch() { + return globalThis.fetch; +} diff --git a/src/rest.ts b/src/rest.ts index 08beb0a..67e2233 100644 --- a/src/rest.ts +++ b/src/rest.ts @@ -14,10 +14,10 @@ * under the License. */ -import nodeFetch, { Response } from 'node-fetch-commonjs'; import { stringify } from 'query-string'; import { makeError } from './errors'; +import { getFetch, Response } from './fetch.node'; import { ApiResponse, VERSION } from './types'; const isNode = @@ -72,7 +72,9 @@ export async function request(url: string, options: RequestOptions = {}) { let response; try { - response = await nodeFetch(fullUrl, opts); + const fetch = getFetch(); + + response = await fetch(fullUrl, opts); } catch (error: any) { const errorMsg = error.message.toLowerCase(); diff --git a/webpack.config.js b/webpack.config.js index 3e656ae..1eb63ef 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -65,7 +65,7 @@ const webEsm = lodash.merge({}, baseConfig, { target: 'web', resolve: { alias: { - 'node-fetch-commonjs': path.resolve('fetch.ts'), + [path.resolve('./src/fetch.node.ts')]: path.resolve('./src/fetch.web.ts'), }, }, }); @@ -82,7 +82,7 @@ const webCjs = lodash.merge({}, baseConfig, { target: 'web', resolve: { alias: { - 'node-fetch-commonjs': path.resolve('fetch.ts'), + [path.resolve('./src/fetch.node.ts')]: path.resolve('./src/fetch.web.ts'), }, }, });