11import type * as express from 'express' ;
22import type { Options } from './types' ;
33import type * as httpProxy from 'http-proxy' ;
4- import camelcase = require( 'camelcase' ) ;
54import { getInstance } from './logger' ;
65const logger = getInstance ( ) ;
76
@@ -15,20 +14,29 @@ export function init(proxy: httpProxy, option: Options): void {
1514 logger . debug ( '[HPM] Subscribed to http-proxy events:' , Object . keys ( handlers ) ) ;
1615}
1716
17+ type HttpProxyEventName = 'error' | 'proxyReq' | 'proxyReqWs' | 'proxyRes' | 'open' | 'close' ;
18+
1819export function getHandlers ( options : Options ) {
1920 // https://github.com/nodejitsu/node-http-proxy#listening-for-proxy-events
20- const proxyEvents = [ 'error' , 'proxyReq' , 'proxyReqWs' , 'proxyRes' , 'open' , 'close' ] ;
21+ const proxyEventsMap : Record < HttpProxyEventName , string > = {
22+ error : 'onError' ,
23+ proxyReq : 'onProxyReq' ,
24+ proxyReqWs : 'onProxyReqWs' ,
25+ proxyRes : 'onProxyRes' ,
26+ open : 'onOpen' ,
27+ close : 'onClose' ,
28+ } ;
29+
2130 const handlers : any = { } ;
2231
23- for ( const event of proxyEvents ) {
32+ for ( const [ eventName , onEventName ] of Object . entries ( proxyEventsMap ) ) {
2433 // all handlers for the http-proxy events are prefixed with 'on'.
2534 // loop through options and try to find these handlers
2635 // and add them to the handlers object for subscription in init().
27- const eventName = camelcase ( 'on ' + event ) ;
28- const fnHandler = options ? options [ eventName ] : null ;
36+ const fnHandler = options ? options [ onEventName ] : null ;
2937
3038 if ( typeof fnHandler === 'function' ) {
31- handlers [ event ] = fnHandler ;
39+ handlers [ eventName ] = fnHandler ;
3240 }
3341 }
3442
0 commit comments