express-mung
Advanced tools
Comparing version 0.5.0 to 0.5.1
13
index.js
@@ -50,6 +50,6 @@ 'use strict'; | ||
// If scalar value, then text/plain | ||
if (isScalar(json)) { | ||
// If munged scalar value, then text/plain | ||
if (originalJson !== json && isScalar(json)) { | ||
res.set('content-type', 'text/plain'); | ||
return res.send(json); | ||
return res.send(String(json)); | ||
} | ||
@@ -72,2 +72,3 @@ | ||
function json_async_hook (json) { | ||
let originalJson = json; | ||
res.json = original; | ||
@@ -88,6 +89,6 @@ if (res.headersSent) | ||
// If scalar value, then text/plain | ||
if (isScalar(json)) { | ||
// If munged scalar value, then text/plain | ||
if (json !== originalJson && isScalar(json)) { | ||
res.set('content-type', 'text/plain'); | ||
return res.send(json); | ||
return res.send(String(json)); | ||
} | ||
@@ -94,0 +95,0 @@ |
{ | ||
"name": "express-mung", | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"description": "Transform an express response (or make until no good)", | ||
@@ -22,4 +22,4 @@ "main": "index.js", | ||
"contributors": [ | ||
"Ido Schachter <ido.schachter@ironsrc.com>", | ||
"Sachin Hegde <sachin.hegde91@gmail.com>" | ||
"Ido Schachter <ido.schachter@ironsrc.com>", | ||
"Sachin Hegde <sachin.hegde91@gmail.com>" | ||
], | ||
@@ -32,10 +32,9 @@ "license": "MIT", | ||
"devDependencies": { | ||
"bluebird": "^3.0.5", | ||
"express": "^4.13.3", | ||
"mocha": "^3.2.0", | ||
"should": "^11.1.1", | ||
"supertest": "^3.0.0" | ||
"bluebird": "^3.5.1", | ||
"express": "^4.16.3", | ||
"mocha": "^5.2.0", | ||
"should": "^13.2.3", | ||
"supertest": "^3.1.0" | ||
}, | ||
"dependencies": { | ||
} | ||
"dependencies": {} | ||
} |
@@ -5,3 +5,3 @@ # express-mung [![Build Status](https://travis-ci.org/richardschneider/express-prefer.svg)](https://travis-ci.org/richardschneider/express-mung) | ||
This package allows synchronous and asynchronous transformation of an express response. This is a similar concept to the express middleware for a request but for a response. Note that the middleware is executed in LIFO order. It is implemented by monkey patching (hooking) the `res.end` or `res.json` methods. | ||
This package allows synchronous and asynchronous transformation of an express response. This is a similar concept to the express middleware for a request but for a response. Note that the middleware is executed in LIFO order. It is implemented by monkey patching (hooking) the `res.end`, `res.json`, or `res.write` methods. | ||
@@ -73,3 +73,3 @@ | ||
`fn(chunk, encoding, req, res)` receives the string or buffer as `chunk`, its `encoding` if applicable (`null` otherwise), `req` and `res`. It returns the modified body. If `undefined` is returned (i.e. nothing) then the original unmodified chunk is used. If `null` is returned, then a 204 No Content HTTP status is returned to client. | ||
`fn(chunk, encoding, req, res)` receives the string or buffer as `chunk`, its `encoding` if applicable (`null` otherwise), `req` and `res`. It returns the modified body. If `undefined` is returned (i.e. nothing) then the original unmodified chunk is used. | ||
@@ -84,2 +84,6 @@ ### Notes | ||
* when `mung.write` detects that a response has completed (i.e. if `res.end` has been called), it will abort. | ||
* calling `res.json` or `res.send` from `mung.write` can lead to unexpected behavior since they end the response internally. | ||
### options | ||
@@ -86,0 +90,0 @@ |
@@ -10,2 +10,5 @@ 'use strict'; | ||
function noop (json, req, res) { | ||
} | ||
function inspect (json, req, res) { | ||
@@ -22,2 +25,6 @@ json.inspected_by = 'me' | ||
} | ||
function life (json, req, res) { | ||
return 42; | ||
} | ||
@@ -110,2 +117,30 @@ function error(json, req, res) { | ||
it('should return a munged number as text/plain', done => { | ||
let server = express() | ||
.use(mung.json(life)) | ||
.get('/', (req, res) => res.status(200).json("the meaning of life").end()); | ||
request(server) | ||
.get('/') | ||
.expect(200) | ||
.expect(res => { | ||
res.text.should.equal('42'); | ||
res.headers.should.have.property('content-type', 'text/plain; charset=utf-8'); | ||
}) | ||
.end(done); | ||
}); | ||
it('should return a number as application/json', done => { | ||
let server = express() | ||
.use(mung.json(noop)) | ||
.get('/', (req, res) => res.status(200).json(42).end()); | ||
request(server) | ||
.get('/') | ||
.expect(200) | ||
.expect(res => { | ||
res.text.should.equal('42'); | ||
res.headers.should.have.property('content-type', 'application/json; charset=utf-8'); | ||
}) | ||
.end(done); | ||
}); | ||
it('should abort if a response is sent', done => { | ||
@@ -112,0 +147,0 @@ function error (json, req, res) { |
@@ -11,2 +11,6 @@ 'use strict'; | ||
function noop (json, req, res) { | ||
return Promise.resolve(json); | ||
} | ||
function inspect (json, req, res) { | ||
@@ -30,2 +34,7 @@ return Promise.resolve(json) | ||
function life (json, req, res) { | ||
return Promise.resolve(json) | ||
.then(json => 42); | ||
} | ||
function error(json, req, res) { | ||
@@ -103,2 +112,30 @@ return Promise.resolve(json) | ||
it('should return a munged number as text/plain', done => { | ||
let server = express() | ||
.use(mung.jsonAsync(life)) | ||
.get('/', (req, res) => res.status(200).json("the meaning of life").end()); | ||
request(server) | ||
.get('/') | ||
.expect(200) | ||
.expect(res => { | ||
res.text.should.equal('42'); | ||
res.headers.should.have.property('content-type', 'text/plain; charset=utf-8'); | ||
}) | ||
.end(done); | ||
}); | ||
it('should return a number as application/json', done => { | ||
let server = express() | ||
.use(mung.jsonAsync(noop)) | ||
.get('/', (req, res) => res.status(200).json(42).end()); | ||
request(server) | ||
.get('/') | ||
.expect(200) | ||
.expect(res => { | ||
res.text.should.equal('42'); | ||
res.headers.should.have.property('content-type', 'application/json; charset=utf-8'); | ||
}) | ||
.end(done); | ||
}); | ||
it('should abort if a response is sent', done => { | ||
@@ -105,0 +142,0 @@ function error (json, req, res) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
31570
716
98