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
Show file tree
Hide file tree
Changes from all 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
@@ -0,0 +1,27 @@
extension CORSMiddleware.Configuration {
/// Instantiate a CORSConfiguration struct that can be used to create a `CORSConfiguration`
/// 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.
@available(*, deprecated, message: "exposedHeaders parameter now accepts [HTTPHeaders.Name]")
public init(
allowedOrigin: CORSMiddleware.AllowOriginSetting,
allowedMethods: [HTTPMethod],
allowedHeaders: [HTTPHeaders.Name],
allowCredentials: Bool = false,
cacheExpiration: Int? = 600,
exposedHeaders: [String]
) {
let exposed = exposedHeaders.map { HTTPHeaders.Name($0) }
self.init(
allowedOrigin: allowedOrigin, allowedMethods: allowedMethods, allowedHeaders: allowedHeaders,
allowCredentials: allowCredentials, cacheExpiration: cacheExpiration, exposedHeaders: exposed
)
}
}
6 changes: 3 additions & 3 deletions Sources/Vapor/Middleware/CORSMiddleware.swift
Expand Up @@ -97,14 +97,14 @@ public final class CORSMiddleware: Middleware {
allowedHeaders: [HTTPHeaders.Name],
allowCredentials: Bool = false,
cacheExpiration: Int? = 600,
exposedHeaders: [String]? = nil
exposedHeaders: [HTTPHeaders.Name]? = nil
) {
self.allowedOrigin = allowedOrigin
self.allowedMethods = allowedMethods.map({ "\($0)" }).joined(separator: ", ")
self.allowedHeaders = allowedHeaders.map({ $0.description }).joined(separator: ", ")
self.allowedHeaders = allowedHeaders.map({ String(describing: $0) }).joined(separator: ", ")
self.allowCredentials = allowCredentials
self.cacheExpiration = cacheExpiration
self.exposedHeaders = exposedHeaders?.joined(separator: ", ")
self.exposedHeaders = exposedHeaders?.map({ String(describing: $0) }).joined(separator: ", ")
}
}

Expand Down