Skip to content

Commit 1c564d5

Browse files
committed
Enabled setting custom 'User-Agent' header in Admin API SDK
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 683d24d commit 1c564d5

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

packages/admin-api/lib/admin-api.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const resolveAPIPrefix = (version) => {
4141
* @param {String} options.url
4242
* @param {String} [options.ghostPath]
4343
* @param {String|Boolean} options.version - a version string like v3.2, v4.1, v5.8 or boolean value identifying presence of Accept-Version header
44-
* @param {Boolean} [options.userAgent] - flag controlling if the 'User-Agent' header should be sent with a request
44+
* @param {String|Boolean} [options.userAgent] - flag controlling if the 'User-Agent' header should be sent with a request
4545
* @param {Function} [options.makeRequest]
4646
* @param {Function} [options.generateToken]
4747
*/
@@ -417,7 +417,11 @@ module.exports = function GhostAdminAPI(options) {
417417
};
418418

419419
if (config.userAgent) {
420-
ghostHeaders['User-Agent'] = `GhostAdminSDK/${packageVersion}`;
420+
if (typeof config.userAgent === 'boolean') {
421+
ghostHeaders['User-Agent'] = `GhostAdminSDK/${packageVersion}`;
422+
} else {
423+
headers['User-Agent'] = config.userAgent;
424+
}
421425
}
422426

423427
if (config.acceptVersionHeader) {

packages/admin-api/test/lib/admin-api.test.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ describe('GhostAdminAPI general', function () {
291291
should.equal(generateTokenSpy.args[0][1], '/admin/');
292292
});
293293

294-
it('does not set "User-Agent" header when disabled', async function () {
294+
it('does not set "User-Agent" header when disabled', async function () {
295295
const makeRequestStub = sinon.stub().returns(Promise.resolve({
296296
config: {}
297297
}));
@@ -314,5 +314,29 @@ describe('GhostAdminAPI general', function () {
314314
should.equal(generateTokenSpy.args[0][0], '5c73def7a21ad85eda5d4faa:d9a3e5b2d6c2a4afb094655c4dc543220be60b3561fa9622e3891213cb4357d0');
315315
should.equal(generateTokenSpy.args[0][1], '/admin/');
316316
});
317+
318+
it('sets a custom value for "User-Agent" header', async function () {
319+
const makeRequestStub = sinon.stub().returns(Promise.resolve({
320+
config: {}
321+
}));
322+
const generateTokenSpy = sinon.spy();
323+
324+
const api = new GhostAdminAPI({
325+
version: true,
326+
url: `http://ghost.local`,
327+
key: '5c73def7a21ad85eda5d4faa:d9a3e5b2d6c2a4afb094655c4dc543220be60b3561fa9622e3891213cb4357d0',
328+
makeRequest: makeRequestStub,
329+
generateToken: generateTokenSpy,
330+
userAgent: 'Custom Value'
331+
});
332+
333+
await api.config.read();
334+
335+
makeRequestStub.calledOnce.should.be.true();
336+
should.equal(makeRequestStub.args[0][0].headers['Accept-Version'], 'v5.0');
337+
should.equal(makeRequestStub.args[0][0].headers['User-Agent'], 'Custom Value');
338+
should.equal(generateTokenSpy.args[0][0], '5c73def7a21ad85eda5d4faa:d9a3e5b2d6c2a4afb094655c4dc543220be60b3561fa9622e3891213cb4357d0');
339+
should.equal(generateTokenSpy.args[0][1], '/admin/');
340+
});
317341
});
318342
});

0 commit comments

Comments
 (0)