|
1 | 1 | import axios from 'axios';
|
2 | 2 | import packageInfo from '../package.json';
|
3 | 3 |
|
| 4 | +const USER_AGENT_DEFAULT = true; |
| 5 | + |
4 | 6 | const packageVersion = packageInfo.version;
|
5 | 7 |
|
6 | 8 | const defaultAcceptVersionHeader = 'v5.0';
|
@@ -50,11 +52,12 @@ const defaultMakeRequest = ({url, method, params, headers}) => {
|
50 | 52 | * @param {String} options.key
|
51 | 53 | * @param {String} [options.ghostPath]
|
52 | 54 | * @param {String|Boolean} options.version - a version string like v3, v4, v5 or boolean value identifying presence of Accept-Version header
|
| 55 | + * @param {Boolean} [options.userAgent] - flag controlling if the 'User-Agent' header should be sent with a request |
53 | 56 | * @param {Function} [options.makeRequest]
|
54 | 57 | */
|
55 |
| -export default function GhostContentAPI({url, key, version, ghostPath = 'ghost', makeRequest = defaultMakeRequest}) { |
| 58 | +export default function GhostContentAPI({url, key, version, userAgent, ghostPath = 'ghost', makeRequest = defaultMakeRequest}) { |
56 | 59 | if (this instanceof GhostContentAPI) {
|
57 |
| - return GhostContentAPI({url, key, version, ghostPath, makeRequest}); |
| 60 | + return GhostContentAPI({url, key, version, userAgent, ghostPath, makeRequest}); |
58 | 61 | }
|
59 | 62 |
|
60 | 63 | if (version === undefined) {
|
@@ -100,6 +103,11 @@ export default function GhostContentAPI({url, key, version, ghostPath = 'ghost',
|
100 | 103 | if (key && !/[0-9a-f]{26}/.test(key)) {
|
101 | 104 | throw new Error(`${name} Config Invalid: 'key' ${key} must have 26 hex characters`);
|
102 | 105 | }
|
| 106 | + |
| 107 | + if (userAgent === undefined) { |
| 108 | + userAgent = USER_AGENT_DEFAULT; |
| 109 | + } |
| 110 | + |
103 | 111 | const api = ['posts', 'authors', 'tags', 'pages', 'settings', 'tiers', 'newsletters', 'offers'].reduce((apiObject, resourceType) => {
|
104 | 112 | function browse(options = {}, memberToken) {
|
105 | 113 | return makeApiRequest(resourceType, options, null, memberToken);
|
@@ -142,7 +150,10 @@ export default function GhostContentAPI({url, key, version, ghostPath = 'ghost',
|
142 | 150 | Authorization: `GhostMembers ${membersToken}`
|
143 | 151 | } : {};
|
144 | 152 |
|
145 |
| - headers['User-Agent'] = `GhostContentSDK/${packageVersion}`; |
| 153 | + if (userAgent) { |
| 154 | + headers['User-Agent'] = `GhostContentSDK/${packageVersion}`; |
| 155 | + } |
| 156 | + |
146 | 157 | if (acceptVersionHeader) {
|
147 | 158 | headers['Accept-Version'] = acceptVersionHeader;
|
148 | 159 | }
|
|
0 commit comments