cors function
Middleware
cors({})
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});
};
};
}