1.0.1 • Published 9 years ago

action-url v1.0.1

Weekly downloads
6
License
MIT
Repository
github
Last release
9 years ago

npm version Build Status Code Climate Coverage Status Bower version

action-url

Keep your routes in one place and change them with ease.

Contents

Guide

Install

Using npm

npm install action-url

To save to dependencies use

npm install action-url --save
Initialize

Require the module

var AUrl = require('action-url');

Create an endpoint to your api

var endpoint = AUrl('url_to_your_api');
Register routes

The verbose way

endpoint.action('Controller', 'ActionOne', 'urlOne');
endpoint.action('Controller', 'ActionTwo', 'urlTwo');

The lazy coder's way, chain multiple routes to a controller

endpoint.controller('Controller')
    .action('ActionOne', 'urlOne')
    .action('ActionTwo', 'urlTwo');
Retrieving URLs

To retrieve a url simply call the method url

endpoint('Controller','ActionOne'); //returns urlOne

If you have a url with wildcards in it you can pass an object with the wildcards' values and get the correct url

// e.g Lets say that ActionOne from Controller has the following url with wildcards
// url = '/:controller/:id'
// and you want the url for '/image/15'

var params = {
    controller : image
    id : 15
};

endpoint.url('Controller', 'ActionOne', params); // returns /image/15

Api

Constructor

var endpoint = require('action-url')(baseURL);
ParameterTypeRequiredDescription
baseURLurlfalsethe api endpoint url. Omit if on same server.

.controller(name)

.controller(name) -> control

It creates and returns a new controller or retrieves an existing one.

ParameterTypeRequiredDescription
namestringtruethe name of the controller to create or retrieve.

.action(controller, action, route)

.action(controller, action, route)

It registers a new route to the action of the controller

ParameterTypeRequiredDescription
controllerstringtrue
actionstringtrue
routeurltrue

Controller instance

Methodsreturn typedescription
.action(name, url)Controllerbinds a new action with a route.
.action(name, route)
.action(name, route) -> Controller

Binds a new action with a route. Throws error if the action exists already.

ParameterTypeRequiredDescription
namestringtruethe name of the new route
routestringtruea url

.hasController(name)

.hasController(name) -> bool

It checks if a controller exists.

ParameterTypeRequiredDescription
namestringtruethe name of the controller to check.

.urlRaw(controller, action)

.urlRaw(controller, action) -> url

Returns the original url route as specified in .action('name',''route')

ParameterTypeRequiredDescription
controllerstringtrue
actionstringtrue

.url(controller, action, params)

.url(controller, action, [params]) -> url

Returns the original url route as specified in .action('name',''route')

ParameterTypeRequiredDescription
controllerstringtruethrows error if null.
actionstringtruethrows error if null.
paramsobjectfalsean object with key-value pairs that correspond to the route's wildcards.

Roadmap

Feel free to suggest new features or anything that might be useful

Changelog

    1.0.1    security updates

License

Copyright (c) 2015 George Kaimakas

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.