# autoroute-base

> flexible autoroute, template for creating autoroutes.

Latest version **0.0.7** (published 2015-06-30) · ISC license · 0 weekly downloads

## Install

```sh
npm install autoroute-base
pnpm add autoroute-base
yarn add autoroute-base
bun add autoroute-base
```

## 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.0.7 |
| Published | 2015-06-30 |
| First published | 2015-06-24 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Jon Nyman |
| Maintainers | jon49 |
| Keywords | autoroute, auto-route, routing, route, controller |

## Links

- npm: https://www.npmjs.com/package/autoroute-base
- Repository: https://github.com/jon49/autoroute-base
- Issues: https://github.com/jon49/autoroute-base/issues
- npm.io page: https://npm.io/package/autoroute-base

## Dependencies (1)

- [require-glob](https://npm.io/package/require-glob.md) ^1.3.1

## Alternatives

- [express-promise-router](https://npm.io/package/express-promise-router.md) — 736.1K weekly downloads
- [next-usequerystate](https://npm.io/package/next-usequerystate.md) — 29.8K weekly downloads
- [@bitkyc08/opencodex](https://npm.io/package/@bitkyc08/opencodex.md) — 4.6K weekly downloads
- [lynkr](https://npm.io/package/lynkr.md) — 575 weekly downloads
- [baremetal.js](https://npm.io/package/baremetal.js.md) — 42 weekly downloads

## Recent versions

- 0.0.7 (latest) — 2015-06-30
- 0.0.4 — 2015-06-26
- 0.0.3 — 2015-06-26
- 0.0.2 — 2015-06-24
- 0.0.0 — 2015-06-24

## README

# Auto-route Base

## Summary

`Auto-route base` is a skeleton for creating custom auto routing capabilities.
Retrieves code automatically.

## Installation

Install with `npm`:

```
npm install --save autoroute-base
```

## [Project Status](http://www.walkercoderanger.com/blog/2015/06/advice-for-open-source-projects/)

- Stable/Release [![Build Status](https://secure.travis-ci.org/jon49/autoroute-base.png?branch=master)](http://travis-ci.org/jon49/autoroute-base)
- Active (June 26, 2015)

## Example

See the test for a simple example.

`autoroute-express-promise` example (in TypeScript - to see a vanilla JavaScript example see below).

```js
import {createAutoRoute, method as method_} from 'autoroute-base'
import _ = require('lodash')

const toControllers = _.curry(({message, baseRoute, sendWrapper, routeName}: AutoRouteExpressPromise.ToControllersOptions, controllerMethod: AutoRouteExpressPromise.ControllerMethod) => {

    const [routeMethodIndex, baseController] = controllerMethod,
          methodName = method_[routeMethodIndex], // named route method
          messageInfo = {routeName: routeName, methodName: methodName}

    // Wrap base controller in express style callback.
    baseRoute[methodName]((request: any, client: any, error: any) => {

        message.call(messageInfo, messageInfo)

        var send = <any> _.compose(client.send.bind(client), sendWrapper)

        baseController(request).then(send).catch(error).done()
    })

})

const createRoutes = _.curry((o: AutoRouteExpressPromise.Options, routeDefinitions: AutoRouteExpressPromise.RouteDefinition[]) => {
    _.forEach(routeDefinitions, routeDef => {
        const {route, methods} = routeDef,
              startRoute = o.baseRoute(route), // Get router
              options_: AutoRouteExpressPromise.ToControllersOptions = <any> _.assign({}, o, {routeName: route, baseRoute: startRoute})
        _.forEach(methods, toControllers(options_)) // attach routes/methods to router
    })
})

export const routes = _.curry((options: AutoRouteExpressPromise.Options, glob: string[]) => {
    createAutoRoute({createRoutes: createRoutes(options), glob: glob})
})

export const method = method_
```

Vanilla JavaScript example of above.

```js
var autoroute_base_1 = require('autoroute-base');
var _ = require('lodash');
var toControllers = _.curry(function (_a, controllerMethod) {
    var message = _a.message, baseRoute = _a.baseRoute, sendWrapper = _a.sendWrapper, routeName = _a.routeName;
    var routeMethodIndex = controllerMethod[0], baseController = controllerMethod[1], methodName = autoroute_base_1.method[routeMethodIndex], messageInfo = { routeName: routeName, methodName: methodName };
    // Wrap base controller in express style callback.
    baseRoute[methodName](function (request, client, error) {
        message.call(messageInfo, messageInfo);
        var send = _.compose(client.send.bind(client), sendWrapper);
        baseController(request).then(send).catch(error).done();
    });
});
var createRoutes = _.curry(function (o, routeDefinitions) {
    _.forEach(routeDefinitions, function (routeDef) {
        var route = routeDef.route, methods = routeDef.methods, startRoute = o.baseRoute(route), options_ = _.assign({}, o, { routeName: route, baseRoute: startRoute });
        _.forEach(methods, toControllers(options_)); // attach routes/methods to router
    });
});
exports.routes = _.curry(function (options, glob) {
    autoroute_base_1.createAutoRoute({ createRoutes: createRoutes(options), glob: glob });
});
exports.method = autoroute_base_1.method;
```

## API

```js
import {createAutoRoute, method} from 'autoroute-base'
```

### `createAutoRoute(options)`

where `options`:

```js
{
    glob: string[]
    _globOptions?: any
    createRoutes?: (postGlobResult: any) => any
}
```
**glob**: (Required) Array of globs for files to grab. E.g.,
`['./controllers/**/index.js']`. `autoroute-base` uses a custom post parse
to get back the route/controllers in a cleaner manner for easy processing,
assuming you are using the same set up I used.

**_globOptions**: (Optional) [Options used in
`require-glob`](https://www.npmjs.com/package/require-glob#options).
Note, to override the `autoroute-base` `reducer` behaviour do `{reducer: null}`
(changes to native behaviour) or `{reducer: CustomReducer}`.

**createRoutes**: (Optional) After grabbing all the files to use. It is time to
process them for your custom project. Do your post processing here. This will
use the result of the `require-glob`.

### `method`

This is an enumeration of all the `express.js` methods:

`get, post, put, head, delete, options, trace, copy, lock, mkcol, move, purge, propfind, proppatch, unlock, report, mkactivity, checkout, merge, "m-search", notify, subscribe, unsubscribe, patch, search, connect`

E.g.,

```js
method.get // => 0
method[0] // => "get"
```

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