diff --git a/Sources/HTTPTypes/HTTPRequest.swift b/Sources/HTTPTypes/HTTPRequest.swift index 269ad6e..ee3c319 100644 --- a/Sources/HTTPTypes/HTTPRequest.swift +++ b/Sources/HTTPTypes/HTTPRequest.swift @@ -318,6 +318,23 @@ extension HTTPRequest: Codable { } } +extension HTTPRequest.Method: Codable { + public func encode(to encoder: any Encoder) throws { + var container = encoder.singleValueContainer() + try container.encode(self.rawValue) + } + + public init(from decoder: any Decoder) throws { + let container = try decoder.singleValueContainer() + let rawMethod = try container.decode(String.self) + guard let method = HTTPRequest.Method(rawMethod) else { + throw DecodingError.dataCorruptedError(in: container, debugDescription: "\"\(rawMethod)\" is not a valid method") + } + + self = method + } +} + extension HTTPRequest.Method { /// GET /// diff --git a/Sources/HTTPTypes/HTTPResponse.swift b/Sources/HTTPTypes/HTTPResponse.swift index 863bdae..6bd1152 100644 --- a/Sources/HTTPTypes/HTTPResponse.swift +++ b/Sources/HTTPTypes/HTTPResponse.swift @@ -281,6 +281,18 @@ extension HTTPResponse: Codable { } } +extension HTTPResponse.Status: Codable { + public func encode(to encoder: any Encoder) throws { + var container = encoder.singleValueContainer() + try container.encode(self.code) + } + + public init(from decoder: any Decoder) throws { + let code = try decoder.singleValueContainer().decode(Int.self) + self.init(code: code) + } +} + extension HTTPResponse.Status { // MARK: 1xx