1- import * as httpProxy from 'http-proxy' ;
2- import * as _ from 'lodash' ;
1+ import express from 'express' ;
2+ import httpProxy from 'http-proxy' ;
3+ import _ from 'lodash' ;
34import { createConfig } from './config-factory' ;
45import * as contextMatcher from './context-matcher' ;
56import * as handlers from './handlers' ;
67import { getArrow , getInstance } from './logger' ;
78import * as PathRewriter from './path-rewriter' ;
89import * as Router from './router' ;
10+ import { Filter , IRequest , IRequestHandler , IResponse , Options } from './types' ;
911
1012export class HttpProxyMiddleware {
1113 private logger = getInstance ( ) ;
1214 private config ;
1315 private wsInternalSubscribed = false ;
14- private proxyOptions ;
15- private proxy ;
16+ private proxyOptions : Options ;
17+ private proxy : httpProxy ;
1618 private pathRewriter ;
1719
18- constructor ( context , opts ) {
20+ constructor ( context : Filter | Options , opts ?: Options ) {
1921 this . config = createConfig ( context , opts ) ;
2022 this . proxyOptions = this . config . options ;
2123
@@ -45,7 +47,11 @@ export class HttpProxyMiddleware {
4547 }
4648
4749 // https://github.com/Microsoft/TypeScript/wiki/'this'-in-TypeScript#red-flags-for-this
48- public middleware = async ( req , res , next ) => {
50+ public middleware : IRequestHandler = async (
51+ req : IRequest ,
52+ res : IResponse ,
53+ next : express . NextFunction
54+ ) => {
4955 if ( this . shouldProxy ( this . config . context , req ) ) {
5056 const activeProxyOptions = await this . prepareProxyRequest ( req ) ;
5157 this . proxy . web ( req , res , activeProxyOptions ) ;
@@ -55,7 +61,7 @@ export class HttpProxyMiddleware {
5561
5662 if ( this . proxyOptions . ws === true ) {
5763 // use initial request to access the server object to subscribe to http upgrade event
58- this . catchUpgradeRequest ( req . connection . server ) ;
64+ this . catchUpgradeRequest ( ( req . connection as any ) . server ) ;
5965 }
6066 } ;
6167
@@ -68,7 +74,7 @@ export class HttpProxyMiddleware {
6874 }
6975 } ;
7076
71- private handleUpgrade = async ( req , socket , head ) => {
77+ private handleUpgrade = async ( req : IRequest , socket , head ) => {
7278 if ( this . shouldProxy ( this . config . context , req ) ) {
7379 const activeProxyOptions = await this . prepareProxyRequest ( req ) ;
7480 this . proxy . ws ( req , socket , head , activeProxyOptions ) ;
@@ -84,7 +90,7 @@ export class HttpProxyMiddleware {
8490 * @param {Object } req [description]
8591 * @return {Boolean }
8692 */
87- private shouldProxy = ( context , req ) => {
93+ private shouldProxy = ( context , req : IRequest ) => {
8894 const path = req . originalUrl || req . url ;
8995 return contextMatcher . match ( context , path , req ) ;
9096 } ;
@@ -97,7 +103,7 @@ export class HttpProxyMiddleware {
97103 * @param {Object } req
98104 * @return {Object } proxy options
99105 */
100- private prepareProxyRequest = async req => {
106+ private prepareProxyRequest = async ( req : IRequest ) => {
101107 // https://github.com/chimurai/http-proxy-middleware/issues/17
102108 // https://github.com/chimurai/http-proxy-middleware/issues/94
103109 req . url = req . originalUrl || req . url ;
@@ -133,7 +139,7 @@ export class HttpProxyMiddleware {
133139 } ;
134140
135141 // Modify option.target when router present.
136- private applyRouter = async ( req , options ) => {
142+ private applyRouter = async ( req : IRequest , options ) => {
137143 let newTarget ;
138144
139145 if ( options . router ) {
@@ -151,7 +157,7 @@ export class HttpProxyMiddleware {
151157 } ;
152158
153159 // rewrite path
154- private applyPathRewrite = ( req , pathRewriter ) => {
160+ private applyPathRewrite = ( req : IRequest , pathRewriter ) => {
155161 if ( pathRewriter ) {
156162 const path = pathRewriter ( req . url , req ) ;
157163
@@ -166,10 +172,11 @@ export class HttpProxyMiddleware {
166172 }
167173 } ;
168174
169- private logError = ( err , req , res ) => {
175+ private logError = ( err , req : IRequest , res : IResponse ) => {
170176 const hostname =
171177 ( req . headers && req . headers . host ) || ( req . hostname || req . host ) ; // (websocket) || (node0.10 || node 4/5)
172- const target = this . proxyOptions . target . host || this . proxyOptions . target ;
178+ const target =
179+ ( this . proxyOptions . target as any ) . host || this . proxyOptions . target ;
173180 const errorMessage =
174181 '[HPM] Error occurred while trying to proxy request %s from %s to %s (%s) (%s)' ;
175182 const errReference =
0 commit comments