From ba372f2180894c1872c5afb51850f851e48078a9 Mon Sep 17 00:00:00 2001 From: Valentin Corral Date: Wed, 26 Apr 2017 09:43:23 +0200 Subject: [PATCH] Updated to Vapor 2 --- Package.swift | 3 ++- Sources/FlashMiddleware.swift | 10 +++++----- Sources/Helper.swift | 8 ++++---- Tests/FlashTests/FlashTests.swift | 8 ++++---- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/Package.swift b/Package.swift index d2f5b2a..42b9a39 100644 --- a/Package.swift +++ b/Package.swift @@ -3,6 +3,7 @@ import PackageDescription let package = Package( name: "Flash", dependencies: [ - .Package(url: "https://github.com/vapor/vapor.git", majorVersion: 1) + .Package(url: "https://github.com/vapor/vapor.git", Version(2,0,0, prereleaseIdentifiers: ["beta"])), + .Package(url: "https://github.com/vapor/auth-provider.git", Version(1,0,0, prereleaseIdentifiers: ["beta"])) ] ) diff --git a/Sources/FlashMiddleware.swift b/Sources/FlashMiddleware.swift index 2130eb9..8e865c5 100644 --- a/Sources/FlashMiddleware.swift +++ b/Sources/FlashMiddleware.swift @@ -1,6 +1,6 @@ import Vapor import HTTP -import Auth +import AuthProvider public class FlashMiddleware: Middleware { public init() {} @@ -30,16 +30,16 @@ public class FlashMiddleware: Middleware { /// - Throws: Error public static func handleRequest(_ request: Request) throws { // Init flash node - let requestFlash = try request.session().data[Helper.flashKey, Helper.State.new.rawValue] ?? Node([]) - + let requestFlash = try request.assertSession().data[Helper.flashKey, Helper.State.new.rawValue] ?? Node([]) + // Copy new node to old node - try request.session().data[Helper.flashKey, Helper.State.old.rawValue] = requestFlash + try request.assertSession().data[Helper.flashKey, Helper.State.old.rawValue] = requestFlash // Apply new node to request storage request.storage[Helper.flashKey] = requestFlash // Clear new node - try request.session().data[Helper.flashKey, Helper.State.new.rawValue] = nil + try request.assertSession().data[Helper.flashKey, Helper.State.new.rawValue] = nil } /// Retrieve flash storage from response and add it to session diff --git a/Sources/Helper.swift b/Sources/Helper.swift index b163c7f..ab79224 100644 --- a/Sources/Helper.swift +++ b/Sources/Helper.swift @@ -28,7 +28,7 @@ public final class Helper { /// - message: String message /// - Throws: Error public func add(_ type: FlashType, _ message: String) throws { - try request.session().data[Helper.flashKey, State.new.rawValue, type.rawValue] = Node(message) + try request.assertSession().data[Helper.flashKey, State.new.rawValue, type.rawValue] = Node(message) } /// Add a message to a custom key @@ -38,7 +38,7 @@ public final class Helper { /// - message: String message /// - Throws: Error public func add(_ custom: String, _ message: String) throws { - try request.session().data[Helper.flashKey, State.new.rawValue, custom] = Node(message) + try request.assertSession().data[Helper.flashKey, State.new.rawValue, custom] = Node(message) } /// Refresh session, move current flash messages to "new" again, @@ -47,14 +47,14 @@ public final class Helper { /// - Throws: Error public func refresh() throws { // Copy old node to new node - try request.session().data[Helper.flashKey, State.new.rawValue] = try request.session().data[Helper.flashKey, State.old.rawValue] ?? Node([]) + try request.assertSession().data[Helper.flashKey, State.new.rawValue] = try request.assertSession().data[Helper.flashKey, State.old.rawValue] ?? Node([]) } /// Clear the session /// /// - Throws: Error public func clear() throws { - try request.session().data[Helper.flashKey] = nil + try request.assertSession().data[Helper.flashKey] = nil request.storage[Helper.flashKey] = nil } } diff --git a/Tests/FlashTests/FlashTests.swift b/Tests/FlashTests/FlashTests.swift index b021878..55ef3a9 100644 --- a/Tests/FlashTests/FlashTests.swift +++ b/Tests/FlashTests/FlashTests.swift @@ -4,10 +4,10 @@ import HTTP class FlashTests: XCTestCase { func testMiddleware() throws { - let flashMiddleware = FlashMiddleware() + let _ = FlashMiddleware() - let request = try Request(method: .get, uri: "uri") - let responder = TestResponse() + let _ = try Request(method: .get, uri: "uri") + let _ = TestResponse() // Need a session middleware //let _ = try flashMiddleware.respond(to: request, chainingTo: responder) @@ -18,7 +18,7 @@ class FlashTests: XCTestCase { func testHelper() throws { do { let request = try Request(method: .get, uri: "uri") - let helper = Helper(request: request) + let _ = Helper(request: request) // Need a session middleware //try helper.add("custom", "message")