import { BalmError , BalmProxyConfig } from '@balm-core/index' ; function handleProxyConfigError ( ) { const configuration = '{ context: string | array, options: object }' ; BalmJS . logger . error ( 'proxy middleware' , `Proxy config must be an object (${ configuration }) or array ([${ configuration }])` ) ; } function httpProxyMiddleware ( ) { const httpProxy = require ( 'http-proxy-middleware' ) ; const middleware = [ ] ; const proxyConfig = BalmJS . config . server . proxyConfig ; if ( proxyConfig ) { try { if ( BalmJS . utils . isObject ( proxyConfig ) ) { const config = proxyConfig as BalmProxyConfig ; if ( config . context && config . options ) { middleware . push ( httpProxy . createProxyMiddleware ( config . context , config . options ) ) ; } else { handleProxyConfigError ( ) ; } } else if ( BalmJS . utils . isArray ( proxyConfig ) ) { for ( const config of proxyConfig as BalmProxyConfig [ ] ) { if ( config . context && config . options ) { middleware . push ( httpProxy . createProxyMiddleware ( config . context , config . options ) ) ; } else { handleProxyConfigError ( ) ; } } } else { handleProxyConfigError ( ) ; } } catch ( error ) { const { message } = error as BalmError ; BalmJS . logger . error ( 'proxy middleware' , message ) ; } } return middleware ; } export default httpProxyMiddleware ;