Skip to content

Commit

Permalink
feat: CJS rewrite, node v16+ supported, drop all dependencies (now ze…
Browse files Browse the repository at this point in the history
…ro deps), use ctx.href vs. ctx.originalUrl
  • Loading branch information
titanism committed Sep 2, 2024
1 parent f978c1a commit 2ec0c7a
Show file tree
Hide file tree
Showing 22 changed files with 314 additions and 218 deletions.
4 changes: 0 additions & 4 deletions .babelrc

This file was deleted.

3 changes: 3 additions & 0 deletions .commitlintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: ['@commitlint/config-conventional']
};
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
2 changes: 0 additions & 2 deletions .eslintignore

This file was deleted.

3 changes: 0 additions & 3 deletions .eslintrc

This file was deleted.

1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
4 changes: 0 additions & 4 deletions .github/FUNDING.yml

This file was deleted.

24 changes: 24 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: CI
on:
- push
- pull_request
jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os:
- ubuntu-latest
node_version:
- 18
name: Node ${{ matrix.node_version }} on ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- name: Setup node
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node_version }}
- name: Install dependencies
run: npm install
- name: Run tests
run: npm run test
22 changes: 12 additions & 10 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@

# osx
.DS_Store

# project
node_modules/
logs
*.log
npm-debug.log*
*.idea
coverage/
lib
.idea
node_modules
coverage
.nyc_output
locales/
package-lock.json
yarn.lock

Thumbs.db
tmp/
temp/
*.lcov
5 changes: 5 additions & 0 deletions .lintstagedrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
"*.md": filenames => filenames.map(filename => `remark ${filename} -qfo`),
'package.json': 'fixpack',
'*.js': 'xo --fix'
};
1 change: 0 additions & 1 deletion .npmignore

This file was deleted.

1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
5 changes: 5 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
singleQuote: true,
bracketSpacing: true,
trailingComma: 'none'
};
1 change: 1 addition & 0 deletions .remarkignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test/snapshots/**/*.md
3 changes: 3 additions & 0 deletions .remarkrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
plugins: ['preset-github']
};
5 changes: 5 additions & 0 deletions .xo-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
prettier: true,
space: true,
extends: ['xo-lass']
};
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License

Copyright (c) 2016- Nick Baugh <[email protected]> (http://niftylettuce.com/)
Copyright (c) 2016 Forward Email LLC <[email protected]> (https://forwardemail.net)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
96 changes: 46 additions & 50 deletions Readme.md → README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,50 @@
# ctx-paginate
# [**ctx-paginate**](https://github.com/koajs/ctx-paginate)

[![NPM Version][npm-image]][npm-url]
[![NPM Downloads][downloads-image]][downloads-url]
[![MIT License][license-image]][license-url]
[![Slack][slack-image]][slack-url]
[![build status](https://github.com/koajs/ctx-paginate/actions/workflows/ci.yml/badge.svg)](https://github.com/koajs/ctx-paginate/actions/workflows/ci.yml)
[![code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)
[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
[![made with lass](https://img.shields.io/badge/made_with-lass-95CC28.svg)](https://lass.js.org)
[![license](https://img.shields.io/github/license/koajs/ctx-paginate.svg)](LICENSE)

> Koa pagination middleware and view helpers. Maintained for [Forward Email](https://forwardemail.net) [@forwardemail](https://github.com/forwardemail).

## Table of Contents

* [Install](#install)
* [Usage](#usage)
* [API](#api)
* [paginate.middleware(limit, maxLimit)](#paginatemiddlewarelimit-maxlimit)
* [paginate.href(ctx)](#paginatehrefctx)
* [paginate.hasPreviousPages](#paginatehaspreviouspages)
* [paginate.hasNextPages(ctx)](#paginatehasnextpagesctx)
* [paginate.getArrayPages(ctx)](#paginategetarraypagesctx)
* [Example](#example)
* [License](#license)

> Koa pagination middleware and view helpers.

## Install

```sh
npm install -s koa-ctx-paginate
npm install koa-ctx-paginate
```


## Usage

> Import every method:
```js
import * as paginate from 'koa-ctx-paginate';
const paginate = require('koa-ctx-paginate');
```

> Import selective methods:
```js
import { middleware } from 'koa-ctx-paginate';
const { middleware } = require('koa-ctx-paginate');
```


## API

### paginate.middleware(limit, maxLimit)
Expand All @@ -35,8 +53,8 @@ This middleware validates and supplies default values to `ctx.paginate.skip` (an

#### Arguments

- `limit` a Number to limit results returned per page (defaults to `10`)
- `maxLimit` a Number to restrict the number of results returned to per page (defaults to `50`) – through this, users will not be able to override this limit (e.g. they can't pass `?limit=10000` and crash your server)
* `limit` a Number to limit results returned per page (defaults to `10`)
* `maxLimit` a Number to restrict the number of results returned to per page (defaults to `50`) – through this, users will not be able to override this limit (e.g. they can't pass `?limit=10000` and crash your server)

### paginate.href(ctx)

Expand All @@ -60,12 +78,12 @@ Note that if you pass only one argument with a type of Object, then it will gene

#### Arguments

- `ctx` (**required**) – the request object returned from Koa middleware invocation
* `ctx` (**required**) – the request object returned from Koa middleware invocation

#### Returned function arguments when invoked with `ctx`

- `prev` (optional) – a Boolean to determine whether or not to increment the hyperlink returned by `1` (e.g. for "Next" page links)
- `params` (optional) – an Object of querystring parameters that will override the current querystring in `ctx.query` (note that this will also override the `page` querystring value if `page` is present as a key in the `params` object) (e.g. if you want to make a link that allows the user to change the current querystring to sort by name, you would have `params` equal to `{ sort: 'name' }`)
* `prev` (optional) – a Boolean to determine whether or not to increment the hyperlink returned by `1` (e.g. for "Next" page links)
* `params` (optional) – an Object of querystring parameters that will override the current querystring in `ctx.query` (note that this will also override the `page` querystring value if `page` is present as a key in the `params` object) (e.g. if you want to make a link that allows the user to change the current querystring to sort by name, you would have `params` equal to `{ sort: 'name' }`)

### paginate.hasPreviousPages

Expand All @@ -81,11 +99,11 @@ When executed with `ctx`, it will return a function that accepts two required ar

#### Arguments

- `ctx` (**required**) – the request object returned from Koa middleware invocation
* `ctx` (**required**) – the request object returned from Koa middleware invocation

#### Returned function arguments when invoked with `ctx`

- `pageCount` (**required**) – a Number representing the total number of pages for the given query executed on the page
* `pageCount` (**required**) – a Number representing the total number of pages for the given query executed on the page

### paginate.getArrayPages(ctx)

Expand All @@ -94,25 +112,26 @@ Get all the page urls with limit.

#### Arguments

- `ctx` (**required**) – the request object returned from Koa middleware invocation
* `ctx` (**required**) – the request object returned from Koa middleware invocation

#### Returned function arguments when invoked with `ctx`

- `limit` (**optional**) – Default: 3, a Number representing the total number of pages for the given query executed on the page.
- `pageCount` (**required**) – a Number representing the total number of pages for the given query executed on the page.
- `currentPage` (**required**) – a Number representing the current page.
* `limit` (**optional**) – Default: 3, a Number representing the total number of pages for the given query executed on the page.
* `pageCount` (**required**) – a Number representing the total number of pages for the given query executed on the page.
* `currentPage` (**required**) – a Number representing the current page.


## Example

```js
// # app.js

import koa from 'koa';
import Router from 'koa-router';
import * as paginate from 'koa-ctx-paginate';
const koa = require('koa');
const Router = require('koa-router');
const paginate = require('koa-ctx-paginate');

// e.g. `Users` is a database model created with Mongoose
import { Users } from '../models';
const { Users } = require('../models');

const app = koa();
const router = new Router();
Expand All @@ -122,16 +141,12 @@ app.use(paginate.middleware(10, 50));

// let's get paginated list of users
router.get('/users', async function (ctx, next) {

try {

const [ results, itemCount ] = await Promise.all([
Users.find({}).limit(ctx.query.limit).skip(ctx.paginate.skip).lean().exec(),
Users.countDocuments({})
]);

const pageCount = Math.ceil(itemCount / ctx.query.limit);

if (ctx.is('json')) {
// inspired by Stripe's API response for list objects
ctx.body = {
Expand All @@ -147,11 +162,9 @@ router.get('/users', async function (ctx, next) {
pages: paginate.getArrayPages(ctx)(3, pageCount, ctx.query.page)
});
}

} catch (err) {
ctx.throw(err);
}

});

app.use(router.routes());
Expand Down Expand Up @@ -181,7 +194,7 @@ ul
include _paginate
```

> Bootstrap 4.x (used by [Lad][]):
> Bootstrap 4.x (used by \[Lad]\[]):
```pug
//- _paginate.pug
Expand Down Expand Up @@ -257,24 +270,7 @@ if paginate.hasPreviousPages || paginate.hasNextPages(pageCount)
i.fa.fa-arrow-circle-right
```

## License

[MIT][license-url]

[npm-image]: https://img.shields.io/npm/v/koa-ctx-paginate.svg?style=flat

[npm-url]: https://npmjs.org/package/koa-ctx-paginate

[downloads-image]: http://img.shields.io/npm/dm/koa-ctx-paginate.svg?style=flat

[downloads-url]: https://npmjs.org/package/koa-ctx-paginate

[license-image]: http://img.shields.io/badge/license-MIT-blue.svg?style=flat

[license-url]: LICENSE

[slack-url]: https://join.slack.com/t/ladjs/shared_invite/zt-fqei6z11-Bq2trhwHQxVc5x~ifiZG0g/

[slack-image]: https://img.shields.io/badge/chat-join%20slack-brightgreen
## License

[lad]: https://lad.js.org
[MIT](LICENSE) © Forward Email LLC
Loading

0 comments on commit 2ec0c7a

Please sign in to comment.