-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First impl that makes version and api work together
- Loading branch information
1 parent
827f0ab
commit 4a3ebe0
Showing
7 changed files
with
202 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
var path = require('path'); | ||
var versionVerifier = require('./versionVerifier'); | ||
|
||
|
||
class Endpoints { | ||
/** | ||
* @param {RouterConfig} configuration | ||
*/ | ||
constructor(configuration) { | ||
this._mapping = {}; | ||
this._config = configuration; | ||
} | ||
|
||
/** | ||
* Add a new configuration to the endpoint mapping. | ||
* @param {string} path | ||
* @param {string} method | ||
* @param {Array.<number|string|RegExp>} [versions] | ||
* @param {RouterConfig} config | ||
*/ | ||
add(path, method, versions, config) { | ||
versions = versions.length ? versions : [ 0 ]; | ||
path = this._unversion(path); | ||
config.versions = versions; | ||
for (let version of versions) { | ||
this._mapping[version] = this._mapping[version] || {}; | ||
this._mapping[version][path] = this._mapping[version][path] || {}; | ||
this._mapping[version][path][method.toUpperCase()] = config; | ||
} | ||
} | ||
|
||
/** | ||
* Retrieve the mapping configuration for an endpoint. | ||
* @param {string} path | ||
* @param {string} method | ||
* @param {string} incomingVersion | ||
* @returns {*} | ||
*/ | ||
get(path, method, incomingVersion = 0) { | ||
path = this._unversion(path); | ||
try { | ||
let config = {}; | ||
for (let version in this._mapping) { | ||
if (versionVerifier.validateVersion(incomingVersion, version)) { | ||
config[version] = config[version] || {}; | ||
config[version][path] = config[version][path] || {}; | ||
config[version][path][method] = this._mapping[version][path][method]; | ||
} | ||
} | ||
if (Object.keys(config).length == 1) { | ||
return config[Object.keys(config)[0]][path][method]; | ||
} | ||
// TODO not sure where and how to use this, but the format is wrong. | ||
return config; | ||
} catch (e) { | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Removes the version prefix from a given path (if there is one) | ||
* @param {string} path | ||
* @returns {string} | ||
* @private | ||
*/ | ||
_unversion(path) { | ||
if (path.startsWith('/v:' + this._config.param)) { | ||
return path.substr(('/v:' + this._config.param).length); | ||
} | ||
return path; | ||
} | ||
|
||
/** | ||
* | ||
* @param {string} [prefix] | ||
* @param {string|number|RegExp} [version] | ||
*/ | ||
list(prefix = this._config.prefix, version = 0) { | ||
let map = {}; | ||
for (let prop in this._mapping[version]) { | ||
map[path.join(prefix, prop)] = this._mapping[version][prop]; | ||
} | ||
return map; | ||
|
||
} | ||
} | ||
|
||
module.exports = Endpoints; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* global describe, it, beforeEach, afterEach */ | ||
|
||
var async = require('async'); | ||
var expect = require('chai').expect; | ||
var express = require('express'); | ||
var request = require('supertest'); | ||
|
||
var Router = require('..'); | ||
|
||
describe('Router', () => { | ||
it('should support both api configuration and versioning in one', done => { | ||
var router = Router(); | ||
router.get('/test', 1, { | ||
params: { | ||
var1: 'number', | ||
var2: 'string(foo)' | ||
} | ||
}, (req, res) => res.end('success')); | ||
|
||
var app = express(); | ||
app.use(router); | ||
async.series([ | ||
cb => request(app).get('/test').expect(404).end(cb), | ||
cb => request(app).get('/v1/test').expect(422).end(cb), | ||
cb => request(app).get('/v1/test?var1=25').expect(200, 'success').end(cb) | ||
], done); | ||
}); | ||
}); |
Oops, something went wrong.