Skip to content

Commit

Permalink
add condition indirection layer
Browse files Browse the repository at this point in the history
  • Loading branch information
XVincentX committed Mar 16, 2019
1 parent 832bdde commit d61ea23
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion lib/conditions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function register({ name, handler, schema }) {
conditions[name] = config => {
const validationResult = validate(config);
if (validationResult.isValid) {
return req => handler(req, config);
return handler(config);
}

logger.error(`Condition ${chalk.default.red.bold(name)} config validation failed: ${validationResult.error}`);
Expand Down
34 changes: 17 additions & 17 deletions lib/conditions/predefined.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ module.exports = [
},
{
name: 'always',
handler: () => true,
handler: () => () => true,
schema: {
$id: 'http://express-gateway.io/schemas/conditions/always.json'
}
}, {
// Not sure if anyone would ever use this in real life, but it is a
// "legitimate" condition, and is useful during tests.
name: 'never',
handler: () => false,
handler: () => () => false,
schema: {
$id: 'http://express-gateway.io/schemas/conditions/never.json'
}
}, {
name: 'allOf',
handler: (req, actionConfig) => actionConfig.conditions.every(subItem => req.matchEGCondition(subItem)),
handler: config => req => config.conditions.every(subItem => req.matchEGCondition(subItem)),
schema: {
$id: 'http://express-gateway.io/schemas/conditions/allOf.json',
type: 'object',
Expand All @@ -45,7 +45,7 @@ module.exports = [
}
}, {
name: 'oneOf',
handler: (req, actionConfig) => actionConfig.conditions.some(subItem => req.matchEGCondition(subItem)),
handler: config => req => config.conditions.some(subItem => req.matchEGCondition(subItem)),
schema: {
$id: 'http://express-gateway.io/schemas/conditions/oneOf.json',
type: 'object',
Expand All @@ -59,7 +59,7 @@ module.exports = [
}
}, {
name: 'not',
handler: (req, actionConfig) => !req.matchEGCondition(actionConfig.condition),
handler: config => req => !req.matchEGCondition(config.condition),
schema: {
$id: 'http://express-gateway.io/schemas/conditions/not.json',
type: 'object',
Expand All @@ -70,7 +70,7 @@ module.exports = [
}
}, {
name: 'pathMatch',
handler: (req, actionConfig) => req.url.match(new RegExp(actionConfig.pattern)) !== null,
handler: config => req => req.url.match(new RegExp(config.pattern)) !== null,
schema: {
$id: 'http://express-gateway.io/schemas/conditions/pathMatch.json',
type: 'object',
Expand All @@ -85,7 +85,7 @@ module.exports = [
}
}, {
name: 'pathExact',
handler: (req, actionConfig) => req.url === actionConfig.path,
handler: config => req => req.url === config.path,
schema: {
$id: 'http://express-gateway.io/schemas/conditions/pathExact.json',
type: 'object',
Expand All @@ -99,11 +99,11 @@ module.exports = [
}
}, {
name: 'method',
handler: (req, actionConfig) => {
if (Array.isArray(actionConfig.methods)) {
return actionConfig.methods.includes(req.method);
handler: config => req => {
if (Array.isArray(config.methods)) {
return config.methods.includes(req.method);
} else {
return req.method === actionConfig.methods;
return req.method === config.methods;
}
},
schema: {
Expand All @@ -128,9 +128,9 @@ module.exports = [
}
}, {
name: 'hostMatch',
handler: (req, actionConfig) => {
handler: config => req => {
if (req.headers.host) {
return minimatch(req.headers.host, actionConfig.pattern);
return minimatch(req.headers.host, config.pattern);
}
return false;
},
Expand All @@ -147,7 +147,7 @@ module.exports = [
}
}, {
name: 'expression',
handler: (req, conditionConfig) => req.egContext.match(conditionConfig.expression),
handler: config => req => req.egContext.match(config.expression),
schema: {
$id: 'http://express-gateway.io/schemas/conditions/expression.json',
type: 'object',
Expand All @@ -162,21 +162,21 @@ module.exports = [
},
{
name: 'authenticated',
handler: req => req.isAuthenticated(),
handler: () => req => req.isAuthenticated(),
schema: {
$id: 'http://express-gateway.io/schemas/conditions/authenticated.json'
}
},
{
name: 'anonymous',
handler: req => req.isUnauthenticated(),
handler: () => req => req.isUnauthenticated(),
schema: {
$id: 'http://express-gateway.io/schemas/conditions/anonymous.json'
}
},
{
name: 'tlsClientAuthenticated',
handler: req => req.client.authorized,
handler: () => req => req.client.authorized,
schema: {
$id: 'http://express-gateway.io/schemas/conditions/tlsClientAuthenticated.json'
}
Expand Down
2 changes: 1 addition & 1 deletion test/plugins/condition.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('gateway condition with plugins', () => {
plugins: {
conditions: [{
name: 'test-condition',
handler: function (req, conditionConfig) {
handler: conditionConfig => req => {
should(conditionConfig.param1).ok();
should(req.url).be.eql('/test');
return (conditionConfig.param1 === req.url);
Expand Down

0 comments on commit d61ea23

Please sign in to comment.