Skip to content

Commit 7f6fd20

Browse files
committed
✨ Added v6 support to Content-API library
no issue - adds `v6` to supported versions list - updates URL prefixing function to ensure `v6` doesn't result in a versioned URL
1 parent 0e23e85 commit 7f6fd20

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ const USER_AGENT_DEFAULT = true;
66

77
const packageVersion = packageInfo.version;
88

9-
const defaultAcceptVersionHeader = 'v5.0';
10-
const supportedVersions = ['v2', 'v3', 'v4', 'v5', 'canary'];
9+
const defaultAcceptVersionHeader = 'v6.0';
10+
const supportedVersions = ['v2', 'v3', 'v4', 'v5', 'v6', 'canary'];
1111
const name = '@tryghost/content-api';
1212

1313
/**
@@ -20,14 +20,15 @@ const name = '@tryghost/content-api';
2020
const resolveAPIPrefix = (version) => {
2121
let prefix;
2222

23-
// NOTE: the "version.match(/^v5\.\d+/)" expression should be changed to "version.match(/^v\d+\.\d+/)" once Ghost v5 is out
24-
if (version === 'v5' || version === undefined || version.match(/^v5\.\d+/)) {
25-
prefix = `/content/`;
26-
} else if (version.match(/^v\d+\.\d+/)) {
27-
const versionPrefix = /^(v\d+)\.\d+/.exec(version)[1];
23+
// Only v2, v3, v4, and canary need version prefixes in the URL
24+
if (version === 'v2' || version === 'v3' || version === 'v4' || version === 'canary') {
25+
prefix = `/${version}/content/`;
26+
} else if (version && version.match(/^v[2-4]\.\d+/)) {
27+
const versionPrefix = /^(v[2-4])\.\d+/.exec(version)[1];
2828
prefix = `/${versionPrefix}/content/`;
2929
} else {
30-
prefix = `/${version}/content/`;
30+
// Default for v5, v6, undefined, etc. - no version prefix
31+
prefix = `/content/`;
3132
}
3233

3334
return prefix;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ describe('GhostContentApi', function () {
102102

103103
makeRequestStub.calledOnce.should.be.true();
104104
should.equal(makeRequestStub.args[0][0].url, 'http://ghost.local/ghost/api/canary/content/settings/');
105-
should.equal(makeRequestStub.args[0][0].headers['Accept-Version'], 'v5.0');
105+
should.equal(makeRequestStub.args[0][0].headers['Accept-Version'], 'v6.0');
106106
should.equal(makeRequestStub.args[0][0].headers['User-Agent'], `GhostContentSDK/${packageVersion}`);
107107
});
108108

@@ -211,7 +211,7 @@ describe('GhostContentApi', function () {
211211
await api.settings.browse();
212212

213213
makeRequestStub.calledOnce.should.be.true();
214-
should.equal(makeRequestStub.args[0][0].headers['Accept-Version'], 'v5.0');
214+
should.equal(makeRequestStub.args[0][0].headers['Accept-Version'], 'v6.0');
215215
should.equal(makeRequestStub.args[0][0].headers['User-Agent'], `GhostContentSDK/${packageVersion}`);
216216
});
217217

@@ -234,7 +234,7 @@ describe('GhostContentApi', function () {
234234

235235
makeRequestStub.calledOnce.should.be.true();
236236
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');
237+
should.equal(makeRequestStub.args[0][0].headers['Accept-Version'], 'v6.0');
238238
should.equal(makeRequestStub.args[0][0].headers['User-Agent'], undefined);
239239
});
240240

@@ -257,7 +257,7 @@ describe('GhostContentApi', function () {
257257

258258
makeRequestStub.calledOnce.should.be.true();
259259
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');
260+
should.equal(makeRequestStub.args[0][0].headers['Accept-Version'], 'v6.0');
261261
should.equal(makeRequestStub.args[0][0].headers['User-Agent'], 'I_LOVE_CUSTOM_THINGS');
262262
});
263263
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const should = require('should');
2+
const sinon = require('sinon');
3+
4+
const GhostContentApi = require('../../cjs/content-api');
5+
const packageJSON = require('../../package.json');
6+
const packageVersion = packageJSON.version;
7+
8+
describe('GhostContentApi v6', function () {
9+
it('Uses non-versioned URL and correct Accept-Version header for v6.0', async function () {
10+
const makeRequestStub = sinon.stub().returns(Promise.resolve({
11+
data: {
12+
settings: {}
13+
}
14+
}));
15+
16+
const api = new GhostContentApi({
17+
version: 'v6.0',
18+
url: 'http://ghost.local',
19+
key: '0123456789abcdef0123456789',
20+
makeRequest: makeRequestStub
21+
});
22+
23+
await api.settings.browse();
24+
25+
makeRequestStub.calledOnce.should.be.true();
26+
should.equal(makeRequestStub.args[0][0].url, 'http://ghost.local/ghost/api/content/settings/');
27+
should.equal(makeRequestStub.args[0][0].headers['Accept-Version'], 'v6.0');
28+
should.equal(makeRequestStub.args[0][0].headers['User-Agent'], `GhostContentSDK/${packageVersion}`);
29+
});
30+
});

0 commit comments

Comments
 (0)