action-url v1.0.1
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);
Parameter | Type | Required | Description |
---|---|---|---|
baseURL | url | false | the 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.
Parameter | Type | Required | Description |
---|---|---|---|
name | string | true | the 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
Parameter | Type | Required | Description |
---|---|---|---|
controller | string | true | |
action | string | true | |
route | url | true |
Controller instance
Methods | return type | description |
---|---|---|
.action(name, url) | Controller | binds 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.
Parameter | Type | Required | Description |
---|---|---|---|
name | string | true | the name of the new route |
route | string | true | a url |
.hasController(name)
.hasController(name) -> bool
It checks if a controller exists.
Parameter | Type | Required | Description |
---|---|---|---|
name | string | true | the name of the controller to check. |
.urlRaw(controller, action)
.urlRaw(controller, action) -> url
Returns the original url route as specified in .action('name',''route')
Parameter | Type | Required | Description |
---|---|---|---|
controller | string | true | |
action | string | true |
.url(controller, action, params)
.url(controller, action, [params]) -> url
Returns the original url route as specified in .action('name',''route')
Parameter | Type | Required | Description |
---|---|---|---|
controller | string | true | throws error if null. |
action | string | true | throws error if null. |
params | object | false | an 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.