diff --git a/package.json b/package.json index 570f1be4..3da7e831 100644 --- a/package.json +++ b/package.json @@ -83,7 +83,7 @@ "ws": "8.10.0" }, "dependencies": { - "@types/http-proxy": "^1.17.9", + "@types/http-proxy": "^1.17.10", "debug": "^4.3.4", "http-proxy": "^1.18.1", "is-glob": "^4.0.1", diff --git a/src/http-proxy-middleware.ts b/src/http-proxy-middleware.ts index 20b3d342..f1465ff9 100644 --- a/src/http-proxy-middleware.ts +++ b/src/http-proxy-middleware.ts @@ -14,7 +14,7 @@ export class HttpProxyMiddleware { private wsInternalSubscribed = false; private serverOnCloseSubscribed = false; private proxyOptions: Options; - private proxy: httpProxy; + private proxy: httpProxy; private pathRewriter; constructor(options: Options) { @@ -75,7 +75,7 @@ export class HttpProxyMiddleware { } }; - private registerPlugins(proxy: httpProxy, options: Options) { + private registerPlugins(proxy: httpProxy, options: Options) { const plugins = getPlugins(options); plugins.forEach((plugin) => { debug(`register plugin: "${getFunctionName(plugin)}"`); diff --git a/src/types.ts b/src/types.ts index f943ff6c..69c29728 100644 --- a/src/types.ts +++ b/src/types.ts @@ -26,7 +26,7 @@ export type Filter = | ((pathname: string, req: TReq) => boolean); export interface Plugin { - (proxyServer: httpProxy, options: Options): void; + (proxyServer: httpProxy, options: Options): void; } export interface OnProxyEvent { diff --git a/test/types.spec.ts b/test/types.spec.ts index 46257129..b575cc24 100644 --- a/test/types.spec.ts +++ b/test/types.spec.ts @@ -260,6 +260,9 @@ describe('http-proxy-middleware TypeScript Types', () => { const proxy: RequestHandler = middleware({ target: 'http://www.example.org', on: { + error: (err: Error & { code?: string }, req, res) => { + err.code; + }, proxyRes: responseInterceptor(async (buffer, proxyRes, req, res) => { req.myRequestParams; res.myResponseParams; @@ -282,17 +285,47 @@ describe('http-proxy-middleware TypeScript Types', () => { expect(proxy).toBeDefined(); }); - it('should work with express.Request with fixRequestBody in plugins', () => { - const proxy: RequestHandler = middleware({ + it('should work with generic types in plugins', () => { + const proxy: RequestHandler = middleware({ target: 'http://www.example.org', plugins: [ (proxyServer, options) => { - proxyServer.on('proxyReq', fixRequestBody); + proxyServer.on('proxyReq', (proxyReq, req, res, options) => { + req.params; + res.status(200).send('OK'); + }); }, ], }); expect(proxy).toBeDefined(); }); + + it('should work with contextual Express types with shipped plugins', () => { + const app = express(); + app.use( + middleware({ + target: 'http://www.example.org', + plugins: [ + (proxyServer, options) => { + // fixRequestBody + proxyServer.on('proxyReq', fixRequestBody); + + // responseInterceptor + proxyServer.on( + 'proxyRes', + responseInterceptor(async (buffer, proxyRes, req, res) => { + req.params; + res.status(200).send('OK'); + return buffer; + }) + ); + }, + ], + }) + ); + + expect(app).toBeDefined(); + }); }); }); diff --git a/yarn.lock b/yarn.lock index 6dad4993..819fb155 100644 --- a/yarn.lock +++ b/yarn.lock @@ -984,10 +984,10 @@ dependencies: "@types/node" "*" -"@types/http-proxy@^1.17.9": - version "1.17.9" - resolved "https://registry.yarnpkg.com/@types/http-proxy/-/http-proxy-1.17.9.tgz#7f0e7931343761efde1e2bf48c40f02f3f75705a" - integrity sha512-QsbSjA/fSk7xB+UXlCT3wHBy5ai9wOcNDWwZAtud+jXhwOM3l+EYZh8Lng4+/6n8uar0J7xILzqftJdJ/Wdfkw== +"@types/http-proxy@^1.17.10": + version "1.17.10" + resolved "https://registry.yarnpkg.com/@types/http-proxy/-/http-proxy-1.17.10.tgz#e576c8e4a0cc5c6a138819025a88e167ebb38d6c" + integrity sha512-Qs5aULi+zV1bwKAg5z1PWnDXWmsn+LxIvUGv6E2+OOMYhclZMO+OXd9pYVf2gLykf2I7IV2u7oTHwChPNsvJ7g== dependencies: "@types/node" "*"