Skip to content
← Back to rules

unicorn/no-invalid-fetch-options 正确性

This rule is turned on by default.

它的作用

禁止在 fetch()new Request() 中使用无效选项。具体来说,此规则确保当方法为 GETHEAD 时不会提供 body,因为这会导致 TypeError

为什么这不好?

当方法为 GETHEAD 且提供了 body 时,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" });

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "unicorn/no-invalid-fetch-options": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/no-invalid-fetch-options": "error",
  },
});
bash
oxlint --deny unicorn/no-invalid-fetch-options

版本

此规则添加于 v0.15.12。

参考资料