0.0.1 • Published 7 years ago

http-infer v0.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

http-infer

Convert a simple return value from a route function into a description of a http response. The response is of the format accepted by http-tell.

infer(value) -> {
	// headers
	code,
	message,
	headers,

	// body
	json,
	raw,
};

Return values are interpreted as:

  • undefined will by translated into a 204 (No content) http response
  • Any object with a code property will be thought to be a valid response definition and is therefore returned as is
  • Any other value will be wrapped as { code: 200, json: value }

Examples

var infer = require('http-infer');
var tell = require('http-tell');

http.createServer(function( request, response ){
	tell(response, infer())
		.then(() => console.log('Completed a 200 request without content'));
})
	.listen(8000);

Streaming JSON:

var infer = require('http-infer');
var tell = require('http-tell');
var pull = require('pull-stream');

http.createServer(function( request, response ){
	tell(response, infer(pull.values([ 'a', 'b', 'c' ])))
		.then(() => console.log('Streamed all values to the client'));
})
	.listen(8000);

"Pass through" objects with a code

var infer = require('http-infer');
var tell = require('http-tell');

http.createServer(function( request, response ){
	tell(response, infer({
		code: 400,
		json: { error: 'Something bad' },
	}))
		.then(() => console.log('Send a 400 response'));
})
	.listen(8000);