Skip to content

Commit 6e1aed3

Browse files
fix(types): proxy (#5101)
1 parent 8ea7cb8 commit 6e1aed3

File tree

2 files changed

+74
-126
lines changed

2 files changed

+74
-126
lines changed

lib/Server.js

+34-42
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,6 @@ const schema = require("./options.json");
126126
* @typedef {(ProxyConfigArrayItem | ((req?: Request | undefined, res?: Response | undefined, next?: NextFunction | undefined) => ProxyConfigArrayItem))[]} ProxyConfigArray
127127
*/
128128

129-
/**
130-
* @typedef {{ [url: string]: string | ProxyConfigArrayItem }} ProxyConfigMap
131-
*/
132-
133129
/**
134130
* @typedef {Object} OpenApp
135131
* @property {string} [name]
@@ -198,7 +194,7 @@ const schema = require("./options.json");
198194
* @property {boolean} [http2]
199195
* @property {"http" | "https" | "spdy" | string | ServerConfiguration} [server]
200196
* @property {boolean | "sockjs" | "ws" | string | WebSocketServerConfiguration} [webSocketServer]
201-
* @property {ProxyConfigMap | ProxyConfigArrayItem | ProxyConfigArray} [proxy]
197+
* @property {ProxyConfigArray} [proxy]
202198
* @property {boolean | string | Open | Array<string | Open>} [open]
203199
* @property {boolean} [setupExitSignals]
204200
* @property {boolean | ClientConfiguration} [client]
@@ -1317,48 +1313,45 @@ class Server {
13171313
* }
13181314
*/
13191315
if (typeof options.proxy !== "undefined") {
1320-
/** @type {ProxyConfigArray} */
1321-
(options.proxy) =
1322-
/** @type {ProxyConfigArray} */
1323-
(options.proxy).map((item) => {
1324-
if (typeof item === "function") {
1325-
return item;
1326-
}
1316+
options.proxy = options.proxy.map((item) => {
1317+
if (typeof item === "function") {
1318+
return item;
1319+
}
13271320

1328-
/**
1329-
* @param {"info" | "warn" | "error" | "debug" | "silent" | undefined | "none" | "log" | "verbose"} level
1330-
* @returns {"info" | "warn" | "error" | "debug" | "silent" | undefined}
1331-
*/
1332-
const getLogLevelForProxy = (level) => {
1333-
if (level === "none") {
1334-
return "silent";
1335-
}
1321+
/**
1322+
* @param {"info" | "warn" | "error" | "debug" | "silent" | undefined | "none" | "log" | "verbose"} level
1323+
* @returns {"info" | "warn" | "error" | "debug" | "silent" | undefined}
1324+
*/
1325+
const getLogLevelForProxy = (level) => {
1326+
if (level === "none") {
1327+
return "silent";
1328+
}
13361329

1337-
if (level === "log") {
1338-
return "info";
1339-
}
1330+
if (level === "log") {
1331+
return "info";
1332+
}
13401333

1341-
if (level === "verbose") {
1342-
return "debug";
1343-
}
1334+
if (level === "verbose") {
1335+
return "debug";
1336+
}
13441337

1345-
return level;
1346-
};
1338+
return level;
1339+
};
13471340

1348-
if (typeof item.logLevel === "undefined") {
1349-
item.logLevel = getLogLevelForProxy(
1350-
compilerOptions.infrastructureLogging
1351-
? compilerOptions.infrastructureLogging.level
1352-
: "info",
1353-
);
1354-
}
1341+
if (typeof item.logLevel === "undefined") {
1342+
item.logLevel = getLogLevelForProxy(
1343+
compilerOptions.infrastructureLogging
1344+
? compilerOptions.infrastructureLogging.level
1345+
: "info",
1346+
);
1347+
}
13551348

1356-
if (typeof item.logProvider === "undefined") {
1357-
item.logProvider = () => this.logger;
1358-
}
1349+
if (typeof item.logProvider === "undefined") {
1350+
item.logProvider = () => this.logger;
1351+
}
13591352

1360-
return item;
1361-
});
1353+
return item;
1354+
});
13621355
}
13631356

13641357
if (typeof options.setupExitSignals === "undefined") {
@@ -2045,8 +2038,7 @@ class Server {
20452038
* }
20462039
* ]
20472040
*/
2048-
/** @type {ProxyConfigArray} */
2049-
(this.options.proxy).forEach((proxyConfigOrCallback) => {
2041+
this.options.proxy.forEach((proxyConfigOrCallback) => {
20502042
/**
20512043
* @type {RequestHandler}
20522044
*/

types/lib/Server.d.ts

+40-84
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,6 @@ declare class Server {
153153
/**
154154
* @typedef {(ProxyConfigArrayItem | ((req?: Request | undefined, res?: Response | undefined, next?: NextFunction | undefined) => ProxyConfigArrayItem))[]} ProxyConfigArray
155155
*/
156-
/**
157-
* @typedef {{ [url: string]: string | ProxyConfigArrayItem }} ProxyConfigMap
158-
*/
159156
/**
160157
* @typedef {Object} OpenApp
161158
* @property {string} [name]
@@ -216,7 +213,7 @@ declare class Server {
216213
* @property {boolean} [http2]
217214
* @property {"http" | "https" | "spdy" | string | ServerConfiguration} [server]
218215
* @property {boolean | "sockjs" | "ws" | string | WebSocketServerConfiguration} [webSocketServer]
219-
* @property {ProxyConfigMap | ProxyConfigArrayItem | ProxyConfigArray} [proxy]
216+
* @property {ProxyConfigArray} [proxy]
220217
* @property {boolean | string | Open | Array<string | Open>} [open]
221218
* @property {boolean} [setupExitSignals]
222219
* @property {boolean | ClientConfiguration} [client]
@@ -323,69 +320,6 @@ declare class Server {
323320
)[];
324321
};
325322
trustedTypesPolicyName: {
326-
/**
327-
* @typedef {Object} Open
328-
* @property {string | string[] | OpenApp} [app]
329-
* @property {string | string[]} [target]
330-
*/
331-
/**
332-
* @typedef {Object} NormalizedOpen
333-
* @property {string} target
334-
* @property {import("open").Options} options
335-
*/
336-
/**
337-
* @typedef {Object} WebSocketURL
338-
* @property {string} [hostname]
339-
* @property {string} [password]
340-
* @property {string} [pathname]
341-
* @property {number | string} [port]
342-
* @property {string} [protocol]
343-
* @property {string} [username]
344-
*/
345-
/**
346-
* @typedef {boolean | ((error: Error) => void)} OverlayMessageOptions
347-
*/
348-
/**
349-
* @typedef {Object} ClientConfiguration
350-
* @property {"log" | "info" | "warn" | "error" | "none" | "verbose"} [logging]
351-
* @property {boolean | { warnings?: OverlayMessageOptions, errors?: OverlayMessageOptions, runtimeErrors?: OverlayMessageOptions }} [overlay]
352-
* @property {boolean} [progress]
353-
* @property {boolean | number} [reconnect]
354-
* @property {"ws" | "sockjs" | string} [webSocketTransport]
355-
* @property {string | WebSocketURL} [webSocketURL]
356-
*/
357-
/**
358-
* @typedef {Array<{ key: string; value: string }> | Record<string, string | string[]>} Headers
359-
*/
360-
/**
361-
* @typedef {{ name?: string, path?: string, middleware: ExpressRequestHandler | ExpressErrorRequestHandler } | ExpressRequestHandler | ExpressErrorRequestHandler} Middleware
362-
*/
363-
/**
364-
* @typedef {Object} Configuration
365-
* @property {boolean | string} [ipc]
366-
* @property {Host} [host]
367-
* @property {Port} [port]
368-
* @property {boolean | "only"} [hot]
369-
* @property {boolean} [liveReload]
370-
* @property {DevMiddlewareOptions<Request, Response>} [devMiddleware]
371-
* @property {boolean} [compress]
372-
* @property {"auto" | "all" | string | string[]} [allowedHosts]
373-
* @property {boolean | ConnectHistoryApiFallbackOptions} [historyApiFallback]
374-
* @property {boolean | Record<string, never> | BonjourOptions} [bonjour]
375-
* @property {string | string[] | WatchFiles | Array<string | WatchFiles>} [watchFiles]
376-
* @property {boolean | string | Static | Array<string | Static>} [static]
377-
* @property {boolean | ServerOptions} [https]
378-
* @property {boolean} [http2]
379-
* @property {"http" | "https" | "spdy" | string | ServerConfiguration} [server]
380-
* @property {boolean | "sockjs" | "ws" | string | WebSocketServerConfiguration} [webSocketServer]
381-
* @property {ProxyConfigMap | ProxyConfigArrayItem | ProxyConfigArray} [proxy]
382-
* @property {boolean | string | Open | Array<string | Open>} [open]
383-
* @property {boolean} [setupExitSignals]
384-
* @property {boolean | ClientConfiguration} [client]
385-
* @property {Headers | ((req: Request, res: Response, context: DevMiddlewareContext<Request, Response>) => Headers)} [headers]
386-
* @property {(devServer: Server) => void} [onListening]
387-
* @property {(middlewares: Middleware[], devServer: Server) => Middleware[]} [setupMiddlewares]
388-
*/
389323
description: string;
390324
type: string;
391325
};
@@ -423,6 +357,35 @@ declare class Server {
423357
)[];
424358
};
425359
ClientWebSocketTransport: {
360+
/**
361+
* @typedef {{ name?: string, path?: string, middleware: ExpressRequestHandler | ExpressErrorRequestHandler } | ExpressRequestHandler | ExpressErrorRequestHandler} Middleware
362+
*/
363+
/**
364+
* @typedef {Object} Configuration
365+
* @property {boolean | string} [ipc]
366+
* @property {Host} [host]
367+
* @property {Port} [port]
368+
* @property {boolean | "only"} [hot]
369+
* @property {boolean} [liveReload]
370+
* @property {DevMiddlewareOptions<Request, Response>} [devMiddleware]
371+
* @property {boolean} [compress]
372+
* @property {"auto" | "all" | string | string[]} [allowedHosts]
373+
* @property {boolean | ConnectHistoryApiFallbackOptions} [historyApiFallback]
374+
* @property {boolean | Record<string, never> | BonjourOptions} [bonjour]
375+
* @property {string | string[] | WatchFiles | Array<string | WatchFiles>} [watchFiles]
376+
* @property {boolean | string | Static | Array<string | Static>} [static]
377+
* @property {boolean | ServerOptions} [https]
378+
* @property {boolean} [http2]
379+
* @property {"http" | "https" | "spdy" | string | ServerConfiguration} [server]
380+
* @property {boolean | "sockjs" | "ws" | string | WebSocketServerConfiguration} [webSocketServer]
381+
* @property {ProxyConfigArray} [proxy]
382+
* @property {boolean | string | Open | Array<string | Open>} [open]
383+
* @property {boolean} [setupExitSignals]
384+
* @property {boolean | ClientConfiguration} [client]
385+
* @property {Headers | ((req: Request, res: Response, context: DevMiddlewareContext<Request, Response>) => Headers)} [headers]
386+
* @property {(devServer: Server) => void} [onListening]
387+
* @property {(middlewares: Middleware[], devServer: Server) => Middleware[]} [setupMiddlewares]
388+
*/
426389
anyOf: {
427390
$ref: string;
428391
}[];
@@ -516,6 +479,9 @@ declare class Server {
516479
};
517480
HeaderObject: {
518481
type: string;
482+
/**
483+
* @type {FSWatcher[]}
484+
*/
519485
additionalProperties: boolean;
520486
properties: {
521487
key: {
@@ -539,9 +505,6 @@ declare class Server {
539505
$ref: string;
540506
};
541507
minItems: number;
542-
/**
543-
* @type {Socket[]}
544-
*/
545508
instanceof?: undefined;
546509
}
547510
| {
@@ -641,11 +604,6 @@ declare class Server {
641604
OnListening: {
642605
instanceof: string;
643606
description: string;
644-
/**
645-
* @param {Port} port
646-
* @param {string} host
647-
* @returns {Promise<number | string>}
648-
*/
649607
link: string;
650608
};
651609
Open: {
@@ -773,19 +731,20 @@ declare class Server {
773731
}
774732
)[];
775733
description: string;
776-
link: string;
734+
link: string /** @type {WebSocketURL} */;
777735
};
778736
Proxy: {
779737
type: string;
780738
items: {
781739
anyOf: (
782740
| {
783741
type: string;
784-
/** @type {WebSocketURL} */ instanceof?: undefined;
742+
instanceof?: undefined;
785743
}
786744
| {
787745
instanceof: string;
788-
/** @type {ClientConfiguration} */ type?: undefined;
746+
/** @type {{ type: WebSocketServerConfiguration["type"], options: NonNullable<WebSocketServerConfiguration["options"]> }} */
747+
type?: undefined;
789748
}
790749
)[];
791750
};
@@ -811,6 +770,7 @@ declare class Server {
811770
ServerString: {
812771
type: string;
813772
minLength: number;
773+
/** @type {string} */
814774
cli: {
815775
exclude: boolean;
816776
};
@@ -1190,7 +1150,7 @@ declare class Server {
11901150
};
11911151
WatchFilesString: {
11921152
type: string;
1193-
/** @type {NormalizedStatic} */ minLength: number;
1153+
minLength: number;
11941154
};
11951155
WebSocketServer: {
11961156
anyOf: {
@@ -1652,7 +1612,6 @@ declare namespace Server {
16521612
ByPass,
16531613
ProxyConfigArrayItem,
16541614
ProxyConfigArray,
1655-
ProxyConfigMap,
16561615
OpenApp,
16571616
Open,
16581617
NormalizedOpen,
@@ -1810,9 +1769,6 @@ type ProxyConfigArray = (
18101769
next?: NextFunction | undefined,
18111770
) => ProxyConfigArrayItem)
18121771
)[];
1813-
type ProxyConfigMap = {
1814-
[url: string]: string | ProxyConfigArrayItem;
1815-
};
18161772
type OpenApp = {
18171773
name?: string | undefined;
18181774
arguments?: string[] | undefined;
@@ -1903,7 +1859,7 @@ type Configuration = {
19031859
http2?: boolean | undefined;
19041860
server?: string | ServerConfiguration | undefined;
19051861
webSocketServer?: string | boolean | WebSocketServerConfiguration | undefined;
1906-
proxy?: ProxyConfigArrayItem | ProxyConfigMap | ProxyConfigArray | undefined;
1862+
proxy?: ProxyConfigArray | undefined;
19071863
open?: string | boolean | Open | (string | Open)[] | undefined;
19081864
setupExitSignals?: boolean | undefined;
19091865
client?: boolean | ClientConfiguration | undefined;

0 commit comments

Comments
 (0)