Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

koa-pagination

Package Overview
Dependencies
Maintainers
2
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-pagination

Koa Pagination

  • 0.3.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
320
increased by22.61%
Maintainers
2
Weekly downloads
 
Created
Source

Koa Pagination

npm version build status Code Climate Test Coverage

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

Installation

Choose your preferred method:

  • npm: npm install --save koa-pagination
  • Download: koa-pagination

Configuration

The middleware can be configured with the following parameters:

  • Maximum: Maximum number of items allowed per page (50 by default).
  • Unit: Range unit to be used when no Range header is provided (items by default).

You can change the defaults by doing:

paginate({
  maximum: 100,
  unit: 'bytes'
});

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-5/*'

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

Running tests

npm test

FAQs

Package last updated on 09 Feb 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc