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 中

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

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

在 Strapi 5 中

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

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

迁移

🌐 Migration


注意

🌐 Notes

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

手动操作

🌐 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 中

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

在 Strapi 5 中

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