@tryghost/admin-api 1.6.0 → 1.7.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.
Files changed (2) hide show
  1. package/lib/index.js +45 -22
  2. package/package.json +2 -2
package/lib/index.js CHANGED
@@ -202,29 +202,52 @@ module.exports = function GhostAdminAPI(options) {
202
202
  if (data.file) {
203
203
  formData = new FormData();
204
204
  formData.append('file', fs.createReadStream(data.file));
205
+ // NOTE: this default "image" doesn't work for all upload endpoints. Should be moved from here and required as
206
+ // an explicit method parameter. Leaving it here for now as I'm focusing on a different problem.
205
207
  formData.append('purpose', data.purpose || 'image');
206
208
 
207
209
  if (data.ref) {
208
210
  formData.append('ref', data.ref);
209
211
  }
210
212
 
213
+ if (data.thumbnail) {
214
+ formData.append('thumbnail', fs.createReadStream(data.thumbnail));
215
+ }
216
+
211
217
  return formData;
212
218
  }
213
219
  }
214
220
 
215
221
  api.images = {
216
222
  upload(data) {
217
- if (!data) {
218
- return Promise.reject(new Error('Missing data'));
219
- }
220
-
221
- if (!isValidUpload(data)) {
222
- return Promise.reject(new Error('Must be of FormData or include path'));
223
- }
223
+ return makeUploadRequest('images', data, endpointFor('images/upload'));
224
+ }
225
+ };
224
226
 
225
- let formData = getFormData(data);
227
+ api.media = {
228
+ /**
229
+ *
230
+ * @param {Object} data
231
+ * @param {String} data.file - file path to a media file
232
+ * @param {String} [data.thumbnail] - file path to a thumbnail file
233
+ * @param {String} [data.purpose]
234
+ * @returns Promise<Object>
235
+ */
236
+ upload(data) {
237
+ return makeUploadRequest('media', data, endpointFor('media/upload'));
238
+ }
239
+ };
226
240
 
227
- return makeUploadRequest('images', formData, endpointFor('images/upload'));
241
+ api.files = {
242
+ /**
243
+ *
244
+ * @param {Object} data
245
+ * @param {String} data.file - file path to a media file
246
+ * @param {String} [data.ref] - reference field returned in the response
247
+ * @returns Promise<Object>
248
+ */
249
+ upload(data) {
250
+ return makeUploadRequest('files', data, endpointFor('files/upload'));
228
251
  }
229
252
  };
230
253
 
@@ -242,17 +265,7 @@ module.exports = function GhostAdminAPI(options) {
242
265
 
243
266
  api.themes = {
244
267
  upload(data) {
245
- if (!data) {
246
- return Promise.reject(new Error('Missing data'));
247
- }
248
-
249
- if (!isValidUpload(data)) {
250
- return Promise.reject(new Error('Must be of FormData or include path'));
251
- }
252
-
253
- let formData = getFormData(data);
254
-
255
- return makeUploadRequest('themes', formData, endpointFor('themes/upload'));
268
+ return makeUploadRequest('themes', data, endpointFor('themes/upload'));
256
269
  },
257
270
  activate(name) {
258
271
  if (!name) {
@@ -266,14 +279,24 @@ module.exports = function GhostAdminAPI(options) {
266
279
  return api;
267
280
 
268
281
  function makeUploadRequest(resourceType, data, endpoint) {
282
+ if (!data) {
283
+ return Promise.reject(new Error('Missing data'));
284
+ }
285
+
286
+ if (!isValidUpload(data)) {
287
+ return Promise.reject(new Error('Must be of FormData or include path'));
288
+ }
289
+
290
+ let formData = getFormData(data);
291
+
269
292
  const headers = {
270
- 'Content-Type': `multipart/form-data; boundary=${data._boundary}`
293
+ 'Content-Type': `multipart/form-data; boundary=${formData._boundary}`
271
294
  };
272
295
 
273
296
  return makeApiRequest({
274
297
  endpoint: endpoint,
275
298
  method: 'POST',
276
- body: data,
299
+ body: formData,
277
300
  headers
278
301
  }).then((apiData) => {
279
302
  if (!Array.isArray(apiData[resourceType])) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryghost/admin-api",
3
- "version": "1.6.0",
3
+ "version": "1.7.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": "f8813610f5ba4eed4fbcbd025eddf78795fd8aab"
34
+ "gitHead": "bb3a33a1ff85c0d3efb2972e8d6dd72df458c271"
35
35
  }