JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 168
  • Score
    100M100P100Q76640F
  • License MIT

Koa Pagination

Package Exports

  • koa-pagination

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (koa-pagination) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Koa Pagination

Build Status

Koa Pagination is a middleware to handle Range Pagination Headers using Range & Content-Range entity-headers.

Installation

Choose your preferred method:

Configuration

The middleware can be configured with the following parameters:

  • Maximum: Maximum number of items allowed per page (50 items by default).

You can change the defaults by doing:

paginate({
  maximum: 100
});

Usage

var koa = require('koa');
var paginate = require('koa-pagination');

var app = koa();

app.get('/', paginate(), function *() {
  // `paginate` middleware will inject a `pagination` object in the `koa` context,
  // which will allow you to use the `pagination.offset` and `pagination.limit`
  // in your data retrieval methods.
  this.body = foobar.getData({
    limit: this.pagination.limit,
    offset: this.pagination.offset
  });

  // This is needed in order to expose the length in `Content-Range` header.
  this.pagination.length = foobar.count();
});

app.listen(3000);

Request

You can provide the Range header specifying the items you want to retrieve. For instance to retrieve the first 5 elements:

'Range: items=0-5'

Response

This will generate a response with the following Content-Range header:

'Content-Range: items 0-4/*'

The * will be replaced with the total number of items provided in the length variable.

Running tests

npm test