@tryghost/content-api 1.7.3 → 1.9.1

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,9 +2,106 @@
2
2
 
3
3
  var axios = require('axios');
4
4
 
5
+ var name$1 = "@tryghost/content-api";
6
+ var version = "1.9.1";
7
+ var repository = "https://github.com/TryGhost/SDK/tree/master/packages/content-api";
8
+ var author = "Ghost Foundation";
9
+ var license = "MIT";
10
+ var main = "cjs/content-api.js";
11
+ var unpkg = "umd/content-api.min.js";
12
+ var module$1 = "es/content-api.js";
13
+ var source = "lib/index.js";
14
+ var files = [
15
+ "LICENSE",
16
+ "README.md",
17
+ "cjs/",
18
+ "lib/",
19
+ "umd/",
20
+ "es/"
21
+ ];
22
+ var scripts = {
23
+ dev: "echo \"Implement me!\"",
24
+ pretest: "yarn build",
25
+ test: "NODE_ENV=testing c8 --all --reporter text --reporter cobertura mocha './test/**/*.test.js'",
26
+ build: "rollup -c",
27
+ lint: "eslint . --ext .js --cache",
28
+ prepare: "NODE_ENV=production yarn build",
29
+ posttest: "yarn lint"
30
+ };
31
+ var publishConfig = {
32
+ access: "public"
33
+ };
34
+ var devDependencies = {
35
+ "@babel/core": "7.17.9",
36
+ "@babel/polyfill": "7.12.1",
37
+ "@babel/preset-env": "7.16.11",
38
+ "@rollup/plugin-json": "4.1.0",
39
+ c8: "7.11.0",
40
+ "core-js": "3.22.0",
41
+ "eslint-plugin-ghost": "1.5.0",
42
+ mocha: "7.2.0",
43
+ rollup: "2.70.2",
44
+ "rollup-plugin-babel": "4.4.0",
45
+ "rollup-plugin-commonjs": "10.1.0",
46
+ "rollup-plugin-node-resolve": "5.2.0",
47
+ "rollup-plugin-replace": "2.2.0",
48
+ "rollup-plugin-terser": "7.0.2",
49
+ should: "13.2.3",
50
+ sinon: "9.2.4"
51
+ };
52
+ var dependencies = {
53
+ axios: "^0.21.1"
54
+ };
55
+ var gitHead = "6c0165f2588537f0e00e76567c2ed2f526f7faff";
56
+ var packageInfo = {
57
+ name: name$1,
58
+ version: version,
59
+ repository: repository,
60
+ author: author,
61
+ license: license,
62
+ main: main,
63
+ "umd:main": "umd/content-api.min.js",
64
+ unpkg: unpkg,
65
+ module: module$1,
66
+ source: source,
67
+ files: files,
68
+ scripts: scripts,
69
+ publishConfig: publishConfig,
70
+ devDependencies: devDependencies,
71
+ dependencies: dependencies,
72
+ gitHead: gitHead
73
+ };
74
+
75
+ const packageVersion = packageInfo.version;
76
+
77
+ // NOTE: bump this default when Ghost v5 is released
78
+ const defaultAcceptVersionHeader = 'v4.0';
5
79
  const supportedVersions = ['v2', 'v3', 'v4', 'v5', 'canary'];
6
80
  const name = '@tryghost/content-api';
7
81
 
82
+ /**
83
+ * This method can go away in favor of only sending 'Accept-Version` headers
84
+ * once the Ghost API removes a concept of version from it's URLS (with Ghost v5)
85
+ *
86
+ * @param {string} [version] version in `v{major}` format
87
+ * @returns {string}
88
+ */
89
+ const resolveAPIPrefix = (version) => {
90
+ let prefix;
91
+
92
+ // NOTE: the "version.match(/^v5\.\d+/)" expression should be changed to "version.match(/^v\d+\.\d+/)" once Ghost v5 is out
93
+ if (version === 'v5' || version === undefined || version.match(/^v5\.\d+/)) {
94
+ prefix = `/content/`;
95
+ } else if (version.match(/^v\d+\.\d+/)) {
96
+ const versionPrefix = /^(v\d+)\.\d+/.exec(version)[1];
97
+ prefix = `/${versionPrefix}/content/`;
98
+ } else {
99
+ prefix = `/${version}/content/`;
100
+ }
101
+
102
+ return prefix;
103
+ };
104
+
8
105
  const defaultMakeRequest = ({url, method, params, headers}) => {
9
106
  return axios[method](url, {
10
107
  params,
@@ -24,7 +121,7 @@ const defaultMakeRequest = ({url, method, params, headers}) => {
24
121
  * @param {String} options.url
25
122
  * @param {String} options.key
26
123
  * @param {String} [options.ghostPath]
27
- * @param {String} [options.version]
124
+ * @param {String|Boolean} options.version - a version string like v3, v4, v5 or boolean value identifying presence of Accept-Version header
28
125
  * @param {Function} [options.makeRequest]
29
126
  * @param {String} [options.host] Deprecated
30
127
  */
@@ -45,9 +142,34 @@ function GhostContentAPI({url, key, host, version, ghostPath = 'ghost', makeRequ
45
142
  return GhostContentAPI({url, key, version, ghostPath, makeRequest});
46
143
  }
47
144
 
48
- if (version && !supportedVersions.includes(version)) {
145
+ if (version === undefined) {
146
+ throw new Error(`${name} Config Missing: 'version' is required. E.g. ${supportedVersions.join(',')}`);
147
+ }
148
+
149
+ let acceptVersionHeader;
150
+ if (typeof version === 'boolean') {
151
+ if (version === true) {
152
+ acceptVersionHeader = defaultAcceptVersionHeader;
153
+ }
154
+ version = undefined;
155
+ } else if (version && !supportedVersions.includes(version) && !(version.match(/^v\d+\.\d+/))) {
49
156
  throw new Error(`${name} Config Invalid: 'version' ${version} is not supported`);
157
+ } else {
158
+ if (version === 'canary') {
159
+ // eslint-disable-next-line
160
+ console.warn(`${name}: The 'version' parameter has a deprecated format 'canary', please use 'v{major}.{minor}' format instead`);
161
+
162
+ acceptVersionHeader = defaultAcceptVersionHeader;
163
+ } else if (version.match(/^v\d+$/)) {
164
+ // eslint-disable-next-line
165
+ console.warn(`${name}: The 'version' parameter has a deprecated format 'v{major}', please use 'v{major}.{minor}' format instead`);
166
+
167
+ acceptVersionHeader = `${version}.0`;
168
+ } else {
169
+ acceptVersionHeader = version;
170
+ }
50
171
  }
172
+
51
173
  if (!url) {
52
174
  throw new Error(`${name} Config Missing: 'url' is required. E.g. 'https://site.com'`);
53
175
  }
@@ -101,14 +223,13 @@ function GhostContentAPI({url, key, host, version, ghostPath = 'ghost', makeRequ
101
223
  Authorization: `GhostMembers ${membersToken}`
102
224
  } : {};
103
225
 
104
- if (!version || ['v4', 'v5', 'canary'].includes(version)) {
105
- headers['Accept-Version'] = version || 'v5';
226
+ headers['User-Agent'] = `GhostContentSDK/${packageVersion}`;
227
+ if (acceptVersionHeader) {
228
+ headers['Accept-Version'] = acceptVersionHeader;
106
229
  }
107
230
 
108
231
  params = Object.assign({key}, params);
109
- const apiUrl = version
110
- ? `${url}/${ghostPath}/api/${version}/content/${resourceType}/${id ? id + '/' : ''}`
111
- : `${url}/${ghostPath}/api/content/${resourceType}/${id ? id + '/' : ''}`;
232
+ const apiUrl = `${url}/${ghostPath}/api${resolveAPIPrefix(version)}${resourceType}/${id ? id + '/' : ''}`;
112
233
 
113
234
  return makeRequest({
114
235
  url: apiUrl,