diff --git a/lib/class-helper.js b/lib/class-helper.js index de3a0a7..e6d22dd 100644 --- a/lib/class-helper.js +++ b/lib/class-helper.js @@ -23,7 +23,7 @@ var classHelper = module.exports = { */ generateAPIDoc: function(aClass, opts) { return { - apiVersion: opts.version, + apiVersion: opts.version || '1', swaggerVersion: opts.swaggerVersion, basePath: opts.basePath, resourcePath: urlJoin('/', opts.resourcePath), diff --git a/lib/route-helper.js b/lib/route-helper.js index 263bbd2..8f8c10e 100644 --- a/lib/route-helper.js +++ b/lib/route-helper.js @@ -119,6 +119,16 @@ var routeHelper = module.exports = { // be removed. var accepts = routeHelper.convertAcceptsToSwagger(route, classDef); var returns = routeHelper.convertReturnsToSwagger(route, classDef); + var responseMessages = [ + { + code: 200, + message: 'Request was successful', + responseModel: returns.model || prepareDataType(returns.type) || 'void' + } + ]; + if (route.errors) { + responseMessages.push.apply(responseMessages, route.errors); + } debug('route %j', route); @@ -128,17 +138,19 @@ var routeHelper = module.exports = { // `items` and `format` fields. operations: [routeHelper.extendWithType({ method: routeHelper.convertVerb(route.verb), + deprecated: route.deprecated, // [rfeng] Swagger UI doesn't escape '.' for jQuery selector nickname: route.method.replace(/\./g, '_'), // Per the spec: // https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md#523-operation-object // This is the only object that may have a type of 'void'. type: returns.model || returns.type || 'void', - parameters: accepts, - // TODO(schoon) - We don't have descriptions for this yet. - responseMessages: [], summary: route.description, // TODO(schoon) - Excerpt? - notes: '' // TODO(schoon) - `description` metadata? + notes: route.notes, // TODO(schoon) - `description` metadata? + consumes: ['application/json', 'application/xml', 'text/xml'], + produces: ['application/json', 'application/javascript', 'application/xml', 'text/javascript', 'text/xml'], + parameters: accepts, + responseMessages: responseMessages })] }; @@ -192,15 +204,21 @@ var routeHelper = module.exports = { } var out = { - paramType: paramType || type, name: name, - description: accepts.description, - type: accepts.type, required: !!accepts.required, + paramType: paramType || type, + type: accepts.type, + $ref: accepts.model, + items: accepts.items, + uniqueItems: accepts.uniqueItems, + format: accepts.format, + pattern: accepts.pattern, defaultValue: accepts.defaultValue, + enum: accepts.enum, minimum: accepts.minimum, maximum: accepts.maximum, - allowMultiple: false + allowMultiple: accepts.allowMultiple, + description: accepts.description }; out = routeHelper.extendWithType(out); diff --git a/lib/swagger.js b/lib/swagger.js index 2bea1a1..6084adb 100644 --- a/lib/swagger.js +++ b/lib/swagger.js @@ -100,7 +100,8 @@ function addRoute(app, uri, doc) { if (hasBasePath) { var headers = req.headers; var host = headers.Host || headers.host; - doc.basePath = req.protocol + '://' + host + initialPath; + var protocol = headers['x-forwarded-proto'] || headers['X-Forwarded-Proto'] || ctx.req.protocol + doc.basePath = protocol + '://' + host + initialPath; } res.status(200).send(doc); }); @@ -116,13 +117,14 @@ function generateResourceDoc(opts) { return { swaggerVersion: opts.swaggerVersion, apiVersion: opts.version, - apis: [], // See https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md#513-info-object - info: opts.apiInfo + info: opts.apiInfo, // TODO Authorizations // https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md#514-authorizations-object - // TODO Produces/Consumes - // https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md#52-api-declaration + consumes: ['application/json', 'application/xml', 'text/xml'], + produces: ['application/json', 'application/javascript', 'application/xml', 'text/javascript', 'text/xml'], + apis: [], + models: opts.models }; } @@ -135,7 +137,7 @@ function getVersion() { try { version = require(path.join(process.cwd(), 'package.json')).version; } catch(e) { - version = ''; + version = '1'; } return version; } diff --git a/package.json b/package.json index 05eb9fb..9184320 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "url": "https://github.com/strongloop/loopback-explorer/issues" }, "devDependencies": { - "loopback": "1.x", + "loopback": "git+https://github.com/shelbys/loopback.git#loopback-v2", "mocha": "~1.20.1", "supertest": "~0.13.0", "chai": "~1.9.1" @@ -31,7 +31,7 @@ "url": "https://github.com/strongloop/loopback-explorer/blob/master/LICENSE" }, "dependencies": { - "swagger-ui": "~2.0.18", + "swagger-ui": "git+https://github.com/shelbys/swagger-ui.git", "debug": "~1.0.3", "lodash.clonedeep": "^2.4.1", "lodash.defaults": "^2.4.1",