Say we have a cursor based mostly paginated API the place a number of endpoints may be paginated. The response of such an endpoint is all the time as follows:
{
"nextCursor": "someString",
"PAYLOAD_KEY": <generic response>
}
So the payload all the time returns a cursor and the payload key is dependent upon the precise endpoint we use. For instance if now we have GET /customers
it is likely to be customers
and the worth of the important thing be an array of objects or we may cal a GET /some-large-object
and the important thing being merchandise
and the payload be an object.
Backside line the response is all the time an object with a cursor and one different key and it is related worth.
Attempting to make this generic in Swift I used to be pondering of this:
public struct Paginable<Physique>: Codable the place Physique: Codable {
public let physique: Physique
public let cursor: String?
non-public enum CodingKeys: String, CodingKey {
case physique, cursor
}
}
Now the one subject with this code is that it expects the Physique
to be accessible beneath the "physique"
key which is not the case.
We may have a struct Person: Codable
and the paginable specialised as Paginable<[Users]>
the place the API response object would have the important thing customers
for the array.
My query is how can I make this generic Paginable
struct work in order that I can specify the JSON payload key from the Physique
sort?