Enabled or not JSON API support thanks to configuration

This commit is contained in:
Aurélien Georget 2016-01-29 16:01:10 +01:00
parent 9ba5137777
commit 744bf74069
4 changed files with 20 additions and 17 deletions

View File

@ -52,7 +52,7 @@ module.exports = {
formatBody: function * (ctx) {
const body = ctx.request.body;
const values = _.assign({}, body.data.attributes, _.mapValues(body.data.relationships, function (relation, key) {
const values = _.assign({}, body.data.attributes, _.mapValues(body.data.relationships, function (relation) {
return _.isArray(relation.data) ? _.map(relation.data, 'id') : relation.data.id;
}));

View File

@ -50,13 +50,11 @@ module.exports = {
*/
serialize: function (ctx, type, object, value) {
// TODO:
// - Handle configuration with a file to improve flexibility of JSON API support
const toSerialize = {
topLevelLinks: {self: ctx.request.origin + ctx.request.url},
keyForAttribute: 'camelCase',
pluralizeType: false,
included: true,
typeForAttribute: function (currentType) {
if (strapi.models.hasOwnProperty(type)) {
return _.first(_.reject(_.map(strapi.models[type].associations, function (relation) {
@ -66,15 +64,20 @@ module.exports = {
}
};
// Assign custom configurations
if (_.isPlainObject(strapi.config.jsonapi) && !_.isEmpty(strapi.config.jsonapi)) {
_.assign(toSerialize, _.omit(strapi.config.jsonapi, 'enabled'));
}
const PK = utils.getPK(type);
if (_.isArray(value) && !_.isEmpty(value)) {
// Array
if (!_.isNull(PK)) {
_.forEach(value, function (record) {
if (record.hasOwnProperty(PK)) {
record[PK] = record[PK].toString();
}
if (record.hasOwnProperty(PK)) {
record[PK] = record[PK].toString();
}
});
}
@ -126,11 +129,9 @@ module.exports = {
*/
includedRelationShips: function (ctx, toSerialize, type) {
const self = this;
if (strapi.models.hasOwnProperty(type)) {
_.forEach(strapi.models[type].associations, function (relation) {
let PK = utils.getPK(relation.model) || utils.getPK(relation.collection);
const PK = utils.getPK(relation.model) || utils.getPK(relation.collection);
switch (relation.nature) {
case 'oneToOne':

View File

@ -23,10 +23,6 @@ module.exports = function (strapi) {
*/
initialize: function (cb) {
// TODO:
// - Force or not the routes?
// - Add middleware before called the controller action to check parameters structure
function * _interceptor(next) {
const self = this;
@ -66,7 +62,9 @@ module.exports = function (strapi) {
}
}
strapi.app.use(_interceptor);
if ((_.isPlainObject(strapi.config.jsonapi) && strapi.config.jsonapi.enabled === true) || (_.isBoolean(strapi.config.jsonapi) && strapi.config.jsonapi === true)) {
strapi.app.use(_interceptor);
}
cb();
},
@ -78,7 +76,7 @@ module.exports = function (strapi) {
parse: function (strapi) {
return function * (next) {
// Verify Content-Type header and exclude administration and user routes
if (this.request.url.indexOf('admin') !== -1) {
if (this.request.url.indexOf('admin') !== -1 && !(_.isPlainObject(strapi.config.jsonapi) && strapi.config.jsonapi.enabled === true)) {
yield next;
} else if (this.request.type !== 'application/vnd.api+json') {
this.response.status = 406;

View File

@ -87,7 +87,11 @@ module.exports = function (strapi) {
// Add the `globalPolicy`.
policies.push(globalPolicy(endpoint, value, route));
policies.push(JSONAPI.parse(strapi));
// Enabled JSON API support
if ((_.isPlainObject(strapi.config.jsonapi) && strapi.config.jsonapi.enabled === true) || (_.isBoolean(strapi.config.jsonapi) && strapi.config.jsonapi === true)) {
policies.push(JSONAPI.parse(strapi));
}
if (_.isArray(value.policies) && !_.isEmpty(value.policies)) {
_.forEach(value.policies, function (policy) {