mirror of
https://github.com/strapi/strapi.git
synced 2025-11-08 22:32:02 +00:00
Implement fields convert query params
This commit is contained in:
parent
d6dabb6b02
commit
df6b7eca1a
@ -1,6 +1,5 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const _ = require('lodash');
|
|
||||||
const { pick } = require('lodash/fp');
|
const { pick } = require('lodash/fp');
|
||||||
|
|
||||||
const {
|
const {
|
||||||
@ -9,19 +8,19 @@ const {
|
|||||||
convertStartQueryParams,
|
convertStartQueryParams,
|
||||||
convertPopulateQueryParams,
|
convertPopulateQueryParams,
|
||||||
convertFiltersQueryParams,
|
convertFiltersQueryParams,
|
||||||
|
convertFieldsQueryParams,
|
||||||
} = require('@strapi/utils/lib/convert-query-params');
|
} = require('@strapi/utils/lib/convert-query-params');
|
||||||
|
|
||||||
const { contentTypes: contentTypesUtils } = require('@strapi/utils');
|
const { contentTypes: contentTypesUtils } = require('@strapi/utils');
|
||||||
|
|
||||||
const { PUBLISHED_AT_ATTRIBUTE } = contentTypesUtils.constants;
|
const { PUBLISHED_AT_ATTRIBUTE } = contentTypesUtils.constants;
|
||||||
|
|
||||||
|
// TODO: check invalid values / add defaults ....
|
||||||
const transformParamsToQuery = (uid, params = {}) => {
|
const transformParamsToQuery = (uid, params = {}) => {
|
||||||
const model = strapi.getModel(uid);
|
const model = strapi.getModel(uid);
|
||||||
|
|
||||||
const query = {};
|
const query = {};
|
||||||
|
|
||||||
// TODO: check invalid values / add defaults ....
|
|
||||||
|
|
||||||
const {
|
const {
|
||||||
start,
|
start,
|
||||||
page,
|
page,
|
||||||
@ -72,7 +71,7 @@ const transformParamsToQuery = (uid, params = {}) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (fields) {
|
if (fields) {
|
||||||
query.select = _.castArray(fields);
|
query.select = convertFieldsQueryParams(fields);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (populate) {
|
if (populate) {
|
||||||
|
|||||||
@ -95,7 +95,7 @@ const convertPopulateQueryParams = (populate, depth = 0) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
throw new Error(
|
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) {
|
if (fields) {
|
||||||
query.select = _.castArray(fields);
|
query.select = convertFieldsQueryParams(fields);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (populate) {
|
if (populate) {
|
||||||
@ -136,6 +136,25 @@ const convertNestedPopulate = subPopulate => {
|
|||||||
return query;
|
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
|
// NOTE: We could validate the parameters are on existing / non private attributes
|
||||||
const convertFiltersQueryParams = filters => filters;
|
const convertFiltersQueryParams = filters => filters;
|
||||||
|
|
||||||
@ -162,6 +181,7 @@ module.exports = {
|
|||||||
convertLimitQueryParams,
|
convertLimitQueryParams,
|
||||||
convertPopulateQueryParams,
|
convertPopulateQueryParams,
|
||||||
convertFiltersQueryParams,
|
convertFiltersQueryParams,
|
||||||
|
convertFieldsQueryParams,
|
||||||
VALID_REST_OPERATORS,
|
VALID_REST_OPERATORS,
|
||||||
QUERY_OPERATORS,
|
QUERY_OPERATORS,
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user