Skip to content

Commit

Permalink
Add more parameters to URLQueryEncoder initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Dec 28, 2021
1 parent 4a40389 commit a69397f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ encoder.encode(user, forKey: "id", isDeepObject: true)
// Query: "id[role]=admin&id[name]=kean")"
```

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

Expand Down
26 changes: 15 additions & 11 deletions Sources/URLQueryEncoder/URLQueryEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import Foundation

public final class URLQueryEncoder {
public var explode = true
public var delimiter = ","
public var isDeepObject = false
public var explode: Bool
public var delimiter: String
public var isDeepObject: Bool

private var _explode = true
private var _delimiter = ","
private var _isDeepObject = false
private var _explode: Bool
private var _delimiter: String
private var _isDeepObject: Bool

/// By default, `.iso8601`.
public var dateEncodingStrategy: DateEncodingStrategy = .iso8601
Expand Down Expand Up @@ -58,7 +58,15 @@ public final class URLQueryEncoder {
return components
}

public init() {}

public init(explode: Bool = true, delimiter: String = ",", isDeepObject: Bool = false) {
self.explode = explode
self._explode = explode
self.delimiter = delimiter
self._delimiter = delimiter
self.isDeepObject = isDeepObject
self._isDeepObject = isDeepObject
}

/// Encodes value for the given key.
@discardableResult
Expand All @@ -84,10 +92,6 @@ public final class URLQueryEncoder {
return self
}

public init<T: Encodable>(encoding body: T) {
encode(body, forKey: "value")
}

public static func encode<T: Encodable>(_ body: T) -> URLQueryEncoder {
let encoder = URLQueryEncoder()
encoder.encode(body, forKey: "value")
Expand Down
2 changes: 1 addition & 1 deletion Tests/URLQueryEncoderTests/URLQueryEncoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ final class QueryEncoderTests: XCTestCase {
let user = User(role: "admin", name: "kean")

// THEN
let query = URLQueryEncoder(encoding: user).percentEncodedQuery
let query = URLQueryEncoder.encode(user).percentEncodedQuery

XCTAssertEqual(query, "role=admin&name=kean")
}
Expand Down

0 comments on commit a69397f

Please sign in to comment.