Skip to content

Commit 39d539d

Browse files
committed
Enabled setting custom 'User-Agent' header
refs https://github.com/TryGhost/Toolbox/issues/348 refs https://github.com/TryGhost/Toolbox/issues/301 - Allows for a full control over the 'User-Agent' header. Enables customizing the header in whatever way the client needs (for example prepending a value when there's a wrapping library using the SDK)
1 parent c90947f commit 39d539d

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const defaultMakeRequest = ({url, method, params, headers}) => {
5353
* @param {String} options.key
5454
* @param {String} [options.ghostPath]
5555
* @param {String|Boolean} options.version - a version string like v3, v4, v5 or boolean value identifying presence of Accept-Version header
56-
* @param {Boolean} [options.userAgent] - flag controlling if the 'User-Agent' header should be sent with a request
56+
* @param {String|Boolean} [options.userAgent] - value controlling the 'User-Agent' header should be sent with a request
5757
* @param {Function} [options.makeRequest]
5858
*/
5959
export default function GhostContentAPI({url, key, version, userAgent, ghostPath = 'ghost', makeRequest = defaultMakeRequest}) {
@@ -152,7 +152,11 @@ export default function GhostContentAPI({url, key, version, userAgent, ghostPath
152152
} : {};
153153

154154
if (userAgent) {
155-
headers['User-Agent'] = `GhostContentSDK/${packageVersion}`;
155+
if (typeof userAgent === 'boolean') {
156+
headers['User-Agent'] = `GhostContentSDK/${packageVersion}`;
157+
} else {
158+
headers['User-Agent'] = userAgent;
159+
}
156160
}
157161

158162
if (acceptVersionHeader) {

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,5 +237,28 @@ describe('GhostContentApi', function () {
237237
should.equal(makeRequestStub.args[0][0].headers['Accept-Version'], 'v5.0');
238238
should.equal(makeRequestStub.args[0][0].headers['User-Agent'], undefined);
239239
});
240+
241+
it('Sets a custom User-Agent header', async function () {
242+
const makeRequestStub = sinon.stub().returns(Promise.resolve({
243+
data: {
244+
settings: {}
245+
}
246+
}));
247+
248+
const api = new GhostContentApi({
249+
version: 'canary',
250+
url: `http://ghost.local`,
251+
key: '0123456789abcdef0123456789',
252+
makeRequest: makeRequestStub,
253+
userAgent: 'I_LOVE_CUSTOM_THINGS'
254+
});
255+
256+
await api.settings.browse();
257+
258+
makeRequestStub.calledOnce.should.be.true();
259+
should.equal(makeRequestStub.args[0][0].url, 'http://ghost.local/ghost/api/canary/content/settings/');
260+
should.equal(makeRequestStub.args[0][0].headers['Accept-Version'], 'v5.0');
261+
should.equal(makeRequestStub.args[0][0].headers['User-Agent'], 'I_LOVE_CUSTOM_THINGS');
262+
});
240263
});
241264
});

0 commit comments

Comments
 (0)