MCPcopy
hub / github.com/tinyhttp/tinyhttp / extendMiddleware

Function extendMiddleware

packages/app/src/extend.ts:42–103  ·  view source on GitHub ↗
(app: App)

Source from the content-addressed store, hash-verified

40 * Extends Request and Response objects with custom properties and methods
41 */
42export 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

Callers 1

handlerMethod · 0.90

Calls 15

getResponseHeaderFunction · 0.90
getRequestHeaderFunction · 0.90
getProtocolFunction · 0.90
getHostnameFunction · 0.90
getSubdomainsFunction · 0.90
getIPFunction · 0.90
getIPsFunction · 0.90
getRangeFromHeaderFunction · 0.90
getAcceptsFunction · 0.90
getAcceptsCharsetsFunction · 0.90
getAcceptsEncodingsFunction · 0.90
getAcceptsLanguagesFunction · 0.90

Tested by

no test coverage detected