|
| 1 | +// |
| 2 | +// HTTPRequestParameter.swift |
| 3 | +// FlyingFox |
| 4 | +// |
| 5 | +// Created by Simon Whitty on 11/07/2024. |
| 6 | +// Copyright © 2024 Simon Whitty. All rights reserved. |
| 7 | +// |
| 8 | +// Distributed under the permissive MIT license |
| 9 | +// Get the latest version from here: |
| 10 | +// |
| 11 | +// https://github.com/swhitty/FlyingFox |
| 12 | +// |
| 13 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 14 | +// of this software and associated documentation files (the "Software"), to deal |
| 15 | +// in the Software without restriction, including without limitation the rights |
| 16 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 17 | +// copies of the Software, and to permit persons to whom the Software is |
| 18 | +// furnished to do so, subject to the following conditions: |
| 19 | +// |
| 20 | +// The above copyright notice and this permission notice shall be included in all |
| 21 | +// copies or substantial portions of the Software. |
| 22 | +// |
| 23 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 24 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 25 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 26 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 27 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 28 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 29 | +// SOFTWARE. |
| 30 | +// |
| 31 | + |
| 32 | +import Foundation |
| 33 | + |
| 34 | +/// Converts values from `HTTPRoute.Parameter` with `HTTPRequest` |
| 35 | +public protocol HTTPRouteParameterValue { |
| 36 | + init(parameter: String) throws |
| 37 | +} |
| 38 | + |
| 39 | +extension String: HTTPRouteParameterValue { |
| 40 | + public init(parameter: String) { |
| 41 | + self.init(parameter) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +extension Int: HTTPRouteParameterValue { |
| 46 | + public init(parameter: String) throws { |
| 47 | + guard let value = Int(parameter)else { |
| 48 | + throw HTTPRouteParameterInvalid() |
| 49 | + } |
| 50 | + self = value |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +public struct HTTPRouteParameterInvalid: Error { } |
| 55 | + |
| 56 | +public extension HTTPRoute { |
| 57 | + |
| 58 | + func extractParameters(from request: HTTPRequest) -> [HTTPRequest.RouteParameter] { |
| 59 | + let pathComponents = request.path |
| 60 | + .split(separator: "/", omittingEmptySubsequences: true) |
| 61 | + |
| 62 | + return parameters |
| 63 | + .compactMap { |
| 64 | + switch $0 { |
| 65 | + case let .path(name: name, index: index): |
| 66 | + if pathComponents.indices.contains(index) { |
| 67 | + return HTTPRequest.RouteParameter( |
| 68 | + name: name, |
| 69 | + value: String(pathComponents[index]) |
| 70 | + ) |
| 71 | + } else { |
| 72 | + return nil |
| 73 | + } |
| 74 | + case let .query(name: name, index: index): |
| 75 | + if let value = request.query[index] { |
| 76 | + return HTTPRequest.RouteParameter( |
| 77 | + name: name, |
| 78 | + value: value |
| 79 | + ) |
| 80 | + } else { |
| 81 | + return nil |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +#if compiler(>=5.9) |
| 89 | +extension HTTPRoute { |
| 90 | + |
| 91 | + func extractParameterValues<each P: HTTPRouteParameterValue>( |
| 92 | + of type: (repeat each P).Type = (repeat each P).self, |
| 93 | + from request: HTTPRequest |
| 94 | + ) throws -> (repeat each P) { |
| 95 | + let parameters = extractParameters(from: request).map(\.value) |
| 96 | + var idx = 0 |
| 97 | + return try (repeat getParameter(at: &idx, parameters: parameters, type: (each P).self)) |
| 98 | + } |
| 99 | + |
| 100 | + private func getParameter<P: HTTPRouteParameterValue>(at index: inout Int, parameters: [String], type: P.Type) throws -> P { |
| 101 | + defer { index += 1 } |
| 102 | + |
| 103 | + guard parameters.indices.contains(index) else { |
| 104 | + throw HTTPRouteParameterInvalid() |
| 105 | + } |
| 106 | + |
| 107 | + return try P(parameter: parameters[index]) |
| 108 | + } |
| 109 | +} |
| 110 | +#endif |
0 commit comments