Skip to content

Commit c150f17

Browse files
authored
Merge pull request #331 from contentful/feat/add-override-accept-encoding-header
2 parents c7841fe + 2721dcf commit c150f17

File tree

3 files changed

+5
-28
lines changed

3 files changed

+5
-28
lines changed

src/create-http-client.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import type { AxiosInstance, CreateHttpClientParams } from './types'
88
import rateLimitRetry from './rate-limit'
99
import asyncToken from './async-token'
1010

11-
import { isNode, getNodeVersion } from './utils'
12-
1311
// Matches 'sub.host:port' or 'host:port' and extracts hostname and port
1412
// Also enforces toplevel domain specified, no spaces and no protocol
1513
const HOST_REGEX = /^(?!\w+:\/\/)([^\s:]+\.?[^\s:]+)(?::(\d+))?(?!:)$/
@@ -87,14 +85,6 @@ export default function createHttpClient(
8785
config.headers.Authorization = 'Bearer ' + config.accessToken
8886
}
8987

90-
// Set these headers only for node because browsers don't like it when you
91-
// override user-agent or accept-encoding.
92-
// The SDKs should set their own X-Contentful-User-Agent.
93-
if (isNode()) {
94-
config.headers['user-agent'] = 'node.js/' + getNodeVersion()
95-
config.headers['Accept-Encoding'] = 'gzip'
96-
}
97-
9888
const axiosOptions = {
9989
// Axios
10090
baseURL,

src/utils.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
export function isNode(): boolean {
22
/**
3-
* Polyfills of 'process' might set process.browser === true
4-
*
5-
* See:
6-
* https://github.com/webpack/node-libs-browser/blob/master/mock/process.js#L8
7-
* https://github.com/defunctzombie/node-process/blob/master/browser.js#L156
8-
**/
9-
return typeof process !== 'undefined' && !process.browser
3+
* Save way to check for the global scope which should confirm if an environment is node
4+
* For reference: https://stackoverflow.com/a/31090240
5+
*/
6+
const isNodeFunc = new Function('try {return this===global;}catch(e){return false;}')
7+
return isNodeFunc()
108
}
119

1210
export function isReactNative(): boolean {

test/unit/utils-test.spec.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,6 @@ describe('utils-test', () => {
77
expect(isNode()).toEqual(true)
88
})
99

10-
it('Detects node properly with babel-polyfill', () => {
11-
// @ts-ignore
12-
global.process.browser = true
13-
// detects non-node environment with babel-polyfill
14-
expect(isNode()).toEqual(false)
15-
// property here as it does not exist on type 'Process'.
16-
// TODO It's unclear why we are using the browser
17-
// @ts-ignore
18-
delete global.process.browser
19-
})
20-
2110
it('Detects node version', () => {
2211
const version = getNodeVersion()
2312
expect(

0 commit comments

Comments
 (0)