Add url-join so path.join() doesn't break windows

This commit is contained in:
Samuel Reed 2014-07-10 13:49:09 -05:00
parent 781ac2bd12
commit 75713f16f2
5 changed files with 19 additions and 9 deletions

View File

@ -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
});

View File

@ -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)
};

View File

@ -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

8
lib/url-join.js Normal file
View File

@ -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, '/');
};

View File

@ -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) {