paginationMiddleware

Source
import { paginationMiddleware } from "@prestojs/rest";
paginationMiddleware(paginatorClass,?options)

Middleware to activate pagination on an endpoint.

This should not be added globally. It should be added to each Endpoint that requires pagination.

Usage:

new Endpoint(new UrlPattern('/users/'), {
middleware: [paginationMiddleware()]
})

Or to customise the paginatorClass

new Endpoint(new UrlPattern('/users/'), {
middleware: [paginationMiddleware(PageNumberPaginator)]
})
ParameterTypeDescription
*paginatorClassPaginatorInterfaceClass

The pagination class to use. Defaults to InferredPaginator.

options.getPaginationState
Function

Function that returns the state for a paginator based on the response. If not provided uses the static getPaginationState method on the paginatorClass. You can use this method if your backend needs to transform the response before being handled by paginatorClass. This can be useful to use a built in paginator (eg. PageNumberPaginator) where a data structure from the backend differs from that expected.

options.resultPathstring

Optional path to where in data the pagination state exists. Dotted notation is accepted (see below example).

If this is provided only this section of the data object will be updated. For example if the return data looked like:

{
records: {
users: {
count: 10,
results: [...]
},
products: [...]
}
extra: { ... }
}

And resultPath was set to records.users then the returned data would be

{
records: {
users: [...], // pagination state extracted, `results` set
products: [...], // unchanged
}
extra: { ... } // unchanged
}
MiddlewareObject
KeyTypeDescription
init
Function
prepare
Function
process
Function