Skip to content

Commit

Permalink
Add Vapor 3 API shims (#2373)
Browse files Browse the repository at this point in the history
* add vapor 3 api shims

* fix trailing whitespace

* deprecate expectation alias

* deprecate expectation alias
  • Loading branch information
tanner0101 committed Jun 26, 2020
1 parent 5adcd2e commit dc2aa1e
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 2 deletions.
9 changes: 8 additions & 1 deletion Package.swift
Expand Up @@ -8,7 +8,8 @@ let package = Package(
],
products: [
.library(name: "Vapor", targets: ["Vapor"]),
.library(name: "XCTVapor", targets: ["XCTVapor"])
.library(name: "XCTVapor", targets: ["XCTVapor"]),
.library(name: "_Vapor3", targets: ["_Vapor3"]),
],
dependencies: [
// HTTP client library built on SwiftNIO
Expand Down Expand Up @@ -83,10 +84,16 @@ let package = Package(
.product(name: "RoutingKit", package: "routing-kit"),
.product(name: "WebSocketKit", package: "websocket-kit"),
]),
// Vapor 3 API shim
.target(name: "_Vapor3", dependencies: [
.target(name: "Vapor"),
.product(name: "_NIO1APIShims", package: "swift-nio")
]),

// Development
.target(name: "Development", dependencies: [
.target(name: "Vapor"),
.target(name: "_Vapor3"),
]),

// Testing
Expand Down
3 changes: 2 additions & 1 deletion Sources/Development/routes.swift
@@ -1,4 +1,5 @@
import Vapor
import _Vapor3

struct Creds: Content {
var email: String
Expand All @@ -12,7 +13,7 @@ public func routes(_ app: Application) throws {


// ( echo -e 'POST /slow-stream HTTP/1.1\r\nContent-Length: 1000000000\r\n\r\n'; dd if=/dev/zero; ) | nc localhost 8080
app.on(.POST, "slow-stream", body: .stream) { req -> EventLoopFuture<String> in
app.on(.POST, "slow-stream", body: .stream) { req -> Future<String> in
let done = req.eventLoop.makePromise(of: String.self)

var total = 0
Expand Down
2 changes: 2 additions & 0 deletions Sources/_Vapor3/Config.swift
@@ -0,0 +1,2 @@
@available(*, unavailable, message: "Use `Application` instead.")
public typealias Config = Void
1 change: 1 addition & 0 deletions Sources/_Vapor3/Exports.swift
@@ -0,0 +1 @@
@_exported import _NIO1APIShims
43 changes: 43 additions & 0 deletions Sources/_Vapor3/Future.swift
@@ -0,0 +1,43 @@
import Vapor

@available(*, deprecated, renamed: "EventLoopFuture")
public typealias Future = EventLoopFuture

extension EventLoopFuture {
@available(*, deprecated, renamed: "Value")
public typealias Expectation = Value

@available(*, deprecated, message: "The `to` parameter has been removed and this method can no longer throw.")
public func map<T>(to type: T.Type, _ callback: @escaping (Value) throws -> T) -> EventLoopFuture<T> {
return self.flatMapThrowing(callback)
}


@available(*, deprecated, message: "The `to` parameter has been removed and this method can no longer throw.")
public func flatMap<T>(to type: T.Type, _ callback: @escaping (Value) throws -> EventLoopFuture<T>) -> EventLoopFuture<T> {
return self.flatMap { input in
do {
return try callback(input)
} catch {
return self.eventLoop.makeFailedFuture(error)
}
}
}

@available(*, deprecated, renamed: "flatMapErrorThrowing")
public func catchMap(_ callback: @escaping (Error) throws -> (Value)) -> EventLoopFuture<Value> {
return self.flatMapErrorThrowing(callback)
}


@available(*, deprecated, message: "Use `flatMapError` with internal do / catch that returns a failed future.")
public func catchFlatMap(_ callback: @escaping (Error) throws -> (EventLoopFuture<Value>)) -> EventLoopFuture<Value> {
return self.flatMapError { inputError in
do {
return try callback(inputError)
} catch {
return self.eventLoop.makeFailedFuture(error)
}
}
}
}
2 changes: 2 additions & 0 deletions Sources/_Vapor3/NIOServerConfig.swift
@@ -0,0 +1,2 @@
@available(*, unavailable, message: "Use `app.http.server.configuration` instead.")
public typealias NIOServerConfig = Void
4 changes: 4 additions & 0 deletions Sources/_Vapor3/Promise.swift
@@ -0,0 +1,4 @@
import Vapor

@available(*, deprecated, renamed: "EventLoopPromise")
public typealias Promise = EventLoopPromise
2 changes: 2 additions & 0 deletions Sources/_Vapor3/Services.swift
@@ -0,0 +1,2 @@
@available(*, unavailable, message: "Use `Application` instead.")
public typealias Services = Void

0 comments on commit dc2aa1e

Please sign in to comment.