-
-
Notifications
You must be signed in to change notification settings - Fork 346
/
hooks.ts
52 lines (49 loc) · 1.46 KB
/
hooks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import express from 'express';
import logger from '../../../common/logger';
import { ProxyOptions } from './types';
export const registerBeforeHook = (
app: express.Express,
config: ProxyOptions,
stateSetupPath: string
): void => {
app.use(async (req, res, next) => {
if (config.beforeEach !== undefined) {
logger.trace("registered 'beforeEach' hook");
if (req.path === stateSetupPath) {
logger.debug("executing 'beforeEach' hook");
try {
await config.beforeEach();
} catch (e) {
logger.error(`error executing 'beforeEach' hook: ${e.message}`);
logger.debug(`Stack trace was: ${e.stack}`);
next(new Error(`error executing 'beforeEach' hook: ${e.message}`));
}
}
}
next();
});
};
export const registerAfterHook = (
app: express.Express,
config: ProxyOptions,
stateSetupPath: string
): void => {
app.use(async (req, res, next) => {
if (config.afterEach !== undefined) {
logger.trace("registered 'afterEach' hook");
next();
if (req.path !== stateSetupPath) {
logger.debug("executing 'afterEach' hook");
try {
await config.afterEach();
} catch (e) {
logger.error(`error executing 'afterEach' hook: ${e.message}`);
logger.debug(`Stack trace was: ${e.stack}`);
next(new Error(`error executing 'afterEach' hook: ${e.message}`));
}
}
} else {
next();
}
});
};