Skip to content

Commit ed9b2fe

Browse files
committed
fix(httpClient): Send library version with each request headers
1 parent 50263d1 commit ed9b2fe

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

lib/create-http-client.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* @flow */
2-
import {defaults} from 'lodash/object'
32
import qs from 'querystring'
3+
import packageFile from '../package.json'
44

55
type HttpClientParams = {
66
space: string,
@@ -19,13 +19,13 @@ export default function createHttpClient (axios: Object, {space, accessToken, in
1919
let [hostname, port] = (host && host.split(':')) || []
2020
hostname = hostname || 'cdn.contentful.com'
2121
port = port || (insecure ? 80 : 443)
22+
headers = headers || {}
23+
headers['Content-Type'] = 'application/vnd.contentful.delivery.v1+json'
24+
headers['X-Contentful-User-Agent'] = 'contentful.js/' + packageFile.version
2225

2326
return axios.create({
2427
baseURL: `${insecure ? 'http' : 'https'}://${hostname}:${port}/spaces/${space}/`,
25-
headers: defaults({
26-
'Content-Type': 'application/vnd.contentful.delivery.v1+json',
27-
'X-Contentful-User-Agent': 'contentful.js/3.x'
28-
}, headers),
28+
headers: headers,
2929
agent: agent,
3030
params: {
3131
access_token: accessToken

test/unit/create-http-client-test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@ test('Calls axios with expected URL', t => {
88
create: sinon.stub()
99
}
1010

11+
createHttpClient.__Rewire__('packageFile', {
12+
version: 'version'
13+
})
14+
1115
createHttpClient(axios, {
1216
accessToken: 'clientAccessToken',
1317
space: 'clientSpaceId'
1418
})
1519

1620
t.equals(axios.create.args[0][0].baseURL, 'https://cdn.contentful.com:443/spaces/clientSpaceId/')
21+
t.equals(axios.create.args[0][0].headers['X-Contentful-User-Agent'], 'contentful.js/version')
1722
t.end()
1823
})

0 commit comments

Comments
 (0)