Skip to content
This repository has been archived by the owner on Apr 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #4 from valen90/feature/vapor-two
Browse files Browse the repository at this point in the history
Updated to Vapor 2
  • Loading branch information
steffendsommer authored Apr 27, 2017
2 parents 46e1af1 + ba372f2 commit 022529c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"]))
]
)
10 changes: 5 additions & 5 deletions Sources/FlashMiddleware.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Vapor
import HTTP
import Auth
import AuthProvider

public class FlashMiddleware: Middleware {
public init() {}
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions Sources/Helper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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
}
}
8 changes: 4 additions & 4 deletions Tests/FlashTests/FlashTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand Down

0 comments on commit 022529c

Please sign in to comment.