blob: 8e8b447c69e019dac808e97f9affaa54c6c58110 [file] [log] [blame]
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
library shelf_cors;
import 'package:shelf/shelf.dart';
/// Middleware which adds [CORS headers](
/// to shelf responses. Also handles preflight (OPTIONS) requests.
/// By default, allows access from everywhere.
Middleware createCorsHeadersMiddleware(
{Map<String, String> corsHeaders = const {
'Access-Control-Allow-Origin': '*'
}}) {
// Handle preflight (OPTIONS) requests by just adding headers and an empty
// response.
Response handleOptionsRequest(Request request) {
if (request.method == 'OPTIONS') {
return Response.ok(null, headers: corsHeaders);
} else {
return null;
Response addCorsHeaders(Response response) =>
response?.change(headers: corsHeaders);
return createMiddleware(
requestHandler: handleOptionsRequest, responseHandler: addCorsHeaders);