npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

koa-paginate

v0.0.4

Published

A koa pagination middleware.

Downloads

428

Readme

koa-paginate

A koa pagination middleware

Install

npm install koa-paginate

Example

var app = require("koa")(),
    router = require("koa-router")(app),
    paginate = require("paginate");

// error handler
app.use(function* (next) {
    try {
        yield next;
    } catch (err) {
        // in case a MaxLimitError is thrown
        if (err instanceof paginate.MaxLimitError) {
            this.response.status = 400;
            this.response.body = "Wow, " + err.limit + " it's too much. We accept a maximum of " + err.maxLimit;
        }
        // throw the error to the parent middleware
        else {
            throw err;
        }
    }
});

// add middleware
app.use(paginate.middleware({
    // in case the limit is null
    defaultLimit: 20,
    // throws an error when exceeded
    maxLimit: 200
}));

app.use(router);

app.get("/", function* () {

    // this wraps the data and add the nextHref property
    this.paginate = true;

	// get the limit and offset (comes from the querystring)
    var limit = this.pagination.limit;
    var offset = this.pagination.offset;

    //fake data
    var items = [];
    for (var i = 0; i < 2000; i++) {
        if (i >= offset && i < offset + limit) {
            items.push(i);
        }
    }

    this.body = items;
});


app.listen(3000);

which produces

{
    data: [
	    285,
	    286,
	    287,
	    288,
	    289
    ],
    nextHref: "http://localhost:3000/?limit=5&offset=285"
}