Skip to content

Commit 8eea9de

Browse files
author
Denis Gursky
authored
fix fetch wrapping (#84)
1 parent 6e145db commit 8eea9de

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

fetch.ts renamed to src/fetch.node.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,9 @@
1414
* under the License.
1515
*/
1616

17-
// Using browser's fetch in the browser env
18-
export default globalThis.fetch;
17+
import nodeFetch from 'node-fetch-commonjs';
18+
export type { Response } from 'node-fetch-commonjs';
19+
20+
export function getFetch() {
21+
return nodeFetch;
22+
}

src/fetch.web.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Copyright 2021 RelationalAI, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy
6+
* of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
// Always using fetch from the global object in the browser
18+
// This allows to wrap fetch in the app if necessary
19+
export function getFetch() {
20+
return globalThis.fetch;
21+
}

src/rest.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
* under the License.
1515
*/
1616

17-
import nodeFetch, { Response } from 'node-fetch-commonjs';
1817
import { stringify } from 'query-string';
1918

2019
import { makeError } from './errors';
20+
import { getFetch, Response } from './fetch.node';
2121
import { ApiResponse, VERSION } from './types';
2222

2323
const isNode =
@@ -72,7 +72,9 @@ export async function request<T>(url: string, options: RequestOptions = {}) {
7272
let response;
7373

7474
try {
75-
response = await nodeFetch(fullUrl, opts);
75+
const fetch = getFetch();
76+
77+
response = await fetch(fullUrl, opts);
7678
} catch (error: any) {
7779
const errorMsg = error.message.toLowerCase();
7880

webpack.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const webEsm = lodash.merge({}, baseConfig, {
6565
target: 'web',
6666
resolve: {
6767
alias: {
68-
'node-fetch-commonjs': path.resolve('fetch.ts'),
68+
[path.resolve('./src/fetch.node.ts')]: path.resolve('./src/fetch.web.ts'),
6969
},
7070
},
7171
});
@@ -82,7 +82,7 @@ const webCjs = lodash.merge({}, baseConfig, {
8282
target: 'web',
8383
resolve: {
8484
alias: {
85-
'node-fetch-commonjs': path.resolve('fetch.ts'),
85+
[path.resolve('./src/fetch.node.ts')]: path.resolve('./src/fetch.web.ts'),
8686
},
8787
},
8888
});

0 commit comments

Comments
 (0)