Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to HB2 beta1 #1

Merged
merged 1 commit into from Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions Starter/Package.resolved
Expand Up @@ -14,8 +14,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/hummingbird-project/hummingbird.git",
"state" : {
"revision" : "8cb3e42b6bffa4c78cf424bde0490c911a56f073",
"version" : "2.0.0-alpha.3"
"revision" : "dd3663447be43bb3c813579960715c110c66bb18",
"version" : "2.0.0-beta.1"
}
},
{
Expand Down Expand Up @@ -104,17 +104,17 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio.git",
"state" : {
"revision" : "635b2589494c97e48c62514bc8b37ced762e0a62",
"version" : "2.63.0"
"revision" : "fc63f0cf4e55a4597407a9fc95b16a2bc44b4982",
"version" : "2.64.0"
}
},
{
"identity" : "swift-nio-extras",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-extras.git",
"state" : {
"revision" : "363da63c1966405764f380c627409b2f9d9e710b",
"version" : "1.21.0"
"revision" : "a3b640d7dc567225db7c94386a6e71aded1bfa63",
"version" : "1.22.0"
}
},
{
Expand Down Expand Up @@ -167,8 +167,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/swift-server/swift-service-lifecycle.git",
"state" : {
"revision" : "55f45e39dd23c6cad82d4d529e22961cb5e493aa",
"version" : "2.4.0"
"revision" : "d7fe0e731499a8dcce53bf4cbbc812c8e565d3a7",
"version" : "2.4.1"
}
},
{
Expand Down
4 changes: 2 additions & 2 deletions Starter/Package.swift
Expand Up @@ -7,7 +7,7 @@ let package = Package(
.macOS(.v14),
],
dependencies: [
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-alpha.3"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-beta.1"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.0.0"),
],
targets: [
Expand All @@ -23,7 +23,7 @@ let package = Package(
name: "AppTests",
dependencies: [
.target(name: "App"),
.product(name: "HummingbirdXCT", package: "hummingbird"),
.product(name: "HummingbirdTesting", package: "hummingbird"),
]
),
]
Expand Down
12 changes: 6 additions & 6 deletions Starter/Sources/App/Application+build.swift
Expand Up @@ -2,13 +2,13 @@ import Foundation
import Hummingbird
import Logging

func buildApplication() async throws -> some HBApplicationProtocol {
func buildApplication() async throws -> some ApplicationProtocol {

let router = HBRouter(context: MyBaseRequestContext.self)
let router = Router(context: MyBaseRequestContext.self)

router.middlewares.add(HBLogRequestsMiddleware(.info))
router.middlewares.add(HBFileMiddleware())
router.middlewares.add(HBCORSMiddleware(
router.middlewares.add(LogRequestsMiddleware(.info))
router.middlewares.add(FileMiddleware())
router.middlewares.add(CORSMiddleware(
allowOrigin: .originBased,
allowHeaders: [.contentType],
allowMethods: [.get, .post, .delete, .patch]
Expand All @@ -20,7 +20,7 @@ func buildApplication() async throws -> some HBApplicationProtocol {

MyController().addRoutes(to: router.group("api"))

return HBApplication(
return Application(
router: router,
configuration: .init(
address: .hostname("localhost", port: 8080)
Expand Down
8 changes: 4 additions & 4 deletions Starter/Sources/App/Controllers/MyController.swift
Expand Up @@ -5,7 +5,7 @@ import NIO
struct MyController<Context: MyRequestContext> {

func addRoutes(
to group: HBRouterGroup<Context>
to group: RouterGroup<Context>
) {
group
.get(use: list)
Expand All @@ -14,7 +14,7 @@ struct MyController<Context: MyRequestContext> {

@Sendable
func list(
_ request: HBRequest,
_ request: Request,
context: Context
) async throws -> [MyModel] {
[
Expand All @@ -26,9 +26,9 @@ struct MyController<Context: MyRequestContext> {

@Sendable
func create(
_ request: HBRequest,
_ request: Request,
context: Context
) async throws -> HBEditedResponse<MyModel> {
) async throws -> EditedResponse<MyModel> {
// context.myValue
let input = try await request.decode(
as: MyModel.self,
Expand Down
2 changes: 1 addition & 1 deletion Starter/Sources/App/Models/MyModel.swift
Expand Up @@ -4,4 +4,4 @@ struct MyModel: Codable {
let title: String
}

extension MyModel: HBResponseCodable {}
extension MyModel: ResponseCodable {}
6 changes: 3 additions & 3 deletions Starter/Sources/App/RequestContexts/MyRequestContext.swift
Expand Up @@ -2,13 +2,13 @@ import Hummingbird
import Logging
import NIOCore

protocol MyRequestContext: HBRequestContext {
protocol MyRequestContext: RequestContext {
var myValue: String? { get set }
}

struct MyBaseRequestContext: MyRequestContext {

var coreContext: HBCoreRequestContext
var coreContext: CoreRequestContext
var myValue: String?

init(
Expand All @@ -22,7 +22,7 @@ struct MyBaseRequestContext: MyRequestContext {
self.myValue = nil
}

var requestDecoder: HBRequestDecoder {
var requestDecoder: RequestDecoder {
MyRequestDecoder()
}
}
Expand Down
16 changes: 8 additions & 8 deletions Starter/Sources/App/RequestDecoders/MyRequestDecoder.swift
@@ -1,26 +1,26 @@
import Hummingbird

struct MyRequestDecoder: HBRequestDecoder {
struct MyRequestDecoder: RequestDecoder {

func decode<T>(
_ type: T.Type,
from request: HBRequest,
context: some HBBaseRequestContext
from request: Request,
context: some BaseRequestContext
) async throws -> T where T: Decodable {
guard let header = request.headers[.contentType] else {
throw HBHTTPError(.badRequest)
throw HTTPError(.badRequest)
}
guard let mediaType = HBMediaType(from: header) else {
throw HBHTTPError(.badRequest)
guard let mediaType = MediaType(from: header) else {
throw HTTPError(.badRequest)
}
let decoder: HBRequestDecoder
let decoder: RequestDecoder
switch mediaType {
case .applicationJson:
decoder = JSONDecoder()
case .applicationUrlEncoded:
decoder = URLEncodedFormDecoder()
default:
throw HBHTTPError(.badRequest)
throw HTTPError(.badRequest)
}
return try await decoder.decode(
type,
Expand Down