KituraContracts is a library containing type definitions shared by client (e.g. KituraKit) and server (e.g. Kitura) code. These shared type definitions include Codable Closure Aliases, RequestError, QueryEncoder, QueryDecoder, Coder, Identifier Protocol and Extensions to String and Int, which add conformity to the Identifier protocol.
KituraContracts represents the types and protocols that are common to both the Kitura server and KituraKit client side library. If you are using Kitura or KituraKit, your project does not need to depend on KituraContracts explicitly.
Add the KituraContracts
package to the dependencies within your application’s Package.swift
file. Substitute "x.x.x"
with the latest KituraContracts
release.
.package(url: "https://github.com/IBM-Swift/KituraContracts.git", from: "x.x.x")
Add KituraContracts
to your target's dependencies:
.target(name: "example", dependencies: ["KituraContracts"]),
import KituraContracts
This example, shows how to use a shared type definition for RequestError
within a router POST method on users
. If no errors occurred and you have a User
you can respond with the user and pass nil as the RequestError?
value. If there has been an error you can respond with an appropriate error and pass nil for the User?
.
public struct User: Codable {
...
}
router.post("/users") { (user: User, respondWith: (User?, RequestError?) -> Void) in
if databaseConnectionIsOk {
...
respondWith(user, nil)
} else {
...
respondWith(nil, .internalServerError)
}
}
The 1.x.x releases were tested on macOS and Linux using the Swift 4.1 binaries. Please note that this is the default version of Swift that is included in Xcode 9.3.
For more information visit our API reference.
We love to talk server-side Swift and Kitura. Join our Slack to meet the team!
This library is licensed under Apache 2.0. Full license text is available in LICENSE.