Skip to content
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
8 changes: 6 additions & 2 deletions fetch.ts → src/fetch.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
21 changes: 21 additions & 0 deletions src/fetch.web.ts
Original file line number Diff line number Diff line change
@@ -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;
}
6 changes: 4 additions & 2 deletions src/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -72,7 +72,9 @@ export async function request<T>(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();

Expand Down
4 changes: 2 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
},
},
});
Expand All @@ -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'),
},
},
});
Expand Down