Socket
Socket
Sign inDemoInstall

base-routes

Package Overview
Dependencies
23
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.1.0 to 0.2.0

288

index.js

@@ -1,10 +0,4 @@

/*!
* base-routes <https://github.com/jonschlinkert/base-routes>
*
* Copyright (c) 2015, Jon Schlinkert.
* Licensed under the MIT License.
*/
'use strict';
var debug = require('debug')('base:routes');
var utils = require('./utils');

@@ -14,81 +8,95 @@

return function(app) {
if (!utils.isValid(app)) return;
/**
* Add `Router` and `Route` to the prototype
* The `Router` and `Route` classes are on the instance, in case they need
* to be accessed directly.
*
* ```js
* var router = new app.Router();
* var route = new app.Route();
* ```
* @api public
*/
app.Router = utils.routes.Router;
app.Route = utils.routes.Route;
this.define('Router', utils.router.Router);
this.define('Route', utils.router.Route);
/**
* Lazily initalize `router`, to allow options to
* be passed in after init.
*/
app.lazyRouter = function(methods) {
if (typeof this.router === 'undefined') {
this.define('router', new this.Router({
methods: utils.methods
}));
}
if (typeof methods !== 'undefined') {
this.router.method(methods);
}
};
/**
* Handle a middleware `method` for `view`.
* Handle a middleware `method` for `file`.
*
* ```js
* app.handle('customMethod', view, callback);
* app.handle('customMethod', file, callback);
* ```
* @name .handle
* @param {String} `method` Name of the router method to handle. See [router methods](./docs/router.md)
* @param {Object} `view` View object
* @param {Object} `file` View object
* @param {Function} `callback` Callback function
* @return {Object}
* @return {undefined}
* @api public
*/
app.handle = function(method, view, locals, cb) {
if (typeof locals === 'function') {
cb = locals;
locals = {};
this.define('handle', function(method, file, next) {
debug('handling "%s" middleware for "%s"', method, file.basename);
if (typeof next !== 'function') {
throw new TypeError('expected callback to be a function');
}
this.lazyRouter();
if (!view.options.handled) {
view.options.handled = [];
file.options = file.options || {};
if (!file.options.handled) {
file.options.handled = [];
}
if (typeof cb !== 'function') {
cb = this.handleError(method, view);
var cb = this.handleError(method, file, next);
file.options.method = method;
file.options.handled.push(method);
this.emit(method, file);
// if not an instance of `Templates`, or if we're inside a collection
// or the collection is not specified on view.options just handle the route and return
if (!this.isTemplates || !(this.isApp && file.options.collection)) {
this.router.handle(file, cb);
return;
}
view.options.method = method;
view.options.handled.push(method);
this.router.handle(view, this.handleError(method, view, cb));
return this;
};
// handle the app routes first, then handle the collection routes
var collection = this[file.options.collection];
this.router.handle(file, function(err) {
if (err) return cb(err);
collection.handle(method, file, cb);
});
});
/**
* Run the given middleware handler only if the view has not
* already been handled by the method.
* Run the given middleware handler only if the file has not already been handled
* by `method`.
*
* @name .handleView
* @param {Object} `method`
* @param {Object} `view`
* @param {Object} `locals`
* ```js
* app.handleOnce('onLoad', file, callback);
* ```
* @name .handleOnce
* @param {Object} `method` The name of the handler method to call.
* @param {Object} `file`
* @return {undefined}
* @api public
*/
app.handleView = function(method, view, locals/*, cb*/) {
if (!view.options.handled) {
view.options.handled = [];
this.define('handleOnce', function(method, file, cb) {
if (typeof cb !== 'function') {
throw new TypeError('expected callback to be a function');
}
if (view.options.handled.indexOf(method) < 0) {
this.handle.apply(this, arguments);
this.emit(method, view, locals);
if (!file.options.handled) {
file.options.handled = [];
}
return this;
};
if (file.options.handled.indexOf(method) === -1) {
this.handle(method, file, cb);
return;
}
cb(null, file);
});

@@ -99,29 +107,40 @@ /**

app.handleError = function(method, view, cb) {
if (typeof cb !== 'function') cb = utils.identity;
this.define('handleError', function(method, file, next) {
var app = this;
return function(err) {
if (err) {
if (err._handled) return cb();
if (err._handled === true) {
next();
return;
}
err._handled = true;
err.reason = app._name + '#handle' + method + ': ' + view.path;
app.emit('error', err);
return cb(err);
err.source = err.stack.split('\n')[1].trim();
err.reason = app._name + '#handle("' + method + '"): ' + file.path;
if (app.hasListeners('error')) {
app.emit('error', err);
}
if (typeof next !== 'function') throw err;
next(err);
return;
}
cb(null, view);
if (typeof next !== 'function') {
throw new TypeError('expected a callback function');
}
next(null, file);
};
};
});
/**
* Create a new Route for the given path. Each route contains
* a separate middleware stack.
* Create a new Route for the given path. Each route contains a separate middleware
* stack. See the [route API documentation][route-api] for details on adding handlers
* and middleware to routes.
*
* See the [route API documentation][route-api] for details on
* adding handlers and middleware to routes.
*
* ```js
* app.create('posts');
* app.route(/blog/)
* .all(function(view, next) {
* // do something with view
* .all(function(file, next) {
* // do something with file
* next();

@@ -134,12 +153,40 @@ * });

* @param {String} `path`
* @return {Object} `Route` for chaining
* @return {Object} Returns the instance for chaining.
* @api public
*/
app.route = function(/*path*/) {
this.define('route', function(/*path*/) {
this.lazyRouter();
return this.router.route.apply(this.router, arguments);
};
});
/**
* Add callback triggers to route parameters, where `name` is the name of
* the parameter and `fn` is the callback function.
*
* ```js
* app.param('title', function(view, next, title) {
* //=> title === 'foo.js'
* next();
* });
*
* app.onLoad('/blog/:title', function(view, next) {
* //=> view.path === '/blog/foo.js'
* next();
* });
* ```
* @name .param
* @param {String} `name`
* @param {Function} `fn`
* @return {Object} Returns the instance for chaining.
* @api public
*/
this.define('param', function(/*name, fn*/) {
this.lazyRouter();
this.router.param.apply(this.router, arguments);
return this;
});
/**
* Special route method that works just like the `router.METHOD()`

@@ -161,74 +208,71 @@ * methods, except that it matches all verbs.

app.all = function(path/*, callback*/) {
this.define('all', function(path/*, callback*/) {
var route = this.route(path);
route.all.apply(route, [].slice.call(arguments, 1));
return this;
};
});
/**
* Add callback triggers to route parameters, where
* `name` is the name of the parameter and `fn` is the
* callback function.
* Add a router handler method to the instance. Interchangeable with
* the [handlers]() method.
*
* ```js
* app.param('title', function(view, next, title) {
* //=> title === 'foo.js'
* next();
* });
*
* app.onLoad('/blog/:title', function(view, next) {
* //=> view.path === '/blog/foo.js'
* next();
* });
* app.handler('onFoo');
* // or
* app.handler(['onFoo', 'onBar']);
* ```
* @name .param
* @param {String} `name`
* @param {Function} `fn`
* @return {Object} Returns the instance of `Templates` for chaining.
* @name .handler
* @param {String} `method` Name of the handler method to define.
* @return {Object} Returns the instance for chaining
* @api public
*/
app.param = function(/*name, fn*/) {
this.lazyRouter();
this.router.param.apply(this.router, arguments);
return this;
};
this.define('handler', function(method) {
this.handlers(method);
});
/**
* Add a router handler.
* Add one or more router handler methods to the instance.
*
* @param {String} `method` Method name.
* ```js
* app.handlers(['onFoo', 'onBar', 'onBaz']);
* // or
* app.handlers('onFoo');
* ```
* @name .handlers
* @param {Array|String} `methods` One or more method names to define.
* @return {Object} Returns the instance for chaining
* @api public
*/
app.handler = function(methods) {
this.handlers(methods);
};
this.define('handlers', function(methods) {
this.lazyRouter(methods);
mixinHandlers(methods);
});
/**
* Add default Router handlers to Templates.
* Lazily initalize `router`, to allow options and custom methods to be
* define after instantiation.
*/
app.handlers = function(methods) {
this.lazyRouter(methods);
this.define('lazyRouter', function(methods) {
if (typeof this.router !== 'undefined') return;
this.define('router', new this.Router({
methods: utils.methods.concat(methods || [])
}));
});
// Mix router handler methods onto the intance
mixinHandlers(utils.methods);
function mixinHandlers(methods) {
utils.arrayify(methods).forEach(function(method) {
this.define(method, function(path) {
var route = this.route(path);
app.define(method, function(path) {
var route = app.route(path);
var args = [].slice.call(arguments, 1);
route[method].apply(route, args);
return this;
}.bind(this));
}.bind(this));
};
// Add router methods to Templates
utils.methods.forEach(function(method) {
app[method] = function(path) {
var route = this.route(path);
var args = [].slice.call(arguments, 1);
route[method].apply(route, args);
return this;
};
});
return app;
});
});
}
};
};
{
"name": "base-routes",
"description": "Plugin for adding routes support to your `base` application. Requires templates support to work.",
"version": "0.1.0",
"homepage": "https://github.com/jonschlinkert/base-routes",
"version": "0.2.0",
"homepage": "https://github.com/node-base/base-routes",
"author": "Jon Schlinkert (https://github.com/jonschlinkert)",
"repository": "jonschlinkert/base-routes",
"repository": "node-base/base-routes",
"bugs": {
"url": "https://github.com/jonschlinkert/base-routes/issues"
"url": "https://github.com/node-base/base-routes/issues"
},

@@ -24,22 +24,43 @@ "license": "MIT",

"dependencies": {
"en-route": "^0.7.3",
"lazy-cache": "^0.2.4"
"debug": "^2.2.0",
"en-route": "^0.7.5",
"is-registered": "^0.1.3",
"is-valid-instance": "^0.1.0",
"lazy-cache": "^2.0.1"
},
"devDependencies": {
"assemble-core": "^0.3.0",
"engine-base": "^0.1.2",
"mocha": "*",
"should": "^7.1.1"
"base-app": "^0.2.2",
"gulp-format-md": "^0.1.9",
"mocha": "^2.4.5",
"should": "^8.3.1"
},
"keywords": [
"base",
"routes"
],
"verb": {
"related": {
"list": [
"base",
"base-option",
"base-plugins",
"en-route",
"base-methods",
"base-plugins",
"base-options",
"templates"
]
}
},
"toc": true,
"layout": "default",
"tasks": [
"readme"
],
"plugins": [
"gulp-format-md"
],
"lint": {
"reflinks": true
},
"reflinks": [
"verb"
]
}
}

@@ -1,11 +0,23 @@

# base-routes [![NPM version](https://badge.fury.io/js/base-routes.svg)](http://badge.fury.io/js/base-routes) [![Build Status](https://travis-ci.org/jonschlinkert/base-routes.svg)](https://travis-ci.org/jonschlinkert/base-routes)
# base-routes [![NPM version](https://img.shields.io/npm/v/base-routes.svg?style=flat)](https://www.npmjs.com/package/base-routes) [![NPM downloads](https://img.shields.io/npm/dm/base-routes.svg?style=flat)](https://npmjs.org/package/base-routes) [![Build Status](https://img.shields.io/travis/node-base/base-routes.svg?style=flat)](https://travis-ci.org/node-base/base-routes)
> Plugin for adding routes support to your `base` application. Requires templates support to work.
Plugin for adding routes support to your `base` application. Requires templates support to work.
## TOC
- [Install](#install)
- [Usage](#usage)
- [API](#api)
- [Related projects](#related-projects)
- [Contributing](#contributing)
- [Building docs](#building-docs)
- [Running tests](#running-tests)
- [Author](#author)
- [License](#license)
## Install
Install with [npm](https://www.npmjs.com/)
Install with [npm](https://www.npmjs.com/):
```sh
$ npm i base-routes --save
$ npm install base-routes --save
```

@@ -17,3 +29,3 @@

var routes = require('base-routes');
var Base = require('base-methods');
var Base = require('base-app');

@@ -26,12 +38,19 @@ var app = new Base();

### [.handle](index.js#L52)
**Example**
Handle a middleware `method` for `view`.
```js
var router = new app.Router();
var route = new app.Route();
```
### [.handle](index.js#L38)
Handle a middleware `method` for `file`.
**Params**
* `method` **{String}**: Name of the router method to handle. See [router methods](./docs/router.md)
* `view` **{Object}**: View object
* `file` **{Object}**: View object
* `callback` **{Function}**: Callback function
* `returns` **{Object}**
* `returns` **{undefined}**

@@ -41,16 +60,29 @@ **Example**

```js
app.handle('customMethod', view, callback);
app.handle('customMethod', file, callback);
```
### [.route](index.js#L136)
### [.handleOnce](index.js#L88)
Create a new Route for the given path. Each route contains a separate middleware stack.
Run the given middleware handler only if the file has not already been handled by `method`.
See the [route API documentation][route-api] for details on
adding handlers and middleware to routes.
**Params**
* `method` **{Object}**: The name of the handler method to call.
* `file` **{Object}**
* `returns` **{undefined}**
**Example**
```js
app.handleOnce('onLoad', file, callback);
```
### [.route](index.js#L155)
Create a new Route for the given path. Each route contains a separate middleware stack. See the [route API documentation][route-api] for details on adding handlers and middleware to routes.
**Params**
* `path` **{String}**
* `returns` **{Object}** `Route`: for chaining
* `returns` **{Object}**: Returns the instance for chaining.

@@ -62,4 +94,4 @@ **Example**

app.route(/blog/)
.all(function(view, next) {
// do something with view
.all(function(file, next) {
// do something with file
next();

@@ -71,4 +103,28 @@ });

### [.all](index.js#L158)
### [.param](index.js#L182)
Add callback triggers to route parameters, where `name` is the name of the parameter and `fn` is the callback function.
**Params**
* `name` **{String}**
* `fn` **{Function}**
* `returns` **{Object}**: Returns the instance for chaining.
**Example**
```js
app.param('title', function(view, next, title) {
//=> title === 'foo.js'
next();
});
app.onLoad('/blog/:title', function(view, next) {
//=> view.path === '/blog/foo.js'
next();
});
```
### [.all](index.js#L205)
Special route method that works just like the `router.METHOD()` methods, except that it matches all verbs.

@@ -91,11 +147,10 @@

### [.param](index.js#L187)
### [.handler](index.js#L226)
Add callback triggers to route parameters, where `name` is the name of the parameter and `fn` is the callback function.
Add a router handler method to the instance. Interchangeable with the [handlers](#handlers) method.
**Params**
* `name` **{String}**
* `fn` **{Function}**
* `returns` **{Object}**: Returns the instance of `Templates` for chaining.
* `method` **{String}**: Name of the handler method to define.
* `returns` **{Object}**: Returns the instance for chaining

@@ -105,11 +160,22 @@ **Example**

```js
app.param('title', function(view, next, title) {
//=> title === 'foo.js'
next();
});
app.handler('onFoo');
// or
app.handler(['onFoo', 'onBar']);
```
app.onLoad('/blog/:title', function(view, next) {
//=> view.path === '/blog/foo.js'
next();
});
### [.handlers](index.js#L244)
Add one or more router handler methods to the instance.
**Params**
* `methods` **{Array|String}**: One or more method names to define.
* `returns` **{Object}**: Returns the instance for chaining
**Example**
```js
app.handlers(['onFoo', 'onBar', 'onBaz']);
// or
app.handlers('onFoo');
```

@@ -119,8 +185,28 @@

* [base-methods](https://www.npmjs.com/package/base-methods): Starter for creating a node.js application with a handful of common methods, like `set`, `get`,… [more](https://www.npmjs.com/package/base-methods) | [homepage](https://github.com/jonschlinkert/base-methods)
* [base-options](https://www.npmjs.com/package/base-options): Adds a few options methods to base-methods, like `option`, `enable` and `disable`. See the readme… [more](https://www.npmjs.com/package/base-options) | [homepage](https://github.com/jonschlinkert/base-options)
* [base-plugins](https://www.npmjs.com/package/base-plugins): Upgrade's plugin support in base-methods to allow plugins to be called any time after init. | [homepage](https://github.com/jonschlinkert/base-plugins)
You might also be interested in these projects:
* [base-option](https://www.npmjs.com/package/base-option): Adds a few options methods to base, like `option`, `enable` and `disable`. See the readme… [more](https://www.npmjs.com/package/base-option) | [homepage](https://github.com/node-base/base-option)
* [base-plugins](https://www.npmjs.com/package/base-plugins): Upgrade's plugin support in base applications to allow plugins to be called any time after… [more](https://www.npmjs.com/package/base-plugins) | [homepage](https://github.com/node-base/base-plugins)
* [base](https://www.npmjs.com/package/base): base is the foundation for creating modular, unit testable and highly pluggable node.js applications, starting… [more](https://www.npmjs.com/package/base) | [homepage](https://github.com/node-base/base)
* [en-route](https://www.npmjs.com/package/en-route): Routing for static site generators, build systems and task runners, heavily based on express.js routes… [more](https://www.npmjs.com/package/en-route) | [homepage](https://github.com/jonschlinkert/en-route)
* [templates](https://www.npmjs.com/package/templates): System for creating and managing template collections, and rendering templates with any node.js template engine.… [more](https://www.npmjs.com/package/templates) | [homepage](https://github.com/jonschlinkert/templates)
## Contributing
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/node-base/base-routes/issues/new).
## Building docs
Generate readme and API documentation with [verb](https://github.com/verbose/verb):
```sh
$ npm install verb && npm run docs
```
Or, if [verb](https://github.com/verbose/verb) is installed globally:
```sh
$ verb
```
## Running tests

@@ -131,9 +217,5 @@

```sh
$ npm i -d && npm test
$ npm install -d && npm test
```
## Contributing
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/base-routes/issues/new).
## Author

@@ -143,12 +225,12 @@

+ [github/jonschlinkert](https://github.com/jonschlinkert)
+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
* [github/jonschlinkert](https://github.com/jonschlinkert)
* [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
## License
Copyright © 2015 Jon Schlinkert
Released under the MIT license.
Copyright © 2016, [Jon Schlinkert](https://github.com/jonschlinkert).
Released under the [MIT license](https://github.com/node-base/base-routes/blob/master/LICENSE).
***
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on November 29, 2015._
_This file was generated by [verb](https://github.com/verbose/verb), v0.9.0, on May 17, 2016._
'use strict';
/**
* Module dependencies
*/
var debug = require('debug')('base:routes');
var utils = require('lazy-cache')(require);
/**
* Temporarily re-assign `require` to trick browserify and
* webpack into reconizing lazy dependencies.
*
* This tiny bit of ugliness has the huge dual advantage of
* only loading modules that are actually called at some
* point in the lifecycle of the application, whilst also
* allowing browserify and webpack to find modules that
* are depended on but never actually called.
*/
var fn = require;

@@ -27,14 +12,15 @@ require = utils;

require('en-route', 'routes');
require('en-route', 'router');
require('is-registered');
require('is-valid-instance');
require = fn;
/**
* Restore `require`
* Cast `val` to an array.
*/
require = fn;
utils.arrayify = function(val) {
return val ? (Array.isArray(val) ? val : [val]) : [];
};
/**
* Default router methods
*/
utils.methods = [

@@ -55,20 +41,14 @@ 'onLoad',

/**
* Return the given value as-is.
*/
utils.identity = function(val) {
return val;
utils.isValid = function(app) {
if (!utils.isValidInstance(app, ['app', 'collection', 'views'])) {
return false;
}
if (utils.isRegistered(app, 'base-routes')) {
return false;
}
debug('loading routes methods');
return true;
};
/**
* Arrayify the given value by casting it to an array.
*/
utils.arrayify = function(val) {
if (!val) return [];
return Array.isArray(val) ? val : [val];
};
/**
* Expose `utils` modules

@@ -75,0 +55,0 @@ */

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc