mirror of
https://github.com/strapi/strapi.git
synced 2025-09-27 17:29:14 +00:00
Handle fields selection in query
This commit is contained in:
parent
5995bccf8d
commit
d318f1cc65
@ -82,15 +82,34 @@ module.exports = {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
fetchQuery: function * (ctx) {
|
fetchQuery: function * (ctx) {
|
||||||
_.forEach(_.keys(ctx.query), function (key) {
|
// Use context namespace for passing information throug middleware
|
||||||
if (_.includes(['include', 'fields', 'sort', 'page', 'filter'], key)) {
|
ctx.state.filter = {
|
||||||
throw {
|
fields: {}
|
||||||
status: 400,
|
};
|
||||||
body: {
|
|
||||||
message: 'Parameter `' + key + '` is not supported yet'
|
_.forEach(ctx.query, function (value, key) {
|
||||||
}
|
if (_.includes(['include', 'sort', 'page', 'filter'], key)) {
|
||||||
};
|
throw {
|
||||||
}
|
status: 400,
|
||||||
|
body: {
|
||||||
|
message: 'Parameter `' + key + '` is not supported yet'
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} else if (key.indexOf('fields') !== -1) {
|
||||||
|
const alias = key.match(/\[(.*?)\]/)[1];
|
||||||
|
const type = utils.getType(ctx, alias);
|
||||||
|
|
||||||
|
if (_.isUndefined(type)) {
|
||||||
|
throw {
|
||||||
|
status: 403,
|
||||||
|
body: {
|
||||||
|
message: 'Wrong `type` in fields parameters'
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.state.filter.fields[type] = value.split(',');
|
||||||
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -91,14 +91,14 @@ module.exports = {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
toSerialize.attributes = _.keys(_.last(value));
|
toSerialize.attributes = ctx.state.filter.fields[type] || _.keys(_.last(value));
|
||||||
} else if (_.isObject(value) && !_.isEmpty(value)) {
|
} else if (_.isObject(value) && !_.isEmpty(value)) {
|
||||||
// Object
|
// Object
|
||||||
if (!_.isNull(PK) && value.hasOwnProperty(PK)) {
|
if (!_.isNull(PK) && value.hasOwnProperty(PK)) {
|
||||||
value[PK] = value[PK].toString();
|
value[PK] = value[PK].toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
toSerialize.attributes = _.keys(value);
|
toSerialize.attributes = ctx.state.filter.fields[type] || _.keys(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (object) {
|
switch (object) {
|
||||||
@ -125,7 +125,7 @@ module.exports = {
|
|||||||
|
|
||||||
// Display JSON API version support
|
// Display JSON API version support
|
||||||
if (_.isPlainObject(strapi.config.jsonapi) && strapi.config.jsonapi.hasOwnProperty('showVersion') && strapi.config.jsonapi.showVersion === true) {
|
if (_.isPlainObject(strapi.config.jsonapi) && strapi.config.jsonapi.hasOwnProperty('showVersion') && strapi.config.jsonapi.showVersion === true) {
|
||||||
return _.assign(new JSONAPI(type, value, toSerialize), {
|
return _.assign(new JSONAPI.Serializer(type, value, toSerialize), {
|
||||||
jsonapi: {
|
jsonapi: {
|
||||||
version: '1.0'
|
version: '1.0'
|
||||||
}
|
}
|
||||||
@ -157,7 +157,7 @@ module.exports = {
|
|||||||
ref: PK,
|
ref: PK,
|
||||||
included: strapi.config.jsonapi.included || false,
|
included: strapi.config.jsonapi.included || false,
|
||||||
ignoreRelationshipData: strapi.config.jsonapi.ignoreRelationshipData || false,
|
ignoreRelationshipData: strapi.config.jsonapi.ignoreRelationshipData || false,
|
||||||
attributes: _.keys(_.omit(strapi.models[type].attributes, _.isFunction)),
|
attributes: ctx.state.filter.fields[relation.model] || _.keys(_.omit(strapi.models[type].attributes, _.isFunction)),
|
||||||
relationshipLinks: {
|
relationshipLinks: {
|
||||||
self: function (record) {
|
self: function (record) {
|
||||||
if (record.hasOwnProperty(PK) && availableRoutes.relSlSelf) {
|
if (record.hasOwnProperty(PK) && availableRoutes.relSlSelf) {
|
||||||
@ -193,7 +193,7 @@ module.exports = {
|
|||||||
included: strapi.config.jsonapi.included || false,
|
included: strapi.config.jsonapi.included || false,
|
||||||
ignoreRelationshipData: strapi.config.jsonapi.ignoreRelationshipData || false,
|
ignoreRelationshipData: strapi.config.jsonapi.ignoreRelationshipData || false,
|
||||||
typeForAttribute: relation.collection,
|
typeForAttribute: relation.collection,
|
||||||
attributes: _.keys(_.omit(strapi.models[type].attributes, _.isFunction)),
|
attributes: ctx.state.filter.fields[relation.collection] || _.keys(_.omit(strapi.models[type].attributes, _.isFunction)),
|
||||||
relationshipLinks: {
|
relationshipLinks: {
|
||||||
self: function (record) {
|
self: function (record) {
|
||||||
if (record.hasOwnProperty(PK) && availableRoutes.relSlSelf) {
|
if (record.hasOwnProperty(PK) && availableRoutes.relSlSelf) {
|
||||||
|
@ -53,9 +53,6 @@ module.exports = {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
getType: function (ctx, supposedType) {
|
getType: function (ctx, supposedType) {
|
||||||
// TODO:
|
|
||||||
// - Parse the URL and try to extract useful information to find the type
|
|
||||||
|
|
||||||
// Relationships or related ressource
|
// Relationships or related ressource
|
||||||
if (strapi.models.hasOwnProperty(supposedType.toLowerCase()) && ctx.params.hasOwnProperty('relation')) {
|
if (strapi.models.hasOwnProperty(supposedType.toLowerCase()) && ctx.params.hasOwnProperty('relation')) {
|
||||||
return _.first(_.reject(_.map(strapi.models[supposedType.toLowerCase()].associations, function (relation) {
|
return _.first(_.reject(_.map(strapi.models[supposedType.toLowerCase()].associations, function (relation) {
|
||||||
@ -63,6 +60,17 @@ module.exports = {
|
|||||||
}), _.isUndefined));
|
}), _.isUndefined));
|
||||||
} else if (strapi.models.hasOwnProperty(supposedType.toLowerCase())) {
|
} else if (strapi.models.hasOwnProperty(supposedType.toLowerCase())) {
|
||||||
return supposedType.toLowerCase();
|
return supposedType.toLowerCase();
|
||||||
|
} else if (!strapi.models.hasOwnProperty(supposedType.toLowerCase())) {
|
||||||
|
// Deep search based on the relation alias
|
||||||
|
const tryFindType = _.reject(_.flattenDeep(_.map(strapi.models, function (model) {
|
||||||
|
return _.map(model.associations, function (relation) {
|
||||||
|
return (supposedType.toLowerCase() === relation.alias) ? relation.model || relation.collection : undefined;
|
||||||
|
});
|
||||||
|
})), _.isUndefined);
|
||||||
|
|
||||||
|
if (!_.isUndefined(tryFindType)) {
|
||||||
|
return _.first(tryFindType);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return undefined;
|
return undefined;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user