@tryghost/content-api 1.7.1 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,6 +2,8 @@
2
2
 
3
3
  var axios = require('axios');
4
4
 
5
+ // NOTE: bump this default when Ghost v5 is released
6
+ const defaultAcceptVersionHeader = 'v4.0';
5
7
  const supportedVersions = ['v2', 'v3', 'v4', 'v5', 'canary'];
6
8
  const name = '@tryghost/content-api';
7
9
 
@@ -24,10 +26,9 @@ const defaultMakeRequest = ({url, method, params, headers}) => {
24
26
  * @param {String} options.url
25
27
  * @param {String} options.key
26
28
  * @param {String} [options.ghostPath]
27
- * @param {String} [options.version]
29
+ * @param {String|Boolean} options.version - a version string like v3, v4, v5 or boolean value identifying presence of Accept-Version header
28
30
  * @param {Function} [options.makeRequest]
29
31
  * @param {String} [options.host] Deprecated
30
- * @returns
31
32
  */
32
33
  function GhostContentAPI({url, key, host, version, ghostPath = 'ghost', makeRequest = defaultMakeRequest}) {
33
34
  /**
@@ -46,9 +47,34 @@ function GhostContentAPI({url, key, host, version, ghostPath = 'ghost', makeRequ
46
47
  return GhostContentAPI({url, key, version, ghostPath, makeRequest});
47
48
  }
48
49
 
49
- if (version && !supportedVersions.includes(version)) {
50
+ if (version === undefined) {
51
+ throw new Error(`${name} Config Missing: 'version' is required. E.g. ${supportedVersions.join(',')}`);
52
+ }
53
+
54
+ let acceptVersionHeader;
55
+ if (typeof version === 'boolean') {
56
+ if (version === true) {
57
+ acceptVersionHeader = defaultAcceptVersionHeader;
58
+ }
59
+ version = undefined;
60
+ } else if (version && !supportedVersions.includes(version) && !(version.match(/^v\d+\.\d+/))) {
50
61
  throw new Error(`${name} Config Invalid: 'version' ${version} is not supported`);
62
+ } else {
63
+ if (version === 'canary') {
64
+ // eslint-disable-next-line
65
+ console.warn(`${name}: The 'version' parameter has a deprecated format 'canary', please use 'v{major}.{minor}' format instead`);
66
+
67
+ acceptVersionHeader = defaultAcceptVersionHeader;
68
+ } else if (version.match(/^v\d+$/)) {
69
+ // eslint-disable-next-line
70
+ console.warn(`${name}: The 'version' parameter has a deprecated format 'v{major}', please use 'v{major}.{minor}' format instead`);
71
+
72
+ acceptVersionHeader = `${version}.0`;
73
+ } else {
74
+ acceptVersionHeader = version;
75
+ }
51
76
  }
77
+
52
78
  if (!url) {
53
79
  throw new Error(`${name} Config Missing: 'url' is required. E.g. 'https://site.com'`);
54
80
  }
@@ -102,8 +128,8 @@ function GhostContentAPI({url, key, host, version, ghostPath = 'ghost', makeRequ
102
128
  Authorization: `GhostMembers ${membersToken}`
103
129
  } : {};
104
130
 
105
- if (!version || ['v4', 'v5', 'canary'].includes(version)) {
106
- headers['Accept-Version'] = version || 'v5';
131
+ if (acceptVersionHeader) {
132
+ headers['Accept-Version'] = acceptVersionHeader;
107
133
  }
108
134
 
109
135
  params = Object.assign({key}, params);