Skip to content

Commit

Permalink
chore(): initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonCust committed May 24, 2018
0 parents commit 70f2399
Show file tree
Hide file tree
Showing 13 changed files with 753 additions and 0 deletions.
1 change: 1 addition & 0 deletions .coveralls.yml
@@ -0,0 +1 @@
repo_token: OQzN7KZoaF2l1lfWtQHxJerdjMqf5f01z
4 changes: 4 additions & 0 deletions .eslintignore
@@ -0,0 +1,4 @@
node_modules/
coverage/
package.json
package-lock.json
37 changes: 37 additions & 0 deletions .eslintrc
@@ -0,0 +1,37 @@
{
"extends": [
"eslint:recommended",
"plugin:jest/recommended",
"google"
],
"env": {
"jest": true,
"node": true,
"es6": true
},
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module",
"ecmaFeatures": {
"impliedStrict": true
}
},
"plugins": [
"node",
"jest",
"import"
],
"rules": {
"max-len": "warn",
"indent": ["error", 2],
"linebreak-style": ["error", "unix"],
"quotes": ["error", "single"],
"semi": ["error", "always"],
"comma-dangle": ["error", "never"],
"no-use-before-define": ["error", { "functions": false }],
"space-before-function-paren": ["error", { "anonymous": "always", "named": "never", "asyncArrow": "always" }],
"no-param-reassign": ["error", { "props": false }],
"function-paren-newline": ["error", "consistent"],
"no-console": "off"
}
}
10 changes: 10 additions & 0 deletions .gitignore
@@ -0,0 +1,10 @@
# Dependency directory
# Commenting this out is preferred by some people, see
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git-
node_modules
coverage
npm-debug.log
package-lock.json
.vscode
.tmp
.DS_Store
11 changes: 11 additions & 0 deletions .travis.yml
@@ -0,0 +1,11 @@
language: node_js
node_js:
- node
- lts/*
- '10'
- '9'
- '8'
notifications:
email: false
slack:
secure: m9ZBq3AKJ9nfQj73nAweUKrBAkpILHWL5KHW5ZDe2Q+UK9VUno8Ul8IBYf35rUctTtUDdlrWDdhGjhz3a2d+e6DOoySh44dfLeu38L0Ho4v4SJdZTOMDh1iP9GwYI4GdtnTyBG4LO5a8N8te+STgEg9iyNhEcxIVCXz0DMem+vO2/q0fk3aR1n7OfkMshEKgifaqhrfggZYTnF1Mp9hPryO/dR5mDzGURhFrhEzWhiT49lLIRcLIJfup4BXynWlD6N0FS1D1oHMHcBxXahh0IAdhNQKfs1saHLSLwBzz/OO5/0tMmtlPErW/gMz0p7EEwdpf74CRNe71o0eEi5Y+ps4uQaKaWRLfvHoGqLoanMEzqgXP1OO8NcvXERJ9eCoSpEBYmj+tH7sgi2t0B2Qv1qJh8JZA2p3IbPG71nHRfyQVD7XcfJ5YwyRrggV0WhonuGDokd+3WLRcsT5YS6d76nQ1I/nGzfUagwhsyOLUm+IqEHzp3k4rnEmcwFbTTZzlxhXO5qbioDdNOTYaKulT/dQe6hwpM/MMpK/lvRDike24WLycbNpwmHE+bkLhbrvHraNrfbLCPFcLGSKstPxVjuRFVIzbokFW2TnQXS2lVNok3MXIt+fPM4OqUuRbdCpIHY40h8NFCsT0nYzeoM6HBbXLmekyFNjKvZmvCB++Udg=
146 changes: 146 additions & 0 deletions README.md
@@ -0,0 +1,146 @@
# @CentralPing/express-pagination

[![Build Status](https://travis-ci.org/CentralPing/express-pagination.svg?branch=master)](https://travis-ci.org/CentralPing/express-pagination)
[![Coverage Status](https://coveralls.io/repos/github/CentralPing/express-pagination/badge.svg)](https://coveralls.io/github/CentralPing/express-pagination)
[![Dependency Status](https://david-dm.org/CentralPing/express-pagination.svg)](https://david-dm.org/CentralPing/express-pagination)
[![Greenkeeper Status](https://badges.greenkeeper.io/CentralPing/express-pagination.svg)](https://greenkeeper.io/)
[![Known Vulnerabilities](https://snyk.io/test/github/centralping/express-pagination/badge.svg)](https://snyk.io/test/github/centralping/express-pagination)

A middleware wrapper for [expressjs](http://expressjs.com) for validating pagination parameters and generating pagination links by [`@CentralPing/list-pagination`](https://github.com/CentralPing/express-pagination).

## Installation

`npm i --save https://github.com/CentralPing/express-pagination`

## API Reference

<a name="module_express-pagination..tokenize"></a>

### express-pagination~tokenize([options]) ⇒ <code>function</code>
Configures Express middleware for composing optional pagination tokens.

**Kind**: inner method of [<code>express-pagination</code>](#module_express-pagination)
**Returns**: <code>function</code> - Configured Express middleware

| Param | Type | Default |
| --- | --- | --- |
| [options] | <code>Object</code> | |
| [options.readFrom] | <code>String</code> | <code>query</code> |
| [options.writeTo] | <code>String</code> | <code>pagination</code> |
| [options.uuidKey] | <code>String</code> | |
| [options.secret] | <code>String</code> | |

<a name="module_express-pagination..validate"></a>

### express-pagination~validate(options) ⇒ <code>function</code>
Configures Express middleware for validating pagination query parameters.

**Kind**: inner method of [<code>express-pagination</code>](#module_express-pagination)
**Returns**: <code>function</code> - Configured Express middleware

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| options | <code>Object</code> | | |
| [options.countValid] | <code>\*</code> | <code>&#x27;1&#x27;</code> | Indicates what a truthy value is (outside of `true`). Can be a single value or an array of values. |
| [options.max] | <code>Number</code> | <code>25</code> | The maximum and default list items allowed. |
| [options.sort] | <code>String</code> \| <code>Array.&lt;String&gt;</code> | <code>id</code> | The default field(s) to sort the list by. |
| [options.sortValid] | <code>String</code> \| <code>Array.&lt;String&gt;</code> | <code>[id]</code> | The allowed fields to sort the list by. The schema will automatically match descending annotation (i.e. `-id`). |


## Examples

### For Default Validation

```js
const express = require('express');
const { tokenize, validate } = require('express-pagination');

const app = express();

app.get('/foos',
validate,
(req, res, next) => {
res.locals.list = getList(res.locals.query);
next();
},
tokenize,
(req, res) => {
res.json({
pages: res.locals.pagination,
list: res.locals.list
})
}
);

/*
GET request -> /foos
// After validate
res.locals would be set to:
{
query: {
// Defaults
limit: 25,
sort: ['id'],
filter: {}
}
}
// After tokenize (assuming a list of 25 items)
res.locals would include:
{
pagination: {
self: TOKEN,
first: TOKEN,
next: TOKEN // Omitted if less than 25 items
}
}
GET request -> /foos?limit=5&sort=-foo&filter.foo='bar'
// After validate
res.locals would be set to:
{
query: {
limit: 5,
sort: ['-foo', 'id'],
filter: {foo: 'bar'}
}
}
// After tokenize (assuming a list of 5 items)
res.locals would include:
{
pagination: {
self: TOKEN,
first: TOKEN,
next: TOKEN
}
}
GET request -> /foos?page=NEXT_TOKEN
// After validate (assuming orignal limit, sort, and filter from previous example)
res.locals would be set to:
{
query: {
limit: 5,
sort: ['-foo', 'id'],
filter: {foo: 'bar'},
// Subsequent next tokens will add more cursors, e.g. [6, 3],
// where the current cursor will always be at the 0 index position
cursors: [3],
type: 'next'
}
}
// After tokenize (assuming another list of 5 items)
res.locals would include:
{
pagination: {
self: TOKEN,
first: TOKEN,
next: TOKEN,
prev: TOKEN
}
}
*/
```

## License

MIT
118 changes: 118 additions & 0 deletions docs/readme.hbs
@@ -0,0 +1,118 @@
# @CentralPing/express-pagination

[![Build Status](https://travis-ci.org/CentralPing/express-pagination.svg?branch=master)](https://travis-ci.org/CentralPing/express-pagination)
[![Coverage Status](https://coveralls.io/repos/github/CentralPing/express-pagination/badge.svg)](https://coveralls.io/github/CentralPing/express-pagination)
[![Dependency Status](https://david-dm.org/CentralPing/express-pagination.svg)](https://david-dm.org/CentralPing/express-pagination)
[![Greenkeeper Status](https://badges.greenkeeper.io/CentralPing/express-pagination.svg)](https://greenkeeper.io/)
[![Known Vulnerabilities](https://snyk.io/test/github/centralping/express-pagination/badge.svg)](https://snyk.io/test/github/centralping/express-pagination)

A middleware wrapper for [expressjs](http://expressjs.com) for validating pagination parameters and generating pagination links by [`@CentralPing/list-pagination`](https://github.com/CentralPing/express-pagination).

## Installation

`npm i --save https://github.com/CentralPing/express-pagination`

## API Reference

{{#module name="express-pagination"~}}
{{>body~}}
{{>members~}}
{{/module}}

## Examples

### For Default Validation

```js
const express = require('express');
const { tokenize, validate } = require('express-pagination');

const app = express();

app.get('/foos',
validate,
(req, res, next) => {
res.locals.list = getList(res.locals.query);
next();
},
tokenize,
(req, res) => {
res.json({
pages: res.locals.pagination,
list: res.locals.list
})
}
);

/*
GET request -> /foos
// After validate
res.locals would be set to:
{
query: {
// Defaults
limit: 25,
sort: ['id'],
filter: {}
}
}
// After tokenize (assuming a list of 25 items)
res.locals would include:
{
pagination: {
self: TOKEN,
first: TOKEN,
next: TOKEN // Omitted if less than 25 items
}
}

GET request -> /foos?limit=5&sort=-foo&filter.foo='bar'
// After validate
res.locals would be set to:
{
query: {
limit: 5,
sort: ['-foo', 'id'],
filter: {foo: 'bar'}
}
}
// After tokenize (assuming a list of 5 items)
res.locals would include:
{
pagination: {
self: TOKEN,
first: TOKEN,
next: TOKEN
}
}

GET request -> /foos?page=NEXT_TOKEN
// After validate (assuming orignal limit, sort, and filter from previous example)
res.locals would be set to:
{
query: {
limit: 5,
sort: ['-foo', 'id'],
filter: {foo: 'bar'},
// Subsequent next tokens will add more cursors, e.g. [6, 3],
// where the current cursor will always be at the 0 index position
cursors: [3],
type: 'next'
}
}
// After tokenize (assuming another list of 5 items)
res.locals would include:
{
pagination: {
self: TOKEN,
first: TOKEN,
next: TOKEN,
prev: TOKEN
}
}
*/
```

## License

MIT
6 changes: 6 additions & 0 deletions lib/index.js
@@ -0,0 +1,6 @@
// Set options as a parameter, environment variable, or rc file.

/* eslint-disable-next-line no-global-assign */
// require = require('esm')(module);

module.exports = require('./main.js');
6 changes: 6 additions & 0 deletions lib/index.spec.unit.js
@@ -0,0 +1,6 @@
// const {validate, body, params, query} = require('./index');

describe('[Unit] `index`', () => {
it('should export a `` function', () => {
});
});

0 comments on commit 70f2399

Please sign in to comment.