Changed config endpoint for Admin API v2

no issue

- re-designed config endpoint
- timezones.json should live in Ghost-SDK long-term
This commit is contained in:
kirrg001 2019-02-24 18:37:11 +01:00 committed by Katharina Irrgang
parent f9974a91a9
commit bd7da54ce3
12 changed files with 81 additions and 151 deletions

View File

@ -0,0 +1,24 @@
const {isPlainObject} = require('lodash');
const config = require('../../config');
const labs = require('../../services/labs');
const ghostVersion = require('../../lib/ghost-version');
module.exports = {
docName: 'config',
read: {
permissions: false,
query() {
return {
version: ghostVersion.full,
environment: config.get('env'),
database: config.get('database').client,
mail: isPlainObject(config.get('mail')) ? config.get('mail').transport : '',
labs: labs.getAll(),
clientExtensions: config.get('clientExtensions') || {},
enableDeveloperExperiments: config.get('enableDeveloperExperiments') || false
};
}
}
};

View File

@ -1,58 +0,0 @@
const Promise = require('bluebird');
const {isPlainObject} = require('lodash');
const urlService = require('../../services/url');
const config = require('../../config');
const labs = require('../../services/labs');
const settingsCache = require('../../services/settings/cache');
const ghostVersion = require('../../lib/ghost-version');
function fetchAvailableTimezones() {
const timezones = require('../../data/timezones.json');
return timezones;
}
function getAboutConfig() {
return {
version: ghostVersion.full,
environment: config.get('env'),
database: config.get('database').client,
mail: isPlainObject(config.get('mail')) ? config.get('mail').transport : ''
};
}
function getBaseConfig() {
return {
useGravatar: !config.isPrivacyDisabled('useGravatar'),
publicAPI: labs.isSet('publicAPI'),
blogUrl: urlService.utils.urlFor('home', true),
blogTitle: settingsCache.get('title'),
clientExtensions: config.get('clientExtensions'),
enableDeveloperExperiments: config.get('enableDeveloperExperiments')
};
}
module.exports = {
docName: 'configuration',
read: {
permissions: false,
data: [
'key'
],
query({data}) {
if (!data.key) {
return Promise.resolve(getBaseConfig());
}
if (data.key === 'about') {
return Promise.resolve(getAboutConfig());
}
// Timezone endpoint
if (data.key === 'timezones') {
return Promise.resolve(fetchAvailableTimezones());
}
return Promise.resolve();
}
}
};

View File

@ -107,8 +107,8 @@ module.exports = {
return shared.pipeline(require('./authors'), localUtils);
},
get configuration() {
return shared.pipeline(require('./configuration'), localUtils);
get config() {
return shared.pipeline(require('./config'), localUtils);
},
get publicSettings() {

View File

@ -0,0 +1,9 @@
const debug = require('ghost-ignition').debug('api:v2:utils:serializers:output:config');
module.exports = {
all(data, apiConfig, frame) {
frame.response = data;
debug(frame.response);
}
};

View File

@ -1,11 +0,0 @@
const debug = require('ghost-ignition').debug('api:v2:utils:serializers:output:configuration');
module.exports = {
all(configuration, apiConfig, frame) {
frame.response = {
configuration: configuration ? [configuration] : []
};
debug(frame.response);
}
};

View File

@ -83,8 +83,8 @@ module.exports = {
return require('./authors');
},
get configuration() {
return require('./configuration');
get config() {
return require('./config');
},
get themes() {

View File

@ -13,10 +13,15 @@ labs.isSet = function isSet(flag) {
if (flag === 'members' && config.get('enableDeveloperExperiments')) {
return true;
}
var labsConfig = settingsCache.get('labs');
return labsConfig && labsConfig[flag] && labsConfig[flag] === true;
};
labs.getAll = () => {
return settingsCache.get('labs');
};
labs.enabledHelper = function enabledHelper(options, callback) {
const errDetails = {};
let errString;

View File

@ -20,7 +20,7 @@ const notImplemented = function (req, res, next) {
users: ['GET'],
themes: ['POST'],
subscribers: ['GET', 'PUT', 'DELETE', 'POST'],
configuration: ['GET'],
config: ['GET'],
webhooks: ['POST', 'DELETE']
};

View File

@ -24,8 +24,7 @@ module.exports = function apiRoutes() {
router.get('/site', http(apiv2.site.read));
// ## Configuration
router.get('/configuration', http(apiv2.configuration.read));
router.get('/configuration/:key', mw.authAdminApi, http(apiv2.configuration.read));
router.get('/config', mw.authAdminApi, http(apiv2.config.read));
// ## Posts
router.get('/posts', mw.authAdminApi, http(apiv2.posts.browse));

View File

@ -0,0 +1,35 @@
const should = require('should');
const supertest = require('supertest');
const testUtils = require('../../../utils');
const localUtils = require('./utils');
const config = require('../../../../server/config');
const ghost = testUtils.startGhost;
let request;
describe('Config API', function () {
before(function () {
return ghost()
.then(function () {
request = supertest.agent(config.get('url'));
})
.then(function () {
return localUtils.doAuth(request);
});
});
it('can retrieve config and all expected properties', function () {
return request
.get(localUtils.API.getApiQuery('config/'))
.set('Origin', config.get('url'))
.expect('Content-Type', /json/)
.expect('Cache-Control', testUtils.cacheRules.private)
.expect(200)
.then((res) => {
localUtils.API.checkResponse(res.body, 'config');
// full version
res.body.version.should.match(/\d+\.\d+\.\d+/);
});
});
});

View File

@ -1,75 +0,0 @@
const should = require('should');
const supertest = require('supertest');
const testUtils = require('../../../utils');
const localUtils = require('./utils');
const config = require('../../../../server/config');
const ghost = testUtils.startGhost;
let request;
describe('Configuration API', function () {
before(function () {
return ghost()
.then(function () {
request = supertest.agent(config.get('url'));
})
.then(function () {
return localUtils.doAuth(request);
});
});
it('can retrieve public configuration and all expected properties', function (done) {
request.get(localUtils.API.getApiQuery('configuration/'))
.set('Origin', config.get('url'))
.expect('Content-Type', /json/)
.expect('Cache-Control', testUtils.cacheRules.private)
.expect(200)
.end(function (err, res) {
if (err) {
return done(err);
}
should.exist(res.body.configuration);
res.body.configuration.should.be.an.Array().with.lengthOf(1);
const props = res.body.configuration[0];
props.blogUrl.should.eql('http://127.0.0.1:2369/');
props.useGravatar.should.eql(false);
// value not available, because settings API was not called yet
props.hasOwnProperty('blogTitle').should.eql(true);
done();
});
});
it('can read about config and get all expected properties', function (done) {
request.get(localUtils.API.getApiQuery('configuration/about/'))
.set('Origin', config.get('url'))
.expect('Content-Type', /json/)
.expect('Cache-Control', testUtils.cacheRules.private)
.expect(200)
.end(function (err, res) {
if (err) {
return done(err);
}
should.exist(res.body.configuration);
res.body.configuration.should.be.an.Array().with.lengthOf(1);
const props = res.body.configuration[0];
// Check the structure
props.should.have.property('version').which.is.a.String();
props.should.have.property('environment').which.is.a.String();
props.should.have.property('database').which.is.a.String();
props.should.have.property('mail').which.is.a.String();
// Check a few values
props.environment.should.match(/^testing/);
props.version.should.eql(require('../../../../../package.json').version);
done();
});
});
});

View File

@ -22,6 +22,8 @@ const expectedProperties = {
action: ['id', 'resource_type', 'actor_type', 'event', 'created_at', 'actor'],
config: ['version', 'environment', 'database', 'mail', 'labs', 'clientExtensions', 'enableDeveloperExperiments'],
post: _(schema.posts)
.keys()
// by default we only return mobildoc