Skip to content

Commit

Permalink
Merge pull request #1149 from someone635/patch-2
Browse files Browse the repository at this point in the history
Proposal - Allow matching hooks to multiple actions with "|"
  • Loading branch information
icebob authored Nov 7, 2022
2 parents 150896f + 450e4d2 commit 6a854d8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
34 changes: 10 additions & 24 deletions src/middlewares/action-hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ module.exports = function actionHookMiddleware(broker) {

// Hooks in service
/** @type {Array<String>?} List of hooks names that match the action name */
const beforeHookMatches =
hooks && hooks.before
? Object.keys(hooks.before).filter(hookName => {
// Global hook. Skip it
if (hookName === "*") return false;

return match(name, hookName);
})
: null;
const matchHook = hookName => {
if (hookName === "*") return false;
const patterns = hookName.split("|");
return patterns.some(pattern => match(name, pattern));
};

const beforeHookMatches =
hooks && hooks.before ? Object.keys(hooks.before).filter(matchHook) : null;

/** @type {Array<Function>?} List of hooks that match the action name */
const beforeHook =
Expand All @@ -89,14 +89,7 @@ module.exports = function actionHookMiddleware(broker) {

/** @type {Array<String>?} List of hooks names that match the action name */
const afterHookMatches =
hooks && hooks.after
? Object.keys(hooks.after).filter(hookName => {
// Global hook. Skip it
if (hookName === "*") return false;

return match(name, hookName);
})
: null;
hooks && hooks.after ? Object.keys(hooks.after).filter(matchHook) : null;

/** @type {Array<Function>?} List of hooks that match the action name */
const afterHook =
Expand All @@ -108,14 +101,7 @@ module.exports = function actionHookMiddleware(broker) {

/** @type {Array<String>?} List of hooks names that match the action name */
const errorHookMatches =
hooks && hooks.error
? Object.keys(hooks.error).filter(hookName => {
// Global hook. Skip it
if (hookName === "*") return false;

return match(name, hookName);
})
: null;
hooks && hooks.error ? Object.keys(hooks.error).filter(matchHook) : null;

/** @type {Array<Function>?} List of hooks that match the action name */
const errorHook =
Expand Down
20 changes: 20 additions & 0 deletions test/unit/middlewares/action-hook.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,16 @@ describe("Test ActionHookMiddleware", () => {
FLOW.push("before-hook-2");
ctx.params.third = 3;
}
],
"find|invalid": [
function (ctx) {
FLOW.push("before-hook-3");
}
],
"invalid1|invalid2": [
function (ctx) {
FLOW.push("before-hook-4"); //not added
}
]
},
after: {
Expand All @@ -203,6 +213,14 @@ describe("Test ActionHookMiddleware", () => {
return Object.assign(res, { c: 300 });
}
],

"invalid|find": [
function (ctx, res) {
FLOW.push("after-hook-3");
return res;
}
],

"*": [
function (ctx, res) {
FLOW.push("after-all-hook-1");
Expand Down Expand Up @@ -246,11 +264,13 @@ describe("Test ActionHookMiddleware", () => {
"before-all-hook-2",
"before-hook-1",
"before-hook-2",
"before-hook-3",
"before-action-hook",
"handler-1-2-3",
"after-action-hook",
"after-hook-1",
"after-hook-2",
"after-hook-3",
"after-all-hook-1",
"after-all-hook-2"
]);
Expand Down

0 comments on commit 6a854d8

Please sign in to comment.