(app: App)
| 40 | * Extends Request and Response objects with custom properties and methods |
| 41 | */ |
| 42 | export const extendMiddleware = <EngineOptions>(app: App) => ( |
| 43 | req: Request, |
| 44 | res: Response<EngineOptions>, |
| 45 | next: NextFunction |
| 46 | ) => { |
| 47 | const { settings } = app |
| 48 | |
| 49 | res.get = getResponseHeader(res) |
| 50 | req.get = getRequestHeader(req) |
| 51 | |
| 52 | if (settings?.bindAppToReqRes) { |
| 53 | req.app = app |
| 54 | res.app = app |
| 55 | } |
| 56 | |
| 57 | if (settings?.networkExtensions) { |
| 58 | req.protocol = getProtocol(req) |
| 59 | req.secure = req.protocol === 'https' |
| 60 | req.connection = Object.assign(req.socket, { encrypted: req.secure }) |
| 61 | req.hostname = getHostname(req) |
| 62 | req.subdomains = getSubdomains(req, settings.subdomainOffset) |
| 63 | req.ip = getIP(req) |
| 64 | req.ips = getIPs(req) |
| 65 | } |
| 66 | |
| 67 | req.query = getQueryParams(req.url) |
| 68 | |
| 69 | req.range = getRangeFromHeader(req) |
| 70 | req.accepts = getAccepts(req) |
| 71 | req.acceptsCharsets = getAcceptsCharsets(req) |
| 72 | req.acceptsEncodings = getAcceptsEncodings(req) |
| 73 | req.acceptsLanguages = getAcceptsLanguages(req) |
| 74 | |
| 75 | req.xhr = checkIfXMLHttpRequest(req) |
| 76 | |
| 77 | res.header = res.set = setHeader<Response>(res) |
| 78 | res.send = send<Request, Response>(req, res) |
| 79 | res.json = json<Response>(res) |
| 80 | res.status = status<Response>(res) |
| 81 | res.sendStatus = sendStatus<Request, Response>(req, res) |
| 82 | res.sendFile = sendFile<Request, Response>(req, res) |
| 83 | res.type = setContentType<Response>(res) |
| 84 | res.location = setLocationHeader<Request, Response>(req, res) |
| 85 | res.links = setLinksHeader<Response>(res) |
| 86 | res.vary = setVaryHeader<Response>(res) |
| 87 | res.cookie = setCookie<Request, Response>(req, res) |
| 88 | res.clearCookie = clearCookie<Request, Response>(req, res) |
| 89 | res.render = renderTemplate<EngineOptions>(req, res, app) |
| 90 | res.format = formatResponse(req, res, next) |
| 91 | res.redirect = redirect(req, res, next) |
| 92 | res.attachment = attachment<Response>(res) |
| 93 | res.download = download<Request, Response>(req, res) |
| 94 | res.append = append<Response>(res) |
| 95 | res.locals = res.locals || Object.create(null) |
| 96 | |
| 97 | if (settings?.freshnessTesting) { |
| 98 | req.fresh = getFreshOrStale.bind(null, req, res) |
| 99 | req.stale = !req.fresh |
no test coverage detected