Skip to content

CorsOptions ​

CorsOptions configures CORS for every route in an HttpRouter. Its required fields are origin, methods, allowedHeaders, exposedHeaders, credentials, and maxAge in seconds.

methods accepts only the standard uppercase HTTP method literals: "CONNECT", "DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT", "QUERY", and "TRACE". allowedHeaders remains string[] so applications can authorize custom request headers such as "X-Request-Id" and "X-Api-Key".

When credentials is true, origin must list explicit origins. "*" is rejected by the compiler and runtime because browsers do not allow credentialed wildcard origins.

Call app.cors(options) before listen(...). CORS is browser policy, not authentication or authorization.

tyrus
import { CorsOptions, createRouter, HttpRouter } from "tyrus:http";

const options: CorsOptions = {
    origin: ["https://app.example.com"],
    methods: ["GET", "POST", "OPTIONS", "QUERY"],
    allowedHeaders: ["Content-Type", "Authorization"],
    exposedHeaders: ["x-request-id"],
    credentials: true,
    maxAge: 3600,
};

const app: HttpRouter = createRouter();
app.cors(options);