Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@tryghost/admin-api

Package Overview
Dependencies
Maintainers
11
Versions
84
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tryghost/admin-api - npm Package Compare versions

Comparing version
0.0.1
to
0.0.2
+73
-29
cjs/admin-api.js
'use strict';
var axios = require('axios');
var FormData = require('form-data');
var fs = require('fs');
var jwt = require('jsonwebtoken');

@@ -51,27 +53,30 @@

// TODO: these data manipulations and validations are resource specific
// should extract them into separate data mappings methods per resource type
if (!data.author && !data.authors) {
return Promise.reject(new Error('Missing author data. Expected `author` id or `authors` ids array'));
}
const mapped = {};
let authors = [];
if (data.author) {
authors.push({id: data.author});
delete data.author;
} else {
authors = data.authors.map(id => ({id}));
delete data.authors;
}
if (resourceType === 'posts') {
// TODO: these data manipulations and validations are resource specific
// should extract them into separate data mappings methods per resource type
if (!data.author && !data.authors) {
return Promise.reject(new Error('Missing author data. Expected `author` id or `authors` ids array'));
}
data.authors = authors;
let authors = [];
if (data.author) {
authors.push({id: data.author});
delete data.author;
} else {
authors = data.authors.map(id => ({id}));
delete data.authors;
}
// resource data should not contain id or slug information
delete data.id;
delete data.slug;
data.authors = authors;
const wrapped = {};
wrapped[resourceType] = [data];
// resource data should not contain id or slug information
delete data.id;
delete data.slug;
return makeRequest(resourceType, options, wrapped, 'POST');
mapped[resourceType] = [data];
}
return makeResourceRequest(resourceType, options, mapped, 'POST');
}

@@ -87,6 +92,6 @@ function edit(data, options = {}) {

const wrapped = {};
const mapped = {};
if (data.id) {
wrapped.id = data.id;
mapped.id = data.id;
delete data.id;

@@ -96,8 +101,8 @@ }

if (data.slug) {
wrapped.slug = data.slug;
mapped.slug = data.slug;
}
wrapped[resourceType] = [data];
mapped[resourceType] = [data];
return makeRequest(resourceType, options, wrapped, 'PUT');
return makeResourceRequest(resourceType, options, mapped, 'PUT');
}

@@ -113,6 +118,6 @@ function destroy(data, options = {}) {

return makeRequest(resourceType, options, data, 'DELETE');
return makeResourceRequest(resourceType, options, data, 'DELETE');
}
function browse(options = {}) {
return makeRequest(resourceType, options);
return makeResourceRequest(resourceType, options);
}

@@ -130,3 +135,3 @@ function read(data, options = {}) {

return makeRequest(resourceType, params, data);
return makeResourceRequest(resourceType, params, data);
}

@@ -145,5 +150,44 @@

api.images = {
add(data) {
if (!data) {
return Promise.reject(new Error('Missing data'));
}
if (typeof data !== FormData && !data.path) {
return Promise.reject(new Error('Must be of FormData or include path'));
}
let formData;
if (data.path) {
formData = new FormData();
formData.append('uploadimage', fs.createReadStream(data.path));
}
return makeImageRequest(formData || data);
}
};
return api;
function makeRequest(resourceType, params, data = {}, method = 'GET') {
function makeImageRequest(data) {
const endpoint = `/${ghostPath}/api/${version}/admin/images/`;
const url = `${host}${endpoint}`;
const headers = {
Authorization: `Ghost ${token(endpoint, key)}`,
'Content-Type': `multipart/form-data; boundary=${data._boundary}`
};
return axios({
url: url,
method: 'POST',
data: data,
headers: headers
}).then((res) => {
return res.data;
});
}
function makeResourceRequest(resourceType, params, data = {}, method = 'GET') {
delete params.id;

@@ -150,0 +194,0 @@ let id;

import axios from 'axios';
import FormData from 'form-data';
import fs from 'fs';
import token from './token';

@@ -37,27 +39,30 @@

// TODO: these data manipulations and validations are resource specific
// should extract them into separate data mappings methods per resource type
if (!data.author && !data.authors) {
return Promise.reject(new Error('Missing author data. Expected `author` id or `authors` ids array'));
}
const mapped = {};
let authors = [];
if (data.author) {
authors.push({id: data.author});
delete data.author;
} else {
authors = data.authors.map(id => ({id}));
delete data.authors;
}
if (resourceType === 'posts') {
// TODO: these data manipulations and validations are resource specific
// should extract them into separate data mappings methods per resource type
if (!data.author && !data.authors) {
return Promise.reject(new Error('Missing author data. Expected `author` id or `authors` ids array'));
}
data.authors = authors;
let authors = [];
if (data.author) {
authors.push({id: data.author});
delete data.author;
} else {
authors = data.authors.map(id => ({id}));
delete data.authors;
}
// resource data should not contain id or slug information
delete data.id;
delete data.slug;
data.authors = authors;
const wrapped = {};
wrapped[resourceType] = [data];
// resource data should not contain id or slug information
delete data.id;
delete data.slug;
return makeRequest(resourceType, options, wrapped, 'POST');
mapped[resourceType] = [data];
}
return makeResourceRequest(resourceType, options, mapped, 'POST');
}

@@ -73,6 +78,6 @@ function edit(data, options = {}) {

const wrapped = {};
const mapped = {};
if (data.id) {
wrapped.id = data.id;
mapped.id = data.id;
delete data.id;

@@ -82,8 +87,8 @@ }

if (data.slug) {
wrapped.slug = data.slug;
mapped.slug = data.slug;
}
wrapped[resourceType] = [data];
mapped[resourceType] = [data];
return makeRequest(resourceType, options, wrapped, 'PUT');
return makeResourceRequest(resourceType, options, mapped, 'PUT');
}

@@ -99,6 +104,6 @@ function destroy(data, options = {}) {

return makeRequest(resourceType, options, data, 'DELETE');
return makeResourceRequest(resourceType, options, data, 'DELETE');
}
function browse(options = {}) {
return makeRequest(resourceType, options);
return makeResourceRequest(resourceType, options);
}

@@ -116,3 +121,3 @@ function read(data, options = {}) {

return makeRequest(resourceType, params, data);
return makeResourceRequest(resourceType, params, data);
}

@@ -131,5 +136,44 @@

api.images = {
add(data) {
if (!data) {
return Promise.reject(new Error('Missing data'));
}
if (typeof data !== FormData && !data.path) {
return Promise.reject(new Error('Must be of FormData or include path'));
}
let formData;
if (data.path) {
formData = new FormData();
formData.append('uploadimage', fs.createReadStream(data.path));
}
return makeImageRequest(formData || data);
}
};
return api;
function makeRequest(resourceType, params, data = {}, method = 'GET') {
function makeImageRequest(data) {
const endpoint = `/${ghostPath}/api/${version}/admin/images/`;
const url = `${host}${endpoint}`;
const headers = {
Authorization: `Ghost ${token(endpoint, key)}`,
'Content-Type': `multipart/form-data; boundary=${data._boundary}`
};
return axios({
url: url,
method: 'POST',
data: data,
headers: headers
}).then((res) => {
return res.data;
});
}
function makeResourceRequest(resourceType, params, data = {}, method = 'GET') {
delete params.id;

@@ -136,0 +180,0 @@ let id;

{
"name": "@tryghost/admin-api",
"version": "0.0.1",
"version": "0.0.2",
"repository": "https://github.com/TryGhost/Ghost-SDKs/tree/master/packages/admin-api",

@@ -44,5 +44,6 @@ "author": "Ghost Foundation",

"axios": "0.18.0",
"form-data": "^2.3.3",
"jsonwebtoken": "^8.4.0"
},
"gitHead": "a9801ce945a59180fbcfdd92595e776b9d64a0cc"
"gitHead": "0350bcd9cb92309f9d56c5c43702c45045b69def"
}

Sorry, the diff of this file is too big to display