generated from pixel-foundry/swift-package
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from pixel-foundry/endpoint-builder
Implement initial EndpointBuilder
- Loading branch information
Showing
15 changed files
with
568 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
{ | ||
"pins" : [ | ||
{ | ||
"identity" : "routing-kit", | ||
"kind" : "remoteSourceControl", | ||
"location" : "https://github.com/vapor/routing-kit", | ||
"state" : { | ||
"revision" : "2a92a7eac411a82fb3a03731be5e76773ebe1b3e", | ||
"version" : "4.9.0" | ||
} | ||
}, | ||
{ | ||
"identity" : "swift-http-types", | ||
"kind" : "remoteSourceControl", | ||
"location" : "https://github.com/apple/swift-http-types", | ||
"state" : { | ||
"revision" : "12358d55a3824bd5fed310b999ea8cf83a9a1a65", | ||
"version" : "1.0.3" | ||
} | ||
}, | ||
{ | ||
"identity" : "swift-log", | ||
"kind" : "remoteSourceControl", | ||
"location" : "https://github.com/apple/swift-log.git", | ||
"state" : { | ||
"revision" : "e97a6fcb1ab07462881ac165fdbb37f067e205d5", | ||
"version" : "1.5.4" | ||
} | ||
}, | ||
{ | ||
"identity" : "swift-macro-testing", | ||
"kind" : "remoteSourceControl", | ||
"location" : "https://github.com/pointfreeco/swift-macro-testing", | ||
"state" : { | ||
"revision" : "10dcef36314ddfea6f60442169b0b320204cbd35", | ||
"version" : "0.2.2" | ||
} | ||
}, | ||
{ | ||
"identity" : "swift-snapshot-testing", | ||
"kind" : "remoteSourceControl", | ||
"location" : "https://github.com/pointfreeco/swift-snapshot-testing", | ||
"state" : { | ||
"revision" : "8e68404f641300bfd0e37d478683bb275926760c", | ||
"version" : "1.15.2" | ||
} | ||
}, | ||
{ | ||
"identity" : "swift-syntax", | ||
"kind" : "remoteSourceControl", | ||
"location" : "https://github.com/apple/swift-syntax.git", | ||
"state" : { | ||
"revision" : "64889f0c732f210a935a0ad7cda38f77f876262d", | ||
"version" : "509.1.1" | ||
} | ||
} | ||
], | ||
"version" : 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,42 @@ | ||
// swift-tools-version:5.8 | ||
// swift-tools-version:5.9 | ||
import CompilerPluginSupport | ||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "SwiftPackage", | ||
name: "endpoint-builder", | ||
products: [ | ||
.library(name: "SwiftPackage", targets: ["SwiftPackage"]) | ||
.library(name: "EndpointBuilder", targets: ["EndpointBuilder"]) | ||
], | ||
dependencies: [ | ||
.package(url: "https://github.com/apple/swift-http-types", from: "1.0.0"), | ||
.package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.0"), | ||
.package(url: "https://github.com/pointfreeco/swift-macro-testing", from: "0.2.0"), | ||
.package(url: "https://github.com/vapor/routing-kit", from: "4.9.0") | ||
], | ||
targets: [ | ||
.target(name: "SwiftPackage"), | ||
.testTarget(name: "Tests", dependencies: ["SwiftPackage"]) | ||
.target( | ||
name: "EndpointBuilder", | ||
dependencies: [ | ||
.byName(name: "EndpointBuilderMacros"), | ||
.product(name: "HTTPTypes", package: "swift-http-types"), | ||
.product(name: "RoutingKit", package: "routing-kit") | ||
] | ||
), | ||
.testTarget(name: "EndpointBuilderTests", dependencies: ["EndpointBuilder"]), | ||
.macro( | ||
name: "EndpointBuilderMacros", | ||
dependencies: [ | ||
.product(name: "RoutingKit", package: "routing-kit"), | ||
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"), | ||
.product(name: "SwiftCompilerPlugin", package: "swift-syntax") | ||
] | ||
), | ||
.testTarget( | ||
name: "EndpointBuilderMacrosTests", | ||
dependencies: [ | ||
.byName(name: "EndpointBuilderMacros"), | ||
.product(name: "MacroTesting", package: "swift-macro-testing") | ||
] | ||
) | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1 @@ | ||
# Swift Package Repository Template | ||
|
||
This is a project template for Swift packages. | ||
|
||
* Sensible `.gitignore` | ||
* `.swiftlint.yml` for linting | ||
* `.editorconfig` enforcing tabs with a size of 2. | ||
# Endpoint Builder |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import Foundation | ||
|
||
/// HTTP authorization schemes | ||
public enum Authorization: Sendable, Hashable { | ||
|
||
/// Basic authorization | ||
case basic(username: String, password: String) | ||
|
||
/// Bearer authorization | ||
case bearer(token: String) | ||
|
||
/// HTTP header string value | ||
public var headerValue: String { | ||
switch self { | ||
case let .basic(username, password): | ||
let encoded = Data("\(username):\(password)".utf8).base64EncodedString() | ||
return "Basic \(encoded)" | ||
case let .bearer(token): | ||
return "Bearer \(token)" | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import Foundation | ||
import HTTPTypes | ||
import RoutingKit | ||
|
||
/// Describes an API endpoint | ||
public protocol Endpoint: Sendable { | ||
|
||
associatedtype BodyContent: Codable | ||
associatedtype Response: Codable | ||
|
||
/// The path components for this endpoint | ||
static var path: [PathComponent] { get } | ||
|
||
/// The HTTP method for this endpoint | ||
static var httpMethod: HTTPRequest.Method { get } | ||
|
||
/// The response type of this endpoint | ||
static var responseType: Response.Type { get } | ||
|
||
/// Authorization headers to be sent along with the request | ||
var authorization: Authorization? { get } | ||
|
||
/// Request body content | ||
var body: BodyContent { get } | ||
|
||
/// The URL path for this endpoint | ||
var path: String { get } | ||
|
||
} | ||
|
||
public extension Endpoint { | ||
|
||
var authorization: Authorization? { | ||
nil | ||
} | ||
|
||
var body: Never { | ||
fatalError() | ||
} | ||
|
||
static var responseType: Never.Type { | ||
fatalError() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import HTTPTypes | ||
import RoutingKit | ||
|
||
/// Conforms a type to the `Endpoint` protocol. | ||
/// | ||
/// Generates a `path` string for the given path components, | ||
/// along with a helper type for initializing any path parameters. | ||
@attached(extension, conformances: Endpoint) | ||
@attached(member, names: named(PathParameters), named(pathParameters), named(path)) | ||
public macro Endpoint() = #externalMacro(module: "EndpointBuilderMacros", type: "EndpointMacro") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import Foundation | ||
|
||
extension Never: Codable { | ||
|
||
public init(from decoder: Decoder) throws { | ||
throw DecodingError.dataCorrupted( | ||
DecodingError.Context(codingPath: [], debugDescription: "Never values cannot be decoded.") | ||
) | ||
} | ||
|
||
public func encode(to encoder: Encoder) throws {} | ||
|
||
} |
Oops, something went wrong.