Skip to main content

strapi-utils 已重构

🌐 strapi-utils refactored

在 Strapi 5 中,strapi-utils 核心包已经被重构。本页列出了新增、移除及其他更新内容。

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

此页面是重大更改数据库的一部分,提供关于重大更改的信息以及从 Strapi v4 迁移到 Strapi 5 的附加说明。

🌐 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
(see utils-public-interface)

更改列表

🌐 List of changes

元素变更描述
arrays 工具已添加,并将 stringIncludes 方法移到其中(参见 附加说明)。
  • dates 工具
  • objects 工具
  • async 工具
  • strings 工具
已添加(参见 附加说明)。
strings.getCommonPath已添加
nameToSlug移动到 strings.nameToSlug
nameToCollectionName已移动到 strings.nameToCollectionName
stringIncludes移动到 arrays.includesString
stringEquals移动到 strings.isEqual
isCamelCase已移动到 strings.isCamelCase
isKebabCase移动到 strings.isKebabCase
toKebabCase已移动到 strings.toKebabCase
toRegressedEnumValue已移动到 strings.toRegressedEnumValue
startsWithANumber已移动到 strings.startsWithANumber
joinBy已移动到 strings.joinBy
keysDeep已移动到 objects.keysDeep
generateTimestampCode已移动到 dates.timestampCode
pipeAsync移动到 async.pipe
mapAsync移动到 async.map
reduceAsync移动到 async.reduce
convertQueryParams已替换(参见附加说明)。
validatesanitize已更新(参见 附加说明)。
getCommonBeginning已移除
  • getConfigUrls
  • getAbsoluteAdminUrl
  • getAbsoluteServerUrl
已移除
forEachAsync已移除
removeUndefined已移除
templateConfiguration 已移除(见附加说明)。

附加说明

🌐 Additional Notes

  • templateConfiguration:这在加载旧的 v3 配置文件(JSON 格式)以允许使用模板时使用。如果插件开发者仍在使用该函数,且确实需要使用模板,应该用真正的模板库来替代它的使用。

  • arrays 工具:要使用这些新工具:

    1. 在你的代码中使用 import { arrays, dates, strings, objects } from '@strapi/utils'; 导入它们。
    2. 例如,将它们用作 arrays.includesStringstrings.isEqual
  • convertQueryParams 被替换:

    // 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();
  • validatesanitize 现在是 strapi.contentAPI 功能的一部分:

    // 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();