Skip to content

Commit ddfbb80

Browse files
committed
add clearer field names and more documentation
1 parent 4f498eb commit ddfbb80

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

Source/LDSwiftEventSource.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import FoundationNetworking
88
import os.log
99
#endif
1010

11-
public typealias HeaderTransform = ([String: String]) -> [String: String]
12-
1311
public class EventSource {
1412
private let esDelegate: EventSourceDelegate
1513

@@ -58,7 +56,7 @@ public class EventSource {
5856
/// Additional headers to be set on the request
5957
public var headers: [String: String] = [:]
6058
/// Provides the ability to add conditional headers
61-
public var extraHeaders: HeaderTransform = { $0 }
59+
public var headerTransform: HeaderTransform = { $0 }
6260
/// The minimum amount of time to wait before reconnecting after a failure
6361
public var reconnectTime: TimeInterval = 1.0
6462
/// The maximum amount of time to wait before reconnecting after a failure
@@ -148,7 +146,7 @@ class EventSourceDelegate: NSObject, URLSessionDataDelegate {
148146
urlRequest.httpMethod = self.config.method
149147
urlRequest.httpBody = self.config.body
150148
urlRequest.setValue(self.lastEventId, forHTTPHeaderField: "Last-Event-ID")
151-
urlRequest.allHTTPHeaderFields = self.config.extraHeaders(
149+
urlRequest.allHTTPHeaderFields = self.config.headerTransform(
152150
urlRequest.allHTTPHeaderFields?.merging(self.config.headers) { $1 } ?? self.config.headers
153151
)
154152
let task = session.dataTask(with: urlRequest)

Source/Types.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import Foundation
55
/// it has the ability to tell EventSource to stop reconnecting.
66
public typealias ConnectionErrorHandler = (Error) -> ConnectionErrorAction
77

8+
/// Type for a function that will take in the current http headers
9+
/// and return a new set of http headers to be used when connecting
10+
/// and reconnecting to a stream.
11+
public typealias HeaderTransform = ([String: String]) -> [String: String]
12+
813
/// Potential actions a ConnectionErrorHandler can return
914
public enum ConnectionErrorAction {
1015
/// Specifies that the error should be logged normally and dispatched to the EventHandler.

Tests/LDSwiftEventSourceTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ final class LDSwiftEventSourceTests: XCTestCase {
2929
config.body = testBody
3030
config.lastEventId = "eventId"
3131
config.headers = testHeaders
32-
config.extraHeaders = { _ in [:] }
32+
config.headerTransform = { _ in [:] }
3333
config.reconnectTime = 2.0
3434
config.maxReconnectTime = 60.0
3535
config.backoffResetThreshold = 120.0
@@ -40,7 +40,7 @@ final class LDSwiftEventSourceTests: XCTestCase {
4040
XCTAssertEqual(config.body, testBody)
4141
XCTAssertEqual(config.lastEventId, "eventId")
4242
XCTAssertEqual(config.headers, testHeaders)
43-
XCTAssertEqual(config.extraHeaders(config.headers), [:])
43+
XCTAssertEqual(config.headerTransform(config.headers), [:])
4444
XCTAssertEqual(config.reconnectTime, 2.0)
4545
XCTAssertEqual(config.maxReconnectTime, 60.0)
4646
XCTAssertEqual(config.backoffResetThreshold, 120.0)

0 commit comments

Comments
 (0)