Skip to content

useStaticResponseMethods

¥Summary

¥How to configure

biome.json
{
"linter": {
"rules": {
"suspicious": {
"useStaticResponseMethods": "error"
}
}
}
}

¥Description

尽可能使用静态 Response 方法而不是 new Response() 构造函数。

¥Use static Response methods instead of new Response() constructor when possible.

new Response(JSON.stringify({ value: 1 })) 可以简化为 Response.json()new Response(null, { status: 301, headers: { location: 'https://example.com' } }) 可以简化为 Response.redirect()

¥new Response(JSON.stringify({ value: 1 })) can be simplified to Response.json(). new Response(null, { status: 301, headers: { location: 'https://example.com' } }) can be simplified to Response.redirect().

这些方法更简洁,更能突出代码意图,但当需要添加额外选项(例如额外的头文件)时,它们不能直接替代。

¥These methods are more concise and emphasize the intent of the code better, however they are not a direct replacement when additional options such as extra headers are needed.

对于 Response.redirect()location 标头也必须是完整的 URL,因为服务器运行时(Node、Deno 等)会对相对 URL 报错。

¥In case of Response.redirect(), the location header must also be a full URL, because server runtimes (Node, Deno, etc.) will throw an error for relative URLs.

¥Examples

¥Invalid

new Response(JSON.stringify({ value: 1 }));
code-block.js:1:1 lint/suspicious/useStaticResponseMethods  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Use Response.json() instead of new Response(JSON.stringify()).

> 1 │ new Response(JSON.stringify({ value: 1 }));
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

Response.json() is more concise and emphasizes the intent of the code better.

Unsafe fix: Replace with Response.json().

1 - new·Response(JSON.stringify({·value:·1·}));
1+ Response.json({·value:·1·});
2 2

new Response(JSON.stringify({ value: 0 }), {
headers: {
'Content-Type': 'application/json',
}
})
code-block.js:1:1 lint/suspicious/useStaticResponseMethods  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Use Response.json() instead of new Response(JSON.stringify()).

> 1 │ new Response(JSON.stringify({ value: 0 }), {
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 2 │ headers: {
> 3 │ ‘Content-Type’: ‘application/json’,
> 4 │ }
> 5 │ })
^^
6 │

Response.json() is more concise and emphasizes the intent of the code better.

Unsafe fix: Replace with Response.json().

1 - new·Response(JSON.stringify({·value:·0·}),·{
2 - ····headers:·{
3 - ········Content-Type:·application/json,
4 - ····}
5 - })
1+ Response.json({·value:·0·})
6 2

new Response(null, {
headers: {
location: 'https://example.com',
},
status: 302,
})
code-block.js:1:1 lint/suspicious/useStaticResponseMethods  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Use Response.redirect() instead of new Response().

> 1 │ new Response(null, {
^^^^^^^^^^^^^^^^^^^^
> 2 │ headers: {
> 3 │ location: ‘https://example.com’,
> 4 │ },
> 5 │ status: 302,
> 6 │ })
^^
7 │

Response.redirect() is more concise and emphasizes the intent of the code better.

Unsafe fix: Replace with Response.redirect().

1 - new·Response(null,·{
2 - ···headers:·{
3 - ·······location:·https://example.com,
4 - ···},
5 - ···status:·302,
6 - })
1+ Response.json(https://example.com,·302)
7 2

¥Valid

// JSON.stringify() with a replacer function
new Response(JSON.stringify({ value: 0 }, () => {}))
new Response(null, {
headers: {
location: 'https://example.com',
'x-foo': 'extra-header',
},
status: 302,
})
new Response(null, {
headers: {
location: '/relative-url',
},
status: 302,
})

¥Related links