diff --git a/README.md b/README.md index 9fb5fec..5770b73 100644 --- a/README.md +++ b/README.md @@ -10,10 +10,26 @@ ## 📦 Installation -Update your `Package.swift` file. +Add `Sugar` to the package dependencies (in your `Package.swift` file): ```swift -.package(url: "https://github.com/nodes-vapor/sugar.git", from: "4.0.0") +dependencies: [ + ..., + .package(url: "https://github.com/nodes-vapor/sugar.git", from: "4.0.0-beta") +] +``` + +as well as to your target (e.g. "App"): + +```swift +targets: [ + ... + .target( + name: "App", + dependencies: [... "Sugar" ...] + ), + ... +] ``` ## Getting started 🚀 diff --git a/Sources/Sugar/Authentication/JWTAuthenticationMiddleware.swift b/Sources/Sugar/Authentication/JWTAuthenticationMiddleware.swift index 72e58a8..db88564 100644 --- a/Sources/Sugar/Authentication/JWTAuthenticationMiddleware.swift +++ b/Sources/Sugar/Authentication/JWTAuthenticationMiddleware.swift @@ -41,9 +41,7 @@ public final class JWTAuthenticationMiddleware: Middlewar do { jwt = try JWT(from: bearer.token, verifiedUsing: signer) } catch let error as JWTError where error.identifier == "exp" { - return try Future - .transform(to: HTTPResponse(status: .unauthorized), on: req) - .encode(for: req) + return try req.future(HTTPResponse(status: .unauthorized)).encode(for: req) } guard shouldAuthenticate else { diff --git a/Sources/Sugar/Helpers/Future.swift b/Sources/Sugar/Helpers/Future.swift index bcb5a5e..6c63476 100644 --- a/Sources/Sugar/Helpers/Future.swift +++ b/Sources/Sugar/Helpers/Future.swift @@ -19,10 +19,6 @@ public extension Future { try callback(expectation).map { _ in expectation } } } - - static func transform(to value: T, on worker: Worker) -> Future { - return Future.map(on: worker) { value } - } } public extension Future where Expectation: OptionalType { diff --git a/Sources/Sugar/Helpers/TagContext.swift b/Sources/Sugar/Helpers/TagContext.swift new file mode 100644 index 0000000..e028c09 --- /dev/null +++ b/Sources/Sugar/Helpers/TagContext.swift @@ -0,0 +1,10 @@ +import TemplateKit + +extension TagContext { + /// Throws an error if the parameter count is larger than the supplied number `n`. + public func requireParameterCount(upTo n: Int) throws { + guard parameters.count <= n else { + throw error(reason: "Invalid parameter count: \(parameters.count)/\(n)") + } + } +}