Skip to content

useConsistentResponse

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

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.

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.

new Response(JSON.stringify({ value: 1 }));
code-block.js:1:1 lint/nursery/useConsistentResponse  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/nursery/useConsistentResponse  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/nursery/useConsistentResponse  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

// 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,
})
biome.json
{
"linter": {
"rules": {
"nursery": {
"useConsistentResponse": "error"
}
}
}
}