- Services has been refactored to be more type-safe. (#2098)
Services
, Container
, and Provider
have been replaced by a pattern built on Swift extensions. This is best explained with examples.
Telling Vapor to use Leaf:
import Leaf
import Vapor
// beta.1
s.register(ViewRenderer.self) { c in
return c.make(LeafRenderer.self)
}
// beta.2
app.views.use(.leaf)
Registering a factory service:
// beta.1
s.register(Foo.self) { c in
return Foo(...)
}
app.make(Foo.self).bar()
// beta.2
extension Application {
var foo: Foo {
return Foo(...)
}
}
app.foo.bar()
Registering a singleton service:
// beta.1
s.register(Foo.self) { c in
return Foo(...)
}
app.make(Foo.self).bar = 0
app.make(Foo.self).bar += 1
print(app.make(Foo.self).bar) // 1
// beta.2
extension Application {
var foo: Foo {
if let existing = self.storage[FooKey.self] as? Foo {
return existing
} else {
let new = Foo()
self.storage[FooKey.self] = new
return new
}
}
private struct FooKey: StorageKey {
typealias Value = Foo
}
}
app.foo.bar = 0
app.foo.bar += 1
print(app.foo.bar) // 1
This new pattern of extending Application
also works with Request
:
extension Application {
var foo: Foo { ... }
}
extension Request {
var bar: Bar {
return self.application.foo.bar(for: self)
}
}
-
Validations has been refactored to yield better and more type-safe errors (#2071)
-
Authentication methods are now grouped under a new
req.auth
helper (#2111) -
All authenticators now accept
Request
in their authenticate methods (#2111) -
Added new
ErrorSource
struct to theAbortError
protocol (#2093)
This new struct makes it easier to pass around information about where an error came from. It also makes it easier to indicate that a given error has no source information. This helps the logger avoid muddying logs with useless error source information.