refactor: isHookValid

This commit is contained in:
Barış Soner Uşaklı
2026-02-21 18:15:22 -05:00
parent a54083e341
commit 63c9a6e03c

View File

@@ -159,10 +159,7 @@ async function fireFilterHook(hook, hookList, params) {
}
async function fireMethod(hookObj, params) {
if (typeof hookObj.method !== 'function') {
if (process.env.NODE_ENV === 'development') {
winston.warn(`[plugins] Expected method for hook '${hook}' in plugin '${hookObj.id}' not found, skipping.`);
}
if (!isHookValid(hook, hookObj)) {
return params;
}
@@ -184,21 +181,27 @@ async function fireActionHook(hook, hookList, params) {
return;
}
for (const hookObj of hookList) {
if (typeof hookObj.method !== 'function') {
if (process.env.NODE_ENV === 'development') {
winston.warn(`[plugins] Expected method for hook '${hook}' in plugin '${hookObj.id}' not found, skipping.`);
}
} else {
try {
// eslint-disable-next-line
await hookObj.method(params);
} catch (err) {
winston.error(`[plugins] Error in hook ${hookObj.id}@${hookObj.hook} \n${err.stack}`);
}
if (!isHookValid(hook, hookObj)) {
continue;
}
try {
// eslint-disable-next-line
await hookObj.method(params);
} catch (err) {
winston.error(`[plugins] Error in hook ${hookObj.id}@${hookObj.hook} \n${err.stack}`);
}
}
}
function isHookValid(hook, hookObj) {
const isValid = typeof hookObj.method === 'function';
if (!isValid && process.env.NODE_ENV === 'development') {
winston.warn(`[plugins] Expected method for hook '${hook}' in plugin '${hookObj.id}' not found, skipping.`);
}
return isValid;
}
// https://advancedweb.hu/how-to-add-timeout-to-a-promise-in-javascript/
const timeout = (prom, time, error) => {
let timer;
@@ -218,10 +221,7 @@ async function fireStaticHook(hook, hookList, params) {
const noErrorHooks = ['static:app.load', 'static:assets.prepare', 'static:app.preload'];
async function fireMethod(hookObj, params) {
if (typeof hookObj.method !== 'function') {
if (process.env.NODE_ENV === 'development') {
winston.warn(`[plugins] Expected method for hook '${hook}' in plugin '${hookObj.id}' not found, skipping.`);
}
if (!isHookValid(hook, hookObj)) {
return params;
}
@@ -255,17 +255,16 @@ async function fireResponseHook(hook, hookList, params) {
return;
}
for (const hookObj of hookList) {
if (typeof hookObj.method !== 'function') {
if (process.env.NODE_ENV === 'development') {
winston.warn(`[plugins] Expected method for hook '${hook}' in plugin '${hookObj.id}' not found, skipping.`);
}
} else {
// Skip remaining hooks if headers have been sent
if (params.res.headersSent) {
return;
}
// eslint-disable-next-line
await hookObj.method(params);
if (!isHookValid(hook, hookObj)) {
continue;
}
// Skip remaining hooks if headers have been sent
if (params.res.headersSent) {
return;
}
// eslint-disable-next-line no-await-in-loop
await hookObj.method(params);
}
}