-
Notifications
You must be signed in to change notification settings - Fork 864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeScript regression in 2.0.3 #713
Comments
Hi. I'm getting the same TypeScript error with hpm 2.0.2 on https://github.com/accelist/nextjs-starter Personally, I haven't used NextJS yet. For now you could use a temporary workaround with const apiProxy = createProxyMiddleware(apiProxyOptions) as any; Not sure if you can chain middlewares in NextJS; if it is possible perhaps you could your logic to a separate middleware; This way you don't have the "call" the HPM middleware like you are currently doing. |
#721 uses the |
Hi, thanks for troubleshooting. I am 1000% certain that the TypeScript error does not appear when using hpm 2.0.2. (But I do believe you, of course) We are running that template for multiple projects in our organization and the TypeScript errors are encountered by many teams if they tried upgrading to hpm 2.0.3 but not when hpm 2.0.2 is pinned to the
Seeing that Next.js API endpoints support connect / express compatible middlewares and HPM is advertised in the README that it is a connect / express compatible middleware, they probably will work seamlessly if weren't for the type error. We just need to make sure that the middleware type definition is callable.
I think the other "API routes using 3rd party middlewares" example in the Next.js documentation page also demonstrates "calling" the middleware, so I think it's not a very alien practice imo. Copy pasted from the official Next.js documentation: import Cors from 'cors'
// Initializing the cors middleware
const cors = Cors({
methods: ['GET', 'HEAD'],
})
// Helper method to wait for a middleware to execute before continuing
// And to throw an error when an error happens in a middleware
function runMiddleware(req, res, fn) {
return new Promise((resolve, reject) => {
fn(req, res, (result) => {
if (result instanceof Error) {
return reject(result)
}
return resolve(result)
})
})
}
async function handler(req, res) {
// Run the middleware
await runMiddleware(req, res, cors)
// Rest of the API logic
res.json({ message: 'Hello Everyone!' })
}
export default handler |
For those looking for a more permanent solution for Next.js, drop the This is a sample source code for forwarding an API route to a back-end server using |
http-proxy-middleware It's still in beta. You can give it a try and install it with Happy to hear your feedback. https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/servers.md#nextjs // Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from 'next';
import { createProxyMiddleware } from 'http-proxy-middleware';
const proxyMiddleware = createProxyMiddleware({
target: 'http://jsonplaceholder.typicode.com',
changeOrigin: true,
pathRewrite: {
'^/api/users': '/users',
},
});
export default function handler(req: NextApiRequest, res: NextApiResponse) {
proxyMiddleware(req, res, (result: unknown) => {
if (result instanceof Error) {
throw result;
}
});
}
export const config = {
api: {
externalResolver: true,
},
};
// curl http://localhost:3000/api/users |
Using apollo client I had to change the config in this way:
otherwise the request get stuck in pending. |
Checks
http-proxy-middleware
.Describe the bug (be clear and concise)
This is the entire code.
In version 2.0.2, that code can be does not emit compile error in TypeScript.
But when upgrading to version 2.0.3, an error appeared:
Step-by-step reproduction instructions
Place the above code in a TypeScript project.
If require a reproduction repository, run these commands:
Expected behavior (be clear and concise)
No TypeScript type error in version 2..0.3
How is http-proxy-middleware used in your project?
What http-proxy-middleware configuration are you using?
What OS/version and node/version are you seeing the problem?
Additional context (optional)
No response
The text was updated successfully, but these errors were encountered: