swagger-router
Advanced tools
Comparing version 0.0.4 to 0.1.0
59
index.js
"use strict"; | ||
var P = require('bluebird'); | ||
var url = require('url'); | ||
@@ -104,3 +105,5 @@ /*** | ||
this.params = params || {}; | ||
this.urlObj = null; | ||
if (uri && uri.constructor === URI) { | ||
this.urlObj = uri.urlObj; | ||
// this.path is considered immutable, so can be shared with other URI | ||
@@ -110,2 +113,11 @@ // instances | ||
} else if (uri && (uri.constructor === String || Array.isArray(uri))) { | ||
if (uri.constructor === String) { | ||
if (/^[^\/]+:/.test(uri)) { | ||
this.urlObj = url.parse(uri); | ||
// Work around encoding difference for {} between node 0.10 & | ||
// 0.12 / iojs. 0.10 leaves those chars as they are in .path, | ||
// newer node versions percent-encode them. | ||
uri = uri.substr(this.urlObj.resolve('/').length - 1); | ||
} | ||
} | ||
this.path = parsePath(uri, asPattern); | ||
@@ -121,11 +133,20 @@ } else if (uri !== '') { | ||
* @return {String} the complete path of this URI object | ||
* @param {string} format Either 'simplePattern' or 'fullPattern'. [optional] | ||
* @param {object} options { | ||
* format {string} Either 'simplePattern' or 'fullPattern'. [optional] | ||
* params {object} parameters to use during serialization | ||
* } | ||
* @return {string} URI path | ||
*/ | ||
URI.prototype.toString = function (format) { | ||
var uriStr = ''; | ||
URI.prototype.toString = function (options) { | ||
// b/c | ||
if (!options || options.constructor === String) { | ||
options = { format: options }; | ||
} | ||
var params = options.params || this.params; | ||
var uriStr = this.urlObj && this.urlObj.resolve('/').replace(/\/$/,'') | ||
|| ''; | ||
for (var i = 0; i < this.path.length; i++) { | ||
var segment = this.path[i]; | ||
if (segment && segment.constructor === Object) { | ||
var segmentValue = this.params[segment.name]; | ||
var segmentValue = params[segment.name]; | ||
if (segmentValue === undefined) { | ||
@@ -136,5 +157,9 @@ segmentValue = segment.pattern; | ||
if (segmentValue !== undefined) { | ||
if (!format || format === 'simplePattern' || !segment.name) { | ||
// Normal mode | ||
uriStr += '/' + encodeURIComponent(segmentValue); | ||
if (!options.format || options.format === 'simplePattern' || !segment.name) { | ||
if (segment.modifier === '+') { | ||
uriStr += '/' + segmentValue; | ||
} else { | ||
// Normal mode | ||
uriStr += '/' + encodeURIComponent(segmentValue); | ||
} | ||
} else { | ||
@@ -145,5 +170,5 @@ uriStr += '/{' + (segment.modifier || '') | ||
} | ||
} else if (format && !segment.modifier) { | ||
} else if (options.format && !segment.modifier) { | ||
uriStr += '/{' + encodeURIComponent(segment.name) + '}'; | ||
} else if (format) { | ||
} else if (options.format) { | ||
uriStr += '{' + (segment.modifier || '') | ||
@@ -170,5 +195,10 @@ + encodeURIComponent(segment.name) | ||
* Expand all parameters in the URI and return a new URI. | ||
* @param {object} params (optional) Parameters to use for expansion. Uses | ||
* URI-assigned parameters if not supplied. | ||
* @return {URI} | ||
*/ | ||
URI.prototype.expand = function() { | ||
URI.prototype.expand = function(params) { | ||
if (!params) { | ||
params = this.params; | ||
} | ||
var res = new Array(this.path.length); | ||
@@ -178,3 +208,3 @@ for (var i = 0; i < this.path.length; i++) { | ||
if (segment && segment.constructor === Object) { | ||
var segmentValue = this.params[segment.name]; | ||
var segmentValue = params[segment.name]; | ||
if (segmentValue === undefined) { | ||
@@ -195,3 +225,3 @@ segmentValue = segment.pattern; | ||
} | ||
res[i] = segmentValue; | ||
res[i] = segmentValue + ''; // coerce segments to string | ||
} else { | ||
@@ -201,3 +231,6 @@ res[i] = segment; | ||
} | ||
return new URI(res); | ||
var uri = new URI(res); | ||
// FIXME: handle this in the constructor! | ||
uri.urlObj = this.urlObj; | ||
return uri; | ||
}; | ||
@@ -204,0 +237,0 @@ |
{ | ||
"name": "swagger-router", | ||
"version": "0.0.4", | ||
"version": "0.1.0", | ||
"description": "An efficient swagger 2 based router with support for multiple APIs. For use in RESTBase.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -308,2 +308,7 @@ 'use strict'; | ||
it('{/patterns} dynamic expand', function() { | ||
var uri = new URI('/{domain:some}/path/to{/optionalPath}', {}, true); | ||
deepEqual(uri.expand({optionalPath: 'foo'}).toString(), '/some/path/to/foo'); | ||
}); | ||
it('{+patterns} empty', function() { | ||
@@ -320,2 +325,7 @@ var uri = new URI('/{domain:some}/path/to/{+rest}', {}, true); | ||
it('{+patterns} dynamic expand', function() { | ||
var uri = new URI('/{domain:some}/path/to/{+rest}',{}, true); | ||
deepEqual(uri.expand({rest: 'foo'}).toString(), '/some/path/to/foo'); | ||
}); | ||
it('decoding / encoding', function() { | ||
@@ -363,2 +373,16 @@ var uri = new URI('/{domain:some}/a%2Fb/to/100%/%FF', {domain: 'foo/bar'}, true); | ||
}); | ||
it('handle protocols', function() { | ||
var uri = new URI('https://test.com/v1/page/title'); | ||
deepEqual(uri.urlObj.protocol, 'https:'); | ||
deepEqual(uri.path[0], 'v1'); | ||
deepEqual(uri.toString(), 'https://test.com/v1/page/title'); | ||
}); | ||
it('handle protocols & patterns', function() { | ||
var uri = new URI('https://test.com/v1/page/{title}', | ||
{title: 'testTitle'}, true); | ||
deepEqual(uri.startsWith('/v1/page'), true); | ||
deepEqual(uri.toString(), 'https://test.com/v1/page/testTitle'); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
35478
10
923