From fc5684eef7322bc4816401e2f4c1415bbd7b17a8 Mon Sep 17 00:00:00 2001 From: Rumen Russanov Date: Wed, 22 Jan 2020 16:22:26 +0200 Subject: [PATCH 1/3] Add ability to pass Prefer in order to get specific respose --- lib/route.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/route.js b/lib/route.js index 85c41ac..4daa6b6 100644 --- a/lib/route.js +++ b/lib/route.js @@ -25,12 +25,21 @@ exports.getRouteHandlers = function (method, parsedUrl, action) { logger.log('[DRAKOV]'.red, action.method.green, parsedUrl.uriTemplate.yellow, (this.request && this.request.description ? this.request.description : action.name).blue); - this.response.headers.forEach(function (header) { + const pair = (req.headers.prefer || '').split('='); //split status=200 + let response = this.responses[0]; + if (pair.length == 2 && pair[0] === 'status') { + const preferResponse = this.responses.find(({name}) => name === pair[1]); + if (preferResponse) { + response = preferResponse; + } + } + + response.headers.forEach(function(header) { res.set(header.name, header.value); }); - res.status(+this.response.name); - res.send(buildResponseBody(this.response.body)); + res.status(+response.name); + res.send(buildResponseBody(response.body)); } }; }); From 2ba57b2ad70ee7fcaf34def717cbe11b70cb8f44 Mon Sep 17 00:00:00 2001 From: Rumen Russanov Date: Wed, 22 Jan 2020 16:25:59 +0200 Subject: [PATCH 2/3] expose responses --- lib/route.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/route.js b/lib/route.js index 4daa6b6..7e1e9ac 100644 --- a/lib/route.js +++ b/lib/route.js @@ -6,7 +6,7 @@ exports.getRouteHandlers = function (method, parsedUrl, action) { return { action: action, parsedUrl: parsedUrl, - response: example.responses[0], + responses: example.responses, request: 'undefined' === typeof example.requests[0] ? null : specSchema.validateAndParseSchema(example.requests[0]), execute: function (req, res) { var buildResponseBody = function(specBody){ From 64120c309126e683e6dd4000e66f2ef4c0c79778 Mon Sep 17 00:00:00 2001 From: Rumen Russanov Date: Wed, 22 Jan 2020 16:36:10 +0200 Subject: [PATCH 3/3] fix jshint --- lib/route.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/route.js b/lib/route.js index 7e1e9ac..16dfdfb 100644 --- a/lib/route.js +++ b/lib/route.js @@ -25,10 +25,12 @@ exports.getRouteHandlers = function (method, parsedUrl, action) { logger.log('[DRAKOV]'.red, action.method.green, parsedUrl.uriTemplate.yellow, (this.request && this.request.description ? this.request.description : action.name).blue); - const pair = (req.headers.prefer || '').split('='); //split status=200 - let response = this.responses[0]; - if (pair.length == 2 && pair[0] === 'status') { - const preferResponse = this.responses.find(({name}) => name === pair[1]); + var pair = (req.headers.prefer || '').split('='); //split status=200 + var response = this.responses[0]; + if (pair.length === 2 && pair[0] === 'status') { + var preferResponse = this.responses.find(function (res) { + return res.name === pair[1]; + }); if (preferResponse) { response = preferResponse; }