Comparing version 2.3.0 to 2.4.0
@@ -44,5 +44,6 @@ "use strict"; | ||
this._bodyResolved = false; | ||
const responseValidationEnabled = !!options.onResponseValidationError; | ||
this.req = req; | ||
this.origRes = res; | ||
this.res = new ExegesisResponseImpl_1.default(res); | ||
this.res = new ExegesisResponseImpl_1.default(res, responseValidationEnabled); | ||
this.api = api; | ||
@@ -49,0 +50,0 @@ this.options = options; |
@@ -15,3 +15,4 @@ /// <reference types="node" /> | ||
headersSent: boolean; | ||
constructor(res: http.ServerResponse); | ||
private _responseValidationEnabled; | ||
constructor(res: http.ServerResponse, responseValidationEnabled: boolean); | ||
setStatus(status: number): this; | ||
@@ -22,2 +23,3 @@ status(status: number): this; | ||
json(json: any): this; | ||
pureJson(json: any): this; | ||
setBody(body: any): this; | ||
@@ -24,0 +26,0 @@ set body(body: any); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
class ExegesisResponseImpl { | ||
constructor(res /* | http2.Http2ServerResponse */) { | ||
constructor(res /* | http2.Http2ServerResponse */, responseValidationEnabled) { | ||
this._body = undefined; | ||
@@ -13,2 +13,3 @@ this._afterController = false; | ||
this.connection = res.connection; | ||
this._responseValidationEnabled = responseValidationEnabled; | ||
} | ||
@@ -34,8 +35,18 @@ setStatus(status) { | ||
json(json) { | ||
// TODO: Provide an option to disable this so that we don't have to | ||
// stringify the content, then parse it again when we do response | ||
// validation. | ||
this.set('content-type', 'application/json').setBody(JSON.stringify(json)); | ||
this.set('content-type', 'application/json'); | ||
if (this._responseValidationEnabled) { | ||
// Must stringify here, since the object or any of it's | ||
// nested values could have a toJSON(). Note this means | ||
// we'll have to parse it again when we do validation. | ||
this.setBody(JSON.stringify(json)); | ||
} | ||
else { | ||
this.setBody(json); | ||
} | ||
return this; | ||
} | ||
pureJson(json) { | ||
this.set('content-type', 'application/json').setBody(json); | ||
return this; | ||
} | ||
setBody(body) { | ||
@@ -42,0 +53,0 @@ if (this.ended && !this._afterController) { |
@@ -66,2 +66,3 @@ "use strict"; | ||
const validator = this._responseValidators.get(contentType); | ||
const isJson = contentType.startsWith('application/json'); | ||
if (body === null || body === undefined) { | ||
@@ -83,8 +84,22 @@ return [ | ||
} | ||
else if (typeof body === 'string' && contentType.startsWith('application/json')) { | ||
if (body.trim() === '') { | ||
return validator(undefined).errors; | ||
else if (isJson) { | ||
if (body instanceof Buffer || typeUtils_1.isReadable(body)) { | ||
// Can't validate this. | ||
// TODO: Could probably parse the buffer. | ||
return null; | ||
} | ||
try { | ||
return validator(JSON.parse(body)).errors; | ||
let jsonData; | ||
if (typeof body === 'string') { | ||
if (body.trim() === '') { | ||
jsonData = undefined; | ||
} | ||
else { | ||
jsonData = JSON.parse(body); | ||
} | ||
} | ||
else { | ||
jsonData = body; | ||
} | ||
return validator(jsonData).errors; | ||
} | ||
@@ -91,0 +106,0 @@ catch (err) { |
@@ -33,3 +33,17 @@ /// <reference types="node" /> | ||
set(header: string, value: number | string | string[] | undefined): this; | ||
/** | ||
* Set the JSON content of the response. Note that this will call `JSON.stringify()` | ||
* immediately if response validation is enabled, because there may be `toJSON()` | ||
* functions on the object or any nested values (e.g. if some values are Mongoose objects). | ||
* This means we'll need to parse that string to do validation though. If you | ||
* know your object is a pure POJO, call `res.pureJson()` instead. | ||
*/ | ||
json(json: any): this; | ||
/** | ||
* Sets the JSON content of the response to the object provided. Note that | ||
* while `toJSON()` on the object or any child objects will be | ||
* respsected when the object is serialized, it will be ignored for purposes | ||
* of response validation. | ||
*/ | ||
pureJson(json: any): this; | ||
end(): void; | ||
@@ -36,0 +50,0 @@ setHeader(name: string, value: number | string | string[] | undefined): void; |
{ | ||
"name": "exegesis", | ||
"version": "2.3.0", | ||
"version": "2.4.0", | ||
"description": "Parses OpenAPI documents", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
305473
4742