chimurai / http-proxy-middleware

:zap: The one-liner node.js http-proxy middleware for connect, express, next.js and more
MIT License
10.6k stars 828 forks source link

TS type compatibility #956

Closed nightboyC closed 3 months ago

nightboyC commented 6 months ago

Describe the feature you'd love to see

_proxyReq, _req, _res _req Type incompatible with express req.params

image

Additional context (optional)

No response

nightboyC commented 6 months ago

version 3.0.0-beta.1

nightboyC commented 6 months ago

Just type compatibility, req can obtain the corresponding key value

nightboyC commented 6 months ago

axios.get('/api/getApi')

app.use( '/api/:key', MiddlewareAPIProxy ); path :/ app.use( '/api', MiddlewareAPIProxy ); path: /getApi app.use( '/api/', MiddlewareAPIProxy ); path: /getApi Should it be unnecessary/

/api !== /api/ If it is not possible to modify the type, can I handle it so that I can switch from the first writing method to the third writing method

chimurai commented 3 months ago

You can provide your own Request and Response type in v3.0.0

Here is an example with Next.js createProxyMiddleware<NextApiRequest, NextApiResponse>({ ... })

TypeScript + Next.js

https://github.com/chimurai/http-proxy-middleware/blob/f4f4a773c117b945b5e3e70c88d27b5a19b7d462/recipes/servers.md?plain=1#L73-L79

Javascript + express

/** @type {import('http-proxy-middleware/dist/types').RequestHandler<express.Request, express.Response>} */
const exampleProxy = createProxyMiddleware({
  target: 'http://www.example.org/api', // target host with the same base path
  changeOrigin: true, // needed for virtual hosted sites
});