@pact-foundation/pact
Version:
Pact for all things Javascript
45 lines • 1.96 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerHooks = exports.createHooksState = void 0;
const logger_1 = __importDefault(require("../../../common/logger"));
const createHooksState = () => ({
insideInteraction: false,
errors: [],
});
exports.createHooksState = createHooksState;
const runHook = async (name, hook, hooksState) => {
logger_1.default.debug(`executing '${name}' hook`);
try {
await hook();
}
catch (e) {
const error = e instanceof Error ? e : new Error(String(e));
logger_1.default.error(`error executing '${name}' hook: ${error.message}`);
logger_1.default.debug(`Stack trace was: ${error.stack}`);
hooksState.errors.push(new Error(`error executing '${name}' hook: ${error.message}`));
}
};
const registerHooks = (hooks, hooksState) => async ({ body }, _res, next) => {
const action = body?.action;
if (action === 'setup' && !hooksState.insideInteraction) {
// rising edge: the first "setup" of a new interaction
hooksState.insideInteraction = true;
if (hooks.beforeEach)
await runHook('beforeEach', hooks.beforeEach, hooksState);
}
else if (action === 'teardown' && hooksState.insideInteraction) {
// falling edge: the first "teardown" after the interaction ran
hooksState.insideInteraction = false;
if (hooks.afterEach)
await runHook('afterEach', hooks.afterEach, hooksState);
}
// Always continue with a successful response. A hook failure must never
// become a non-2xx state-change response, or the core will retry it and
// desync interaction tracking. Failures are surfaced by the verifier later.
next();
};
exports.registerHooks = registerHooks;
//# sourceMappingURL=hooks.js.map