@tryghost/admin-api 1.7.0 → 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.
package/lib/index.js CHANGED
@@ -3,7 +3,7 @@ const FormData = require('form-data');
3
3
  const fs = require('fs');
4
4
  const token = require('./token');
5
5
 
6
- const supportedVersions = ['v2', 'v3', 'v4', 'canary'];
6
+ const supportedVersions = ['v2', 'v3', 'v4', 'v5', 'canary'];
7
7
  const packageName = '@tryghost/admin-api';
8
8
 
9
9
  module.exports = function GhostAdminAPI(options) {
@@ -44,10 +44,7 @@ module.exports = function GhostAdminAPI(options) {
44
44
  }
45
45
  }
46
46
 
47
- if (!config.version) {
48
- throw new Error(`${packageName} Config Missing: 'version' is required. E.g. ${supportedVersions.join(',')}`);
49
- }
50
- if (!supportedVersions.includes(config.version)) {
47
+ if (config.version && !supportedVersions.includes(config.version)) {
51
48
  throw new Error(`${packageName} Config Invalid: 'version' ${config.version} is not supported`);
52
49
  }
53
50
  if (!config.url) {
@@ -69,6 +66,11 @@ module.exports = function GhostAdminAPI(options) {
69
66
  throw new Error(`${packageName} Config Invalid: 'key' ${config.key} must have the following format {A}:{B}, where A is 24 hex characters and B is 64 hex characters`);
70
67
  }
71
68
 
69
+ if (config.version === 'v5') {
70
+ // NOTE: the version parameter is supported but not necessary for non-versioned API, starting with Ghost v5
71
+ delete config.version;
72
+ }
73
+
72
74
  const resources = [
73
75
  // @NOTE: stable
74
76
  'posts',
@@ -331,7 +333,10 @@ module.exports = function GhostAdminAPI(options) {
331
333
 
332
334
  function endpointFor(resource, {id, slug, email} = {}) {
333
335
  const {ghostPath, version} = config;
334
- let endpoint = `/${ghostPath}/api/${version}/admin/${resource}/`;
336
+
337
+ let endpoint = version
338
+ ? `/${ghostPath}/api/${version}/admin/${resource}/`
339
+ : `/${ghostPath}/api/admin/${resource}/`;
335
340
 
336
341
  if (id) {
337
342
  endpoint = `${endpoint}${id}/`;
@@ -348,9 +353,15 @@ module.exports = function GhostAdminAPI(options) {
348
353
  const {url: apiUrl, key, version, makeRequest} = config;
349
354
  const url = `${apiUrl}${endpoint}`;
350
355
 
351
- headers = Object.assign({}, headers, {
352
- Authorization: `Ghost ${token(version, key)}`
353
- });
356
+ const ghostHeaders = {
357
+ Authorization: `Ghost ${token(key, version)}`
358
+ };
359
+
360
+ if (!version || ['v4', 'canary'].includes(version)) {
361
+ ghostHeaders['Accept-Version'] = version || 'v5';
362
+ }
363
+
364
+ headers = Object.assign({}, headers, ghostHeaders);
354
365
 
355
366
  return makeRequest({
356
367
  url,
@@ -362,7 +373,7 @@ module.exports = function GhostAdminAPI(options) {
362
373
  /**
363
374
  * @NOTE:
364
375
  *
365
- * If you are overriding `makeRequest`, we can't garantee that the returned format is the same, but
376
+ * If you are overriding `makeRequest`, we can't garante that the returned format is the same, but
366
377
  * we try to detect & return a proper error instance.
367
378
  */
368
379
  if (err.response && err.response.data && err.response.data.errors) {
package/lib/token.js CHANGED
@@ -1,12 +1,19 @@
1
1
  const jwt = require('jsonwebtoken');
2
2
 
3
- module.exports = function token(version, key) {
3
+ /**
4
+ *
5
+ * @param {String} key - API key to sign JWT with
6
+ * @param {String} version - API version to use as a part of audience
7
+ * @returns
8
+ */
9
+ module.exports = function token(key, version) {
4
10
  const [id, secret] = key.split(':');
11
+ const audience = version ? `/${version}/admin/` : '/admin/';
5
12
 
6
13
  return jwt.sign({}, Buffer.from(secret, 'hex'), { // eslint-disable-line no-undef
7
14
  keyid: id,
8
15
  algorithm: 'HS256',
9
16
  expiresIn: '5m',
10
- audience: `/${version}/admin/`
17
+ audience
11
18
  });
12
19
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryghost/admin-api",
3
- "version": "1.7.0",
3
+ "version": "1.8.0",
4
4
  "repository": "https://github.com/TryGhost/SDK/tree/master/packages/admin-api",
5
5
  "author": "Ghost Foundation",
6
6
  "license": "MIT",
@@ -31,5 +31,5 @@
31
31
  "form-data": "^4.0.0",
32
32
  "jsonwebtoken": "^8.4.0"
33
33
  },
34
- "gitHead": "bb3a33a1ff85c0d3efb2972e8d6dd72df458c271"
34
+ "gitHead": "36ffe70168eb2ad3eaefa0dc63a35697b284c9e7"
35
35
  }