@@ -110,22 +110,22 @@ const schema = require("./options.json");
110
110
*/
111
111
112
112
/**
113
- * @typedef {{ [url: string]: string | HttpProxyMiddlewareOptions } } ProxyConfigMap
113
+ * @callback ByPass
114
+ * @param {Request } req
115
+ * @param {Response } res
116
+ * @param {ProxyConfigArrayItem } proxyConfig
114
117
*/
115
118
116
119
/**
117
- * @typedef {HttpProxyMiddlewareOptions[] } ProxyArray
120
+ * @typedef {{ path?: HttpProxyMiddlewareOptionsFilter | undefined, context?: HttpProxyMiddlewareOptionsFilter | undefined } & { bypass?: ByPass } & HttpProxyMiddlewareOptions } ProxyConfigArrayItem
118
121
*/
119
122
120
123
/**
121
- * @callback ByPass
122
- * @param {Request } req
123
- * @param {Response } res
124
- * @param {ProxyConfigArray } proxyConfig
124
+ * @typedef {(ProxyConfigArrayItem | ((req?: Request | undefined, res?: Response | undefined, next?: NextFunction | undefined) => ProxyConfigArrayItem))[] } ProxyConfigArray
125
125
*/
126
126
127
127
/**
128
- * @typedef {{ path? : string | string[] | undefined, context? : string | string[] | HttpProxyMiddlewareOptionsFilter | undefined } & HttpProxyMiddlewareOptions & ByPass } ProxyConfigArray
128
+ * @typedef {{ [url : string] : string | ProxyConfigArrayItem } } ProxyConfigMap
129
129
*/
130
130
131
131
/**
@@ -194,7 +194,7 @@ const schema = require("./options.json");
194
194
* @property {boolean } [http2]
195
195
* @property {"http" | "https" | "spdy" | string | ServerConfiguration } [server]
196
196
* @property {boolean | "sockjs" | "ws" | string | WebSocketServerConfiguration } [webSocketServer]
197
- * @property {ProxyConfigMap | ProxyConfigArray | ProxyArray } [proxy]
197
+ * @property {ProxyConfigMap | ProxyConfigArrayItem | ProxyConfigArray } [proxy]
198
198
* @property {boolean | string | Open | Array<string | Open> } [open]
199
199
* @property {boolean } [setupExitSignals]
200
200
* @property {boolean | ClientConfiguration } [client]
@@ -1380,10 +1380,10 @@ class Server {
1380
1380
Object . prototype . hasOwnProperty . call ( options . proxy , "target" ) ||
1381
1381
Object . prototype . hasOwnProperty . call ( options . proxy , "router" )
1382
1382
) {
1383
- /** @type {ProxyArray } */
1383
+ /** @type {ProxyConfigArray } */
1384
1384
( options . proxy ) = [ /** @type {ProxyConfigMap } */ ( options . proxy ) ] ;
1385
1385
} else {
1386
- /** @type {ProxyArray } */
1386
+ /** @type {ProxyConfigArray } */
1387
1387
( options . proxy ) = Object . keys ( options . proxy ) . map (
1388
1388
/**
1389
1389
* @param {string } context
@@ -1421,50 +1421,48 @@ class Server {
1421
1421
}
1422
1422
}
1423
1423
1424
- /** @type {ProxyArray } */
1424
+ /** @type {ProxyConfigArray } */
1425
1425
( options . proxy ) =
1426
- /** @type {ProxyArray } */
1427
- ( options . proxy ) . map (
1426
+ /** @type {ProxyConfigArray } */
1427
+ ( options . proxy ) . map ( ( item ) => {
1428
+ if ( typeof item === "function" ) {
1429
+ return item ;
1430
+ }
1431
+
1428
1432
/**
1429
- * @param {HttpProxyMiddlewareOptions } item
1430
- * @returns {HttpProxyMiddlewareOptions }
1433
+ * @param {"info" | "warn" | "error" | "debug" | "silent" | undefined | "none" | "log" | "verbose" } level
1434
+ * @returns {"info" | "warn" | "error" | "debug" | "silent" | undefined }
1431
1435
*/
1432
- ( item ) => {
1433
- /**
1434
- * @param {"info" | "warn" | "error" | "debug" | "silent" | undefined | "none" | "log" | "verbose" } level
1435
- * @returns {"info" | "warn" | "error" | "debug" | "silent" | undefined }
1436
- */
1437
- const getLogLevelForProxy = ( level ) => {
1438
- if ( level === "none" ) {
1439
- return "silent" ;
1440
- }
1441
-
1442
- if ( level === "log" ) {
1443
- return "info" ;
1444
- }
1445
-
1446
- if ( level === "verbose" ) {
1447
- return "debug" ;
1448
- }
1449
-
1450
- return level ;
1451
- } ;
1436
+ const getLogLevelForProxy = ( level ) => {
1437
+ if ( level === "none" ) {
1438
+ return "silent" ;
1439
+ }
1452
1440
1453
- if ( typeof item . logLevel === "undefined" ) {
1454
- item . logLevel = getLogLevelForProxy (
1455
- compilerOptions . infrastructureLogging
1456
- ? compilerOptions . infrastructureLogging . level
1457
- : "info"
1458
- ) ;
1441
+ if ( level === "log" ) {
1442
+ return "info" ;
1459
1443
}
1460
1444
1461
- if ( typeof item . logProvider === "undefined " ) {
1462
- item . logProvider = ( ) => this . logger ;
1445
+ if ( level === "verbose " ) {
1446
+ return "debug" ;
1463
1447
}
1464
1448
1465
- return item ;
1449
+ return level ;
1450
+ } ;
1451
+
1452
+ if ( typeof item . logLevel === "undefined" ) {
1453
+ item . logLevel = getLogLevelForProxy (
1454
+ compilerOptions . infrastructureLogging
1455
+ ? compilerOptions . infrastructureLogging . level
1456
+ : "info"
1457
+ ) ;
1466
1458
}
1467
- ) ;
1459
+
1460
+ if ( typeof item . logProvider === "undefined" ) {
1461
+ item . logProvider = ( ) => this . logger ;
1462
+ }
1463
+
1464
+ return item ;
1465
+ } ) ;
1468
1466
}
1469
1467
1470
1468
if ( typeof options . setupExitSignals === "undefined" ) {
@@ -2124,7 +2122,7 @@ class Server {
2124
2122
const { createProxyMiddleware } = require ( "http-proxy-middleware" ) ;
2125
2123
2126
2124
/**
2127
- * @param {ProxyConfigArray } proxyConfig
2125
+ * @param {ProxyConfigArrayItem } proxyConfig
2128
2126
* @returns {RequestHandler | undefined }
2129
2127
*/
2130
2128
const getProxyMiddleware = ( proxyConfig ) => {
@@ -2160,93 +2158,91 @@ class Server {
2160
2158
* }
2161
2159
* ]
2162
2160
*/
2163
- /** @type {ProxyArray } */
2164
- ( this . options . proxy ) . forEach (
2161
+ /** @type {ProxyConfigArray } */
2162
+ ( this . options . proxy ) . forEach ( ( proxyConfigOrCallback ) => {
2165
2163
/**
2166
- * @param { any } proxyConfigOrCallback
2164
+ * @type { RequestHandler }
2167
2165
*/
2168
- ( proxyConfigOrCallback ) => {
2169
- /**
2170
- * @type {RequestHandler }
2171
- */
2172
- let proxyMiddleware ;
2166
+ let proxyMiddleware ;
2173
2167
2174
- let proxyConfig =
2175
- typeof proxyConfigOrCallback === "function"
2176
- ? proxyConfigOrCallback ( )
2177
- : proxyConfigOrCallback ;
2168
+ let proxyConfig =
2169
+ typeof proxyConfigOrCallback === "function"
2170
+ ? proxyConfigOrCallback ( )
2171
+ : proxyConfigOrCallback ;
2178
2172
2179
- proxyMiddleware =
2180
- /** @type {RequestHandler } */
2181
- ( getProxyMiddleware ( proxyConfig ) ) ;
2173
+ proxyMiddleware =
2174
+ /** @type {RequestHandler } */
2175
+ ( getProxyMiddleware ( proxyConfig ) ) ;
2182
2176
2183
- if ( proxyConfig . ws ) {
2184
- this . webSocketProxies . push ( proxyMiddleware ) ;
2185
- }
2177
+ if ( proxyConfig . ws ) {
2178
+ this . webSocketProxies . push ( proxyMiddleware ) ;
2179
+ }
2186
2180
2187
- /**
2188
- * @param {Request } req
2189
- * @param {Response } res
2190
- * @param {NextFunction } next
2191
- * @returns {Promise<void> }
2192
- */
2193
- const handler = async ( req , res , next ) => {
2194
- if ( typeof proxyConfigOrCallback === "function" ) {
2195
- const newProxyConfig = proxyConfigOrCallback ( req , res , next ) ;
2196
-
2197
- if ( newProxyConfig !== proxyConfig ) {
2198
- proxyConfig = newProxyConfig ;
2199
- proxyMiddleware =
2200
- /** @type {RequestHandler } */
2201
- ( getProxyMiddleware ( proxyConfig ) ) ;
2202
- }
2181
+ /**
2182
+ * @param {Request } req
2183
+ * @param {Response } res
2184
+ * @param {NextFunction } next
2185
+ * @returns {Promise<void> }
2186
+ */
2187
+ const handler = async ( req , res , next ) => {
2188
+ if ( typeof proxyConfigOrCallback === "function" ) {
2189
+ const newProxyConfig = proxyConfigOrCallback ( req , res , next ) ;
2190
+
2191
+ if ( newProxyConfig !== proxyConfig ) {
2192
+ proxyConfig = newProxyConfig ;
2193
+ proxyMiddleware =
2194
+ /** @type {RequestHandler } */
2195
+ ( getProxyMiddleware ( proxyConfig ) ) ;
2203
2196
}
2197
+ }
2204
2198
2205
- // - Check if we have a bypass function defined
2206
- // - In case the bypass function is defined we'll retrieve the
2207
- // bypassUrl from it otherwise bypassUrl would be null
2208
- // TODO remove in the next major in favor `context` and `router` options
2209
- const isByPassFuncDefined =
2210
- typeof proxyConfig . bypass === "function" ;
2211
- const bypassUrl = isByPassFuncDefined
2212
- ? await proxyConfig . bypass ( req , res , proxyConfig )
2213
- : null ;
2214
-
2215
- if ( typeof bypassUrl === "boolean" ) {
2216
- // skip the proxy
2217
- // @ts -ignore
2218
- req . url = null ;
2219
- next ( ) ;
2220
- } else if ( typeof bypassUrl === "string" ) {
2221
- // byPass to that url
2222
- req . url = bypassUrl ;
2223
- next ( ) ;
2224
- } else if ( proxyMiddleware ) {
2225
- return proxyMiddleware ( req , res , next ) ;
2226
- } else {
2227
- next ( ) ;
2228
- }
2229
- } ;
2199
+ // - Check if we have a bypass function defined
2200
+ // - In case the bypass function is defined we'll retrieve the
2201
+ // bypassUrl from it otherwise bypassUrl would be null
2202
+ // TODO remove in the next major in favor `context` and `router` options
2203
+ const isByPassFuncDefined = typeof proxyConfig . bypass === "function" ;
2204
+ const bypassUrl = isByPassFuncDefined
2205
+ ? await /** @type {ByPass } */ ( proxyConfig . bypass ) (
2206
+ req ,
2207
+ res ,
2208
+ proxyConfig
2209
+ )
2210
+ : null ;
2211
+
2212
+ if ( typeof bypassUrl === "boolean" ) {
2213
+ // skip the proxy
2214
+ // @ts -ignore
2215
+ req . url = null ;
2216
+ next ( ) ;
2217
+ } else if ( typeof bypassUrl === "string" ) {
2218
+ // byPass to that url
2219
+ req . url = bypassUrl ;
2220
+ next ( ) ;
2221
+ } else if ( proxyMiddleware ) {
2222
+ return proxyMiddleware ( req , res , next ) ;
2223
+ } else {
2224
+ next ( ) ;
2225
+ }
2226
+ } ;
2230
2227
2231
- middlewares . push ( {
2232
- name : "http-proxy-middleware" ,
2233
- middleware : handler ,
2234
- } ) ;
2235
- // Also forward error requests to the proxy so it can handle them.
2236
- middlewares . push ( {
2237
- name : "http-proxy-middleware-error-handler" ,
2238
- middleware :
2239
- /**
2240
- * @param {Error } error
2241
- * @param {Request } req
2242
- * @param {Response } res
2243
- * @param {NextFunction } next
2244
- * @returns {any }
2245
- */
2246
- ( error , req , res , next ) => handler ( req , res , next ) ,
2247
- } ) ;
2248
- }
2249
- ) ;
2228
+ middlewares . push ( {
2229
+ name : "http-proxy-middleware" ,
2230
+ middleware : handler ,
2231
+ } ) ;
2232
+ // Also forward error requests to the proxy so it can handle them.
2233
+ middlewares . push ( {
2234
+ name : "http-proxy-middleware-error-handler" ,
2235
+ middleware :
2236
+ /**
2237
+ * @param {Error } error
2238
+ * @param {Request } req
2239
+ * @param {Response } res
2240
+ * @param {NextFunction } next
2241
+ * @returns {any }
2242
+ */
2243
+ ( error , req , res , next ) => handler ( req , res , next ) ,
2244
+ } ) ;
2245
+ } ) ;
2250
2246
2251
2247
middlewares . push ( {
2252
2248
name : "webpack-dev-middleware" ,
0 commit comments