Repository: vapor/vapor
File: Sources/Development/routes.swift (Swift)

Code before:
        }
        return String(buffer: body)
    }

    asyncRoutes.get("client2") { req -> String in
        let response = try await req.client.get("https://www.google.com")
        guard let body = response.body else {
            throw Abort(.internalServerError)
        }
        return String(buffer: body)
    }
    
    asyncRoutes.get("content") { req in
        Creds(email: "name", password: "password")
    }
    
    asyncRoutes.get("content2") { req async throws -> Creds in
        return Creds(email: "name", password: "password")
    }
    
    asyncRoutes.get("contentArray") { req async throws -> [Creds] in
        let cred1 = Creds(email: "name", password: "password")
        return [cred1]
    }
    
    @Sendable func opaqueRouteTester(_ req: Request) async throws -> some AsyncResponseEncodable {
        "Hello World"
    }
    asyncRoutes.get("opaque", use: opaqueRouteTester)
    
    // Make sure jumping between multiple different types of middleware works
    asyncRoutes.grouped(TestAsyncMiddleware(number: 2), TestMiddleware(number: 3), TestAsyncMiddleware(number: 4), TestMiddleware(number: 5)).get("middleware") { req async throws -> String in
        return "OK"
    }
    
    let basicAuthRoutes = asyncRoutes.grouped(Test.authenticator(), Test.guardMiddleware())
    basicAuthRoutes.get("auth") { req async throws -> String in
        return try req.auth.require(Test.self).name
    }
    
    struct Test: Authenticatable {
        static func authenticator() -> AsyncAuthenticator {
            TestAuthenticator()
        }

        var name: String
    }

    struct TestAuthenticator: AsyncBasicAuthenticator {
        typealias User = Test


Review comment (bug):
This unfortunately masks any issues as is the incorrect usage for functions (it's confusing and very counter intuitive) - could you remove?

Code after:
        }
        return String(buffer: body)
    }

    asyncRoutes.get("client2") { req -> String in
        let response = try await req.client.get("https://www.google.com")
        guard let body = response.body else {
            throw Abort(.internalServerError)
        }
        return String(buffer: body)
    }
    
    asyncRoutes.get("content") { req in
        Creds(email: "name", password: "password")
    }
    
    asyncRoutes.get("content2") { req async throws -> Creds in
        return Creds(email: "name", password: "password")
    }
    
    asyncRoutes.get("contentArray") { req async throws -> [Creds] in
        let cred1 = Creds(email: "name", password: "password")
        return [cred1]
    }
    
    func opaqueRouteTester(_ req: Request) async throws -> some AsyncResponseEncodable {
        "Hello World"
    }
    asyncRoutes.get("opaque", use: opaqueRouteTester)
    
    // Make sure jumping between multiple different types of middleware works
    asyncRoutes.grouped(TestAsyncMiddleware(number: 2), TestMiddleware(number: 3), TestAsyncMiddleware(number: 4), TestMiddleware(number: 5)).get("middleware") { req async throws -> String in
        return "OK"
    }
    
    let basicAuthRoutes = asyncRoutes.grouped(Test.authenticator(), Test.guardMiddleware())
    basicAuthRoutes.get("auth") { req async throws -> String in
        return try req.auth.require(Test.self).name
    }
    
    struct Test: Authenticatable {
        static func authenticator() -> AsyncAuthenticator {
            TestAuthenticator()
        }

        var name: String
    }

    struct TestAuthenticator: AsyncBasicAuthenticator {
        typealias User = Test