strapi-utils 已重构

页面摘要: 在 Strapi 5 中,strapi-utils 包已被重构,工具被重新组织到 arrays、dates、strings、objects 和 async 命名空间中,部分工具被移动、替换为服务方法或被移除。

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

本页面是破坏性变更数据库的一部分,提供有关该破坏性变更的信息,以及从 Strapi v4 迁移到 Strapi 5 的附加说明。

代码迁移工具: utils-public-interface

变更列表

元素变更说明
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 | 已替换(请参阅 附加说明)。 | | validate 和 sanitize | 已更新(请参阅 附加说明)。 | | getCommonBeginning | 已移除 | |

  • getConfigUrls

  • getAbsoluteAdminUrl

  • getAbsoluteServerUrl | 已移除 | | forEachAsync | 已移除 | | removeUndefined | 已移除 | | templateConfiguration  | 已移除(请参阅 附加说明)。 |

附加说明

  • templateConfiguration:当以 JSON 加载旧的 v3 配置文件时,此函数用于支持模板。仍在使用该函数的插件开发者如果确实需要,应将其用法替换为真正的模板库。

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

    1. 在代码中通过 import { arrays, dates, strings, objects } from '@strapi/utils'; 导入它们。
    2. 使用它们,例如 arrays.includesString 或 strings.isEqual。
  • convertQueryParams 已被替换:

    // Strapi v4
    import { convertQueryParams } from '@strapi/utils';
    
    convertQueryParams.convertSortQueryParams(...); // 现为私有函数以简化 api
    convertQueryParams.convertStartQueryParams(...); // 现为私有函数以简化 api
    convertQueryParams.convertLimitQueryParams(...); // 现为私有函数以简化 api
    convertQueryParams.convertPopulateQueryParams(...); // 现为私有函数以简化 api
    convertQueryParams.convertFiltersQueryParams(...); // 现为私有函数以简化 api
    convertQueryParams.convertFieldsQueryParams(...); // 现为私有函数以简化 api
    convertQueryParams.convertPublicationStateParams(...); // 现为私有函数以简化 api
    
    convertQueryParams.transformParamsToQuery(...); // 变为下方示例
    
    // Strapi 5 
    // 这些工具需要 strapi 应用上下文,因此我们决定为此暴露一个 strapi 服务
    strapi.get('query-params').transform();
    
  • validate 和 sanitize 现在是 strapi.contentAPI 函数的一部分:

    // Strapi v4
    import { validate, sanitize } from '@strapi/utils';
    
    validate.contentAPI.xxx();
    sanitize.contentAPI.xxx();
    
    // Strapi 5
    // 这些方法需要 strapi 应用上下文
    strapi.contentAPI.sanitize.xxx();
    strapi.contentAPI.validate.xxx();
    
INFO

有关所有可用工具的完整参考,请参阅 strapi-utils 文档。