mirror of
https://github.com/strapi/strapi.git
synced 2025-11-02 02:44:55 +00:00
Implement fields convert query params
This commit is contained in:
parent
d6dabb6b02
commit
df6b7eca1a
@ -1,6 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
const _ = require('lodash');
|
||||
const { pick } = require('lodash/fp');
|
||||
|
||||
const {
|
||||
@ -9,19 +8,19 @@ const {
|
||||
convertStartQueryParams,
|
||||
convertPopulateQueryParams,
|
||||
convertFiltersQueryParams,
|
||||
convertFieldsQueryParams,
|
||||
} = require('@strapi/utils/lib/convert-query-params');
|
||||
|
||||
const { contentTypes: contentTypesUtils } = require('@strapi/utils');
|
||||
|
||||
const { PUBLISHED_AT_ATTRIBUTE } = contentTypesUtils.constants;
|
||||
|
||||
// TODO: check invalid values / add defaults ....
|
||||
const transformParamsToQuery = (uid, params = {}) => {
|
||||
const model = strapi.getModel(uid);
|
||||
|
||||
const query = {};
|
||||
|
||||
// TODO: check invalid values / add defaults ....
|
||||
|
||||
const {
|
||||
start,
|
||||
page,
|
||||
@ -72,7 +71,7 @@ const transformParamsToQuery = (uid, params = {}) => {
|
||||
}
|
||||
|
||||
if (fields) {
|
||||
query.select = _.castArray(fields);
|
||||
query.select = convertFieldsQueryParams(fields);
|
||||
}
|
||||
|
||||
if (populate) {
|
||||
|
||||
@ -95,7 +95,7 @@ const convertPopulateQueryParams = (populate, depth = 0) => {
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
'Invalid populate parameter. Expected a string or an array of strings or a populate object'
|
||||
'Invalid populate parameter. Expected a string, an array of strings or a populate object'
|
||||
);
|
||||
};
|
||||
|
||||
@ -126,7 +126,7 @@ const convertNestedPopulate = subPopulate => {
|
||||
}
|
||||
|
||||
if (fields) {
|
||||
query.select = _.castArray(fields);
|
||||
query.select = convertFieldsQueryParams(fields);
|
||||
}
|
||||
|
||||
if (populate) {
|
||||
@ -136,6 +136,25 @@ const convertNestedPopulate = subPopulate => {
|
||||
return query;
|
||||
};
|
||||
|
||||
const convertFieldsQueryParams = (fields, depth = 0) => {
|
||||
if (depth === 0 && fields === '*') {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (typeof fields === 'string') {
|
||||
const fieldsValues = fields.split(',').map(value => _.trim(value));
|
||||
return _.uniq(['id', ...fieldsValues]);
|
||||
}
|
||||
|
||||
if (Array.isArray(fields)) {
|
||||
// map convert
|
||||
const fieldsValues = fields.flatMap(value => convertPopulateQueryParams(value, depth + 1));
|
||||
return _.uniq(['id', ...fieldsValues]);
|
||||
}
|
||||
|
||||
throw new Error('Invalid fields parameter. Expected a string or an array of strings');
|
||||
};
|
||||
|
||||
// NOTE: We could validate the parameters are on existing / non private attributes
|
||||
const convertFiltersQueryParams = filters => filters;
|
||||
|
||||
@ -162,6 +181,7 @@ module.exports = {
|
||||
convertLimitQueryParams,
|
||||
convertPopulateQueryParams,
|
||||
convertFiltersQueryParams,
|
||||
convertFieldsQueryParams,
|
||||
VALID_REST_OPERATORS,
|
||||
QUERY_OPERATORS,
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user