mathematic-inc / ts-japi

A highly-modular (typescript-friendly)-framework agnostic library for serializing data to the JSON:API specification
Apache License 2.0
202 stars 15 forks source link

[FR] Pagination - Not enough context #35

Open StevieBoyWonder opened 3 years ago

StevieBoyWonder commented 3 years ago

Issues

The current pagination interface/classes do not accept enough parameters to create proper links

Details

Since the paginator is attached to a Serializer, the Paginator constructor can only accept the object or an Array of objects. However, to correctly use page-based pagination, you need the current query params supplied to a request to be available as well as a metaValue total.

Ex. (Articles Collection has 85 records) GET /articles?page[number]=1&page[size]=10 Should fetch the first ten articles and serialize them. The resulting pagination links should be

{
    first: '/articles?page[number]=1$page[size]=10',
    last: '/articles?page[number]=8&page[size]=10',
    next: 'articles?page[number]=2&page[size]=10',
    prev: null
}

Notice how the information here is only derivable given the request query params. In addition, a total needs to be supplied in some way to the serializer.

jrandolf commented 3 years ago

It might make more sense to pass in a context object rather than what you are asking for; something that can be passed through all the functions.

StevieBoyWonder commented 3 years ago

Yeah, I completely agree and that seems to be what alternative libraries do as well. They have this god level (extraData) param that is supplied at the top level and is propagated through as the structure is created.