diff --git a/.gitignore b/.gitignore index 3e5ac87..5e487e2 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,9 @@ lib-cov *.out *.pid *.gz +.idea +*.iml +*.tgz .idea pids diff --git a/CHANGES.md b/CHANGES.md new file mode 100644 index 0000000..62327b5 --- /dev/null +++ b/CHANGES.md @@ -0,0 +1,313 @@ +2015-02-23, Version 1.7.1 +========================= + + * Remove unused external font "Droid Sans". (Miroslav Bajtoš) + + +2015-02-17, Version 1.7.0 +========================= + + * Made API doc of class use the http.path of the class if available, or the name of the class as a fallback (gandrianakis) + + +2015-01-09, Version 1.6.4 +========================= + + * Prevent double slash in the resource URLs (Miroslav Bajtoš) + + * Allow `uiDirs` to be defined as a String (Simon Ho) + + * Save accessToken in localStorage. Fixes #47 (Samuel Reed) + + +2015-01-06, Version 1.6.3 +========================= + + * Fix bad CLA URL in CONTRIBUTING.md (Ryan Graham) + + * Add X-UA-Compatible tag (Nick Van Dyck) + + +2014-12-12, Version 1.6.2 +========================= + + * Move 200 response to `type` on the operation object. See #75. (Samuel Reed) + + +2014-12-08, Version 1.6.1 +========================= + + * Use full lodash instead of lodash components (Ryan Graham) + + +2014-12-02, Version 1.6.0 +========================= + + * Remove model name from nickname, swagger spec understands op context. (Samuel Reed) + + +2014-11-29, Version 1.5.2 +========================= + + * model-helper: ignore unknown property types (Miroslav Bajtoš) + + +2014-10-24, Version 1.5.1 +========================= + + + +2014-10-24, Version 1.5.0 +========================= + + * Add an option `uiDirs` (Miroslav Bajtoš) + + * swagger: honour X-Forwarded-Proto header (Miroslav Bajtoš) + + +2014-10-21, Version 1.4.0 +========================= + + * Bump version (Raymond Feng) + + * Add integration tests for included models (Miroslav Bajtoš) + + * route-helper: add `responseMessages` (Miroslav Bajtoš) + + * model-helper: support anonymous object types (Miroslav Bajtoš) + + * swagger: include models from accepts/returns args (Miroslav Bajtoš) + + * loopbackStyles: improve spacing in small window (Miroslav Bajtoš) + + * swagger: Deprecate `opts.swaggerVersion` (Miroslav Bajtoš) + + * swagger: use X-Forwarded-Host for basePath (Miroslav Bajtoš) + + * example: use PersistedModel instead of Model (Miroslav Bajtoš) + + * models: include model's `description` (Miroslav Bajtoš) + + * Refactor conversion of data types (Miroslav Bajtoš) + + * Move `convertText` to `typeConverter` (Miroslav Bajtoš) + + * Add support for `context` and `res` param types (Krishna Raman) + + * package: update devDependencies (Miroslav Bajtoš) + + * gitignore: add .idea, *.tgz, *.iml (Miroslav Bajtoš) + + * Support multi-line array `description` and `notes` (Miroslav Bajtoš) + + * Use `1.0.0` as the default app version. (Miroslav Bajtoš) + + * Extend `consumes` and `produces` metadata (Miroslav Bajtoš) + + * route-helper: include `notes` and `deprecated` (Miroslav Bajtoš) + + * Pull model description from ctor.settings first (Shelby Sanders) + + +2014-10-08, Version 1.3.0 +========================= + + * swagger: allow cross-origin requests (Miroslav Bajtoš) + + * Sort endpoints by letter. (Samuel Reed) + + * Add syntax highlighting styles & highlight threshold. (Samuel Reed) + + * Add contribution guidelines (Ryan Graham) + + +2014-09-22, Version 1.2.11 +========================== + + * Bump version (Raymond Feng) + + * Fix how the array of models is iterated (Raymond Feng) + + +2014-09-05, Version 1.2.10 +========================== + + * Bump version (Raymond Feng) + + * Make sure nested/referenced models in array are mapped to swagger (Clark Wang) + + * Make sure nested/referenced models are mapped to swagger (Raymond Feng) + + +2014-08-15, Version 1.2.9 +========================= + + * Bump version (Raymond Feng) + + * Newest Swagger UI requires application/x-www-form-urlencoded. (Samuel Reed) + + * Use `dist` property from swagger-ui package. (Samuel Reed) + + * Fixed undefined modelClass when using polymorphic relations (Navid Nikpour) + + +2014-08-08, Version 1.2.8 +========================= + + * Bump version (Raymond Feng) + + * Fix the type name for a property if model class is used (Raymond Feng) + + +2014-08-04, Version 1.2.7 +========================= + + * Bump version (Raymond Feng) + + * Set up default consumes/produces media types (Raymond Feng) + + * Fix the default opts (Raymond Feng) + + * Add required swagger 1.2 items property for property type array (Ritchie Martori) + + * Allow passing a custom protocol. (Samuel Reed) + + +2014-07-29, Version 1.2.6 +========================= + + * Bump version (Raymond Feng) + + * res.send deprecated - updated to res.status (Geoffroy) + + * Remove hidden properties from definition. (Samuel Reed) + + +2014-07-25, Version 1.2.5 +========================= + + * Bump version (Raymond Feng) + + * Ensure models from relations are included (Raymond Feng) + + +2014-07-22, Version 1.2.4 +========================= + + * model-helper: handle arrays with undefined items (Miroslav Bajtoš) + + +2014-07-22, Version 1.2.3 +========================= + + * model-helper: handle array types with no item type (Miroslav Bajtoš) + + +2014-07-20, Version 1.2.2 +========================= + + * Bump version (Raymond Feng) + + * Properly convert complex return types. (Samuel Reed) + + +2014-07-18, Version 1.2.1 +========================= + + * Bump version (Raymond Feng) + + * Fix up loopback.rest() model definition hack. (Samuel Reed) + + +2014-07-14, Version 1.2.0 +========================= + + * Bump version and update deps (Raymond Feng) + + * s/accessToken/access_token in authorization key name (Samuel Reed) + + * Fix resources if the explorer is at a deep path. (Samuel Reed) + + * Fix debug namespace, express version. (Samuel Reed) + + * Remove forgotten TODO. (Samuel Reed) + + * Simplify `accepts` and `returns` hacks. (Samuel Reed) + + * More consise type tests (Samuel Reed) + + * Remove preMiddleware. (Samuel Reed) + + * Remove swagger.test.js license (Samuel Reed) + + * Remove peerDependencies, use express directly. (Samuel Reed) + + * Add url-join so path.join() doesn't break windows (Samuel Reed) + + * Rename translateKeys to translateDataTypeKeys. (Samuel Reed) + + * Refactor route-helper & add tests. (Samuel Reed) + + * LDL to Swagger fixes & extensions. (Samuel Reed) + + * Use express routes instead of modifying remoting. (Samuel Reed) + + * Fix missing strong-remoting devDependency. (Samuel Reed) + + * Restore existing styles. (Samuel Reed) + + * Allow easy setting of accessToken in explorer UI. (Samuel Reed) + + * Refactor key translations between LDL & Swagger. (Samuel Reed) + + * Refactoring swagger 1.2 rework. (Samuel Reed) + + * Make sure body parameter is shown. (Raymond Feng) + + * Some swagger 1.2 migration cleanup. (Samuel Reed) + + * Fix api resource path and type ref to models. (Raymond Feng) + + * Swagger 1.2 compatability. Moved strong-remoting/ext/swagger to this module. (Samuel Reed) + + * Load swagger ui from `swagger-ui` package instead. (Samuel Reed) + + +2014-05-28, Version 1.1.1 +========================= + + * package.json: add support for loopback 2.x (Miroslav Bajtoš) + + * Make sure X-Powered-By header is disabled (Alex Pica) + + * Fix license url (Raymond Feng) + + * Update to dual MIT/StrongLoop license (Raymond Feng) + + +2014-01-14, Version 1.1.0 +========================= + + * Bump up loopback min version to 1.5 (Miroslav Bajtoš) + + * Use `app.get('restApiRoot')` as default basePath (Miroslav Bajtoš) + + * Replace strong-remoting ext/swagger with app.docs (Miroslav Bajtoš) + + +2014-01-13, Version 1.0.2 +========================= + + * Bump version (Raymond Feng) + + * README: mount REST at /api in the sample code (Miroslav Bajtos) + + * Reorder middleware to fix unit-test failures. (Miroslav Bajtos) + + * Fix loading of loopback dependencies. (Miroslav Bajtos) + + +2013-12-04, Version 1.0.1 +========================= + + * First release! diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ed649b0..f170f19 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -14,7 +14,7 @@ Contributing to `loopback-explorer` is easy. In a few simple steps: * Adhere to code style outlined in the [Google C++ Style Guide][] and [Google Javascript Style Guide][]. - * Sign the [Contributor License Agreement](https://cla.strongloop.com/strongloop/loopback-explorer) + * Sign the [Contributor License Agreement](https://cla.strongloop.com/agreements/strongloop/loopback-explorer) * Submit a pull request through Github. diff --git a/README.md b/README.md index 882e104..9ef49eb 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ app.listen(port); ## Advanced Usage -Many aspects of the explorer are configurable. +Many aspects of the explorer are configurable. See [options](#options) for a description of these options: @@ -34,7 +34,10 @@ See [options](#options) for a description of these options: app.use('/explorer', loopback.basicAuth('user', 'password')); app.use('/explorer', explorer(app, { basePath: '/custom-api-root', - swaggerDistRoot: '/swagger', + uiDirs: [ + path.resolve(__dirname, 'public'), + path.resolve(__dirname, 'node_modules', 'swagger-ui') + ] apiInfo: { 'title': 'My API', 'description': 'Explorer example app.' @@ -67,27 +70,27 @@ Options are passed to `explorer(app, options)`. > and thus needs to report its endpoints as `https`, even though incoming traffic is auto-detected > as `http`. -`swaggerDistRoot`: **String** +`uiDirs`: **Array of Strings** -> Sets a path within your application for overriding Swagger UI files. +> Sets a list of paths within your application for overriding Swagger UI files. -> If present, will search `swaggerDistRoot` first when attempting to load Swagger UI, allowing -> you to pick and choose overrides to the interface. Use this to style your explorer or -> add additional functionality. +> If present, will search `uiDirs` first when attempting to load Swagger UI, +> allowing you to pick and choose overrides to the interface. Use this to +> style your explorer or add additional functionality. > See [index.html](public/index.html), where you may want to begin your overrides. > The rest of the UI is provided by [Swagger UI](https://github.com/wordnik/swagger-ui). `apiInfo`: **Object** -> Additional information about your API. See the +> Additional information about your API. See the > [spec](https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md#513-info-object). `resourcePath`: **String** > Default: `'resources'` -> Sets a different path for the +> Sets a different path for the > [resource listing](https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md#51-resource-listing). > You generally shouldn't have to change this. diff --git a/example/simple.js b/example/simple.js index 3705ca5..70937cd 100644 --- a/example/simple.js +++ b/example/simple.js @@ -3,7 +3,7 @@ var app = loopback(); var explorer = require('../'); var port = 3000; -var Product = loopback.Model.extend('product', { +var Product = loopback.PersistedModel.extend('product', { foo: {type: 'string', required: true}, bar: 'string', aNum: {type: 'number', min: 1, max: 10, required: true, default: 5} @@ -14,6 +14,6 @@ app.model(Product); var apiPath = '/api'; app.use('/explorer', explorer(app, {basePath: apiPath})); app.use(apiPath, loopback.rest()); -console.log('Explorer mounted at localhost:' + port + '/explorer'); +console.log('Explorer mounted at http://localhost:' + port + '/explorer'); app.listen(port); diff --git a/index.js b/index.js index 6c5e003..6048c3a 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,7 @@ var url = require('url'); var path = require('path'); var urlJoin = require('./lib/url-join'); -var _defaults = require('lodash.defaults'); +var _defaults = require('lodash').defaults; var express = require('express'); var swagger = require('./lib/swagger'); var SWAGGER_UI_ROOT = require('swagger-ui').dist; @@ -47,15 +47,29 @@ function explorer(loopbackApplication, options) { }); }); - // Allow specifying a static file root for swagger files. Any files in - // that folder will override those in the swagger-ui distribution. - // In this way one could e.g. make changes to index.html without having + // Allow specifying a static file roots for swagger files. Any files in + // these folders will override those in the swagger-ui distribution. + // In this way one could e.g. make changes to index.html without having // to worry about constantly pulling in JS updates. + if (options.uiDirs) { + if (typeof options.uiDirs === 'string') { + app.use(express.static(options.uiDirs)); + } else if (Array.isArray(options.uiDirs)) { + options.uiDirs.forEach(function(dir) { + app.use(express.static(dir)); + }); + } + } + if (options.swaggerDistRoot) { + console.warn('loopback-explorer: `swaggerDistRoot` is deprecated,' + + ' use `uiDirs` instead'); app.use(express.static(options.swaggerDistRoot)); } + // File in node_modules are overridden by a few customizations app.use(express.static(STATIC_ROOT)); + // Swagger UI distribution app.use(express.static(SWAGGER_UI_ROOT)); diff --git a/lib/class-helper.js b/lib/class-helper.js index e060d98..761b018 100644 --- a/lib/class-helper.js +++ b/lib/class-helper.js @@ -4,6 +4,7 @@ * Module dependencies. */ var modelHelper = require('./model-helper'); +var typeConverter = require('./type-converter'); var urlJoin = require('./url-join'); /** @@ -22,11 +23,16 @@ var classHelper = module.exports = { * @return {Object} API Declaration. */ generateAPIDoc: function(aClass, opts) { + var resourcePath = urlJoin('/', aClass.name); + if(aClass.http && aClass.http.path) { + resourcePath = aClass.http.path; + } + return { apiVersion: opts.version || '1', swaggerVersion: opts.swaggerVersion, basePath: opts.basePath, - resourcePath: urlJoin('/', opts.resourcePath), + resourcePath: urlJoin('/', resourcePath), apis: [], consumes: aClass.http.consumes || opts.consumes, produces: aClass.http.produces || opts.produces, @@ -41,10 +47,12 @@ var classHelper = module.exports = { * @return {Object} API declaration reference. */ generateResourceDocAPIEntry: function(aClass) { - var description = aClass.ctor.settings.description || aClass.ctor.sharedCtor && aClass.ctor.sharedCtor.description; + var description = aClass.ctor.settings.description || + aClass.ctor.sharedCtor && aClass.ctor.sharedCtor.description; + return { path: aClass.http.path, - description: Array.isArray(description) ? description.join('') : description + description: typeConverter.convertText(description) }; } }; diff --git a/lib/model-helper.js b/lib/model-helper.js index 733ab66..a3921fc 100644 --- a/lib/model-helper.js +++ b/lib/model-helper.js @@ -3,9 +3,11 @@ /** * Module dependencies. */ -var _cloneDeep = require('lodash.clonedeep'); +var _cloneDeep = require('lodash').cloneDeep; +var _pick = require('lodash').pick; var translateDataTypeKeys = require('./translate-data-type-keys'); var TYPES_PRIMITIVE = ['array', 'boolean', 'integer', 'number', 'null', 'object', 'string', 'any']; +var typeConverter = require('./type-converter'); /** * Export the modelHelper singleton. @@ -39,8 +41,15 @@ var modelHelper = module.exports = { }; var def = modelClass.definition; - var name = def.name; var out = definitions || {}; + + if (!def) { + // The model does not have any definition, it was most likely + // created as a placeholder for an unknown property type + return out; + } + + var name = def.name; if (out[name]) { // The model is already included return out; @@ -77,13 +86,17 @@ var modelHelper = module.exports = { } // Eke a type out of the constructors we were passed. - prop = modelHelper.LDLPropToSwaggerDataType(prop); - processType(modelClass.app, prop.type, referencedModels); - if (prop.items) { - processType(modelClass.app, prop.items.type, referencedModels); - convertTypeTo$Ref(prop.items); + var swaggerType = modelHelper.LDLPropToSwaggerDataType(prop); + processType(modelClass.app, swaggerType.type, referencedModels); + convertTypeTo$Ref(swaggerType); + if (swaggerType.items) { + processType(modelClass.app, swaggerType.items.type, referencedModels); + convertTypeTo$Ref(swaggerType.items); } + var desc = typeConverter.convertText(prop.description || prop.doc); + if (desc) swaggerType.description = desc; + // Required props sit in a per-model array. if (prop.required || (prop.id && !prop.generated)) { required.push(key); @@ -92,19 +105,17 @@ var modelHelper = module.exports = { // Change mismatched keys. prop = translateDataTypeKeys(prop); - convertTypeTo$Ref(prop); - delete prop.required; delete prop.id; if (prop.description){ - prop.description = Array.isArray(prop.description) ? prop.description.join('') : prop.description; + prop.description = typeConverter.convertText(prop.description); } // Assign this back to the properties object. - properties[key] = prop; + properties[key] = swaggerType; - var propType = def.properties[key].type; + var propType = prop.type; if (typeof propType === 'function' && propType.modelName) { if (referencedModels.indexOf(propType) === -1) { referencedModels.push(propType); @@ -133,12 +144,13 @@ var modelHelper = module.exports = { out[name] = { id: name, additionalProperties: additionalProperties, + description: typeConverter.convertText(def.description), properties: properties, required: required }; if (def.description){ - out[name].description = Array.isArray(def.description) ? def.description.join('') : def.description; + out[name].description = typeConverter.convertText(def.description); } // Generate model definitions for related models @@ -195,9 +207,12 @@ var modelHelper = module.exports = { if (typeof propType === 'function') { // See https://github.com/strongloop/loopback-explorer/issues/32 // The type can be a model class - propType = propType.modelName || propType.name.toLowerCase(); - } else if(Array.isArray(propType)) { - propType = 'array'; + return propType.modelName || propType.name.toLowerCase(); + } else if (Array.isArray(propType)) { + return 'array'; + } else if (typeof propType === 'object') { + // Anonymous objects, they are allowed e.g. in accepts/returns definitions + return 'object'; } return propType; }, @@ -211,38 +226,51 @@ var modelHelper = module.exports = { // Converts a prop defined with the LDL spec to one conforming to the // Swagger spec. // https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md#431-primitives - LDLPropToSwaggerDataType: function LDLPropToSwaggerDataType(prop) { - if (typeof prop === 'string') { - prop = { - type: prop - } - } - var out = _cloneDeep(prop); - out.type = modelHelper.getPropType(out.type); + LDLPropToSwaggerDataType: function LDLPropToSwaggerDataType(ldlType) { + var SWAGGER_DATA_TYPE_FIELDS = [ + 'format', + 'defaultValue', + 'enum', + 'minimum', + 'maximum', + 'uniqueItems', + // loopback-explorer extensions + 'length', + // https://www.npmjs.org/package/swagger-validation + 'pattern' + ]; - if (out.type === 'array') { - var hasItemType = Array.isArray(prop.type) && prop.type.length; - var arrayItem = hasItemType && prop.type[0]; + // Rename LoopBack keys to Swagger keys + ldlType = translateDataTypeKeys(ldlType); + + // Pick only keys supported by Swagger + var swaggerType = _pick(ldlType, SWAGGER_DATA_TYPE_FIELDS); + + swaggerType.type = modelHelper.getPropType(ldlType.type || ldlType); + + if (swaggerType.type === 'array') { + var hasItemType = Array.isArray(ldlType.type) && ldlType.type.length; + var arrayItem = hasItemType && ldlType.type[0]; if (arrayItem) { if(typeof arrayItem === 'object') { - out.items = modelHelper.LDLPropToSwaggerDataType(arrayItem); + swaggerType.items = modelHelper.LDLPropToSwaggerDataType(arrayItem); } else { - out.items = { type: modelHelper.getPropType(arrayItem) }; + swaggerType.items = { type: modelHelper.getPropType(arrayItem) }; } } else { // NOTE: `any` is not a supported type in swagger 1.2 - out.items = { type: 'any' }; + swaggerType.items = { type: 'any' }; } - } else if (out.type === 'date') { - out.type = 'string'; - out.format = 'date'; - } else if (out.type === 'buffer') { - out.type = 'string'; - out.format = 'byte'; - } else if (out.type === 'number') { - out.format = 'double'; // Since all JS numbers are doubles + } else if (swaggerType.type === 'date') { + swaggerType.type = 'string'; + swaggerType.format = 'date'; + } else if (swaggerType.type === 'buffer') { + swaggerType.type = 'string'; + swaggerType.format = 'byte'; + } else if (swaggerType.type === 'number') { + swaggerType.format = 'double'; // Since all JS numbers are doubles } - return out; + return swaggerType; } }; \ No newline at end of file diff --git a/lib/route-helper.js b/lib/route-helper.js index c615d93..32e041d 100644 --- a/lib/route-helper.js +++ b/lib/route-helper.js @@ -5,9 +5,10 @@ */ var debug = require('debug')('loopback:explorer:routeHelpers'); -var _cloneDeep = require('lodash.clonedeep'); -var translateDataTypeKeys = require('./translate-data-type-keys'); +var _cloneDeep = require('lodash').cloneDeep; +var _assign = require('lodash').assign; var modelHelper = require('./model-helper'); +var typeConverter = require('./type-converter'); /** * Export the routeHelper singleton. @@ -61,13 +62,15 @@ var routeHelper = module.exports = { if (typeof arg.http === 'function') return false; // Don't show arguments set to the incoming http request. // Please note that body needs to be shown, such as User.create(). - if (arg.http.source === 'req') return false; + if (arg.http.source === 'req' || + arg.http.source === 'res' || + arg.http.source === 'context') { + + return false; + } return true; }); - // Translate LDL keys to Swagger keys. - accepts = accepts.map(translateDataTypeKeys); - // Turn accept definitions in to parameter docs. accepts = accepts.map(routeHelper.acceptToParameter(route)); @@ -92,19 +95,19 @@ var routeHelper = module.exports = { } } - // Translate LDL keys to Swagger keys. - var returns = routeReturns.map(translateDataTypeKeys); - - // Convert `returns` into a single object for later conversion into an + // Convert `returns` into a single object for later conversion into an // operation object. - if (returns && returns.length > 1) { + if (routeReturns && routeReturns.length > 1) { // TODO ad-hoc model definition in the case of multiple return values. - returns = {model: 'object'}; + routeReturns = { type: 'object' }; } else { - returns = returns[0] || {}; + // 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'. + routeReturns = routeReturns[0] || { type: 'void' }; } - return returns; + return routeReturns; }, /** @@ -181,23 +184,38 @@ var routeHelper = module.exports = { debug('route %j', route); + var responseDoc = modelHelper.LDLPropToSwaggerDataType(returns); + + var responseMessages = [{ + code: route.returns && route.returns.length ? 200 : 204, + message: 'Request was successful' + }]; + + if (route.errors) { + responseMessages.push.apply(responseMessages, route.errors); + } + var apiDoc = { path: routeHelper.convertPathFragments(route.path), - // Create the operation doc. Use `extendWithType` to add the necessary - // `items` and `format` fields. + // Create the operation doc. + // We are using extendWithType to use `type` for the top-level (200) + // response type. We use responseModels for error responses. + // see https://github.com/strongloop/loopback-explorer/issues/75 operations: [routeHelper.extendWithType({ method: routeHelper.convertVerb(route.verb), - // [rfeng] Swagger UI doesn't escape '.' for jQuery selector - nickname: route.method.replace(/\./g, '_'), + // [strml] remove leading model name from op, swagger uses leading + // path as class name so it remains unique between models. + // route.method is always #{className}.#{methodName} + nickname: route.method.replace(/.*?\./, ''), deprecated: route.deprecated, - type: returns.model || returns.type || 'void', - summary: Array.isArray(route.description) ? route.description.join('') : route.description, // TODO(schoon) - Excerpt? - notes: Array.isArray(route.notes) ? route.notes.join('') : 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 - })] + responseMessages: responseMessages, + type: returns.model || returns.type || 'void', + summary: typeConverter.convertText(route.description), + notes: typeConverter.convertText(route.notes) + }, returns)] }; return apiDoc; @@ -264,10 +282,10 @@ var routeHelper = module.exports = { minimum: accepts.minimum, maximum: accepts.maximum, allowMultiple: accepts.allowMultiple, - description: Array.isArray(accepts.description) ? accepts.description.join('') : accepts.description + description: typeConverter.convertText(accepts.description) }; - out = routeHelper.extendWithType(out); + out = routeHelper.extendWithType(out, accepts); // HACK: Derive the type from model if(out.name === 'data' && out.type === 'object') { @@ -279,38 +297,41 @@ var routeHelper = module.exports = { }, /** - * Extends an Operation Object or Parameter object with + * Extends an Operation Object or Parameter object with * a proper Swagger type and optional `format` and `items` fields. * Does not modify original object. * @param {Object} obj Object to extend. - * @return {Object} Extended object. + * @param {Object} ldlType LDL type definition + * @return {Object} Extended object. */ - extendWithType: function extendWithType(obj) { + extendWithType: function extendWithType(obj, ldlType) { obj = _cloneDeep(obj); // Format the `type` property using our LDL converter. - var typeDesc = modelHelper - .LDLPropToSwaggerDataType({type: obj.model || obj.type}); + var typeDesc = modelHelper.LDLPropToSwaggerDataType(ldlType); + // The `typeDesc` may have additional attributes, such as // `format` for non-primitive types. Object.keys(typeDesc).forEach(function(key){ obj[key] = typeDesc[key]; }); - //Ensure brief properties are first - if (typeof obj === 'object') { - var keysToSink = ['authorizations', 'consumes', 'notes', 'produces', - 'parameters', 'responseMessages', 'summary']; - var outKeys = Object.keys(obj); - for (var outKeyIdx in outKeys) { - var outKey = outKeys[outKeyIdx]; - if (keysToSink.indexOf(outKey) != -1) { - var outValue = obj[outKey]; - delete obj[outKey]; - obj[outKey] = outValue; - } - } - } + //Ensure brief properties are first + if (typeof obj === 'object') { + var keysToSink = ['authorizations', 'consumes', 'notes', 'produces', + 'parameters', 'responseMessages', 'summary']; + var outKeys = Object.keys(obj); + for (var outKeyIdx in outKeys) { + var outKey = outKeys[outKeyIdx]; + if (keysToSink.indexOf(outKey) != -1) { + var outValue = obj[outKey]; + delete obj[outKey]; + obj[outKey] = outValue; + } + } + } + + _assign(obj, typeDesc); return obj; } diff --git a/lib/swagger.js b/lib/swagger.js index 553cddb..908b155 100644 --- a/lib/swagger.js +++ b/lib/swagger.js @@ -9,10 +9,13 @@ module.exports = Swagger; */ var path = require('path'); var urlJoin = require('./url-join'); -var _defaults = require('lodash.defaults'); +var _defaults = require('lodash').defaults; var classHelper = require('./class-helper'); var routeHelper = require('./route-helper'); -var _cloneDeep = require('lodash.clonedeep'); +var _cloneDeep = require('lodash').cloneDeep; +var modelHelper = require('./model-helper'); +var cors = require('cors'); +var typeConverter = require('./type-converter'); /** * Create a remotable Swagger module for plugging into `RemoteObjects`. @@ -23,13 +26,25 @@ var _cloneDeep = require('lodash.clonedeep'); * @param {Object} opts Options. */ function Swagger(loopbackApplication, swaggerApp, opts) { + if (opts && opts.swaggerVersion) + console.warn('loopback-explorer\'s options.swaggerVersion is deprecated.'); + opts = _defaults(opts || {}, { swaggerVersion: '1.2', basePath: loopbackApplication.get('restApiRoot') || '/api', resourcePath: 'resources', // Default consumes/produces - consumes: ['application/json', 'application/x-www-form-urlencoded', 'application/xml', 'text/xml'], - produces: ['application/json', 'application/javascript', 'application/xml', 'text/javascript', 'text/xml'], + consumes: [ + 'application/json', + 'application/x-www-form-urlencoded', + 'application/xml', 'text/xml' + ], + produces: [ + 'application/json', + 'application/xml', 'text/xml', + // JSONP content types + 'application/javascript', 'text/javascript' + ], version: getVersion() }); @@ -39,6 +54,8 @@ function Swagger(loopbackApplication, swaggerApp, opts) { var routes = adapter.allRoutes(); var classes = remotes.classes(); + setupCors(swaggerApp, remotes); + // These are the docs we will be sending from the /swagger endpoints. var resourceDoc = generateResourceDoc(opts); var apiDocs = {}; @@ -46,15 +63,15 @@ function Swagger(loopbackApplication, swaggerApp, opts) { // A class is an endpoint root; e.g. /users, /products, and so on. classes.forEach(function (aClass) { var doc = apiDocs[aClass.name] = classHelper.generateAPIDoc(aClass, opts); - var hasPublic = false; + var hasDocumented = false; var methods = aClass.methods() for (var methodKey in methods) { - hasPublic = methods[methodKey].public; - if (hasPublic) { + hasDocumented = methods[methodKey].documented; + if (hasDocumented) { break; } } - if (hasPublic) { + if (hasDocumented) { resourceDoc.apis.push(classHelper.generateResourceDocAPIEntry(aClass)); } @@ -77,14 +94,59 @@ function Swagger(loopbackApplication, swaggerApp, opts) { return item.name === className; })[0]; - if (route.public) { + if (route.documented) { routeHelper.addRouteToAPIDeclaration(route, classDef, doc); } }); + // Add models referenced from routes (e.g. accepts/returns) + Object.keys(apiDocs).forEach(function(className) { + var classDoc = apiDocs[className]; + classDoc.apis.forEach(function(api) { + api.operations.forEach(function(routeDoc) { + routeDoc.parameters.forEach(function(param) { + var type = param.type; + if (type === 'array' && param.items) + type = param.items.type; + + addTypeToModels(type); + }); + + if (routeDoc.type === 'array') { + addTypeToModels(routeDoc.items.type); + } else { + addTypeToModels(routeDoc.type); + } + + routeDoc.responseMessages.forEach(function(msg) { + addTypeToModels(msg.responseModel); + }); + + function addTypeToModels(name) { + if (!name || name === 'void') return; + + var model = loopbackApplication.models[name]; + if (!model) { + var loopback = loopbackApplication.loopback; + if (!loopback) return; + + if (loopback.findModel) { + model = loopback.findModel(name); // LoopBack 2.x + } else { + model = loopback.getModel(name); // LoopBack 1.x + } + } + if (!model) return; + + modelHelper.generateModelDefinition(model, classDoc.models); + } + }); + }); + }); + /** - * The topmost Swagger resource is a description of all (non-Swagger) - * resources available on the system, and where to find more + * The topmost Swagger resource is a description of all (non-Swagger) + * resources available on the system, and where to find more * information about them. */ addRoute(swaggerApp, opts.resourcePath, resourceDoc, opts); @@ -92,6 +154,13 @@ function Swagger(loopbackApplication, swaggerApp, opts) { loopbackApplication.emit('swaggerResources', resourceDoc); } +function setupCors(swaggerApp, remotes) { + var corsOptions = remotes.options && remotes.options.cors || + { origin: true, credentials: true }; + + swaggerApp.use(cors(corsOptions)); +} + /** * Add a route to this remoting extension. * @param {Application} app Express application. @@ -103,21 +172,28 @@ function addRoute(app, uri, doc, opts) { var hasBasePath = Object.keys(doc).indexOf('basePath') !== -1; var initialPath = doc.basePath || ''; + // Remove the trailing slash, see + // https://github.com/strongloop/loopback-explorer/issues/48 + if (initialPath[initialPath.length-1] === '/') + initialPath = initialPath.slice(0, -1); + app.get(urlJoin('/', uri), function(req, res) { // There's a few forces at play that require this "hack". The Swagger spec - // requires a `basePath` to be set in the API descriptions. However, we - // can't guarantee this path is either reachable or desirable if it's set + // requires a `basePath` to be set in the API descriptions. However, we + // can't guarantee this path is either reachable or desirable if it's set // as a part of the options. - // - // The simplest way around this is to reflect the value of the `Host` HTTP - // header as the `basePath`. Because we pre-build the Swagger data, we don't - // know that header at the time the data is built. + // + // The simplest way around this is to reflect the value of the `Host` and/or + // `X-Forwarded-Host` HTTP headers as the `basePath`. + // Because we pre-build the Swagger data, we don't know that header at + // the time the data is built. if (hasBasePath) { var headers = req.headers; - var host = headers['x-forwarded-host'] || headers['X-Forwarded-Host'] || headers.Host || headers.host; - var protocol = headers['x-forwarded-proto'] || headers['X-Forwarded-Proto'] || opts.protocol || req.protocol - doc.basePath = protocol + '://' + host + initialPath; + // NOTE header names (keys) are always all-lowercase + var proto = headers['x-forwarded-proto'] || opts.protocol || req.protocol; + var host = headers['x-forwarded-host'] || headers.host; + doc.basePath = proto + '://' + host + initialPath; } res.status(200).send(doc); }); @@ -133,7 +209,7 @@ function generateResourceDoc(opts) { var apiInfo = _cloneDeep(opts.apiInfo); for (var propertyName in apiInfo) { var property = apiInfo[propertyName]; - apiInfo[propertyName] = Array.isArray(property) ? property.join('') : property; + apiInfo[propertyName] = typeConverter.convertText(property); } return { @@ -159,7 +235,7 @@ function getVersion() { try { version = require(path.join(process.cwd(), 'package.json')).version; } catch(e) { - version = '1'; + version = '1.0.0'; } return version; } diff --git a/lib/translate-data-type-keys.js b/lib/translate-data-type-keys.js index a47c85d..d2ca63a 100644 --- a/lib/translate-data-type-keys.js +++ b/lib/translate-data-type-keys.js @@ -4,12 +4,11 @@ * Module dependencies. */ -var _cloneDeep = require('lodash.clonedeep'); +var _cloneDeep = require('lodash').cloneDeep; // Keys that are different between LDL and Swagger var KEY_TRANSLATIONS = { // LDL : Swagger - 'doc': 'description', 'default': 'defaultValue', 'min': 'minimum', 'max': 'maximum' diff --git a/lib/type-converter.js b/lib/type-converter.js new file mode 100644 index 0000000..c7eb686 --- /dev/null +++ b/lib/type-converter.js @@ -0,0 +1,14 @@ +var typeConverter = module.exports = { + + /** + * Convert a text value that can be expressed either as a string or + * as an array of strings. + * @param {string|Array} value + * @returns {string} + */ + convertText: function(value) { + if (Array.isArray(value)) + return value.join('\n'); + return value; + } +}; diff --git a/package.json b/package.json index 707673a..bf457ea 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "loopback-explorer", - "version": "1.2.13", + "version": "1.7.1", "description": "Browse and test your LoopBack app's APIs", "main": "index.js", "scripts": { @@ -21,20 +21,20 @@ "url": "https://github.com/strongloop/loopback-explorer/issues" }, "devDependencies": { - "loopback": "git+https://github.com/shelbys/loopback.git", - "mocha": "~1.20.1", - "supertest": "~0.13.0", - "chai": "~1.9.1" + "loopback": "git+https://github.com/shelbys/loopback.git#validate_param_enum", + "mocha": "^2.2.5", + "supertest": "~1.0.1", + "chai": "^2.3.0" }, "license": { "name": "Dual MIT/StrongLoop", "url": "https://github.com/strongloop/loopback-explorer/blob/master/LICENSE" }, "dependencies": { - "swagger-ui": "git+https://github.com/shelbys/swagger-ui.git", + "cors": "^2.4.2", "debug": "~1.0.3", - "lodash.clonedeep": "^2.4.1", - "lodash.defaults": "^2.4.1", - "express": "3.x" + "express": "3.x", + "lodash": "^2.4.1", + "swagger-ui": "git+https://github.com/shelbys/swagger-ui.git" } } diff --git a/public/css/loopbackStyles.css b/public/css/loopbackStyles.css index 53a6283..babf1a7 100644 --- a/public/css/loopbackStyles.css +++ b/public/css/loopbackStyles.css @@ -34,6 +34,7 @@ color: #080; } +<<<<<<< HEAD .contentWell { padding-left: 30px; padding-right: 30px; @@ -42,4 +43,14 @@ /* FIXME: Separate the overrides from the rest of the styles, rather than override screen.css entirely. */ +======= +/* Improve spacing when the browser window is small */ +#message-bar, #swagger-ui-container { + padding-left: 30px; + padding-right: 30px; +} +>>>>>>> upstream/master +#api_selector { + padding: 0px 20px; +} diff --git a/public/index.html b/public/index.html index cae33f7..5696c68 100644 --- a/public/index.html +++ b/public/index.html @@ -1,30 +1,29 @@
-