From df6b7eca1a766843ae41714061834564c396ce25 Mon Sep 17 00:00:00 2001 From: Alexandre Bodin Date: Mon, 30 Aug 2021 18:31:09 +0200 Subject: [PATCH] Implement fields convert query params --- .../lib/services/entity-service/params.js | 7 +++--- .../core/utils/lib/convert-query-params.js | 24 +++++++++++++++++-- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/packages/core/strapi/lib/services/entity-service/params.js b/packages/core/strapi/lib/services/entity-service/params.js index 8d59e37f08..cedf2c489f 100644 --- a/packages/core/strapi/lib/services/entity-service/params.js +++ b/packages/core/strapi/lib/services/entity-service/params.js @@ -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) { diff --git a/packages/core/utils/lib/convert-query-params.js b/packages/core/utils/lib/convert-query-params.js index 10d3640d8a..f7880ce3f1 100644 --- a/packages/core/utils/lib/convert-query-params.js +++ b/packages/core/utils/lib/convert-query-params.js @@ -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, };