Skip to content

CloveResponse

The response object handed to route handlers and middlewares. Handlers usually just return a value and let the JSON middleware do the writing; this class is for the cases that need explicit control.

Every mutator returns this, so calls chain.

Properties

MemberTypeDescription
rawServerResponseThe untouched Node response
sentbooleanTrue once a body has been written — through this wrapper or the raw stream
statusCodenumberThe status currently set
contentTypestring | undefinedThe Content-Type currently set
typeIsExplicitbooleanWhether the handler chose the content type rather than inheriting it

sent is what lets the pipeline stand down when a handler wrote the response itself. typeIsExplicit is what lets the JSON middleware step aside.

Status and headers

ts
res.status(201)
res.header("x-request-id", id)
res.set("x-request-id", id)      // alias, for readers coming from Express

type(value)

Sets the Content-Type. Accepts a full MIME type or a shorthand:

ShorthandContent-Type
jsonapplication/json; charset=utf-8
htmltext/html; charset=utf-8
text / txttext/plain; charset=utf-8
xmlapplication/xml; charset=utf-8
csstext/css; charset=utf-8
jstext/javascript; charset=utf-8
csvtext/csv; charset=utf-8
bin / octetapplication/octet-stream

Setting a non-JSON type disables the built-in JSON middleware for that response.

Cookies

ts
cookie(name: string, value: string, opts?: CookieOptions): this
clearCookie(name: string, opts?: CookieOptions): this

Multiple cookie() calls accumulate into the Set-Cookie header list rather than overwriting each other. clearCookie() writes an empty value with an expired date.

CookieOptions

FieldType
domainstring
pathstring
expiresDate
maxAgenumber
httpOnlyboolean
secureboolean
sameSite"strict" | "lax" | "none"
partitionedboolean

Sending a body

send(body?)

Writes a body and ends the response, picking a sensible default content type when none was set:

ArgumentBehaviour
BufferWritten as-is; defaults to application/octet-stream
stringWritten as-is; defaults to text/html
object / arraySerialised as JSON
undefined / nullEnds with no body

json(body)

Always serialises as JSON, defaulting the content type to application/json if none was set.

redirect(location, status?)

Sets the status (default 302) and Location, then ends the response.

end()

Ends the response with no body.

Safe to call twice

All of these are no-ops once sent is true, so a middleware can call end() on a response a handler already finished without corrupting it.

Streaming

Drop to res.raw and opt the route out of JSON handling:

ts
export default get(async (req, res) => {
  res.raw.writeHead(200, { "content-type": "text/event-stream" })
  res.raw.write("data: hello\n\n")
}).meta({ json: false })

sent reports true once anything reaches the raw stream, so the pipeline will not try to write over you.

Released under the MIT License.