Skip to content

Commit 3eb3240

Browse files
feat(createClient): Add Contentful User Agent
1 parent 613de38 commit 3eb3240

File tree

5 files changed

+48
-17
lines changed

5 files changed

+48
-17
lines changed

lib/contentful-management.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@
77
import axios from 'axios'
88
import cloneDeep from 'lodash/cloneDeep'
99
import assign from 'lodash/assign'
10-
import {createHttpClient} from 'contentful-sdk-core'
11-
import version from '../version'
10+
import {createHttpClient, getUserAgentHeader} from 'contentful-sdk-core'
1211
import createContentfulApi from './create-contentful-api'
13-
12+
import version from '../version'
1413
/**
1514
* @typedef {ContentfulManagement} ContentfulManagement
16-
* @property {function(params: {accessToken: string, insecure?: boolean, host?: string, hostUpload?: string, httpAgent?: Object, httpsAgent?: Object, headers?: Object, proxy?:Object}): ClientAPI} createClient - Create a client instance, this is the entry point to the library
15+
* @property {function(params: {accessToken: string, insecure?: boolean, host?: string, hostUpload?: string, httpAgent?: Object, httpsAgent?: Object, headers?: Object, proxy?:Object, application?: string, integration?: string}): ClientAPI} createClient - Create a client instance, this is the entry point to the library
1716
*
1817
* @example
1918
* // require contentful-management
@@ -28,10 +27,16 @@ export function createClient (params) {
2827
defaultHostname: 'api.contentful.com',
2928
defaultHostnameUpload: 'upload.contentful.com'
3029
}
30+
const userAgentHeader = getUserAgentHeader(`contentful-management.js/${version}`,
31+
params.application,
32+
params.integration
33+
)
34+
3135
const requiredHeaders = {
3236
'Content-Type': 'application/vnd.contentful.management.v1+json',
33-
'X-Contentful-User-Agent': 'contentful-management.js/' + version
37+
'X-Contentful-User-Agent': userAgentHeader
3438
}
39+
3540
params = assign(defaultParameters, cloneDeep(params))
3641

3742
if (!params.accessToken) {

package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
"clean": "rimraf dist && rimraf coverage",
2020
"build": "npm run clean && npm run build:ci",
2121
"build:ci": "npm run vendor:version && npm run build:standalone",
22-
"build:standalone": "BABEL_ENV=webpack webpack && BABEL_ENV=webpack NODE_ENV=production webpack -p",
23-
"build:standalone:log": "BABEL_ENV=webpack npm run build && BABEL_ENV=webpack NODE_ENV=production webpack -p --json --profile > webpack-build-log.json",
22+
"build:standalone": "BABEL_ENV=webpack webpack && BABEL_ENV=webpack NODE_ENV=production webpack",
23+
"build:standalone:log": "BABEL_ENV=webpack npm run build && BABEL_ENV=webpack NODE_ENV=production webpack --json --profile > webpack-build-log.json",
2424
"docs:build": "esdoc -c esdoc.json",
2525
"docs:dev": "npm run build && npm run docs:build",
2626
"docs:watch": "watchy -w lib npm run docs:dev",
@@ -33,8 +33,8 @@
3333
"test:only": "BABEL_ENV=test babel-node ./test/runner",
3434
"test:debug": "BABEL_ENV=test babel-node debug ./test/runner",
3535
"test:integration": "BABEL_ENV=test babel-node ./test/integration/integration-tests.js",
36-
"test:browser-local": "BABEL_ENV=test ./node_modules/.bin/karma start karma.conf.local.js",
37-
"test:browser-remote": "BABEL_ENV=test ./node_modules/.bin/karma start karma.conf.saucelabs.js",
36+
"test:browser-local": "BABEL_ENV=test karma start karma.conf.local.js",
37+
"test:browser-remote": "BABEL_ENV=test karma start karma.conf.saucelabs.js",
3838
"test:simulate-ci": "trevor",
3939
"vendor:version": "echo \"module.exports = '`cat package.json|json version`'\" > version.js",
4040
"browser-coverage": "npm run test:cover && opener coverage/lcov-report/index.html",
@@ -55,9 +55,10 @@
5555
],
5656
"dependencies": {
5757
"axios": "~0.15.3",
58-
"contentful-sdk-core": "^3.9.0",
58+
"contentful-sdk-core": "^3.10.1",
5959
"es6-promise": "^4.0.5",
60-
"lodash": "^4.17.4"
60+
"lodash": "^4.17.4",
61+
"platform": "1.3.3"
6162
},
6263
"devDependencies": {
6364
"babel-cli": "^6.7.5",
Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import test from 'tape'
22
import sinon from 'sinon'
33
import {createClient, __RewireAPI__ as createClientRewireApi} from '../../lib/contentful-management'
4-
4+
import version from '../../version'
55
test('Throws if no accessToken is defined', (t) => {
66
t.throws(() => {
77
createClient({space: 'spaceid'})
@@ -10,7 +10,6 @@ test('Throws if no accessToken is defined', (t) => {
1010
})
1111

1212
test('Passes along HTTP client parameters', (t) => {
13-
createClientRewireApi.__Rewire__('version', 'version')
1413
createClientRewireApi.__Rewire__('axios', {create: sinon.stub()})
1514

1615
const createHttpClientStub = sinon.stub()
@@ -19,10 +18,28 @@ test('Passes along HTTP client parameters', (t) => {
1918

2019
const client = createClient({accessToken: 'accesstoken'})
2120
t.ok(createHttpClientStub.args[0][1].headers['Content-Type'], 'sets the content type')
22-
t.equals(createHttpClientStub.args[0][1].headers['X-Contentful-User-Agent'], 'contentful-management.js/version', 'sets the user agent header')
2321
t.ok(client, 'returns a client')
2422
createClientRewireApi.__ResetDependency__('createHttpClient')
2523
createClientRewireApi.__ResetDependency__('wrapHttpClient')
2624
createClientRewireApi.__ResetDependency__('createContentfulApi')
2725
t.end()
2826
})
27+
28+
test('Generate the correct User Agent Header', (t) => {
29+
createClientRewireApi.__Rewire__('axios', {create: sinon.stub()})
30+
31+
const createHttpClientStub = sinon.stub()
32+
createClientRewireApi.__Rewire__('createHttpClient', createHttpClientStub)
33+
createClientRewireApi.__Rewire__('createContentfulApi', sinon.stub().returns({}))
34+
createClient({accessToken: 'accesstoken', application: 'myApplication/1.1.1', integration: 'myIntegration/1.0.0'})
35+
const headerParts = createHttpClientStub.args[0][1].headers['X-Contentful-User-Agent'].split('; ')
36+
t.equal(headerParts.length, 5)
37+
t.equal(headerParts[0], 'app myApplication/1.1.1')
38+
t.equal(headerParts[1], 'integration myIntegration/1.0.0')
39+
t.equal(headerParts[2], `sdk contentful-management.js/${version}`)
40+
41+
createClientRewireApi.__ResetDependency__('createHttpClient')
42+
createClientRewireApi.__ResetDependency__('wrapHttpClient')
43+
createClientRewireApi.__ResetDependency__('createContentfulApi')
44+
t.end()
45+
})

webpack.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var plugins = [
88
'caching': true
99
}),
1010
new webpack.optimize.OccurrenceOrderPlugin(),
11+
new webpack.IgnorePlugin(/vertx/),
1112
new webpack.DefinePlugin({
1213
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
1314
})
@@ -51,6 +52,7 @@ module.exports = [
5152
module: {
5253
loaders: loaders
5354
},
55+
devtool: 'source-map',
5456
plugins: plugins
5557
},
5658
{
@@ -66,6 +68,7 @@ module.exports = [
6668
module: {
6769
loaders: loaders
6870
},
71+
devtool: 'source-map',
6972
plugins: plugins
7073
}
7174
]

yarn.lock

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,14 +1379,15 @@ content-type@~1.0.2:
13791379
version "1.0.2"
13801380
resolved "https://registry.yarnpkg.com/content-type/-/content-type-1.0.2.tgz#b7d113aee7a8dd27bd21133c4dc2529df1721eed"
13811381

1382-
contentful-sdk-core@^3.7.0:
1383-
version "3.7.0"
1384-
resolved "https://registry.yarnpkg.com/contentful-sdk-core/-/contentful-sdk-core-3.7.0.tgz#1573d6e04d55d6e414a271e8759a986af95e322b"
1382+
contentful-sdk-core@^3.10.1:
1383+
version "3.10.1"
1384+
resolved "https://registry.yarnpkg.com/contentful-sdk-core/-/contentful-sdk-core-3.10.1.tgz#9a9149711fbe628a8deeae405681c64dc371fcaa"
13851385
dependencies:
13861386
axios "^0.15.3"
13871387
es6-promise "^4.0.5"
13881388
lodash "^4.17.4"
13891389
npmlog "^4.0.2"
1390+
platform "1.3.3"
13901391
qs "^6.1.0"
13911392

13921393
contentful-sdk-jsdoc@^1.2.2:
@@ -4118,6 +4119,10 @@ pkg-dir@^1.0.0:
41184119
dependencies:
41194120
find-up "^1.0.0"
41204121

4122+
4123+
version "1.3.3"
4124+
resolved "https://registry.yarnpkg.com/platform/-/platform-1.3.3.tgz#646c77011899870b6a0903e75e997e8e51da7461"
4125+
41214126
pluralize@^1.2.1:
41224127
version "1.2.1"
41234128
resolved "https://registry.yarnpkg.com/pluralize/-/pluralize-1.2.1.tgz#d1a21483fd22bb41e58a12fa3421823140897c45"

0 commit comments

Comments
 (0)