Skip to main content

strapi.fetch 使用原生 fetch() API

¥strapi.fetch uses the native fetch() API

在 Strapi 5 中,strapi.fetch 对象现在封装的是 node Fetch API,而不是 node-fetch。

¥In Strapi 5, the strapi.fetch object is now wrapping node Fetch API instead of node-fetch.

此页面是 重大变更数据库 的一部分,提供有关重大更改的信息以及从 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?No

重大更改描述

¥Breaking change description

在 Strapi v4 中

¥In Strapi v4

Strapi.fetch 封装了 node-fetch 的 fetch() 并接受相同的参数。

¥Strapi.fetch wrapped node-fetch’s fetch() and accepted the same parameters.

在 Strapi 5 中

¥In Strapi 5

node-fetch 模块不再使用。strapi.fetch 调用原生 fetch() 方法。

¥The node-fetch module is not used anymore. strapi.fetch calls the native fetch() method.

迁移

¥Migration


注意

¥Notes

  • 这些参数大多兼容,但也存在一些差异。

    ¥The parameters are mostly compatible but there are some differences.

手动程序

¥Manual procedure

如果你的 Strapi v4 代码将 timeout 参数传递给 strapi.fetch,请将其替换为信号属性,如下所示:

¥If your Strapi v4 code passed the timeout parameter to strapi.fetch, replace it with a signal property as follows:

在 Strapi v4 中

¥In Strapi v4

strapi.fetch(url, {
method: 'POST',
body,
headers,
timeout: 1000,
}); // accepts the type RequestInit from node-fetch

在 Strapi 5 中

¥In Strapi 5

strapi.fetch(url, {
method: 'POST',
body,
headers,
signal: AbortSignal.timeout(1000)
}); // accepts the type RequestInit native to Node