express-promise
Advanced tools
| language: node_js | ||
| node_js: | ||
| - "0.10" | ||
| - "0.8" | ||
| - "0.6" |
| require('./spec_helper'); | ||
| var expressPromise = require('..'); | ||
| describe('render', function() { | ||
| it('should work well with one param', function(done) { | ||
| var res = { | ||
| render: function(view) { | ||
| arguments.should.have.length(1); | ||
| view.should.equal('index'); | ||
| done(); | ||
| } | ||
| }; | ||
| expressPromise({methods: ['render']})(null, res); | ||
| res.render('index'); | ||
| }); | ||
| it('should work well with two params and callback', function(done) { | ||
| var res = { | ||
| render: function(view, callback) { | ||
| arguments.should.have.length(2); | ||
| view.should.equal('index'); | ||
| callback().should.equal('test'); | ||
| done(); | ||
| } | ||
| }; | ||
| expressPromise({methods: ['render']})(null, res); | ||
| res.render('index', function() { | ||
| return 'test'; | ||
| }); | ||
| }); | ||
| it('should work well with two params and locals', function(done) { | ||
| var res = { | ||
| render: function(view, locals) { | ||
| arguments.should.have.length(2); | ||
| view.should.equal('index'); | ||
| locals.promise.should.equal('hi'); | ||
| done(); | ||
| } | ||
| }; | ||
| expressPromise({methods: ['render']})(null, res); | ||
| function async(callback) { | ||
| callback(null, 'hi'); | ||
| } | ||
| res.render('index', { | ||
| promise: async.promise() | ||
| }); | ||
| }); | ||
| it('should work well with three params', function(done) { | ||
| var res = { | ||
| render: function(view, locals, callback) { | ||
| arguments.should.have.length(3); | ||
| view.should.equal('index'); | ||
| locals.promise.should.equal('hi'); | ||
| callback().should.equal('test'); | ||
| done(); | ||
| } | ||
| }; | ||
| expressPromise({methods: ['render']})(null, res); | ||
| function async(callback) { | ||
| callback(null, 'hi'); | ||
| } | ||
| res.render('index', { | ||
| promise: async.promise() | ||
| }, function() { | ||
| return 'test'; | ||
| }); | ||
| }); | ||
| }); | ||
+71
| require('./spec_helper'); | ||
| var expressPromise = require('..'); | ||
| describe('send', function() { | ||
| it('should support string body', function(done) { | ||
| var res = { | ||
| send: function(body) { | ||
| arguments.should.have.length(1); | ||
| body.should.equal('hi'); | ||
| done(); | ||
| } | ||
| }; | ||
| expressPromise({methods: ['send']})(null, res); | ||
| res.send('hi'); | ||
| }); | ||
| it('should support buffer body', function(done) { | ||
| var res = { | ||
| send: function(body) { | ||
| arguments.should.have.length(1); | ||
| body.toString().should.equal('hi'); | ||
| done(); | ||
| } | ||
| }; | ||
| expressPromise({methods: ['send']})(null, res); | ||
| res.send(new Buffer('hi')); | ||
| }); | ||
| it('should support promise body', function(done) { | ||
| var res = { | ||
| send: function(body) { | ||
| arguments.should.have.length(1); | ||
| body.should.equal('hi'); | ||
| done(); | ||
| } | ||
| }; | ||
| expressPromise({methods: ['send']})(null, res); | ||
| function async(callback) { | ||
| callback(null, 'hi'); | ||
| } | ||
| res.send(async.promise()); | ||
| }); | ||
| it('should work well with two params with promise', function(done) { | ||
| var res = { | ||
| send: function(status, body) { | ||
| arguments.should.have.length(2); | ||
| status.should.equal(200); | ||
| body.promise.should.equal('hi'); | ||
| done(); | ||
| } | ||
| }; | ||
| expressPromise({methods: ['send']})(null, res); | ||
| function async(callback) { | ||
| callback(null, 'hi'); | ||
| } | ||
| res.send(200, { | ||
| promise: async.promise() | ||
| }); | ||
| }); | ||
| }); | ||
@@ -132,4 +132,4 @@ var resolveAsync = function(object, callback, maxDeep, currentDeep) { | ||
| if (~options.methods.indexOf('send')) { | ||
| var originalResSend = res.json.bind(res); | ||
| res.json = function() { | ||
| var originalResSend = res.send.bind(res); | ||
| res.send = function() { | ||
| var args = arguments; | ||
@@ -139,3 +139,3 @@ var body = args[0]; | ||
| if (2 === args.length) { | ||
| // res.json(body, status) backwards compat | ||
| // res.send(body, status) backwards compat | ||
| if ('number' === typeof args[1]) { | ||
@@ -142,0 +142,0 @@ status = args[1]; |
+1
-1
| { | ||
| "name": "express-promise", | ||
| "version": "0.1.3", | ||
| "version": "0.1.4", | ||
| "scripts": { | ||
@@ -5,0 +5,0 @@ "test": "mocha -R spec" |
+73
-43
| # express-promise | ||
| An [express.js](http://expressjs.com) middleware for easy rendering async query. | ||
| [](https://travis-ci.org/luin/express-promise) | ||
| ## Cases | ||
@@ -29,46 +31,74 @@ ### 1. previously | ||
| app.get('/project/:projectId', function(req, res) { | ||
| var field = req.query.fields.split(';'); | ||
| var result = {}; | ||
| var pending = 0; | ||
| if (field.indexOf('people') !== -1) { | ||
| pending++; | ||
| Project.getField(req.params.projectId).then(function(result) { | ||
| result.people = result; | ||
| if (--pending) { | ||
| output(); | ||
| } | ||
| }); | ||
| } | ||
| if (field.indexOf('tasks') !== -1) { | ||
| pending++; | ||
| Project.getTaskCount(req.params.projectId).then(function(result) { | ||
| result.tasksCount= result; | ||
| if (--pending) { | ||
| output(); | ||
| } | ||
| }); | ||
| } | ||
| function output() { | ||
| res.json(result); | ||
| } | ||
| }); | ||
| app.get('/project/:projectId', function(req, res) { | ||
| var field = req.query.fields.split(';'); | ||
| var result = {}; | ||
| var pending = 0; | ||
| if (field.indexOf('people') !== -1) { | ||
| pending++; | ||
| Project.getField(req.params.projectId).then(function(result) { | ||
| result.people = result; | ||
| if (--pending) { | ||
| output(); | ||
| } | ||
| }); | ||
| } | ||
| if (field.indexOf('tasks') !== -1) { | ||
| pending++; | ||
| Project.getTaskCount(req.params.projectId).then(function(result) { | ||
| result.tasksCount= result; | ||
| if (--pending) { | ||
| output(); | ||
| } | ||
| }); | ||
| } | ||
| function output() { | ||
| res.json(result); | ||
| } | ||
| }); | ||
| ### 2. now | ||
| app.get('/project/:projectId', function(req, res) { | ||
| var field = req.query.fields.split(';'); | ||
| var result = {}; | ||
| if (field.indexOf('people') !== -1) { | ||
| result.people = Project.getField(req.params.projectId); | ||
| } | ||
| if (field.indexOf('tasks') !== -1) { | ||
| result.tasksCount = Project.getTaskCount(req.params.projectId); | ||
| } | ||
| res.json(result); | ||
| }); | ||
| app.get('/project/:projectId', function(req, res) { | ||
| var field = req.query.fields.split(';'); | ||
| var result = {}; | ||
| if (field.indexOf('people') !== -1) { | ||
| result.people = Project.getField(req.params.projectId); | ||
| } | ||
| if (field.indexOf('tasks') !== -1) { | ||
| result.tasksCount = Project.getTaskCount(req.params.projectId); | ||
| } | ||
| res.json(result); | ||
| }); | ||
| ## Install | ||
| $ npm install express-promise | ||
| ## Usage | ||
| app.use(require('express-promise')()); | ||
| ## License | ||
| The MIT License (MIT) | ||
| Copyright (c) 2013 Zihua Li | ||
| 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. |
+21
-0
@@ -8,2 +8,3 @@ require('./spec_helper'); | ||
| json: function(body) { | ||
| arguments.should.have.length(1); | ||
| body.should.equal('hi'); | ||
@@ -21,2 +22,3 @@ done(); | ||
| json: function(body) { | ||
| arguments.should.have.length(1); | ||
| body.promise.should.equal('hi'); | ||
@@ -37,3 +39,22 @@ done(); | ||
| it('should support two arguments', function(done) { | ||
| var res = { | ||
| json: function(status, body) { | ||
| arguments.should.have.length(2); | ||
| status.should.equal(200); | ||
| body.promise.should.equal('hi'); | ||
| done(); | ||
| } | ||
| }; | ||
| expressPromise({methods: ['json']})(null, res); | ||
| function async(callback) { | ||
| callback(null, 'hi'); | ||
| } | ||
| res.json(200, { | ||
| promise: async.promise() | ||
| }); | ||
| }); | ||
| }); | ||
| describe('nested object', function() { | ||
| }); |
14732
55.81%12
20%359
63.93%104
40.54%