From 9a81d2cb6308e3bd296bed78fce876538708ca3e Mon Sep 17 00:00:00 2001 From: Alan Plum Date: Mon, 12 Dec 2016 02:31:47 +0100 Subject: [PATCH] Add support for Swagger tags --- .../@arangodb/foxx/router/swagger-context.js | 27 +++++++++++++++++++ .../@arangodb/foxx/templates/dcRouter.js.tmpl | 3 +++ .../@arangodb/foxx/templates/ecRouter.js.tmpl | 4 +++ 3 files changed, 34 insertions(+) diff --git a/js/server/modules/@arangodb/foxx/router/swagger-context.js b/js/server/modules/@arangodb/foxx/router/swagger-context.js index 8144d0292d..79dd42b94c 100644 --- a/js/server/modules/@arangodb/foxx/router/swagger-context.js +++ b/js/server/modules/@arangodb/foxx/router/swagger-context.js @@ -49,6 +49,14 @@ const PARSED_JSON_MIME = (function (mime) { ])); }(MIME_JSON)); +const repeat = (times, value) => { + const arr = Array(times); + for (let i = 0; i < times; i++) { + arr[i] = value; + } + return arr; +}; + module.exports = exports = class SwaggerContext { constructor (path) { @@ -75,6 +83,7 @@ module.exports = exports = this._pathParams = new Map(); this._pathParamNames = []; this._pathTokens = tokenize(path, this); + this._tags = new Set(); } header (...args) { @@ -262,6 +271,18 @@ module.exports = exports = return this; } + tag (...tags) { + tags = check( + 'endpoint.tag', + tags, + [...repeat(Math.max(1, tags.length), ['tag', 'string'])] + ); + for (const tag of tags) { + this._tags.add(tag); + } + return this; + } + deprecated (...args) { const [flag] = check( 'endpoint.summary', @@ -284,6 +305,9 @@ module.exports = exports = for (const response of swaggerObj._responses.entries()) { this._responses.set(response[0], response[1]); } + for (const tag of swaggerObj._tags) { + this._tags.add(tag); + } if (!this._bodyParam && swaggerObj._bodyParam) { this._bodyParam = swaggerObj._bodyParam; } @@ -335,6 +359,9 @@ module.exports = exports = if (this._summary) { operation.summary = this._summary; } + if (this._tags) { + operation.tags = Array.from(this._tags); + } if (this._bodyParam) { operation.consumes = ( this._bodyParam.contentTypes diff --git a/js/server/modules/@arangodb/foxx/templates/dcRouter.js.tmpl b/js/server/modules/@arangodb/foxx/templates/dcRouter.js.tmpl index 1c3abd1f54..5e095d5de6 100644 --- a/js/server/modules/@arangodb/foxx/templates/dcRouter.js.tmpl +++ b/js/server/modules/@arangodb/foxx/templates/dcRouter.js.tmpl @@ -21,6 +21,9 @@ const router = createRouter(); module.exports = router; +router.tag('<%= document %>'); + + router.get(function (req, res) { res.send(<%= documents %>.all()); }, 'list') diff --git a/js/server/modules/@arangodb/foxx/templates/ecRouter.js.tmpl b/js/server/modules/@arangodb/foxx/templates/ecRouter.js.tmpl index 270101f539..37e485f390 100644 --- a/js/server/modules/@arangodb/foxx/templates/ecRouter.js.tmpl +++ b/js/server/modules/@arangodb/foxx/templates/ecRouter.js.tmpl @@ -20,6 +20,10 @@ const HTTP_CONFLICT = status('conflict'); const router = createRouter(); module.exports = router; + +router.tag('<%= document %>'); + + const New<%= model %> = Object.assign({}, <%= model %>, { schema: Object.assign({}, <%= model %>.schema, { _from: joi.string(),