From 75713f16f22d2500468146e840ae762e110ccbe1 Mon Sep 17 00:00:00 2001 From: Samuel Reed Date: Thu, 10 Jul 2014 13:49:09 -0500 Subject: [PATCH] Add url-join so path.join() doesn't break windows --- index.js | 3 ++- lib/class-helper.js | 4 ++-- lib/swagger.js | 5 +++-- lib/url-join.js | 8 ++++++++ test/swagger.test.js | 8 ++++---- 5 files changed, 19 insertions(+), 9 deletions(-) create mode 100644 lib/url-join.js diff --git a/index.js b/index.js index 125c18c..b57f21f 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,7 @@ * Adds dynamically-updated docs as /explorer */ var path = require('path'); +var urlJoin = require('./lib/url-join'); var _defaults = require('lodash.defaults'); var extend = require('util')._extend; var loopback = require('loopback'); @@ -46,7 +47,7 @@ function explorer(loopbackApplication, options) { // with the relative URI of the resource doc. app.get('/config.json', function(req, res) { var resourcePath = req.originalUrl.replace(/\/config.json(\?.*)?$/, - path.join('/', options.resourcePath)); + urlJoin('/', options.resourcePath)); res.send({ url: resourcePath }); diff --git a/lib/class-helper.js b/lib/class-helper.js index e149ee9..ff788d9 100644 --- a/lib/class-helper.js +++ b/lib/class-helper.js @@ -4,7 +4,7 @@ * Module dependencies. */ var modelHelper = require('./model-helper'); -var path = require('path'); +var urlJoin = require('./url-join'); /** * Export the classHelper singleton. @@ -26,7 +26,7 @@ var classHelper = module.exports = { apiVersion: opts.version, swaggerVersion: opts.swaggerVersion, basePath: opts.basePath, - resourcePath: path.join('/', opts.resourcePath), + resourcePath: urlJoin('/', opts.resourcePath), apis: [], models: modelHelper.generateModelDefinition(aClass) }; diff --git a/lib/swagger.js b/lib/swagger.js index 834cc98..bc1bbe8 100644 --- a/lib/swagger.js +++ b/lib/swagger.js @@ -9,6 +9,7 @@ module.exports = Swagger; */ var debug = require('debug')('loopback-explorer:swagger'); var path = require('path'); +var urlJoin = require('./url-join'); var _defaults = require('lodash.defaults'); var classHelper = require('./class-helper'); var modelHelper = require('./model-helper'); @@ -46,7 +47,7 @@ function Swagger(loopbackApplication, swaggerApp, opts) { resourceDoc.apis.push(classHelper.generateResourceDocAPIEntry(aClass)); // Add the getter for this doc. - var docPath = path.join(opts.resourcePath, aClass.http.path); + var docPath = urlJoin(opts.resourcePath, aClass.http.path); addRoute(swaggerApp, docPath, doc); }); @@ -86,7 +87,7 @@ function addRoute(app, uri, doc) { var hasBasePath = Object.keys(doc).indexOf('basePath') !== -1; var initialPath = doc.basePath || ''; - app.get(path.join('/', uri), function(req, res) { + 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 diff --git a/lib/url-join.js b/lib/url-join.js new file mode 100644 index 0000000..4805191 --- /dev/null +++ b/lib/url-join.js @@ -0,0 +1,8 @@ +'use strict'; + +// Simple url joiner. Ensure we don't have to care about whether or not +// we are fed paths with leading/trailing slashes. +module.exports = function urlJoin() { + var args = Array.prototype.slice.call(arguments); + return args.join('/').replace(/\/+/g, '/'); +}; diff --git a/test/swagger.test.js b/test/swagger.test.js index 13221b3..0129570 100644 --- a/test/swagger.test.js +++ b/test/swagger.test.js @@ -20,10 +20,10 @@ */ var url = require('url'); -var path = require('path'); +var urlJoin = require('../lib/url-join'); var loopback = require('loopback'); var express = require('express'); -var swagger = require('../lib/swagger.js'); +var swagger = require('../lib/swagger'); var request = require('supertest'); var expect = require('chai').expect; @@ -115,14 +115,14 @@ describe('swagger definition', function() { function getSwaggerResources(app, restPath, classPath) { return request(app) - .get(path.join(restPath || '/explorer', '/resources', classPath || '')) + .get(urlJoin(restPath || '/explorer', '/resources', classPath || '')) .set('Accept', 'application/json') .expect('Content-Type', /json/) .expect(200); } function getAPIDeclaration(app, className) { - return getSwaggerResources(app, '', path.join('/', className)); + return getSwaggerResources(app, '', urlJoin('/', className)); } function mountSwagger(options, addlOptions) {