Skip to content

Commit d84bbdf

Browse files
authored
chore: improve typing 'req'
1 parent 415588f commit d84bbdf

2 files changed

Lines changed: 7 additions & 23 deletions

File tree

src/http-proxy-middleware.ts

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,7 @@ export class HttpProxyMiddleware<
7979
// Manually emit 'error' event because httpxy's promise-based API does not emit it automatically.
8080
// This is crucial for backward compatibility with HPM plugins (like error-response-plugin)
8181
// and custom listeners registered via the 'on: { error: ... }' option.
82-
83-
/**
84-
* TODO: Ideally, TReq and TRes should be restricted via "TReq extends http.IncomingMessage = http.IncomingMessage"
85-
* and "TRes extends http.ServerResponse = http.ServerResponse", which allows us to avoid the "req as TReq" below.
86-
*
87-
* However, making TReq and TRes constrained types may cause a breaking change for TypeScript users downstream.
88-
* So we leave this as a TODO for now, and revisit it in a future major release.
89-
*/
90-
this.proxy.emit('error', err as Error, req as TReq, res, activeProxyOptions.target);
82+
this.proxy.emit('error', err as Error, req, res, activeProxyOptions.target);
9183

9284
next?.(err);
9385
}
@@ -135,26 +127,18 @@ export class HttpProxyMiddleware<
135127
}
136128
};
137129

138-
private handleUpgrade = async (req: http.IncomingMessage, socket: net.Socket, head: Buffer) => {
130+
private handleUpgrade = async (req: TReq, socket: net.Socket, head: Buffer) => {
139131
try {
140132
if (this.shouldProxy(this.proxyOptions.pathFilter, req)) {
141-
const proxiedReq = req as TReq;
133+
const proxiedReq = req;
142134
const activeProxyOptions = await this.prepareProxyRequest(proxiedReq);
143135
await this.proxy.ws(proxiedReq, socket, activeProxyOptions, head);
144136
debug('server upgrade event received. Proxying WebSocket');
145137
}
146138
} catch (err) {
147139
// This error does not include the URL as the fourth argument as we won't
148140
// have the URL if `this.prepareProxyRequest` throws an error.
149-
150-
/**
151-
* TODO: Ideally, TReq and TRes should be restricted via "TReq extends http.IncomingMessage = http.IncomingMessage"
152-
* and "TRes extends http.ServerResponse = http.ServerResponse", which allows us to avoid the "req as TReq" below.
153-
*
154-
* However, making TReq and TRes constrained types may cause a breaking change for TypeScript users downstream.
155-
* So we leave this as a TODO for now, and revisit it in a future major release.
156-
*/
157-
this.proxy.emit('error', err as Error, req as TReq, socket);
141+
this.proxy.emit('error', err as Error, req, socket);
158142
}
159143
};
160144

@@ -214,8 +198,8 @@ export class HttpProxyMiddleware<
214198
req: TReq,
215199
pathRewriter: ReturnType<typeof createPathRewriter<TReq>>,
216200
) => {
217-
if (pathRewriter) {
218-
const path = await pathRewriter(req.url as string, req);
201+
if (req.url && pathRewriter) {
202+
const path = await pathRewriter(req.url, req);
219203

220204
if (typeof path === 'string') {
221205
debug('pathRewrite new path: %s', path);

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface RequestHandler<
1515
TNext = NextFunction,
1616
> {
1717
(req: TReq, res: TRes, next?: TNext): Promise<void>;
18-
upgrade: (req: http.IncomingMessage, socket: net.Socket, head: Buffer) => void;
18+
upgrade: (req: TReq, socket: net.Socket, head: Buffer) => void;
1919
}
2020

2121
export type Filter<TReq extends http.IncomingMessage = http.IncomingMessage> =

0 commit comments

Comments
 (0)