1.6.73 • Published 9 years ago

wires-domain v1.6.73

Weekly downloads
3
License
MIT
Repository
-
Last release
9 years ago

wires-domain

An ambitious dependency injection for your project

Installation

npm install wires-domain --save
bower install ws-domain --save

Services

Define few services

domain.service("$a", function() {
	return "Response from $a"
});
domain.service("$b", function($a) {
	return $a
});

Now we can call service "$b", that returns results from service "$a"

domain.require(function($b) {
	//$b is resolved and ready to be used!
})

domain.require always returns a promise.

For more example see test/flow.js

Modules

Modules are ultimately the same as services, with one difference. Modules are required once. All dependencies are resolved once, and closure is stored.

Asynchronous

domain.service("$wait", function() {
	return new Promise(function(resolve, reject) {
		resolve("Some async result")
	})
});

Restful Architecture

2 folders to be created. Services and RestApi. Put all your dependencies into "services" folder.

Require all at once:

require('require-all')('/services');
require('require-all')('/rest');

Connect with express.js

app.use(domain.express());

Restfull example

var domain = require('wires-domain');
domain.path("/:id?", {{
	get: function($res, $params) {
		$res.send({ id : $params.id } )
	}
});

All matched paramaters are combined into "$params" injection

Restful methods

// GET
get: function($res) {
	throw {
		status: 505,
		message: 'Not implemented'
	};
},
// POST
post: function($res) {
	throw {
		status: 505,
		message: 'Not implemented'
	}
},
// PUT
put: function($res) {
	throw {
		status: 505,
		message: 'Not implemented'
	}
},
// DELETE
delete: function() {
	throw {
		status: 505,
		message: 'Not implemented'
	}
}

Restful local injections

$res

Express res

$req

Express req

$params

matched parameters from the url

$next

It is possible to try next candidate. Note, that this is not express "next" function. Let's check an example:

domain.path("/", {
	get: function($res, $nice, $next) {
		$next();
		//$res.send("First")
	}
});

domain.path("/",{
	get: function($res) {
		$res.send("Second")
	}
});

Exceptions

Any exception can be thrown. If an object is a dictionary and it contains "status" key, that will be taken as a http code response. You can combine it with "message"

 domain.service("$a", function($params.id) {
	if ( $params.id === 5 ){
		throw {status : 400, message "You can't access this item"}
	}
 });

Magic Query getters

Body or Query can be pre processed.

domain.path("/", {
	get: function($res, $query, $nice, $next) {
		var myDate = $query.get("name@required,moment('DD-MM-YYYY')");
		var isValid = $query.get("valid@bool");

		return {
			myDate: myDate,
			isValid: isValid
		};
	}
});

required

Set "required" parameter to validate presence of a parameter.

$query.get("name@required");

?name should be defined. Empty string will cause an exception.

int

Set "bool" parameter to get a valid number

$query.get("name@int");

?name should be integer. Empty string will cause an exception.

moment

Set "moment" parameter to get date in momentjs format.

$query.get("date@moment('MM-DD-YYYY')");

?date will be transformed to moment object accordingly

bool

Set "bool" parameter to case and retrived boolean parameter

$query.get("valid@bool");

?valid "1" "true" is going to give true in the end. Anything else is false

You can combing multiple parameters in one query and use $body as well.

$query.get("valid@required('I need this stuff.'),bool");

min && max

First argument is the amount, second (optional) is a custom message.

var name = $body.get("info.name@min(5),max(10, 'Custom message')");

ETags

Creating eTags

Domain path supports eTags with convenient services;

To start working with eTag, add eTag property to your rest end point

domain.path("/:lang?", {
	eTag: 'landing-$lang',
	get: function($query, $assert) {
		return {
			hello: "world"
		}
	}
})

eTag name will be parser and formed based on url query or URL parameters per se. so, requesting /en will create an eTag called 'landing-en'

Updating eTags

To update an eTag, simply require eTag service

domain.path("/update", {
	get: function($eTag) {
		return $eTag.generate('landing-en');
	}
});

$eTag.generate('landing-en') will generate new eTag!

1.6.73

9 years ago

1.6.72

9 years ago

1.6.71

9 years ago

1.6.7

9 years ago

1.6.6

9 years ago

1.6.5

9 years ago

1.6.4

9 years ago

1.6.3

9 years ago

1.6.2

9 years ago

1.6.1

9 years ago

1.5.9

9 years ago

1.5.8

9 years ago

1.5.7

9 years ago

1.5.6

9 years ago

1.5.5

9 years ago

1.5.4

9 years ago

1.5.3

9 years ago

1.5.2

10 years ago

1.5.1

10 years ago

1.5.0

10 years ago

1.4.9

10 years ago

1.4.8

10 years ago

1.4.7

10 years ago

1.4.6

10 years ago

1.4.5

10 years ago

1.4.4

10 years ago

1.4.3

10 years ago

1.4.2

10 years ago

1.4.0

10 years ago

1.3.0

10 years ago

1.2.9

10 years ago

1.2.8

10 years ago

1.2.7

10 years ago

1.2.6

10 years ago

1.2.5

10 years ago

1.2.4

10 years ago

1.2.3

10 years ago

1.2.2

10 years ago

1.2.1

10 years ago

1.2.0

10 years ago

1.1.9

10 years ago

1.1.8

10 years ago

1.1.7

10 years ago

1.1.6

10 years ago

1.1.5

10 years ago

1.1.4

10 years ago

1.1.3

10 years ago

1.1.2

10 years ago

1.1.1

10 years ago

1.0.9

10 years ago

1.0.8

10 years ago

1.0.7

10 years ago

1.0.6

10 years ago

1.0.5

10 years ago

1.0.4

10 years ago

1.0.3

10 years ago

1.0.2

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago

0.0.80

10 years ago

0.0.5

11 years ago

0.0.2

11 years ago

0.0.1

11 years ago

0.0.0

11 years ago