Skip to content

Commit

Permalink
Don't use apply
Browse files Browse the repository at this point in the history
  • Loading branch information
hansott committed Feb 24, 2025
1 parent 2e2e4ac commit c78574d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion library/sources/express/contextFromRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { buildRouteFromURL } from "../../helpers/buildRouteFromURL";
import { getIPAddressFromRequest } from "../../helpers/getIPAddressFromRequest";

export function contextFromRequest(req: Request): Context {
const url = req.protocol + "://" + req.get("host") + req.originalUrl;
const url = `${req.protocol}://${req.get("host")}${req.originalUrl}`;

return {
method: req.method,
Expand Down
17 changes: 11 additions & 6 deletions library/sources/express/wrapRequestHandler.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
/* eslint-disable prefer-rest-params */
import type { RequestHandler } from "express";
import type { RequestHandler, Request, Response, NextFunction } from "express";
import { runWithContext } from "../../agent/Context";
import { contextFromRequest } from "./contextFromRequest";
import { createWrappedFunction } from "../../helpers/wrap";

export function wrapRequestHandler(handler: RequestHandler): RequestHandler {
const fn = createWrappedFunction(handler, function wrap(handler) {
return function wrap(this: RequestHandler) {
if (arguments.length === 0) {
return handler.apply(this);
return function wrap(
this: RequestHandler,
req: Request,
res: Response,
next: NextFunction
) {
if (!req) {
return handler(req, res, next);
}

const context = contextFromRequest(arguments[0]);
const context = contextFromRequest(req);

return runWithContext(context, () => {
return handler.apply(this, arguments);
return handler(req, res, next);
});
};
}) as RequestHandler;
Expand Down

0 comments on commit c78574d

Please sign in to comment.