openapi-typescript
openapi-typescript copied to clipboard
Use in Next.js's middleware.ts causes error
Description
When using the openapi-fetch client in Next.js 14.2.4's middleware.ts file, it causes the app to error
Client Error
Server Error
Reproduction
This can be reproduced by using the openapi-fetch client in the middleware.ts file of Next.js. The error does not occur if you use the standalone fetch client
Causes Error:
// Located in another file
const api = createClient<paths>({
baseUrl: 'https://localhost:5000',
});
export default async function middleware(request: NextRequest) {
await api.GET('/api/v1/auth/security');
return Response.redirect(new URL('/', request.url));
}
Works:
export default async function middleware(request: NextRequest) {
await fetch('http://localhost:5000/api/v1/auth/security');
return Response.redirect(new URL('/', request.url));
}
I made a sandbox using the latest version of Next.js showing this error occurring with a very minimal setup: Sandbox Reproduction
Expected result
Should be able to use openapi-fetch in middleware.ts
Checklist
- [ ] I’m willing to open a PR (see CONTRIBUTING.md)
I believe this is a duplicate of #1569 ?
The solution of manually replacing the fetch for the request with the standalone fetch does work, and because of the small scope of where this error occurs it is not a bad solution. However, since that issue mentioned a fix being merged in, and the follow up mentions specifically that the GET requests are fine, which mine is not, I was unsure if this may be a separate but related issue. However, if you do believe it is a duplicate of the same issue mentioned there, then this issue can be closed as a duplicate.
I'm going to take a look at the issue, I'm supposed to have made a PR trying to resolve this but it doesn't seem to cover some cases.
This issue is stale because it has been open for 90 days with no activity. If there is no activity in the next 7 days, the issue will be closed.
This issue was closed because it has been inactive for 7 days since being marked as stale. Please open a new issue if you believe you are encountering a related problem.
@FreeAoi There is no PR linked with this issue, has this been resolved in the mean time?