mirror of
https://github.com/strapi/strapi.git
synced 2025-12-08 04:54:44 +00:00
Enabled or not JSON API support thanks to configuration
This commit is contained in:
parent
9ba5137777
commit
744bf74069
@ -52,7 +52,7 @@ module.exports = {
|
|||||||
|
|
||||||
formatBody: function * (ctx) {
|
formatBody: function * (ctx) {
|
||||||
const body = ctx.request.body;
|
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;
|
return _.isArray(relation.data) ? _.map(relation.data, 'id') : relation.data.id;
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|||||||
@ -50,13 +50,11 @@ module.exports = {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
serialize: function (ctx, type, object, value) {
|
serialize: function (ctx, type, object, value) {
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// - Handle configuration with a file to improve flexibility of JSON API support
|
|
||||||
const toSerialize = {
|
const toSerialize = {
|
||||||
topLevelLinks: {self: ctx.request.origin + ctx.request.url},
|
topLevelLinks: {self: ctx.request.origin + ctx.request.url},
|
||||||
keyForAttribute: 'camelCase',
|
keyForAttribute: 'camelCase',
|
||||||
pluralizeType: false,
|
pluralizeType: false,
|
||||||
|
included: true,
|
||||||
typeForAttribute: function (currentType) {
|
typeForAttribute: function (currentType) {
|
||||||
if (strapi.models.hasOwnProperty(type)) {
|
if (strapi.models.hasOwnProperty(type)) {
|
||||||
return _.first(_.reject(_.map(strapi.models[type].associations, function (relation) {
|
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);
|
const PK = utils.getPK(type);
|
||||||
|
|
||||||
if (_.isArray(value) && !_.isEmpty(value)) {
|
if (_.isArray(value) && !_.isEmpty(value)) {
|
||||||
// Array
|
// Array
|
||||||
if (!_.isNull(PK)) {
|
if (!_.isNull(PK)) {
|
||||||
_.forEach(value, function (record) {
|
_.forEach(value, function (record) {
|
||||||
if (record.hasOwnProperty(PK)) {
|
if (record.hasOwnProperty(PK)) {
|
||||||
record[PK] = record[PK].toString();
|
record[PK] = record[PK].toString();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,11 +129,9 @@ module.exports = {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
includedRelationShips: function (ctx, toSerialize, type) {
|
includedRelationShips: function (ctx, toSerialize, type) {
|
||||||
const self = this;
|
|
||||||
|
|
||||||
if (strapi.models.hasOwnProperty(type)) {
|
if (strapi.models.hasOwnProperty(type)) {
|
||||||
_.forEach(strapi.models[type].associations, function (relation) {
|
_.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) {
|
switch (relation.nature) {
|
||||||
case 'oneToOne':
|
case 'oneToOne':
|
||||||
|
|||||||
@ -23,10 +23,6 @@ module.exports = function (strapi) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
initialize: function (cb) {
|
initialize: function (cb) {
|
||||||
// TODO:
|
|
||||||
// - Force or not the routes?
|
|
||||||
// - Add middleware before called the controller action to check parameters structure
|
|
||||||
|
|
||||||
function * _interceptor(next) {
|
function * _interceptor(next) {
|
||||||
const self = this;
|
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();
|
cb();
|
||||||
},
|
},
|
||||||
@ -78,7 +76,7 @@ module.exports = function (strapi) {
|
|||||||
parse: function (strapi) {
|
parse: function (strapi) {
|
||||||
return function * (next) {
|
return function * (next) {
|
||||||
// Verify Content-Type header and exclude administration and user routes
|
// 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;
|
yield next;
|
||||||
} else if (this.request.type !== 'application/vnd.api+json') {
|
} else if (this.request.type !== 'application/vnd.api+json') {
|
||||||
this.response.status = 406;
|
this.response.status = 406;
|
||||||
|
|||||||
@ -87,7 +87,11 @@ module.exports = function (strapi) {
|
|||||||
|
|
||||||
// Add the `globalPolicy`.
|
// Add the `globalPolicy`.
|
||||||
policies.push(globalPolicy(endpoint, value, route));
|
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)) {
|
if (_.isArray(value.policies) && !_.isEmpty(value.policies)) {
|
||||||
_.forEach(value.policies, function (policy) {
|
_.forEach(value.policies, function (policy) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user