diff --git a/README.md b/README.md index fb2ee2a..d455b01 100644 --- a/README.md +++ b/README.md @@ -322,7 +322,7 @@ import PackageDescription let package = Package( name: "SomeProject", dependencies: [ - .package(url: "https://github.com/dankinsoid/swift-api-client.git", from: "1.1.2") + .package(url: "https://github.com/dankinsoid/swift-api-client.git", from: "1.1.3") ], targets: [ .target( diff --git a/Sources/SwiftAPIClient/APIClient.swift b/Sources/SwiftAPIClient/APIClient.swift index 3a3654b..a9af65e 100644 --- a/Sources/SwiftAPIClient/APIClient.swift +++ b/Sources/SwiftAPIClient/APIClient.swift @@ -142,6 +142,11 @@ public struct APIClient { return try await operation(configs) } + /// Modifies the client using the provided closure. + public func modifier(_ modifier: (Self) throws -> Self) rethrows -> Self { + try modifier(self) + } + private func createRequest() throws -> (HTTPRequest, Configs) { var configs = Configs() let client = Self.globalModifier(self) diff --git a/Sources/SwiftAPIClient/Modifiers/RequestModifiers.swift b/Sources/SwiftAPIClient/Modifiers/RequestModifiers.swift index 3cb29e9..4609273 100644 --- a/Sources/SwiftAPIClient/Modifiers/RequestModifiers.swift +++ b/Sources/SwiftAPIClient/Modifiers/RequestModifiers.swift @@ -106,6 +106,21 @@ public extension APIClient { func header(_ field: HTTPField.Name, _ value: String, removeCurrent: Bool = false) -> APIClient { headers(HTTPField(name: field, value: value), removeCurrent: removeCurrent) } + + /// Adds or updates a specific HTTP header for the request. + /// - Parameters: + /// - field: The key of the header to add or update. + /// - value: The value for the header. + /// - update: A Boolean to determine whether to remove the current header if it exists. Default is `false`. + /// - Returns: An instance of `APIClient` with modified header. + @_disfavoredOverload + func header(_ field: HTTPField.Name, _ value: CustomStringConvertible?, removeCurrent: Bool = false) -> APIClient { + if let value { + return headers(HTTPField(name: field, value: value.description), removeCurrent: removeCurrent) + } else { + return self + } + } } // MARK: - Body modifiers