2014-07-05 19:32:00 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
|
|
|
|
2014-07-11 18:22:18 +00:00
|
|
|
var debug = require('debug')('loopback:explorer:routeHelpers');
|
2014-12-03 03:09:48 +00:00
|
|
|
var _cloneDeep = require('lodash').cloneDeep;
|
|
|
|
var _assign = require('lodash').assign;
|
2014-07-10 17:50:38 +00:00
|
|
|
var modelHelper = require('./model-helper');
|
2014-10-15 11:54:21 +00:00
|
|
|
var typeConverter = require('./type-converter');
|
2014-07-05 19:32:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Export the routeHelper singleton.
|
|
|
|
*/
|
|
|
|
var routeHelper = module.exports = {
|
|
|
|
/**
|
2014-07-10 17:50:38 +00:00
|
|
|
* Given a route, generate an API description and add it to the doc.
|
|
|
|
* If a route shares a path with another route (same path, different verb),
|
|
|
|
* add it as a new operation under that API description.
|
|
|
|
*
|
2014-07-05 19:32:00 +00:00
|
|
|
* Routes can be translated to API declaration 'operations',
|
|
|
|
* but they need a little massaging first. The `accepts` and
|
|
|
|
* `returns` declarations need some basic conversions to be compatible.
|
|
|
|
*
|
|
|
|
* This method will convert the route and add it to the doc.
|
|
|
|
* @param {Route} route Strong Remoting Route object.
|
|
|
|
* @param {Class} classDef Strong Remoting class.
|
|
|
|
* @param {Object} doc The class's backing API declaration doc.
|
|
|
|
*/
|
|
|
|
addRouteToAPIDeclaration: function (route, classDef, doc) {
|
2014-07-10 17:50:38 +00:00
|
|
|
var api = routeHelper.routeToAPIDoc(route, classDef);
|
|
|
|
var matchingAPIs = doc.apis.filter(function(existingAPI) {
|
|
|
|
return existingAPI.path === api.path;
|
|
|
|
});
|
|
|
|
if (matchingAPIs.length) {
|
|
|
|
matchingAPIs[0].operations.push(api.operations[0]);
|
|
|
|
} else {
|
|
|
|
doc.apis.push(api);
|
2014-07-05 19:32:00 +00:00
|
|
|
}
|
2014-07-10 17:50:38 +00:00
|
|
|
},
|
2014-07-05 19:32:00 +00:00
|
|
|
|
2014-07-10 17:50:38 +00:00
|
|
|
/**
|
|
|
|
* Massage route.accepts.
|
|
|
|
* @param {Object} route Strong Remoting Route object.
|
|
|
|
* @param {Class} classDef Strong Remoting class.
|
2014-07-11 14:03:58 +00:00
|
|
|
* @return {Array} Array of param docs.
|
2014-07-10 17:50:38 +00:00
|
|
|
*/
|
2014-07-11 14:03:58 +00:00
|
|
|
convertAcceptsToSwagger: function convertAcceptsToSwagger(route, classDef) {
|
2014-07-10 17:50:38 +00:00
|
|
|
var split = route.method.split('.');
|
2014-07-11 14:03:58 +00:00
|
|
|
var accepts = _cloneDeep(route.accepts) || [];
|
2014-07-10 17:50:38 +00:00
|
|
|
if (classDef && classDef.sharedCtor &&
|
|
|
|
classDef.sharedCtor.accepts && split.length > 2 /* HACK */) {
|
2014-07-11 14:03:58 +00:00
|
|
|
accepts = accepts.concat(classDef.sharedCtor.accepts);
|
2014-07-05 19:32:00 +00:00
|
|
|
}
|
|
|
|
|
2014-07-10 17:50:38 +00:00
|
|
|
// Filter out parameters that are generated from the incoming request,
|
|
|
|
// or generated by functions that use those resources.
|
2014-07-11 14:03:58 +00:00
|
|
|
accepts = accepts.filter(function(arg){
|
2014-07-10 17:50:38 +00:00
|
|
|
if (!arg.http) return true;
|
|
|
|
// Don't show derived arguments.
|
|
|
|
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().
|
2014-10-14 01:57:39 +00:00
|
|
|
if (arg.http.source === 'req' ||
|
|
|
|
arg.http.source === 'res' ||
|
|
|
|
arg.http.source === 'context') {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2014-07-10 17:50:38 +00:00
|
|
|
return true;
|
|
|
|
});
|
2014-07-05 19:32:00 +00:00
|
|
|
|
2014-07-11 14:03:58 +00:00
|
|
|
// Turn accept definitions in to parameter docs.
|
|
|
|
accepts = accepts.map(routeHelper.acceptToParameter(route));
|
|
|
|
|
|
|
|
return accepts;
|
2014-07-10 17:50:38 +00:00
|
|
|
},
|
2014-07-05 19:32:00 +00:00
|
|
|
|
2014-07-10 17:50:38 +00:00
|
|
|
/**
|
|
|
|
* Massage route.returns.
|
|
|
|
* @param {Object} route Strong Remoting Route object.
|
|
|
|
* @param {Class} classDef Strong Remoting class.
|
2014-07-11 14:03:58 +00:00
|
|
|
* @return {Object} A single returns param doc.
|
2014-07-10 17:50:38 +00:00
|
|
|
*/
|
2014-07-11 14:03:58 +00:00
|
|
|
convertReturnsToSwagger: function convertReturnsToSwagger(route, classDef) {
|
|
|
|
var routeReturns = _cloneDeep(route.returns) || [];
|
2014-07-10 17:50:38 +00:00
|
|
|
// HACK: makes autogenerated REST routes return the correct model name.
|
2014-07-11 14:03:58 +00:00
|
|
|
var firstReturn = routeReturns && routeReturns[0];
|
|
|
|
if (firstReturn && firstReturn.arg === 'data') {
|
|
|
|
if (firstReturn.type === 'object') {
|
|
|
|
firstReturn.type = classDef.name;
|
|
|
|
} else if (firstReturn.type === 'array') {
|
2014-07-18 22:23:51 +00:00
|
|
|
firstReturn.type = [classDef.name];
|
2014-07-10 17:50:38 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-05 19:32:00 +00:00
|
|
|
|
2014-10-15 11:58:13 +00:00
|
|
|
// Convert `returns` into a single object for later conversion into an
|
2014-07-10 17:50:38 +00:00
|
|
|
// operation object.
|
2014-10-15 11:58:13 +00:00
|
|
|
if (routeReturns && routeReturns.length > 1) {
|
2014-07-10 17:50:38 +00:00
|
|
|
// TODO ad-hoc model definition in the case of multiple return values.
|
2014-10-15 11:58:13 +00:00
|
|
|
routeReturns = { type: 'object' };
|
2014-07-10 17:50:38 +00:00
|
|
|
} else {
|
2014-10-15 11:58:13 +00:00
|
|
|
// 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' };
|
2014-07-10 17:50:38 +00:00
|
|
|
}
|
2014-07-05 19:32:00 +00:00
|
|
|
|
2014-10-15 11:58:13 +00:00
|
|
|
return routeReturns;
|
2014-07-10 17:50:38 +00:00
|
|
|
},
|
2014-07-05 19:32:00 +00:00
|
|
|
|
2014-07-10 17:50:38 +00:00
|
|
|
/**
|
|
|
|
* Converts from an sl-remoting-formatted "Route" description to a
|
|
|
|
* Swagger-formatted "API" description.
|
|
|
|
* See https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md#523-operation-object
|
|
|
|
*/
|
|
|
|
routeToAPIDoc: function routeToAPIDoc(route, classDef) {
|
2014-10-15 11:58:13 +00:00
|
|
|
// Some parameters need to be altered; eventually most of this should
|
2014-07-10 17:50:38 +00:00
|
|
|
// be removed.
|
2014-07-11 14:03:58 +00:00
|
|
|
var accepts = routeHelper.convertAcceptsToSwagger(route, classDef);
|
|
|
|
var returns = routeHelper.convertReturnsToSwagger(route, classDef);
|
2014-07-10 17:50:38 +00:00
|
|
|
|
|
|
|
debug('route %j', route);
|
|
|
|
|
2014-10-14 11:42:09 +00:00
|
|
|
var responseDoc = modelHelper.LDLPropToSwaggerDataType(returns);
|
|
|
|
|
|
|
|
var responseMessages = [{
|
|
|
|
code: route.returns && route.returns.length ? 200 : 204,
|
2014-12-12 12:10:45 +00:00
|
|
|
message: 'Request was successful'
|
2014-10-14 11:42:09 +00:00
|
|
|
}];
|
|
|
|
|
|
|
|
if (route.errors) {
|
|
|
|
responseMessages.push.apply(responseMessages, route.errors);
|
|
|
|
}
|
|
|
|
|
2014-07-10 17:50:38 +00:00
|
|
|
var apiDoc = {
|
|
|
|
path: routeHelper.convertPathFragments(route.path),
|
2014-10-14 11:42:09 +00:00
|
|
|
// Create the operation doc.
|
2014-12-12 12:10:45 +00:00
|
|
|
// 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({
|
2014-07-10 17:50:38 +00:00
|
|
|
method: routeHelper.convertVerb(route.verb),
|
2014-11-10 11:10:50 +00:00
|
|
|
// [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(/.*?\./, ''),
|
2014-07-11 14:03:58 +00:00
|
|
|
parameters: accepts,
|
2014-10-14 11:42:09 +00:00
|
|
|
responseMessages: responseMessages,
|
2014-10-15 11:54:21 +00:00
|
|
|
summary: typeConverter.convertText(route.description),
|
|
|
|
notes: typeConverter.convertText(route.notes),
|
2014-10-13 14:32:12 +00:00
|
|
|
deprecated: route.deprecated
|
2014-12-12 12:10:45 +00:00
|
|
|
}, returns)]
|
2014-07-10 17:50:38 +00:00
|
|
|
};
|
2014-07-21 01:04:10 +00:00
|
|
|
|
|
|
|
return apiDoc;
|
2014-07-10 17:50:38 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
convertPathFragments: function convertPathFragments(path) {
|
|
|
|
return path.split('/').map(function (fragment) {
|
|
|
|
if (fragment.charAt(0) === ':') {
|
|
|
|
return '{' + fragment.slice(1) + '}';
|
|
|
|
}
|
|
|
|
return fragment;
|
|
|
|
}).join('/');
|
|
|
|
},
|
|
|
|
|
|
|
|
convertVerb: function convertVerb(verb) {
|
|
|
|
if (verb.toLowerCase() === 'all') {
|
|
|
|
return 'POST';
|
2014-07-05 19:32:00 +00:00
|
|
|
}
|
|
|
|
|
2014-07-10 17:50:38 +00:00
|
|
|
if (verb.toLowerCase() === 'del') {
|
|
|
|
return 'DELETE';
|
2014-07-05 19:32:00 +00:00
|
|
|
}
|
|
|
|
|
2014-07-10 17:50:38 +00:00
|
|
|
return verb.toUpperCase();
|
|
|
|
},
|
2014-07-05 19:32:00 +00:00
|
|
|
|
2014-07-10 17:50:38 +00:00
|
|
|
/**
|
|
|
|
* A generator to convert from an sl-remoting-formatted "Accepts" description
|
|
|
|
* to a Swagger-formatted "Parameter" description.
|
|
|
|
*/
|
|
|
|
acceptToParameter: function acceptToParameter(route) {
|
|
|
|
var type = 'form';
|
|
|
|
|
|
|
|
if (route.verb.toLowerCase() === 'get') {
|
|
|
|
type = 'query';
|
2014-07-05 19:32:00 +00:00
|
|
|
}
|
|
|
|
|
2014-07-10 17:50:38 +00:00
|
|
|
return function (accepts) {
|
|
|
|
var name = accepts.name || accepts.arg;
|
|
|
|
var paramType = type;
|
|
|
|
|
|
|
|
// TODO: Regex. This is leaky.
|
|
|
|
if (route.path.indexOf(':' + name) !== -1) {
|
|
|
|
paramType = 'path';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the http settings for the argument
|
|
|
|
if(accepts.http && accepts.http.source) {
|
|
|
|
paramType = accepts.http.source;
|
|
|
|
}
|
|
|
|
|
|
|
|
var out = {
|
|
|
|
paramType: paramType || type,
|
|
|
|
name: name,
|
2014-10-15 11:54:21 +00:00
|
|
|
description: typeConverter.convertText(accepts.description),
|
2014-07-10 17:50:38 +00:00
|
|
|
required: !!accepts.required,
|
|
|
|
allowMultiple: false
|
2014-07-05 19:32:00 +00:00
|
|
|
};
|
|
|
|
|
2014-10-15 11:58:13 +00:00
|
|
|
out = routeHelper.extendWithType(out, accepts);
|
2014-07-05 19:32:00 +00:00
|
|
|
|
2014-07-10 17:50:38 +00:00
|
|
|
// HACK: Derive the type from model
|
|
|
|
if(out.name === 'data' && out.type === 'object') {
|
|
|
|
out.type = route.method.split('.')[0];
|
|
|
|
}
|
2014-07-05 19:32:00 +00:00
|
|
|
|
2014-07-10 17:50:38 +00:00
|
|
|
return out;
|
|
|
|
};
|
|
|
|
},
|
2014-07-05 19:32:00 +00:00
|
|
|
|
2014-07-10 17:50:38 +00:00
|
|
|
/**
|
2014-10-15 11:58:13 +00:00
|
|
|
* Extends an Operation Object or Parameter object with
|
2014-07-10 17:50:38 +00:00
|
|
|
* a proper Swagger type and optional `format` and `items` fields.
|
|
|
|
* Does not modify original object.
|
|
|
|
* @param {Object} obj Object to extend.
|
2014-10-15 11:58:13 +00:00
|
|
|
* @param {Object} ldlType LDL type definition
|
|
|
|
* @return {Object} Extended object.
|
2014-07-10 17:50:38 +00:00
|
|
|
*/
|
2014-10-15 11:58:13 +00:00
|
|
|
extendWithType: function extendWithType(obj, ldlType) {
|
2014-07-10 17:50:38 +00:00
|
|
|
obj = _cloneDeep(obj);
|
|
|
|
|
|
|
|
// Format the `type` property using our LDL converter.
|
2014-10-15 11:58:13 +00:00
|
|
|
var typeDesc = modelHelper.LDLPropToSwaggerDataType(ldlType);
|
|
|
|
|
2014-07-10 17:50:38 +00:00
|
|
|
// The `typeDesc` may have additional attributes, such as
|
|
|
|
// `format` for non-primitive types.
|
2014-10-15 11:58:13 +00:00
|
|
|
_assign(obj, typeDesc);
|
|
|
|
|
2014-07-10 17:50:38 +00:00
|
|
|
return obj;
|
2014-07-05 19:32:00 +00:00
|
|
|
}
|
2014-07-10 17:50:38 +00:00
|
|
|
};
|
2014-07-05 19:32:00 +00:00
|
|
|
|
|
|
|
|