Are you an LLM? You can read better optimized documentation at /docs/guide/usage/linter/rules/unicorn/no-invalid-fetch-options.md for this page in Markdown format
unicorn/no-invalid-fetch-options 正确性
✅ This rule is turned on by default.
它做了什么
禁止在 fetch() 和 new Request() 中使用无效的选项。具体来说,该规则确保当请求方法为 GET 或 HEAD 时,不会提供请求体,因为这会导致 TypeError。
为什么这是不好的?
当请求方法为 GET 或 HEAD 且提供了请求体时,fetch() 函数会抛出 TypeError。这可能导致代码中出现意外的行为和错误。通过禁止此类无效选项,该规则确保请求配置正确,防止不必要的错误发生。
示例
以下为该规则的 错误 用法示例:
javascript
const response = await fetch("/", { method: "GET", body: "foo=bar" });
const request = new Request("/", { method: "GET", body: "foo=bar" });以下为该规则的 正确 用法示例:
javascript
const response = await fetch("/", { method: "POST", body: "foo=bar" });
const request = new Request("/", { method: "POST", body: "foo=bar" });如何使用
要通过配置文件或 CLI 启用 此规则,可以使用:
json
{
"rules": {
"unicorn/no-invalid-fetch-options": "error"
}
}bash
oxlint --deny unicorn/no-invalid-fetch-options