Skip to content

Commit

Permalink
fix(types): proxy (#5101)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Mar 11, 2024
1 parent 8ea7cb8 commit 6e1aed3
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 126 deletions.
76 changes: 34 additions & 42 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,6 @@ const schema = require("./options.json");
* @typedef {(ProxyConfigArrayItem | ((req?: Request | undefined, res?: Response | undefined, next?: NextFunction | undefined) => ProxyConfigArrayItem))[]} ProxyConfigArray
*/

/**
* @typedef {{ [url: string]: string | ProxyConfigArrayItem }} ProxyConfigMap
*/

/**
* @typedef {Object} OpenApp
* @property {string} [name]
Expand Down Expand Up @@ -198,7 +194,7 @@ const schema = require("./options.json");
* @property {boolean} [http2]
* @property {"http" | "https" | "spdy" | string | ServerConfiguration} [server]
* @property {boolean | "sockjs" | "ws" | string | WebSocketServerConfiguration} [webSocketServer]
* @property {ProxyConfigMap | ProxyConfigArrayItem | ProxyConfigArray} [proxy]
* @property {ProxyConfigArray} [proxy]
* @property {boolean | string | Open | Array<string | Open>} [open]
* @property {boolean} [setupExitSignals]
* @property {boolean | ClientConfiguration} [client]
Expand Down Expand Up @@ -1317,48 +1313,45 @@ class Server {
* }
*/
if (typeof options.proxy !== "undefined") {
/** @type {ProxyConfigArray} */
(options.proxy) =
/** @type {ProxyConfigArray} */
(options.proxy).map((item) => {
if (typeof item === "function") {
return item;
}
options.proxy = options.proxy.map((item) => {
if (typeof item === "function") {
return item;
}

/**
* @param {"info" | "warn" | "error" | "debug" | "silent" | undefined | "none" | "log" | "verbose"} level
* @returns {"info" | "warn" | "error" | "debug" | "silent" | undefined}
*/
const getLogLevelForProxy = (level) => {
if (level === "none") {
return "silent";
}
/**
* @param {"info" | "warn" | "error" | "debug" | "silent" | undefined | "none" | "log" | "verbose"} level
* @returns {"info" | "warn" | "error" | "debug" | "silent" | undefined}
*/
const getLogLevelForProxy = (level) => {
if (level === "none") {
return "silent";
}

if (level === "log") {
return "info";
}
if (level === "log") {
return "info";
}

if (level === "verbose") {
return "debug";
}
if (level === "verbose") {
return "debug";
}

return level;
};
return level;
};

if (typeof item.logLevel === "undefined") {
item.logLevel = getLogLevelForProxy(
compilerOptions.infrastructureLogging
? compilerOptions.infrastructureLogging.level
: "info",
);
}
if (typeof item.logLevel === "undefined") {
item.logLevel = getLogLevelForProxy(
compilerOptions.infrastructureLogging
? compilerOptions.infrastructureLogging.level
: "info",
);
}

if (typeof item.logProvider === "undefined") {
item.logProvider = () => this.logger;
}
if (typeof item.logProvider === "undefined") {
item.logProvider = () => this.logger;
}

return item;
});
return item;
});
}

if (typeof options.setupExitSignals === "undefined") {
Expand Down Expand Up @@ -2045,8 +2038,7 @@ class Server {
* }
* ]
*/
/** @type {ProxyConfigArray} */
(this.options.proxy).forEach((proxyConfigOrCallback) => {
this.options.proxy.forEach((proxyConfigOrCallback) => {
/**
* @type {RequestHandler}
*/
Expand Down
124 changes: 40 additions & 84 deletions types/lib/Server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,6 @@ declare class Server {
/**
* @typedef {(ProxyConfigArrayItem | ((req?: Request | undefined, res?: Response | undefined, next?: NextFunction | undefined) => ProxyConfigArrayItem))[]} ProxyConfigArray
*/
/**
* @typedef {{ [url: string]: string | ProxyConfigArrayItem }} ProxyConfigMap
*/
/**
* @typedef {Object} OpenApp
* @property {string} [name]
Expand Down Expand Up @@ -216,7 +213,7 @@ declare class Server {
* @property {boolean} [http2]
* @property {"http" | "https" | "spdy" | string | ServerConfiguration} [server]
* @property {boolean | "sockjs" | "ws" | string | WebSocketServerConfiguration} [webSocketServer]
* @property {ProxyConfigMap | ProxyConfigArrayItem | ProxyConfigArray} [proxy]
* @property {ProxyConfigArray} [proxy]
* @property {boolean | string | Open | Array<string | Open>} [open]
* @property {boolean} [setupExitSignals]
* @property {boolean | ClientConfiguration} [client]
Expand Down Expand Up @@ -323,69 +320,6 @@ declare class Server {
)[];
};
trustedTypesPolicyName: {
/**
* @typedef {Object} Open
* @property {string | string[] | OpenApp} [app]
* @property {string | string[]} [target]
*/
/**
* @typedef {Object} NormalizedOpen
* @property {string} target
* @property {import("open").Options} options
*/
/**
* @typedef {Object} WebSocketURL
* @property {string} [hostname]
* @property {string} [password]
* @property {string} [pathname]
* @property {number | string} [port]
* @property {string} [protocol]
* @property {string} [username]
*/
/**
* @typedef {boolean | ((error: Error) => void)} OverlayMessageOptions
*/
/**
* @typedef {Object} ClientConfiguration
* @property {"log" | "info" | "warn" | "error" | "none" | "verbose"} [logging]
* @property {boolean | { warnings?: OverlayMessageOptions, errors?: OverlayMessageOptions, runtimeErrors?: OverlayMessageOptions }} [overlay]
* @property {boolean} [progress]
* @property {boolean | number} [reconnect]
* @property {"ws" | "sockjs" | string} [webSocketTransport]
* @property {string | WebSocketURL} [webSocketURL]
*/
/**
* @typedef {Array<{ key: string; value: string }> | Record<string, string | string[]>} Headers
*/
/**
* @typedef {{ name?: string, path?: string, middleware: ExpressRequestHandler | ExpressErrorRequestHandler } | ExpressRequestHandler | ExpressErrorRequestHandler} Middleware
*/
/**
* @typedef {Object} Configuration
* @property {boolean | string} [ipc]
* @property {Host} [host]
* @property {Port} [port]
* @property {boolean | "only"} [hot]
* @property {boolean} [liveReload]
* @property {DevMiddlewareOptions<Request, Response>} [devMiddleware]
* @property {boolean} [compress]
* @property {"auto" | "all" | string | string[]} [allowedHosts]
* @property {boolean | ConnectHistoryApiFallbackOptions} [historyApiFallback]
* @property {boolean | Record<string, never> | BonjourOptions} [bonjour]
* @property {string | string[] | WatchFiles | Array<string | WatchFiles>} [watchFiles]
* @property {boolean | string | Static | Array<string | Static>} [static]
* @property {boolean | ServerOptions} [https]
* @property {boolean} [http2]
* @property {"http" | "https" | "spdy" | string | ServerConfiguration} [server]
* @property {boolean | "sockjs" | "ws" | string | WebSocketServerConfiguration} [webSocketServer]
* @property {ProxyConfigMap | ProxyConfigArrayItem | ProxyConfigArray} [proxy]
* @property {boolean | string | Open | Array<string | Open>} [open]
* @property {boolean} [setupExitSignals]
* @property {boolean | ClientConfiguration} [client]
* @property {Headers | ((req: Request, res: Response, context: DevMiddlewareContext<Request, Response>) => Headers)} [headers]
* @property {(devServer: Server) => void} [onListening]
* @property {(middlewares: Middleware[], devServer: Server) => Middleware[]} [setupMiddlewares]
*/
description: string;
type: string;
};
Expand Down Expand Up @@ -423,6 +357,35 @@ declare class Server {
)[];
};
ClientWebSocketTransport: {
/**
* @typedef {{ name?: string, path?: string, middleware: ExpressRequestHandler | ExpressErrorRequestHandler } | ExpressRequestHandler | ExpressErrorRequestHandler} Middleware
*/
/**
* @typedef {Object} Configuration
* @property {boolean | string} [ipc]
* @property {Host} [host]
* @property {Port} [port]
* @property {boolean | "only"} [hot]
* @property {boolean} [liveReload]
* @property {DevMiddlewareOptions<Request, Response>} [devMiddleware]
* @property {boolean} [compress]
* @property {"auto" | "all" | string | string[]} [allowedHosts]
* @property {boolean | ConnectHistoryApiFallbackOptions} [historyApiFallback]
* @property {boolean | Record<string, never> | BonjourOptions} [bonjour]
* @property {string | string[] | WatchFiles | Array<string | WatchFiles>} [watchFiles]
* @property {boolean | string | Static | Array<string | Static>} [static]
* @property {boolean | ServerOptions} [https]
* @property {boolean} [http2]
* @property {"http" | "https" | "spdy" | string | ServerConfiguration} [server]
* @property {boolean | "sockjs" | "ws" | string | WebSocketServerConfiguration} [webSocketServer]
* @property {ProxyConfigArray} [proxy]
* @property {boolean | string | Open | Array<string | Open>} [open]
* @property {boolean} [setupExitSignals]
* @property {boolean | ClientConfiguration} [client]
* @property {Headers | ((req: Request, res: Response, context: DevMiddlewareContext<Request, Response>) => Headers)} [headers]
* @property {(devServer: Server) => void} [onListening]
* @property {(middlewares: Middleware[], devServer: Server) => Middleware[]} [setupMiddlewares]
*/
anyOf: {
$ref: string;
}[];
Expand Down Expand Up @@ -516,6 +479,9 @@ declare class Server {
};
HeaderObject: {
type: string;
/**
* @type {FSWatcher[]}
*/
additionalProperties: boolean;
properties: {
key: {
Expand All @@ -539,9 +505,6 @@ declare class Server {
$ref: string;
};
minItems: number;
/**
* @type {Socket[]}
*/
instanceof?: undefined;
}
| {
Expand Down Expand Up @@ -641,11 +604,6 @@ declare class Server {
OnListening: {
instanceof: string;
description: string;
/**
* @param {Port} port
* @param {string} host
* @returns {Promise<number | string>}
*/
link: string;
};
Open: {
Expand Down Expand Up @@ -773,19 +731,20 @@ declare class Server {
}
)[];
description: string;
link: string;
link: string /** @type {WebSocketURL} */;
};
Proxy: {
type: string;
items: {
anyOf: (
| {
type: string;
/** @type {WebSocketURL} */ instanceof?: undefined;
instanceof?: undefined;
}
| {
instanceof: string;
/** @type {ClientConfiguration} */ type?: undefined;
/** @type {{ type: WebSocketServerConfiguration["type"], options: NonNullable<WebSocketServerConfiguration["options"]> }} */
type?: undefined;
}
)[];
};
Expand All @@ -811,6 +770,7 @@ declare class Server {
ServerString: {
type: string;
minLength: number;
/** @type {string} */
cli: {
exclude: boolean;
};
Expand Down Expand Up @@ -1190,7 +1150,7 @@ declare class Server {
};
WatchFilesString: {
type: string;
/** @type {NormalizedStatic} */ minLength: number;
minLength: number;
};
WebSocketServer: {
anyOf: {
Expand Down Expand Up @@ -1652,7 +1612,6 @@ declare namespace Server {
ByPass,
ProxyConfigArrayItem,
ProxyConfigArray,
ProxyConfigMap,
OpenApp,
Open,
NormalizedOpen,
Expand Down Expand Up @@ -1810,9 +1769,6 @@ type ProxyConfigArray = (
next?: NextFunction | undefined,
) => ProxyConfigArrayItem)
)[];
type ProxyConfigMap = {
[url: string]: string | ProxyConfigArrayItem;
};
type OpenApp = {
name?: string | undefined;
arguments?: string[] | undefined;
Expand Down Expand Up @@ -1903,7 +1859,7 @@ type Configuration = {
http2?: boolean | undefined;
server?: string | ServerConfiguration | undefined;
webSocketServer?: string | boolean | WebSocketServerConfiguration | undefined;
proxy?: ProxyConfigArrayItem | ProxyConfigMap | ProxyConfigArray | undefined;
proxy?: ProxyConfigArray | undefined;
open?: string | boolean | Open | (string | Open)[] | undefined;
setupExitSignals?: boolean | undefined;
client?: boolean | ClientConfiguration | undefined;
Expand Down

0 comments on commit 6e1aed3

Please sign in to comment.