Skip to main content

strapi-utils refactored

In Strapi 5, the strapi-utils core package has been refactored. This page lists the additions, removals, and other updates.

This page is part of the breaking changes database and provides information about the breaking change and additional instructions to migrate from Strapi v4 to Strapi 5.

🔌 Is this breaking change affecting plugins?Yes
🤖 Is this breaking change automatically handled by a codemod?Yes

List of changes

ElementDescription of the change
arrays utilsAdded, and moved the stringIncludes method inside it (see additional notes).
  • dates utils
  • objects utils
  • async utils
  • strings utils
Added (see additional notes).
strings.getCommonPathAdded
nameToSlugMoved to strings.nameToSlug
nameToCollectionNameMoved to strings.nameToCollectionName
stringIncludesMoved to arrays.includesString
stringEqualsMoved to strings.isEqual
isCamelCaseMoved to strings.isCamelCase
isKebabCaseMoved to strings.isKebabCase
toKebabCaseMoved to strings.toKebabCase
toRegressedEnumValueMoved to strings.toRegressedEnumValue
startsWithANumberMoved to strings.startsWithANumber
joinByMoved to strings.joinBy
keysDeepMoved to objects.keysDeep
generateTimestampCodeMoved to dates.timestampCode
pipeAsyncMoved to async.pipe
mapAsyncMoved to async.map
reduceAsyncMoved to async.reduce
convertQueryParamsReplaced (see additional notes).
validate and sanitizeUpdated (see additional notes).
getCommonBeginningRemoved
  • getConfigUrls
  • getAbsoluteAdminUrl
  • getAbsoluteServerUrl
Removed
forEachAsyncRemoved
removeUndefinedRemoved
templateConfiguration Removed (see additional notes).

Additional Notes

  • templateConfiguration: This was used when loading the old v3 configuration files in JSON to allow for templates. Plugin developers still using the function should replace its usage by a real template library if they really need to.

  • arrays utils: To use these new utils:

    1. Import them in your code with import { arrays, dates, strings, objects } from '@strapi/utils';.
    2. Use them, for instance as arrays.includesString or strings.isEqual.
  • convertQueryParams is replaced:

    // Strapi v4
    import { convertQueryParams } from '@strapi/utils';

    convertQueryParams.convertSortQueryParams(...); // now private function to simplify the api
    convertQueryParams.convertStartQueryParams(...); // now private function to simplify the api
    convertQueryParams.convertLimitQueryParams(...); // now private function to simplify the api
    convertQueryParams.convertPopulateQueryParams(...); // now private function to simplify the api
    convertQueryParams.convertFiltersQueryParams(...); // now private function to simplify the api
    convertQueryParams.convertFieldsQueryParams(...); // now private function to simplify the api
    convertQueryParams.convertPublicationStateParams(...); // now private function to simplify the api

    convertQueryParams.transformParamsToQuery(...); // becomes the example below

    // Strapi 5
    // Those utils required the strapi app context, so we decided to expose a strapi service for it
    strapi.get('query-params').transform();
  • validate and sanitize are now part of the strapi.contentAPI functions:

    // Strapi v4
    import { validate, sanitize } from '@strapi/utils';

    validate.contentAPI.xxx();
    sanitize.contentAPI.xxx();

    // Strapi 5
    // Those methods require the strapi app context
    strapi.contentAPI.sanitize.xxx();
    strapi.contentAPI.validate.xxx();