corsMiddleware function

Handler corsMiddleware(
  1. Handler handler
)

CORS middleware

Implementation

Handler corsMiddleware(Handler handler) => (Request request) async {
      final corsHeaders = {
        'Access-Control-Allow-Origin': [request.headers.last('origin') ?? '*'],
        'Access-Control-Expose-Headers': ['Location'],
      };

      if (request.method == 'options') {
        const methods = ['POST', 'GET', 'DELETE', 'PATCH', 'OPTIONS'];
        return Response(
            204,
            Body(),
            Headers.from({
              ...corsHeaders,
              'Access-Control-Allow-Methods':
                  request.headers['Access-Control-Request-Method'] ?? methods,
              'Access-Control-Allow-Headers':
                  request.headers['Access-Control-Request-Headers'] ?? ['*'],
            }));
      }
      final response = await handler(request);
      response.headers.addAll(corsHeaders);
      return response;
    };