Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Commit

Permalink
Refactor express handlers, update Changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Vlasák committed Jan 17, 2019
1 parent cb9f71f commit 332703e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 34 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
### Fixed
- lint issue due to changes in tslint 5.12.0
- stream write function type
- pretty streams created only when needed

### Added
- coveralls integration

## Changed
- refactoring of express handlers

## [1.0.3] - 2018-11-08

Expand Down
75 changes: 41 additions & 34 deletions src/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,59 @@ import { AckeeLogger } from '.';

const errorSymbol = Symbol.for('error');

type AckeeRequest = Request & { _startAt?: [number, number]; ackId?: string };
type AckeeResponse = Response & { _startAt?: [number, number]; time?: string; out?: object; [errorSymbol]?: any };

export type AckeeLoggerExpressMiddleware = (
this: AckeeLogger,
req: Request & { _startAt?: [number, number]; ackId?: string },
response: Response & { _startAt?: [number, number]; time?: string; out?: object; [errorSymbol]?: any },
req: AckeeRequest,
response: AckeeResponse,
next: any
) => void;

const expressOnHeaders = (req: AckeeRequest, res: AckeeResponse) => () => {
res._startAt = process.hrtime();
const diffFromSeconds = (res._startAt[0] - req._startAt![0]) * 1e3;
const diffFromNanoseconds = (res._startAt[1] - req._startAt![1]) * 1e-6;
const ms = diffFromSeconds + diffFromNanoseconds;
res.time = ms.toFixed(3);
};

const expressOnFinished = (logger: AckeeLogger, req: AckeeRequest) => (_err: Error | null, res: AckeeResponse) => {
const error = res[errorSymbol];
const reqOut = `${res.statusCode} ${req.method} ${req.originalUrl} ${res.time} ms ${req.headers['user-agent']}`;
if (logger.options.ignoredHttpMethods && logger.options.ignoredHttpMethods.includes(req.method)) {
return;
}
const standardOutput = {
data: {
req,
res,
ackId: req.ackId,
},
message: `${reqOut} - Standard output`,
};

if (error) {
logger.error({ error, req, res, ackId: req.ackId }, `${reqOut} - Error handler at the end of app`);
} else if (res.out) {
logger.debug(standardOutput.data, standardOutput.message);
} else {
logger.info(standardOutput.data, standardOutput.message);
}
};

const expressMiddleware: RequestHandler = function(
this: AckeeLogger,
req: Request & { _startAt?: [number, number]; ackId?: string },
response: Response & { _startAt?: [number, number]; time?: string; out?: object; [errorSymbol]?: any },
req: AckeeRequest,
response: AckeeResponse,
next: any
) {
const reqIn = `--- ${req.method} ${req.originalUrl} ${req.headers['user-agent']}`;
this.debug({ req, ackId: req.ackId }, `${reqIn} - Request accepted`);
req._startAt = process.hrtime();
onHeaders(response, () => {
response._startAt = process.hrtime();
const diffFromSeconds = (response._startAt[0] - req._startAt![0]) * 1e3;
const diffFromNanoseconds = (response._startAt[1] - req._startAt![1]) * 1e-6;
const ms = diffFromSeconds + diffFromNanoseconds;
response.time = ms.toFixed(3);
});
onFinished(response, (_err, res) => {
const error = res[errorSymbol];
const reqOut = `${res.statusCode} ${req.method} ${req.originalUrl} ${res.time} ms ${req.headers['user-agent']}`;
if (this.options.ignoredHttpMethods && this.options.ignoredHttpMethods.includes(req.method)) {
return;
}
const standardOutput = {
data: {
req,
res,
ackId: req.ackId,
},
message: `${reqOut} - Standard output`,
};

if (error) {
this.error({ error, req, res, ackId: req.ackId }, `${reqOut} - Error handler at the end of app`);
} else if (res.out) {
this.debug(standardOutput.data, standardOutput.message);
} else {
this.info(standardOutput.data, standardOutput.message);
}
});
onHeaders(response, expressOnHeaders(req, response));
onFinished(response, expressOnFinished(this, req));
next();
};

Expand Down

0 comments on commit 332703e

Please sign in to comment.