Skip to main content

Simple functions for creating and manipulating HTTP Responses.

Works with
This package works with Bun
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Browsers
JSR Score
94%
Published
a week ago (0.12.0)

Response functions

A bunch of functions to aid creation and manipulation of the standard Response.

Response creation shortcuts

These functions are simple common shortcuts for creating Response objects.

Error responses return plain text messages, if you want an alternative content-type, use a new Response instead, or use some middleware or an interceptor to modify the response, wrapping the message in a richer format.

If a particular status hasn't been covered it may be because:

  • possible options are too complex to cover in a simple function, best to just use new Response in that case
  • it's been deprecated, or superseded by a preferred alternative (eg. 301 => 308, 302 => 307)
  • it's not yet been required in a real situation

Please raise an issue to request additional functions if you have a genuine need for it, and can suggest a simple and generally reusable signature.

Status Function
200 OK ok()
202 Accepted accepted()
204 No Content noContent(), or ok with a null body
303 See Other seeOther()
304 Not Modified notModified()
307 Temporary Redirect temporaryRedirect()
308 Permanent Redirect permanentRedirect()
400 Bad Request badRequest()
401 Unauthorized unauthorized()
403 Forbidden forbidden()
404 Not Found notFound()
405 Method Not Allowed methodNotAllowed()
406 Not Acceptable notAcceptable()
502 Bad Gateway badGateway()

Common Content-Type Responses

  • html() creates a 200 OK response with a text/html content-type.
  • json() serializes the given value to JSON in a 200 OK response with an application/json content-type.

Header manipulation

The functions appendHeaders() and setHeaders() allow you to alter the headers of a Response, including copying the Response if it's immutable.

These can be useful within middleware/interceptors to add additional headers to the Response from a handler.

Body manipulation

  • replaceBody() allows you to duplicate a Response but with an alternative body.
  • prependDocType() allows you to prepend <!DOCTYPE html> to a html body whilst preserving its streaming capabilities, mostly useful in conjunction with JSX generated content.

Conditional Responses

  • conditional() will inspect a given Request and Response, and determine whether you need to actually return the Response or a 304 Not Modified instead.
Built and signed on
GitHub Actions
View transparency log