Skip to content

covenix / HttpResponse

Class: HttpResponse<T>

Defined in: src/http-response.ts:34

A JSON response that carries HTTP metadata — status, headers, cookies — alongside the body, so a handler can set them by returning a value instead of reaching for the raw Express res via @Res(). The body is still validated and serialized against the matched @Returns schema, exactly like a bare return; this only adds the surrounding metadata.

Returning one is opt-in: a handler may return T or HttpResponse<T>.

  • status selects which declared @Returns to send (must be one of them); defaults to the route's success status.
  • headers are set on the response; a header matching a declared @Returns(..., { headers }) schema is validated against it, and numbers are stringified. Undeclared headers are allowed.
  • cookies are emitted as Set-Cookie (Express does the formatting).

Example

ts
@Get('me')
@Returns(200, UserSchema, { headers: { 'X-RateLimit-Remaining': z.number().int() } })
me(@Principal() user: User): HttpResponse<User> {
  return new HttpResponse(user, {
    headers: { 'X-RateLimit-Remaining': 42 }, // → "42"
    cookies: [{ name: 'sid', value: token, options: { httpOnly: true, sameSite: 'lax' } }],
  });
}

Extends

Type Parameters

T

T = unknown

Constructors

Constructor

new HttpResponse<T>(body, options?): HttpResponse<T>

Defined in: src/http-response.ts:38

Parameters

body

T

options?

ResponseBaseOptions = {}

Returns

HttpResponse<T>

Overrides

ResponseBase.constructor

Properties

body

readonly body: T

Defined in: src/http-response.ts:36

The response body — validated/serialized against the matched @Returns schema.


cookies

readonly cookies: ResponseCookie[] | undefined

Defined in: src/response.ts:63

Cookies to set (each its own Set-Cookie), if any.

Inherited from

ResponseBase.cookies


headers

readonly headers: Record<string, HeaderValue> | undefined

Defined in: src/response.ts:61

Extra response headers, if set.

Inherited from

ResponseBase.headers


status

readonly status: number | undefined

Defined in: src/response.ts:59

Explicit HTTP status, if set.

Inherited from

ResponseBase.status