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

Inconsistent openapi-typescript import #35

Closed
mattmess1221 opened this issue Aug 22, 2023 · 3 comments
Closed

Inconsistent openapi-typescript import #35

mattmess1221 opened this issue Aug 22, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@mattmess1221
Copy link
Contributor

Environment


  • Operating System: Linux
  • Node Version: v18.16.0
  • Nuxt Version: 3.6.5
  • Nitro Version: 2.5.2
  • Package Manager: [email protected]
  • Builder: vite
  • User Config: modules, apiParty, typescript
  • Runtime Modules: [email protected]
  • Build Modules: -

Reproduction

Reproduction can be done with the playground being run separate from the rest of the repo.

cp -r playground ../api-playground
cd ../api-playground
echo 'shamefully-hoist=true' > .npmrc
pnpm add -D nuxt nuxt-api-party openapi-typescript
pnpm nuxi prepare

Describe the bug

Whether openapi-typescript is imported as a module or function seems to depend on whether the package is installed in node_modules. When in node_modules, it behaves as expected, being imported as a module. Otherwise, it imports the function. I'm not sure whether this is an issue with node or typescript.

I suggest the following change to openapi.ts.

diff --git a/src/openapi.ts b/src/openapi.ts
index 2527990..cc84fd5 100644
--- a/src/openapi.ts
+++ b/src/openapi.ts
@@ -3,6 +3,15 @@ import { useNuxt } from '@nuxt/kit'
 import type { OpenAPI3, OpenAPITSOptions } from 'openapi-typescript'
 import type { Endpoint } from './module'

+function importOpenapiTypescript() {
+  return import('openapi-typescript').then((openapiTS) => {
+    // The import is somehow different during dev and prod
+    if (typeof openapiTS === 'function')
+      return openapiTS
+    return openapiTS.default
+  })
+}
+
 export async function generateTypes(
   endpoints: Record<string, Endpoint>,
   globalOpenAPIOptions: OpenAPITSOptions,
@@ -15,7 +24,7 @@ export async function generateTypes(
       throw new Error('Caught process.exit()')
   })

-  const openapiTS = await import('openapi-typescript')
+  const openapiTS = await importOpenapiTypescript()
   const schemas = await Promise.all(
     Object.keys(endpoints).map(async (id) => {
       let types = ''
@@ -25,7 +34,6 @@ export async function generateTypes(
       runningCount++

       try {
-        // @ts-expect-error: ESM import type mismatch
         types = await openapiTS(schema, {
           commentHeader: '',
           ...globalOpenAPIOptions,

Additional context

No logs were created to indicate an error, just the types not being generated. When I edited the javascript to add a console.error() inside the catch block surrounding openapiTS(), I got the error openapiTS is not a function.

Logs

ERROR  openapiTS is not a function                                                                                                                                                                                                          5:02:09 PM

  at /home/username/api-playground/node_modules/.pnpm/[email protected]/node_modules/nuxt-api-party/dist/module.mjs:20:23
  at async Promise.all (index 0)
@johannschopplich johannschopplich added bug Something isn't working and removed pending triage labels Aug 22, 2023
@johannschopplich
Copy link
Owner

johannschopplich commented Aug 22, 2023

I believe this bug occurs with module resolution bundler, introduced in TypeScript 5. By default, Nuxt uses node module resolution. Just like Vite now recommends (and in their templates uses) the bundler resolution, Nuxt will switch in later versions.

To opt-in to this behaviour, once can use:

export default defineNuxtConfig({
  typescript: {
    tsConfig: {
      compilerOptions: {
        moduleResolution: 'bundler',
      },
    },
  },
})

… Which is already being used by the playground. Types seem to be imported differently, as well as the module import itself.

Edit: Should be fixed in v0.15.2.

@johannschopplich
Copy link
Owner

@killjoy1221 Can you verify that it now works as intended? 🙂

@mattmess1221
Copy link
Contributor Author

mattmess1221 commented Aug 23, 2023 via email

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

No branches or pull requests

2 participants