Skip to content

Commit efec2f5

Browse files
fix: types for the proxy option (#4173)
1 parent c9a3aeb commit efec2f5

File tree

2 files changed

+311
-283
lines changed

2 files changed

+311
-283
lines changed

lib/Server.js

+120-124
Original file line numberDiff line numberDiff line change
@@ -110,22 +110,22 @@ const schema = require("./options.json");
110110
*/
111111

112112
/**
113-
* @typedef {{ [url: string]: string | HttpProxyMiddlewareOptions }} ProxyConfigMap
113+
* @callback ByPass
114+
* @param {Request} req
115+
* @param {Response} res
116+
* @param {ProxyConfigArrayItem} proxyConfig
114117
*/
115118

116119
/**
117-
* @typedef {HttpProxyMiddlewareOptions[]} ProxyArray
120+
* @typedef {{ path?: HttpProxyMiddlewareOptionsFilter | undefined, context?: HttpProxyMiddlewareOptionsFilter | undefined } & { bypass?: ByPass } & HttpProxyMiddlewareOptions } ProxyConfigArrayItem
118121
*/
119122

120123
/**
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
125125
*/
126126

127127
/**
128-
* @typedef {{ path?: string | string[] | undefined, context?: string | string[] | HttpProxyMiddlewareOptionsFilter | undefined } & HttpProxyMiddlewareOptions & ByPass} ProxyConfigArray
128+
* @typedef {{ [url: string]: string | ProxyConfigArrayItem }} ProxyConfigMap
129129
*/
130130

131131
/**
@@ -194,7 +194,7 @@ const schema = require("./options.json");
194194
* @property {boolean} [http2]
195195
* @property {"http" | "https" | "spdy" | string | ServerConfiguration} [server]
196196
* @property {boolean | "sockjs" | "ws" | string | WebSocketServerConfiguration} [webSocketServer]
197-
* @property {ProxyConfigMap | ProxyConfigArray | ProxyArray} [proxy]
197+
* @property {ProxyConfigMap | ProxyConfigArrayItem | ProxyConfigArray} [proxy]
198198
* @property {boolean | string | Open | Array<string | Open>} [open]
199199
* @property {boolean} [setupExitSignals]
200200
* @property {boolean | ClientConfiguration} [client]
@@ -1380,10 +1380,10 @@ class Server {
13801380
Object.prototype.hasOwnProperty.call(options.proxy, "target") ||
13811381
Object.prototype.hasOwnProperty.call(options.proxy, "router")
13821382
) {
1383-
/** @type {ProxyArray} */
1383+
/** @type {ProxyConfigArray} */
13841384
(options.proxy) = [/** @type {ProxyConfigMap} */ (options.proxy)];
13851385
} else {
1386-
/** @type {ProxyArray} */
1386+
/** @type {ProxyConfigArray} */
13871387
(options.proxy) = Object.keys(options.proxy).map(
13881388
/**
13891389
* @param {string} context
@@ -1421,50 +1421,48 @@ class Server {
14211421
}
14221422
}
14231423

1424-
/** @type {ProxyArray} */
1424+
/** @type {ProxyConfigArray} */
14251425
(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+
14281432
/**
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}
14311435
*/
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+
}
14521440

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";
14591443
}
14601444

1461-
if (typeof item.logProvider === "undefined") {
1462-
item.logProvider = () => this.logger;
1445+
if (level === "verbose") {
1446+
return "debug";
14631447
}
14641448

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+
);
14661458
}
1467-
);
1459+
1460+
if (typeof item.logProvider === "undefined") {
1461+
item.logProvider = () => this.logger;
1462+
}
1463+
1464+
return item;
1465+
});
14681466
}
14691467

14701468
if (typeof options.setupExitSignals === "undefined") {
@@ -2124,7 +2122,7 @@ class Server {
21242122
const { createProxyMiddleware } = require("http-proxy-middleware");
21252123

21262124
/**
2127-
* @param {ProxyConfigArray} proxyConfig
2125+
* @param {ProxyConfigArrayItem} proxyConfig
21282126
* @returns {RequestHandler | undefined}
21292127
*/
21302128
const getProxyMiddleware = (proxyConfig) => {
@@ -2160,93 +2158,91 @@ class Server {
21602158
* }
21612159
* ]
21622160
*/
2163-
/** @type {ProxyArray} */
2164-
(this.options.proxy).forEach(
2161+
/** @type {ProxyConfigArray} */
2162+
(this.options.proxy).forEach((proxyConfigOrCallback) => {
21652163
/**
2166-
* @param {any} proxyConfigOrCallback
2164+
* @type {RequestHandler}
21672165
*/
2168-
(proxyConfigOrCallback) => {
2169-
/**
2170-
* @type {RequestHandler}
2171-
*/
2172-
let proxyMiddleware;
2166+
let proxyMiddleware;
21732167

2174-
let proxyConfig =
2175-
typeof proxyConfigOrCallback === "function"
2176-
? proxyConfigOrCallback()
2177-
: proxyConfigOrCallback;
2168+
let proxyConfig =
2169+
typeof proxyConfigOrCallback === "function"
2170+
? proxyConfigOrCallback()
2171+
: proxyConfigOrCallback;
21782172

2179-
proxyMiddleware =
2180-
/** @type {RequestHandler} */
2181-
(getProxyMiddleware(proxyConfig));
2173+
proxyMiddleware =
2174+
/** @type {RequestHandler} */
2175+
(getProxyMiddleware(proxyConfig));
21822176

2183-
if (proxyConfig.ws) {
2184-
this.webSocketProxies.push(proxyMiddleware);
2185-
}
2177+
if (proxyConfig.ws) {
2178+
this.webSocketProxies.push(proxyMiddleware);
2179+
}
21862180

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));
22032196
}
2197+
}
22042198

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+
};
22302227

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+
});
22502246

22512247
middlewares.push({
22522248
name: "webpack-dev-middleware",

0 commit comments

Comments
 (0)