cors function

Middleware cors({
  1. String accessControlAllowOrigin = '*',
  2. List<String> accessControlAllowMethod = const ['*'],
  3. List<String> accessControlAllowHeaders = const ['*'],
  4. int accessControlMaxAge = 86400,
})

A Middleware to add CORS

Implementation

Middleware cors({
  String accessControlAllowOrigin = '*',
  List<String> accessControlAllowMethod = const ['*'],
  List<String> accessControlAllowHeaders = const ['*'],
  int accessControlMaxAge = 86400,
}) {
  return (handler) {
    return (request) async {
      final headers = {
        HttpHeaders.accessControlAllowOriginHeader: accessControlAllowOrigin,
        HttpHeaders.accessControlAllowMethodsHeader:
            accessControlAllowMethod.join(','),
        HttpHeaders.accessControlAllowHeadersHeader:
            accessControlAllowHeaders.join(','),
        HttpHeaders.accessControlMaxAgeHeader: '$accessControlMaxAge',
      };
      if (request.method == HttpMethod.options.name.toUpperCase()) {
        return Response(
          HttpStatus.noContent,
          headers: headers,
        );
      }
      final response = await handler(request);
      return response.change(headers: {...response.headers, ...headers});
    };
  };
}