openapi-ts / openapi-typescript

Generate TypeScript types from OpenAPI 3 specs
https://openapi-ts.dev
MIT License
5.2k stars 419 forks source link

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

Open SkelatorIndy opened 1 month ago

SkelatorIndy commented 1 month ago

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

drwpow commented 1 month ago

I believe this is a duplicate of #1569 ?

SkelatorIndy commented 1 month ago

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 commented 1 month ago

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.