From 665331a47f4450283a1874d797b8a698e9cc3c7d Mon Sep 17 00:00:00 2001 From: Guillaume Nissou Kantorowicz Date: Tue, 3 Dec 2019 15:49:20 +0100 Subject: [PATCH 1/5] make middleware ServiceType compliant --- Sources/Submissions/SubmissionsMiddleware.swift | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Sources/Submissions/SubmissionsMiddleware.swift b/Sources/Submissions/SubmissionsMiddleware.swift index 70014c4..699cdc9 100644 --- a/Sources/Submissions/SubmissionsMiddleware.swift +++ b/Sources/Submissions/SubmissionsMiddleware.swift @@ -5,8 +5,13 @@ import Vapor /// Note: needs to come after `ErrorMiddleware` (if present) to avoid the /// `SubmissionValidationError`s from being transformed into Internal Server errors on the way back /// up the responder chain. -public final class SubmissionsMiddleware: Middleware { +public final class SubmissionsMiddleware: Middleware, ServiceType { + /// See `ServiceType`. + public static func makeService(for container: Container) throws -> SubmissionsMiddleware { + return SubmissionsMiddleware() + } + /// Create a new `SubmissionsMiddleware`. public init() {} From d3fefed68a6361133006054c3f10df1472e95311 Mon Sep 17 00:00:00 2001 From: Guillaume Nissou Kantorowicz Date: Tue, 3 Dec 2019 15:57:06 +0100 Subject: [PATCH 2/5] Improve documentation for middleware --- README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bb6a0e7..32c7ce4 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ First make sure that you've imported Submissions everywhere it's needed: import Submissions ``` -### Adding the Provider +### Adding the Provider and Middleware "Submissions" comes with a light-weight provider that we'll need to register in the `configure` function in our `configure.swift` file: @@ -56,8 +56,15 @@ import Submissions try services.register(SubmissionsProvider()) ``` +It also include a middleware you have to register in your `MiddlewareConfig` : + +```swift +config.use(SubmissionsMiddleware.self) +``` + This makes sure that fields and errors can be stored on the request using a `FieldCache` service. + ## Validating API requests _TODO_ From a5e050eadf0f71df2e3acaa669d9cf33e01180b2 Mon Sep 17 00:00:00 2001 From: Guillaume Nissou Kantorowicz Date: Wed, 4 Dec 2019 17:04:07 +0100 Subject: [PATCH 3/5] Register SubmissionsMiddleware in services Allow vapor to find it as a Middleware --- Sources/Submissions/SubmissionsProvider.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/Submissions/SubmissionsProvider.swift b/Sources/Submissions/SubmissionsProvider.swift index 5321b89..99a8175 100644 --- a/Sources/Submissions/SubmissionsProvider.swift +++ b/Sources/Submissions/SubmissionsProvider.swift @@ -8,6 +8,7 @@ public final class SubmissionsProvider: Provider { /// See `Provider` public func register(_ services: inout Services) throws { + services.register(SubmissionsMiddleware.self) services.register { _ in FieldCache() } } From 771811cc6e0be0fced214a38f8c96220cc2b87a6 Mon Sep 17 00:00:00 2001 From: Guillaume Nissou Kantorowicz Date: Wed, 4 Dec 2019 17:04:17 +0100 Subject: [PATCH 4/5] review correction --- Sources/Submissions/SubmissionsMiddleware.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Submissions/SubmissionsMiddleware.swift b/Sources/Submissions/SubmissionsMiddleware.swift index 699cdc9..ff51d32 100644 --- a/Sources/Submissions/SubmissionsMiddleware.swift +++ b/Sources/Submissions/SubmissionsMiddleware.swift @@ -7,7 +7,7 @@ import Vapor /// up the responder chain. public final class SubmissionsMiddleware: Middleware, ServiceType { - /// See `ServiceType`. + /// See `ServiceType`. public static func makeService(for container: Container) throws -> SubmissionsMiddleware { return SubmissionsMiddleware() } From 221bbb4b5c563df03116b33669d5fc6fbb06d641 Mon Sep 17 00:00:00 2001 From: Guillaume Nissou Kantorowicz Date: Wed, 4 Dec 2019 17:04:49 +0100 Subject: [PATCH 5/5] grammar + specification for middleware registering prerogatives --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 32c7ce4..b9057d9 100644 --- a/README.md +++ b/README.md @@ -56,12 +56,13 @@ import Submissions try services.register(SubmissionsProvider()) ``` -It also include a middleware you have to register in your `MiddlewareConfig` : +It also includes a middleware you have to register in your `MiddlewareConfig` (needs to come after `ErrorMiddleware` if present, to avoid the `SubmissionValidationError`s from being transformed into Internal Server errors on the way back up the responder chain) ```swift config.use(SubmissionsMiddleware.self) ``` + This makes sure that fields and errors can be stored on the request using a `FieldCache` service.