# kane-internal-router

> Koa middleware to create API routes and validate user inputs

Latest version **0.5.1** (published 2018-12-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install kane-internal-router
pnpm add kane-internal-router
yarn add kane-internal-router
bun add kane-internal-router
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.5.1 |
| Published | 2018-12-13 |
| First published | 2018-10-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8.x |
| Dependencies | 5 |
| Unpacked size | 59.2 KB |
| Known vulnerabilities | 0 (+2 in 2 direct dependencies) |
| Install scripts | no |
| Author | Nomad Education |
| Maintainers | nomad-education-developer |

## Links

- npm: https://www.npmjs.com/package/kane-internal-router
- npm.io page: https://npm.io/package/kane-internal-router

## Dependencies (5)

- [joi](https://npm.io/package/joi.md) ^14.0.4
- [co-body](https://npm.io/package/co-body.md) ^6.0.0
- [kane-error](https://npm.io/package/kane-error.md) ^0.6.5
- [koa-router](https://npm.io/package/koa-router.md) ^7.4.0
- [path-to-regexp](https://npm.io/package/path-to-regexp.md) ^2.4.0

## Recent versions

- 0.5.1 (latest) — 2018-12-13
- 0.5.0 — 2018-11-09
- 0.4.2 — 2018-11-09
- 0.4.1 — 2018-11-09
- 0.4.0 — 2018-11-08
- 0.3.1 — 2018-11-07
- 0.3.0 — 2018-11-07
- 0.2.2 — 2018-11-07
- 0.2.1 — 2018-11-05
- 0.2.0 — 2018-11-05
- 0.1.0 — 2018-10-25

## README

# Kane Internal Router
[![Build Status](https://travis-ci.com/nomadeducation/kane-internal-router.svg?token=q2CErnyNpiDUJ8qhqaNZ&branch=master)](https://travis-ci.com/nomadeducation/kane-internal-router)
[![Known Vulnerabilities](https://snyk.io/test/npm/kane-internal-router/badge.svg)](https://snyk.io/test/npm/kane-internal-router)

Koa middleware that uses [koa-router](https://github.com/alexmingoia/koa-router) and [Joi](https://github.com/hapijs/joi) to validate user inputs.

## Requirements

You need to execute `node` version `8` **at least** (`async`/`await`).

## Installation

```bash
# npm
npm install --save kane-internal-router
# yarn
yarn add kane-internal-router
```

## Usage

Calling the package will return a [koa router](https://github.com/alexmingoia/koa-router#router-) with a new method that handles routes with Joi schemas for validation. By default [unknown keys](https://github.com/hapijs/joi/blob/v14.0.1/API.md#validatevalue-schema-options-callback) are **allowed**.

You can validate **headers**, **body** and **params** of a request. If Joi does not return a validation error, the code in the **handler** will be executed.

```js
const Koa = require("koa");
const {Joi, KaneRouter} = require("kane-internal-router");

// by default, the router will:
// - limit the body size to 10MB
// - set a prefix (i.e. "/v2")
// - allow unknown routes and parameters to be invoked (it will however ignore them)
const router = new KaneRouter();

// you can freely add new routes to the router
// the structure is unique as below
const routeConfig = [
    {
        method: "patch",
        path: "/users/:id",
        // see the API for more details:
        // https://github.com/hapijs/joi/blob/v14.0.1/API.md
        validate: {
            params: {
                id: Joi.string().uuid({version: ["uuidv4"]}).required()
            },
            query: {
                where: Joi.string().allow("")
            },
            header: {
                authorization: Joi.string().required()
            },
            body: {
                email: Joi.string().min(6).max(254).lowercase().email(),
                // use email as username if it's empty
                username: Joi.string().min(3).max(254).lowercase().regex(/^[A-Za-z0-9.]+$/, "username"),
            }
        },
        // the handler param can also be an array of Koa middlewares
        handler: (ctx, next) => {/* stuff */}
    }
];
router.add(routeConfig);

const app = new Koa();
const routes = router.middleware();
app.use(routes);
app.listen();
```

The router also exposes a static method to determine if the request can be handled:
```js
const Koa = require("koa");
const {KaneRouter} = require("kane-internal-router");

const app = new Koa();
const router = new KaneRouter();

const routeConfig = [/* config */];
const catchAllRoute = {
        method: "all",
        path: "(.*)",
        handler: (ctx) => {
            const matched = KaneRouter.match(routeConfig, ctx.method, ctx.path);

            if (matched) {
                // stuff
            }
        }
};
router.add(routeConfig);
router.add(catchAllRoute);

app
.use(router.middleware())
.listen();
```

### Linting

Made using `eslint`. To enforce rules to be applied, use `yarn lint:fix`.

### Testing

Simply invoke `yarn test`.

## Contributing
First, install the dependencies using `yarn`:
```bash
yarn install --frozen-lockfile
```

Verify that your project is configured correctly by launching tests:
```bash
yarn test
```

Before you start coding make sure that you've read our [`CONTRIBUTING`](.github/CONTRIBUTING.md) guide!

---
_Source: https://npm.io/package/kane-internal-router · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
