|
1 | 1 | # URLQueryEncoder
|
2 | 2 |
|
3 |
| -A description of this package. |
| 3 | +A customizable Swift `Encoder` that encodes instances of data types as URL query items. Supports all [OpenAPI] query parameter [serialization options](https://swagger.io/docs/specification/serialization/). |
| 4 | + |
| 5 | +## Examples |
| 6 | + |
| 7 | +### Encoding Primitives |
| 8 | + |
| 9 | +```swift |
| 10 | +let encoder = URLQueryEncoder() |
| 11 | +encoder.encode(["id": id]) |
| 12 | + |
| 13 | +print(encoder.queryItems) |
| 14 | +// ("id", "5") |
| 15 | +``` |
| 16 | + |
| 17 | +### Encoding Arrays |
| 18 | + |
| 19 | +```swift |
| 20 | +let ids = [3, 4, 5] |
| 21 | +let encoder = URLQueryEncoder() |
| 22 | +encoder.encode(["id": ids]) |
| 23 | + |
| 24 | +// Query: "id=3&id=4&id=5" |
| 25 | +``` |
| 26 | + |
| 27 | +With an `explode` option disabled: |
| 28 | + |
| 29 | +```swift |
| 30 | +let ids = [3, 4, 5] |
| 31 | +let encoder = URLQueryEncoder() |
| 32 | +encoder.encode(["id": ids], explode: false) |
| 33 | + |
| 34 | +// Query: "id=3,4,5" |
| 35 | +``` |
| 36 | + |
| 37 | +With an `explode` option disabled and a custom delimiter: |
| 38 | + |
| 39 | +```swift |
| 40 | +let ids = [3, 4, 5] |
| 41 | +let encoder = URLQueryEncoder() |
| 42 | +encoder.encode(["id": ids], explode: false, delimiter: "|") |
| 43 | + |
| 44 | +// Query: "id=3|4|5" |
| 45 | +``` |
| 46 | + |
| 47 | +### Encoding Objects |
| 48 | + |
| 49 | +```swift |
| 50 | +let user = User(role: "admin", name: "kean") |
| 51 | + |
| 52 | +let encoder = URLQueryEncoder() |
| 53 | +encoder.encode(user) |
| 54 | + |
| 55 | +// Query: "role=admin&name=kean" |
| 56 | +``` |
| 57 | + |
| 58 | +With an `explode` option disabled: |
| 59 | + |
| 60 | +```swift |
| 61 | +let user = User(role: "admin", name: "kean") |
| 62 | + |
| 63 | +let encoder = URLQueryEncoder() |
| 64 | +encoder.encode(user, explode: false) |
| 65 | + |
| 66 | +// Query: "id=role,admin,name,kean" |
| 67 | +``` |
| 68 | + |
| 69 | +As a "deep" object: |
| 70 | + |
| 71 | +```swift |
| 72 | +let user = User(role: "admin", name: "kean") |
| 73 | + |
| 74 | +let encoder = URLQueryEncoder() |
| 75 | +encoder.encode(user, isDeepObject: true) |
| 76 | + |
| 77 | +// Query: "id[role]=admin&id[name]=kean")" |
| 78 | +``` |
| 79 | + |
| 80 | +## Encoding Options |
| 81 | + |
| 82 | +There are two ways to change the encoding options: settings them directly on `URLQueryEncoder` instance, or passing options in each individual `encode` call. The reason it's designed this way is that in OpenAPI, each parameter can have different serialization options. |
| 83 | + |
| 84 | +```swift |
| 85 | +let encoder = URLQueryEncoder() |
| 86 | +encoder.explode = false |
| 87 | +encoder.isDeepObject = true |
| 88 | +encoder.delimiter = "|" |
| 89 | +encoder.dateEncodingStrategy = .millisecondsSince1970 |
| 90 | +``` |
| 91 | + |
| 92 | +You can use `URLQueryEncoder` to encode more that one parameter are a time: |
| 93 | + |
| 94 | +```swift |
| 95 | +let user = User(role: "admin", name: "kean") |
| 96 | +let ids = [3, 4, 5] |
| 97 | + |
| 98 | +let encoder = URLQueryEncoder() |
| 99 | +encoder.encode(["ids": ids], explode: false) |
| 100 | +encoder.encode(["ids2": ids], explode: true) |
| 101 | +encoder.encode(["user": user], isDeepObject: true) |
| 102 | +encoder.encode(["id": 2]) |
| 103 | + |
| 104 | +// Query: "ids=3,4,5&ids2=3&ids2=4&ids2=5&user[role]=admin&user[name]=kean&id=2") |
| 105 | +``` |
| 106 | + |
| 107 | +## Accessing Results |
| 108 | + |
| 109 | +You can access the encoding results at any time, and they come in different forms: |
| 110 | + |
| 111 | +```swift |
| 112 | +public final class Encoder { |
| 113 | + public var queryItems: [URLQueryItem] |
| 114 | + public var items: [(String, String?)] |
| 115 | + |
| 116 | + public var query: String? |
| 117 | + public var percentEncodedQuery: String? |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +## License |
| 122 | + |
| 123 | +URLQueryEncoder is available under the MIT license. See the LICENSE file for more info. |
0 commit comments