Add url encode switch to api route
This commit is contained in:
parent
790da03409
commit
4969f38b19
|
@ -8,6 +8,7 @@ Until 1.0, breaking changes can occur in minor versions.
|
||||||
### ✨ New features
|
### ✨ New features
|
||||||
|
|
||||||
* `ApiRoute` has more explicit properties for working with post data.
|
* `ApiRoute` has more explicit properties for working with post data.
|
||||||
|
* `ApiRoute` has a new `shouldUrlEncodeParams` parameter.
|
||||||
* `iCloudDocumentSync` is a new protocol for syncing iCloud document changes.
|
* `iCloudDocumentSync` is a new protocol for syncing iCloud document changes.
|
||||||
* `String+Slugify` is a new extension that can convert a string to a slugified version.
|
* `String+Slugify` is a new extension that can convert a string to a slugified version.
|
||||||
* `StandardiCloudDocumentSync` is a new class for syncing iCloud document changes.
|
* `StandardiCloudDocumentSync` is a new class for syncing iCloud document changes.
|
||||||
|
@ -15,7 +16,7 @@ Until 1.0, breaking changes can occur in minor versions.
|
||||||
|
|
||||||
### 💡 Behavior changes
|
### 💡 Behavior changes
|
||||||
|
|
||||||
* `ApiRoute` now url encodes query and post parameter values.
|
* `ApiRoute` has more required properties.
|
||||||
* `URL+setQueryParameter` no longer url encodes the strings you send in.
|
* `URL+setQueryParameter` no longer url encodes the strings you send in.
|
||||||
|
|
||||||
### 💥 Breaking changes
|
### 💥 Breaking changes
|
||||||
|
|
|
@ -46,6 +46,13 @@ public protocol ApiRoute {
|
||||||
performing a request.
|
performing a request.
|
||||||
*/
|
*/
|
||||||
var queryParams: [String: String] { get }
|
var queryParams: [String: String] { get }
|
||||||
|
|
||||||
|
/**
|
||||||
|
Whether or not the route should url encode its post and
|
||||||
|
query params. Return `false` if the params will contain
|
||||||
|
chars that mustn't be url encoded, like array brackets.
|
||||||
|
*/
|
||||||
|
var urlEncodeParams: Bool { get }
|
||||||
}
|
}
|
||||||
|
|
||||||
public extension ApiRoute {
|
public extension ApiRoute {
|
||||||
|
@ -65,7 +72,7 @@ public extension ApiRoute {
|
||||||
var postParamsString: String? {
|
var postParamsString: String? {
|
||||||
var params = URLComponents()
|
var params = URLComponents()
|
||||||
params.queryItems = postParams
|
params.queryItems = postParams
|
||||||
.map { URLQueryItem(name: $0.key, value: $0.value.urlEncoded()) }
|
.map { URLQueryItem(name: $0.key, value: paramValue(for: $0.value)) }
|
||||||
.sorted { $0.name < $1.name }
|
.sorted { $0.name < $1.name }
|
||||||
return params.query
|
return params.query
|
||||||
}
|
}
|
||||||
|
@ -75,7 +82,7 @@ public extension ApiRoute {
|
||||||
*/
|
*/
|
||||||
var queryItems: [URLQueryItem] {
|
var queryItems: [URLQueryItem] {
|
||||||
queryParams
|
queryParams
|
||||||
.map { URLQueryItem(name: $0.key, value: $0.value.urlEncoded()) }
|
.map { URLQueryItem(name: $0.key, value: paramValue(for: $0.value)) }
|
||||||
.sorted { $0.name < $1.name }
|
.sorted { $0.name < $1.name }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,3 +128,10 @@ public extension ApiRoute {
|
||||||
environment.url.appendingPathComponent(path)
|
environment.url.appendingPathComponent(path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private extension ApiRoute {
|
||||||
|
|
||||||
|
func paramValue(for value: String) -> String? {
|
||||||
|
urlEncodeParams ? value.urlEncoded() : value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -84,4 +84,5 @@ private struct TestRoute: ApiRoute {
|
||||||
var postData: Data? { nil }
|
var postData: Data? { nil }
|
||||||
var postParams: [String: String] { ["foo&": "bar&", "baz?": "BAM?"] }
|
var postParams: [String: String] { ["foo&": "bar&", "baz?": "BAM?"] }
|
||||||
var queryParams: [String: String] { ["hello&": "world&", "anyone?": "there?"] }
|
var queryParams: [String: String] { ["hello&": "world&", "anyone?": "there?"] }
|
||||||
|
var urlEncodeParams: Bool { true }
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue