# strapi-utils 已重构

> Source: https://strapi.nodejs.cn/cms/migration/v4-to-v5/breaking-changes/strapi-utils-refactored

🌐 `strapi-utils` refactored

在 Strapi 5 中，`strapi-utils` 包进行了重构，实用工具被重新组织到 `arrays`、`dates`、`strings`、`objects` 和 `async` 命名空间中，并且一些实用工具被移动、替换为服务方法或被移除。

🌐 In Strapi 5, the `strapi-utils` package is refactored with utilities reorganized into `arrays`, `dates`, `strings`, `objects`, and `async` namespaces, and some utilities moved, replaced with service methods, or removed.

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

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

此页面是[重大更改数据库](/cms/migration/v4-to-v5/breaking-changes)的一部分，提供关于重大更改的信息以及从 Strapi v4 迁移到 Strapi 5 的附加说明。

🌐 This page is part of the [breaking changes database](/cms/migration/v4-to-v5/breaking-changes) 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
  - Codemod: [utils-public-interface](https://github.com/strapi/strapi/blob/develop/packages/utils/upgrade/resources/codemods/5.0.0/utils-public-interface.code.ts)

## 更改列表 {#list-of-changes}

🌐 List of changes

| 元素 | 变更描述 |
|---------------------------|-----------------------------------------------------------------------------------------------------|
| `arrays` 工具 | 已添加，并将 `stringIncludes` 方法移到其中（参见 [附加说明](#additional-notes)）。 |
| <ul><li>`dates` 工具</li><li>`objects` 工具</li><li>`async` 工具</li><li>`strings` 工具</li></ul> | 已添加（请参见[附加说明](#additional-notes)）。 |
| `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` | 已替换（参见[附加说明](#additional-notes)）。 |
| `validate` 和 `sanitize` | 已更新（参见 [附加说明](#additional-notes)）。 |
| `getCommonBeginning` | 已移除 |
| <ul><li>`getConfigUrls`</li><li>`getAbsoluteAdminUrl`</li><li>`getAbsoluteServerUrl`</li></ul> | 已移除 |
| `forEachAsync` | 已移除 |
| `removeUndefined` | 已移除 |
| `templateConfiguration`  | 已移除（见[附加说明](#additional-notes)）。 |

## 附加说明 {#additional-notes}

🌐 Additional Notes

- `templateConfiguration`：这在加载旧的 v3 配置文件（JSON 格式）以允许使用模板时使用。如果插件开发者仍在使用该函数，且确实需要使用模板，应该用真正的模板库来替代它的使用。
- `arrays` 工具：要使用这些新工具：
  1. 在你的代码中使用 `import { arrays, dates, strings, objects } from '@strapi/utils';` 导入它们。
  2. 例如，将它们用作 `arrays.includesString` 或 `strings.isEqual`。
- `convertQueryParams` 被替换：

  ```js
  // 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` 和 `sanitize` 现在是 `strapi.contentAPI` 功能的一部分：

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

:::strapi

有关所有可用实用程序的完整参考，请参阅 [`strapi-utils` 文档](/cms/strapi-utils)。

🌐 For the complete reference of all available utilities, see the [`strapi-utils` documentation](/cms/strapi-utils).

:::
