Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CORSMiddleware configuration initializer parameter #2314

Merged
merged 5 commits into from Apr 17, 2020
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions Sources/Vapor/Middleware/CORSMiddleware.swift
Expand Up @@ -91,6 +91,7 @@ public final class CORSMiddleware: Middleware {
/// - allowCredentials: If cookies and other credentials will be sent in the response.
/// - cacheExpiration: Optionally sets expiration of the cached pre-flight request in seconds.
/// - exposedHeaders: Headers exposed in the response of pre-flight request.
@available(swift, obsoleted: 4.3, message: "Please use the initializer where exposedHeaders uses HTTPHeaders.Name")
grosch marked this conversation as resolved.
Show resolved Hide resolved
public init(
allowedOrigin: AllowOriginSetting,
allowedMethods: [HTTPMethod],
Expand All @@ -106,6 +107,32 @@ public final class CORSMiddleware: Middleware {
self.cacheExpiration = cacheExpiration
self.exposedHeaders = exposedHeaders?.joined(separator: ", ")
}

/// Instantiate a CORSConfiguration struct that can be used to create a `CORSConfiguration`
grosch marked this conversation as resolved.
Show resolved Hide resolved
/// middleware for adding support for CORS in your responses.
///
/// - parameters:
/// - allowedOrigin: Setting that controls which origin values are allowed.
/// - allowedMethods: Methods that are allowed for a CORS request response.
/// - allowedHeaders: Headers that are allowed in a response for CORS request.
/// - allowCredentials: If cookies and other credentials will be sent in the response.
/// - cacheExpiration: Optionally sets expiration of the cached pre-flight request in seconds.
/// - exposedHeaders: Headers exposed in the response of pre-flight request.
public init(
allowedOrigin: AllowOriginSetting,
allowedMethods: [HTTPMethod],
allowedHeaders: [HTTPHeaders.Name],
allowCredentials: Bool = false,
cacheExpiration: Int? = 600,
exposedHeaders: [HTTPHeaders.Name]? = nil
) {
self.allowedOrigin = allowedOrigin
self.allowedMethods = allowedMethods.map({ "\($0)" }).joined(separator: ", ")
self.allowedHeaders = allowedHeaders.map({ String(describing: $0) }).joined(separator: ", ")
self.allowCredentials = allowCredentials
self.cacheExpiration = cacheExpiration
self.exposedHeaders = exposedHeaders?.map({ String(describing: $0) }).joined(separator: ", ")
}
}

/// Configuration used for populating headers in response for CORS requests.
Expand Down