Skip to content
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

Use in Next.js's middleware.ts causes error #1718

Open
1 task
SkelatorIndy opened this issue Jun 21, 2024 · 3 comments
Open
1 task

Use in Next.js's middleware.ts causes error #1718

SkelatorIndy opened this issue Jun 21, 2024 · 3 comments
Labels
bug Something isn't working openapi-fetch Relevant to the openapi-fetch library

Comments

@SkelatorIndy
Copy link

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
image

Server Error
image

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

@SkelatorIndy SkelatorIndy added bug Something isn't working openapi-fetch Relevant to the openapi-fetch library labels Jun 21, 2024
@drwpow
Copy link
Contributor

drwpow commented Jun 21, 2024

I believe this is a duplicate of #1569 ?

@SkelatorIndy
Copy link
Author

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.

@FreeAoi
Copy link
Contributor

FreeAoi commented Jun 22, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working openapi-fetch Relevant to the openapi-fetch library
Projects
None yet
Development

No branches or pull requests

3 participants