Skip to content

Raw HTTP Types ​

HttpRequest, HttpResponse, CorsOptions, and CookieOptions are explicit interfaces exported by tyrus:http. Import them only when working at the raw HTTP boundary. Controllers should normally use the request inferred from a route schema and send through HttpReply.

HttpRequest exposes an immutable id, the request method, path, and raw string channels body, params, query, headers, and cookies. The runtime assigns id once per request and sends the compact unpadded Base64URL value in the x-request-id response header. { logger: true } writes a structured access event to stderr with the ID, method, path, status, and duration; it is disabled by default. Query text, headers, cookies, credentials, and bodies are never included in that event. Use HttpAccessLog with app.onResponse when application code needs that metadata. See HttpResponse, CorsOptions, and CookieOptions for their dedicated contracts.

tyrus
import { CorsOptions, createRouter, HttpError, HttpReply, HttpRequest, HttpResponse, HttpRouter, HttpStatus } from "tyrus:http";

interface HealthResponse {
    ok: boolean;
}

function requireAuthorization(request: HttpRequest): HttpResponse | true {
    if (request.headers.includes("authorization")) {
        return true;
    }
    return { statusCode: HttpStatus.Unauthorized, body: "Authorization required", cookies: "" };
}

const cors: CorsOptions = {
    origin: ["https://example.com"],
    methods: ["GET"],
    allowedHeaders: ["Authorization"],
    exposedHeaders: [],
    credentials: false,
    maxAge: 3600,
};

const app: HttpRouter = createRouter();
app.cors(cors);
app.get("/health", {
    response: { 200: HealthResponse },
    preHandler: [requireAuthorization],
    handler: (request, reply: HttpReply<HealthResponse>) => {
        reply.status(HttpStatus.Ok).send({ ok: true });
    },
});

try {
    app.listen(3333);
} catch (error: HttpError) {
    console.error(`${error.code}: ${error.message}`);
}