diff --git a/README.md b/README.md index 63a3d51..df87d4c 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ The router has a POST route that decodes a `User` instance from the request body The router has a GET route that reads a user id from the query parameters and decodes the instance of `User` that is in the session for that id. -``` +```swift public struct User: Codable { let id: String let name: String diff --git a/docs/Classes.html b/docs/Classes.html index d70f0ae..60b9fcf 100644 --- a/docs/Classes.html +++ b/docs/Classes.html @@ -22,7 +22,7 @@ KituraSession Docs - (100% documented) + (97% documented)

@@ -182,7 +182,7 @@

Declaration

diff --git a/docs/Classes/Session.html b/docs/Classes/Session.html index a9a06d0..4192c2e 100644 --- a/docs/Classes/Session.html +++ b/docs/Classes/Session.html @@ -23,7 +23,7 @@ KituraSession Docs - (100% documented) + (97% documented)

@@ -272,7 +272,7 @@

Parameters

diff --git a/docs/Classes/SessionState.html b/docs/Classes/SessionState.html index d16775a..b1521b9 100644 --- a/docs/Classes/SessionState.html +++ b/docs/Classes/SessionState.html @@ -23,7 +23,7 @@ KituraSession Docs - (100% documented) + (97% documented)

@@ -345,7 +345,7 @@

Parameters

-

Retrieve an entry from the session data.

+

Retrieve or store an entry from the session data.

@@ -378,6 +378,33 @@

Parameters

+
  • +
    + + + + subscript(_:) + +
    +
    +
    +
    +
    +
    +

    Undocumented

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public subscript<T: Codable>(key: String) -> T?
    + +
    +
    +
    +
    +
  • @@ -386,7 +413,7 @@

    Parameters

    diff --git a/docs/Enums.html b/docs/Enums.html index 6852438..fee0e97 100644 --- a/docs/Enums.html +++ b/docs/Enums.html @@ -22,7 +22,7 @@ KituraSession Docs - (100% documented) + (97% documented)

    @@ -182,7 +182,7 @@

    Declaration

    diff --git a/docs/Enums/CookieParameter.html b/docs/Enums/CookieParameter.html index d15278e..a03cf4c 100644 --- a/docs/Enums/CookieParameter.html +++ b/docs/Enums/CookieParameter.html @@ -23,7 +23,7 @@ KituraSession Docs - (100% documented) + (97% documented)

    @@ -249,7 +249,7 @@

    Declaration

    diff --git a/docs/Enums/StoreError.html b/docs/Enums/StoreError.html index 509ffea..fef4af0 100644 --- a/docs/Enums/StoreError.html +++ b/docs/Enums/StoreError.html @@ -23,7 +23,7 @@ KituraSession Docs - (100% documented) + (97% documented)

    @@ -156,7 +156,7 @@

    Declaration

    diff --git a/docs/Extensions.html b/docs/Extensions.html index 5361ff1..77bbe0b 100644 --- a/docs/Extensions.html +++ b/docs/Extensions.html @@ -22,7 +22,7 @@ KituraSession Docs - (100% documented) + (97% documented)

    @@ -151,7 +151,7 @@

    Declaration

    diff --git a/docs/Extensions/RouterRequest.html b/docs/Extensions/RouterRequest.html index 659aa94..2089a00 100644 --- a/docs/Extensions/RouterRequest.html +++ b/docs/Extensions/RouterRequest.html @@ -23,7 +23,7 @@ KituraSession Docs - (100% documented) + (97% documented)

    @@ -157,7 +157,7 @@

    Declaration

    diff --git a/docs/Protocols.html b/docs/Protocols.html index b0ad870..9722477 100644 --- a/docs/Protocols.html +++ b/docs/Protocols.html @@ -22,7 +22,7 @@ KituraSession Docs - (100% documented) + (97% documented)

    @@ -204,7 +204,7 @@

    Declaration

    diff --git a/docs/Protocols/Store.html b/docs/Protocols/Store.html index 830cc11..c926eb9 100644 --- a/docs/Protocols/Store.html +++ b/docs/Protocols/Store.html @@ -23,7 +23,7 @@ KituraSession Docs - (100% documented) + (97% documented)

    @@ -373,7 +373,7 @@

    Parameters

    diff --git a/docs/Protocols/TypeSafeSession.html b/docs/Protocols/TypeSafeSession.html index 027205d..dd4fdda 100644 --- a/docs/Protocols/TypeSafeSession.html +++ b/docs/Protocols/TypeSafeSession.html @@ -23,7 +23,7 @@ KituraSession Docs - (100% documented) + (97% documented)

    @@ -569,7 +569,7 @@

    Parameters

    diff --git a/docs/Structs.html b/docs/Structs.html index 51baeda..832fbb9 100644 --- a/docs/Structs.html +++ b/docs/Structs.html @@ -22,7 +22,7 @@ KituraSession Docs - (100% documented) + (97% documented)

    @@ -155,7 +155,7 @@

    Declaration

    diff --git a/docs/Structs/SessionCookie.html b/docs/Structs/SessionCookie.html index 90c052e..589bd1a 100644 --- a/docs/Structs/SessionCookie.html +++ b/docs/Structs/SessionCookie.html @@ -23,7 +23,7 @@ KituraSession Docs - (100% documented) + (97% documented)

    @@ -275,7 +275,7 @@

    Parameters

    diff --git a/docs/badge.svg b/docs/badge.svg index a096fec..5f56a7e 100644 --- a/docs/badge.svg +++ b/docs/badge.svg @@ -1,15 +1,15 @@ - + - + - - + + @@ -18,11 +18,11 @@ documentation - - 100% + + 97% - - 100% + + 97% diff --git a/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Classes.html b/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Classes.html index d70f0ae..60b9fcf 100644 --- a/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Classes.html +++ b/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Classes.html @@ -22,7 +22,7 @@ KituraSession Docs - (100% documented) + (97% documented)

    @@ -182,7 +182,7 @@

    Declaration

    diff --git a/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Classes/Session.html b/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Classes/Session.html index a9a06d0..4192c2e 100644 --- a/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Classes/Session.html +++ b/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Classes/Session.html @@ -23,7 +23,7 @@ KituraSession Docs - (100% documented) + (97% documented)

    @@ -272,7 +272,7 @@

    Parameters

    diff --git a/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Classes/SessionState.html b/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Classes/SessionState.html index d16775a..b1521b9 100644 --- a/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Classes/SessionState.html +++ b/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Classes/SessionState.html @@ -23,7 +23,7 @@ KituraSession Docs - (100% documented) + (97% documented)

    @@ -345,7 +345,7 @@

    Parameters

    -

    Retrieve an entry from the session data.

    +

    Retrieve or store an entry from the session data.

    @@ -378,6 +378,33 @@

    Parameters

    +
  • +
    + + + + subscript(_:) + +
    +
    +
    +
    +
    +
    +

    Undocumented

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public subscript<T: Codable>(key: String) -> T?
    + +
    +
    +
    +
    +
  • @@ -386,7 +413,7 @@

    Parameters

    diff --git a/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Enums.html b/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Enums.html index 6852438..fee0e97 100644 --- a/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Enums.html +++ b/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Enums.html @@ -22,7 +22,7 @@ KituraSession Docs - (100% documented) + (97% documented)

    @@ -182,7 +182,7 @@

    Declaration

    diff --git a/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Enums/CookieParameter.html b/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Enums/CookieParameter.html index d15278e..a03cf4c 100644 --- a/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Enums/CookieParameter.html +++ b/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Enums/CookieParameter.html @@ -23,7 +23,7 @@ KituraSession Docs - (100% documented) + (97% documented)

    @@ -249,7 +249,7 @@

    Declaration

    diff --git a/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Enums/StoreError.html b/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Enums/StoreError.html index 509ffea..fef4af0 100644 --- a/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Enums/StoreError.html +++ b/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Enums/StoreError.html @@ -23,7 +23,7 @@ KituraSession Docs - (100% documented) + (97% documented)

    @@ -156,7 +156,7 @@

    Declaration

    diff --git a/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Extensions.html b/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Extensions.html index 5361ff1..77bbe0b 100644 --- a/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Extensions.html +++ b/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Extensions.html @@ -22,7 +22,7 @@ KituraSession Docs - (100% documented) + (97% documented)

    @@ -151,7 +151,7 @@

    Declaration

    diff --git a/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Extensions/RouterRequest.html b/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Extensions/RouterRequest.html index 659aa94..2089a00 100644 --- a/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Extensions/RouterRequest.html +++ b/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Extensions/RouterRequest.html @@ -23,7 +23,7 @@ KituraSession Docs - (100% documented) + (97% documented)

    @@ -157,7 +157,7 @@

    Declaration

    diff --git a/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Protocols.html b/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Protocols.html index b0ad870..9722477 100644 --- a/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Protocols.html +++ b/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Protocols.html @@ -22,7 +22,7 @@ KituraSession Docs - (100% documented) + (97% documented)

    @@ -204,7 +204,7 @@

    Declaration

    diff --git a/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Protocols/Store.html b/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Protocols/Store.html index 830cc11..c926eb9 100644 --- a/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Protocols/Store.html +++ b/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Protocols/Store.html @@ -23,7 +23,7 @@ KituraSession Docs - (100% documented) + (97% documented)

    @@ -373,7 +373,7 @@

    Parameters

    diff --git a/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Protocols/TypeSafeSession.html b/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Protocols/TypeSafeSession.html index 027205d..dd4fdda 100644 --- a/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Protocols/TypeSafeSession.html +++ b/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Protocols/TypeSafeSession.html @@ -23,7 +23,7 @@ KituraSession Docs - (100% documented) + (97% documented)

    @@ -569,7 +569,7 @@

    Parameters

    diff --git a/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Structs.html b/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Structs.html index 51baeda..832fbb9 100644 --- a/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Structs.html +++ b/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Structs.html @@ -22,7 +22,7 @@ KituraSession Docs - (100% documented) + (97% documented)

    @@ -155,7 +155,7 @@

    Declaration

    diff --git a/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Structs/SessionCookie.html b/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Structs/SessionCookie.html index 90c052e..589bd1a 100644 --- a/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Structs/SessionCookie.html +++ b/docs/docsets/KituraSession.docset/Contents/Resources/Documents/Structs/SessionCookie.html @@ -23,7 +23,7 @@ KituraSession Docs - (100% documented) + (97% documented)

    @@ -275,7 +275,7 @@

    Parameters

    diff --git a/docs/docsets/KituraSession.docset/Contents/Resources/Documents/index.html b/docs/docsets/KituraSession.docset/Contents/Resources/Documents/index.html index adf0219..00f4793 100644 --- a/docs/docsets/KituraSession.docset/Contents/Resources/Documents/index.html +++ b/docs/docsets/KituraSession.docset/Contents/Resources/Documents/index.html @@ -22,7 +22,7 @@ KituraSession Docs - (100% documented) + (97% documented)

    @@ -142,7 +142,7 @@

    Table of Contents

    Swift version

    -

    The latest version of Kitura-Session requires Swift 4.0. You can download this version of the Swift binaries by following this link. Compatibility with other Swift versions is not guaranteed.

    +

    The latest version of Kitura-Session requires Swift 4.0 or later. You can download this version of the Swift binaries by following this link. Compatibility with other Swift versions is not guaranteed.

    API

    In order to use the Session middleware, an instance of Session has to be created:

    @@ -174,6 +174,38 @@

    Example

    First an instance of RedisStore is created (see KituraSessionRedis for more information), then an instance of Session with the store as parameter is created, and finally it is connected to the desired path.

    +

    Codable Session Example

    + +

    The example below defines a User struct and a Router with the sessions middleware.
    +The router has a POST route that decodes a User instance from the request body + and stores it in the request session using the user’s id as the key.
    +The router has a GET route that reads a user id from the query parameters + and decodes the instance of User that is in the session for that id.

    +
    public struct User: Codable {
    +        let id: String
    +        let name: String
    +}
    +let router = Router()
    +router.all(middleware: Session(secret: "secret"))
    +router.post("/user") { request, response, next in
    +         let user = try request.read(as: User.self)
    +         request.session?[user.id] = user
    +         response.status(.created)
    +         response.send(user)
    +         next()
    +}
    +router.get("/user") { request, response, next in
    +         guard let userID = request.queryParameters["userid"] else {
    +            return try response.status(.notFound).end()
    +         }
    +         guard let user: User = request.session?[userID] else {
    +            return try response.status(.internalServerError).end()
    +         }
    +         response.status(.OK)
    +         response.send(user)
    +         next()
    +}
    +

    Plugins

    Swift version

    -

    The latest version of Kitura-Session requires Swift 4.0. You can download this version of the Swift binaries by following this link. Compatibility with other Swift versions is not guaranteed.

    +

    The latest version of Kitura-Session requires Swift 4.0 or later. You can download this version of the Swift binaries by following this link. Compatibility with other Swift versions is not guaranteed.

    API

    In order to use the Session middleware, an instance of Session has to be created:

    @@ -174,6 +174,38 @@

    Example

    First an instance of RedisStore is created (see KituraSessionRedis for more information), then an instance of Session with the store as parameter is created, and finally it is connected to the desired path.

    +

    Codable Session Example

    + +

    The example below defines a User struct and a Router with the sessions middleware.
    +The router has a POST route that decodes a User instance from the request body + and stores it in the request session using the user’s id as the key.
    +The router has a GET route that reads a user id from the query parameters + and decodes the instance of User that is in the session for that id.

    +
    public struct User: Codable {
    +        let id: String
    +        let name: String
    +}
    +let router = Router()
    +router.all(middleware: Session(secret: "secret"))
    +router.post("/user") { request, response, next in
    +         let user = try request.read(as: User.self)
    +         request.session?[user.id] = user
    +         response.status(.created)
    +         response.send(user)
    +         next()
    +}
    +router.get("/user") { request, response, next in
    +         guard let userID = request.queryParameters["userid"] else {
    +            return try response.status(.notFound).end()
    +         }
    +         guard let user: User = request.session?[userID] else {
    +            return try response.status(.internalServerError).end()
    +         }
    +         response.status(.OK)
    +         response.send(user)
    +         next()
    +}
    +

    Plugins