Skip to content

Commit 081a19a

Browse files
committed
Allowed disabling setting of 'User-Agent' header
refs https://github.com/TryGhost/Toolbox/issues/348 refs https://github.com/TryGhost/Toolbox/issues/301 refs #426 - In certain environments (e.g. web browser) the client might want to disable sending User-Agent variable to avoid errors like in #426. - This is also a groundwork to have smarter defaults based on the environment - userAgent header would be disabled by default in browser environments as that's the main use case and will be enabled for nodejs environment
1 parent 7fe05c1 commit 081a19a

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

packages/content-api/lib/content-api.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import axios from 'axios';
22
import packageInfo from '../package.json';
33

4+
const USER_AGENT_DEFAULT = true;
5+
46
const packageVersion = packageInfo.version;
57

68
const defaultAcceptVersionHeader = 'v5.0';
@@ -50,11 +52,12 @@ const defaultMakeRequest = ({url, method, params, headers}) => {
5052
* @param {String} options.key
5153
* @param {String} [options.ghostPath]
5254
* @param {String|Boolean} options.version - a version string like v3, v4, v5 or boolean value identifying presence of Accept-Version header
55+
* @param {Boolean} [options.userAgent] - flag controlling if the 'User-Agent' header should be sent with a request
5356
* @param {Function} [options.makeRequest]
5457
*/
55-
export default function GhostContentAPI({url, key, version, ghostPath = 'ghost', makeRequest = defaultMakeRequest}) {
58+
export default function GhostContentAPI({url, key, version, userAgent, ghostPath = 'ghost', makeRequest = defaultMakeRequest}) {
5659
if (this instanceof GhostContentAPI) {
57-
return GhostContentAPI({url, key, version, ghostPath, makeRequest});
60+
return GhostContentAPI({url, key, version, userAgent, ghostPath, makeRequest});
5861
}
5962

6063
if (version === undefined) {
@@ -100,6 +103,11 @@ export default function GhostContentAPI({url, key, version, ghostPath = 'ghost',
100103
if (key && !/[0-9a-f]{26}/.test(key)) {
101104
throw new Error(`${name} Config Invalid: 'key' ${key} must have 26 hex characters`);
102105
}
106+
107+
if (userAgent === undefined) {
108+
userAgent = USER_AGENT_DEFAULT;
109+
}
110+
103111
const api = ['posts', 'authors', 'tags', 'pages', 'settings', 'tiers', 'newsletters', 'offers'].reduce((apiObject, resourceType) => {
104112
function browse(options = {}, memberToken) {
105113
return makeApiRequest(resourceType, options, null, memberToken);
@@ -142,7 +150,10 @@ export default function GhostContentAPI({url, key, version, ghostPath = 'ghost',
142150
Authorization: `GhostMembers ${membersToken}`
143151
} : {};
144152

145-
headers['User-Agent'] = `GhostContentSDK/${packageVersion}`;
153+
if (userAgent) {
154+
headers['User-Agent'] = `GhostContentSDK/${packageVersion}`;
155+
}
156+
146157
if (acceptVersionHeader) {
147158
headers['Accept-Version'] = acceptVersionHeader;
148159
}

packages/content-api/test/content-api-test/content-api.test.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// Switch these lines once there are useful utils
2-
// const testUtils = require('./utils');
3-
41
const should = require('should');
52
const sinon = require('sinon');
63

@@ -217,5 +214,28 @@ describe('GhostContentApi', function () {
217214
should.equal(makeRequestStub.args[0][0].headers['Accept-Version'], 'v5.0');
218215
should.equal(makeRequestStub.args[0][0].headers['User-Agent'], `GhostContentSDK/${packageVersion}`);
219216
});
217+
218+
it('Removes User-Agent header when set to "false"', async function () {
219+
const makeRequestStub = sinon.stub().returns(Promise.resolve({
220+
data: {
221+
settings: {}
222+
}
223+
}));
224+
225+
const api = new GhostContentApi({
226+
version: 'canary',
227+
url: `http://ghost.local`,
228+
key: '0123456789abcdef0123456789',
229+
makeRequest: makeRequestStub,
230+
userAgent: false
231+
});
232+
233+
await api.settings.browse();
234+
235+
makeRequestStub.calledOnce.should.be.true();
236+
should.equal(makeRequestStub.args[0][0].url, 'http://ghost.local/ghost/api/canary/content/settings/');
237+
should.equal(makeRequestStub.args[0][0].headers['Accept-Version'], 'v5.0');
238+
should.equal(makeRequestStub.args[0][0].headers['User-Agent'], undefined);
239+
});
220240
});
221241
});

0 commit comments

Comments
 (0)