• Setup CORS for a giving Request and Response objects pair using the specified options.

    The default options are:

    • origin: true
    • methods: ["GET", "HEAD", "PUT", "PATCH", "POST", "DELETE"]
    • allowedHeaders: []
    • exposedHeaders: []
    • credentials: false
    • maxAge: 0

    Parameters

    • request: Request

      The Request object

    • response: Response

      The Response object

    • options: CORSOptions = DEFAULT_OPTIONS

      Optional configuration for CORS

    Returns Promise<Response>

    The same Response object mutated

    // Create a response, then setup CORS for it
    export async function loader({ request }: LoaderFunctionArgs) {
    let data = await getData(request);
    let response = json<LoaderData>(data);
    return await cors(request, response);
    }
    // Create response and setup CORS in a single line
    export async function loader({ request }: LoaderFunctionArgs) {
    let data = await getData(request);
    return await cors(request, json<LoaderData>(data));
    }
    // Setup for any data request
    export let handleDataRequest: HandleDataRequestFunction = async (
    response,
    { request }
    ) => {
    return await cors(request, response);
    };
    // Pass a configuration object to setup CORS
    export async function loader({ request }: LoaderFunctionArgs) {
    let data = await getData(request);
    return await cors(request, json<LoaderData>(data), {
    origin: "https://example.com"
    });
    }
    // Mutate response and then return it
    export async function loader({ request }: LoaderFunctionArgs) {
    let data = await getData(request);
    let response = json<LoaderData>(data);
    await cors(request, response); // this mutates the Response object
    return response;
    }