Skip to content

HttpResponse

Factory class passed into route handlers as the response argument. It creates the package's concrete response classes and supplies defaults for common cases, such as 200 OK for body responses and 204 No Content for handlers that intentionally return no body.

Use this class indirectly from route handlers in most applications. Instantiate it directly only when you need the same response helpers outside the route execution path.

Importing

ts
import { HttpResponse } from '@almighty-shogun/bun-server';

Usage

ts
import { HttpResponse, HttpStatus } from '@almighty-shogun/bun-server';

const response = new HttpResponse();

const json = response.json({ ok: true });
const missing = response.notFound({ 'X-Reason': 'missing' });
const invalidMethod = response.notAllowed({ Allow: 'GET, HEAD' });
const redirect = response.redirect('/login');
const html = response.html('<h1>Created</h1>', HttpStatus.Created);

Returns

json<T>(data: T, status: HttpStatus, headers?: HeadersInit): JsonHttpResponse<T>
Creates a JSON response with application/json; charset=utf-8.

html(html: string, status: HttpStatus, headers?: HeadersInit): HtmlHttpResponse
Creates an HTML response with text/html; charset=utf-8.

text(text: string, status: HttpStatus, headers?: HeadersInit): TextHttpResponse
Creates a plain-text response with text/plain; charset=utf-8.

file(source: string | Blob, status: HttpStatus, headers?: HeadersInit, contentType?: string): FileHttpResponse
Creates a file or blob response.

image(source: string | Blob, status: HttpStatus, headers?: HeadersInit, contentType?: ImageContentType): FileHttpResponse
Creates an image response with a typed image content type.

forbidden(headers?: HeadersInit): ForbiddenHttpResponse
Creates a 403 Forbidden response.

notFound(headers?: HeadersInit): NotFoundHttpResponse
Creates a 404 Not Found response.

notAllowed(headers?: HeadersInit): MethodNotAllowedHttpResponse
Creates a 405 Method Not Allowed response.

created(headers?: HeadersInit): CreatedHttpResponse
Creates a 201 Created response.

noContent(headers?: HeadersInit): NoContentHttpResponse
Creates a 204 No Content response.

redirect(location: string | URL, status?: RedirectHttpStatus, headers?: HeadersInit): RedirectHttpResponse
Creates a redirect response with a Location header.

Type signature

ts
declare class HttpResponse {
    json<T = unknown>(data: T, status?: HttpStatus, headers?: HeadersInit): JsonHttpResponse<T>;
    html(html: string, status?: HttpStatus, headers?: HeadersInit): HtmlHttpResponse;
    text(text: string, status?: HttpStatus, headers?: HeadersInit): TextHttpResponse;
    file(source: string | Blob, status?: HttpStatus, headers?: HeadersInit, contentType?: string): FileHttpResponse;
    image(source: string | Blob, status?: HttpStatus, headers?: HeadersInit, contentType?: ImageContentType): FileHttpResponse;
    forbidden(headers?: HeadersInit): ForbiddenHttpResponse;
    notFound(headers?: HeadersInit): NotFoundHttpResponse;
    notAllowed(headers?: HeadersInit): MethodNotAllowedHttpResponse;
    created(headers?: HeadersInit): CreatedHttpResponse;
    noContent(headers?: HeadersInit): NoContentHttpResponse;
    redirect(location: string | URL, status?: RedirectHttpStatus, headers?: HeadersInit): RedirectHttpResponse;
}

All packages are released under the MIT License.