Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: the defaultVersion of default options does not work #2

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 73 additions & 41 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,104 @@

const semver = require('semver');

function KoaRouterVersion() {

this.options = {
requestHeader: 'Accept-Version',
responseHeader: 'X-Api-Version',
routeParam: 'version',
fallbackLatest: false,
defaultVersion: null
};
}
const defaultOptions = {
requestHeader: 'Accept-Version',
responseHeader: 'X-Api-Version',
routeParam: 'version',
fallbackLatest: false,
defaultVersion: null,
};

// sort by version from larger to smaller
const sorter = (a, b) => {
a = a.version;
b = b.version;

return semver.lt(a, b) ? 1 : -1;
};

function find(requested, tuples, fallbackLatest = false) {
if (requested === null || requested === '*') {
function findMiddlewareWithVersion(version, tuples, fallbackLatest = false) {
// if `version` is null or accept all then return the latest
if (version === null || version === '*') {
return tuples[0];
}
for (let i=0; i<tuples.length; i++) {
if (semver.satisfies(tuples[i].version, requested)) {
return tuples[i];

// iterate and test version to match
for (const tuple of tuples) {
if (semver.satisfies(tuple.version, version)) {
return tuple;
}
}

// fallback latest
if (fallbackLatest) {
return tuples[0];
}

return null;
}

KoaRouterVersion.prototype.version = function(versions, options = {}) {
let tuples = [];

if (options.fallbackLatest && options.defaultVersion) {
throw new Error('Can not set options "fallbackLatest" and "defaultVersion" at same time');
}
function version(versions, options = {}) {
// make new opt and override default options
const opt = Object.assign({}, defaultOptions, options);

for (let key in versions) tuples.push({version: key, cb: versions[key]});
tuples.sort(function(a, b) {
a = a.version;
b = b.version;

return semver.lt(a, b) ? 1 : -1;
});
// store different versions' middleware and sort for `opt.fallbackLatest`
const middlewareTuples = [];
for (const version in versions) middlewareTuples.push({version, middleware: versions[version]});
middlewareTuples.sort(sorter);

// router version middleware
return (ctx, next) => {
let requested = null;
const {
requestHeader,
responseHeader,
routeParam,
fallbackLatest,
defaultVersion,
} = opt;
let requestedVersion = null;

const routeParam = options.routeParam || this.options.routeParam;
if (
routeParam !== '' &&
ctx.params.hasOwnProperty(routeParam) &&
typeof ctx.params[routeParam] === 'string'
) {
requested = ctx.params[routeParam].substr(1);
// get version to match from `params`
requestedVersion = ctx.params[routeParam].substr(1);
} else {
requested = ctx.get(options.requestHeader || this.options.requestHeader) || null;
// get version to match from `header`
requestedVersion = ctx.get(requestHeader) || null;
}
if (!requested && options.defaultVersion) {
requested = options.defaultVersion;

// if not find any version to match
// then use defaultVersion
if (!requestedVersion && defaultVersion) {
requestedVersion = defaultVersion;
}
let found = find(requested, tuples, options.fallbackLatest || this.options.fallbackLatest);

// find version matched
const found = findMiddlewareWithVersion(requestedVersion, middlewareTuples, fallbackLatest);
if (found) {
ctx.state.apiVersion = found.version;
ctx.set(options.responseHeader || this.options.responseHeader, ctx.state.apiVersion);
return found.cb(ctx, next);
const {
version,
middleware,
} = found;

// set to state
ctx.state.apiVersion = version;

// set response header
ctx.set(responseHeader, version);

return middleware(ctx, next);
}
ctx.throw(400, 'Version ' + requested + ' is not supported');

// not found or match then throw
ctx.throw(400, 'Version ' + requestedVersion + ' is not supported');
};
};
}

let koaRouterVersion = module.exports = exports = new KoaRouterVersion;
module.exports = {
defaultOptions,
version,
};
11 changes: 0 additions & 11 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,4 @@ describe('KoaRouterVersion', function() {
});
});

it('should use default version', function() {

return expect(() => api.version({},
{
fallbackLatest: true,
defaultVersion: '1.0.0'
}
)).to.throw(Error, 'Can not set options "fallbackLatest" and "defaultVersion" at same time');

});

});