Skip to content

Commit a69397f

Browse files
committed
Add more parameters to URLQueryEncoder initializer
1 parent 4a40389 commit a69397f

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ encoder.encode(user, forKey: "id", isDeepObject: true)
7777
// Query: "id[role]=admin&id[name]=kean")"
7878
```
7979

80-
> If you are encoding a request body using URL-form encoding, you can use a convenience `URLQueryEncoder(encoding: body` initializer.
80+
> If you are encoding a request body using URL-form encoding, you can use a convenience `URLQueryEncoder.encode(body)` method.
8181
8282
## Encoding Options
8383

Sources/URLQueryEncoder/URLQueryEncoder.swift

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
import Foundation
66

77
public final class URLQueryEncoder {
8-
public var explode = true
9-
public var delimiter = ","
10-
public var isDeepObject = false
8+
public var explode: Bool
9+
public var delimiter: String
10+
public var isDeepObject: Bool
1111

12-
private var _explode = true
13-
private var _delimiter = ","
14-
private var _isDeepObject = false
12+
private var _explode: Bool
13+
private var _delimiter: String
14+
private var _isDeepObject: Bool
1515

1616
/// By default, `.iso8601`.
1717
public var dateEncodingStrategy: DateEncodingStrategy = .iso8601
@@ -58,7 +58,15 @@ public final class URLQueryEncoder {
5858
return components
5959
}
6060

61-
public init() {}
61+
62+
public init(explode: Bool = true, delimiter: String = ",", isDeepObject: Bool = false) {
63+
self.explode = explode
64+
self._explode = explode
65+
self.delimiter = delimiter
66+
self._delimiter = delimiter
67+
self.isDeepObject = isDeepObject
68+
self._isDeepObject = isDeepObject
69+
}
6270

6371
/// Encodes value for the given key.
6472
@discardableResult
@@ -84,10 +92,6 @@ public final class URLQueryEncoder {
8492
return self
8593
}
8694

87-
public init<T: Encodable>(encoding body: T) {
88-
encode(body, forKey: "value")
89-
}
90-
9195
public static func encode<T: Encodable>(_ body: T) -> URLQueryEncoder {
9296
let encoder = URLQueryEncoder()
9397
encoder.encode(body, forKey: "value")

Tests/URLQueryEncoderTests/URLQueryEncoderTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ final class QueryEncoderTests: XCTestCase {
232232
let user = User(role: "admin", name: "kean")
233233

234234
// THEN
235-
let query = URLQueryEncoder(encoding: user).percentEncodedQuery
235+
let query = URLQueryEncoder.encode(user).percentEncodedQuery
236236

237237
XCTAssertEqual(query, "role=admin&name=kean")
238238
}

0 commit comments

Comments
 (0)