npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

autoroute-base

v0.0.7

Published

flexible autoroute, template for creating autoroutes.

Downloads

71

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

  • Stable/Release Build Status
  • 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).

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.

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

import {createAutoRoute, method} from 'autoroute-base'

createAutoRoute(options)

where options:

{
    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. 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.,

method.get // => 0
method[0] // => "get"