nuxt-open-fetch icon indicating copy to clipboard operation
nuxt-open-fetch copied to clipboard

Optionally skipping schema based codegen on startup

Open StepanMynarik opened this issue 8 months ago • 8 comments

Describe the feature

Open Fetch does codegen on every startup and now also on every hot reload. We have a huge schema file that changes like once a year. The codegen takes 11 seconds.

Would it be possible to add some option to skip the codegen on startup? It would be a nice DX improvement.

Additional information

  • [x] Would you be willing to help implement this feature?

Final checks

StepanMynarik avatar Mar 12 '25 10:03 StepanMynarik

Same problem here, great suggestion

letoast avatar Jul 08 '25 09:07 letoast

An alternative solution for the long codegen could be to define the path to the openapi-ts-generated types file directly instead of an OpenAPI JSON file in the config. In this mode, the module would just read the provided types file and generation would be done manually using an npx command. The developer can then decide when to update the types themselves (when they change something on the backend).

The automatic codegen is the only thing discouraging me from using this library, but the rest seems very nice.

itzTerra avatar Aug 31 '25 13:08 itzTerra

I implement a feature that should cache your generated openapiTS files. In future versions, I may expand this cache to not include stuff like api client name, but I want to avoid breaking anything for now.

Norbiros avatar Aug 31 '25 15:08 Norbiros

I implement a feature that should cache your generated openapiTS files. In future versions, I may expand this cache to not include stuff like api client name, but I want to avoid breaking anything for now.

I updated to version 0.13.4 to immediately try it, but the caching didn't seem to kick in in my case. The .nuxt/cache/open-fetch folder is there, but remains empty no matter what.

So I checked the code in src/module.ts and saw that only local files support caching. The only large schema I have (40 MB, don't ask 😆) is a local file. So I don't understand where is the problem.

No errors in the console either.

My config looks like this:

openFetch: {
    disableNitroPlugin: true,
    clients: {
      abra: {
        // !!! This is the huge schema file !!!
        schema: "./server/abra_api/schema_postprocessed.json",
        baseURL: process.env.NUXT_OPEN_FETCH_ABRA_BASE_URL,
        headers: {
         *REDACTED*
        },
      },
      mapyCz: {
        schema: "https://api.mapy.cz/v1/docs/geocode/openapi.json",
        baseURL: process.env.NUXT_OPEN_FETCH_MAPY_CZ_BASE_URL,
        headers: {
          *REDACTED*
        },
      },
      foxentry: {
        schema: "https://raw.githubusercontent.com/Foxentry/OAS/refs/heads/main/openapi.yaml",
        baseURL: process.env.NUXT_OPEN_FETCH_FOXENTRY_BASE_URL,
        headers: {
          *REDACTED*
        },
      },
    },
  },

StepanMynarik avatar Sep 01 '25 14:09 StepanMynarik

Maybe there is an issue with resolving relative payhs? I'll try looking into that today and if not this week for sure.

Norbiros avatar Sep 03 '25 06:09 Norbiros

Hmm… interesting. Does type checking pass correctly in the new version?

I haven’t been able to reproduce the issue myself. Could you provide more details? Specifically:

  • a full working example app that demonstrates the problem or
  • try opening the source of addCachedSchemaTemplate in your IDE and add some console logs to trace its behaviour.

If that still doesn’t help, I can improve the current logging system of Nuxt Open Fetch and release a new version, but I don't have a lot of time this week.

Norbiros avatar Sep 04 '25 16:09 Norbiros

@Norbiros

Thanks for the tip!

I added some logs and found out what the problem is. My schema schema: "./server/abra_api/schema_postprocessed.json", is resolved to file path /C:/Work/*redacted*/sync-bridge/server/abra_api/schema_postprocessed.json in the addCachedSchemaTemplate function.

Notice the / character at the start of the path, just before C:/. No clue where it came from.

Therefore the condition on this line if (typeof filePath === "string" && existsSync(filePath)) { resolves as false and the caching is always skipped, even though the path leads to a local file.

StepanMynarik avatar Sep 08 '25 08:09 StepanMynarik

UPDATE: So I found out that the schema path is wrapped in URL. So in addCachedSchemaTemplate the URL pathname is read, which in my case looks like this:

URL { href: 'file:///C:/Work/*redacted*/sync-bridge/server/abra_api/schema_postprocessed.json',                                                                                                                                                                  
  origin: 'null',
  protocol: 'file:',
  username: '',
  password: '',
  host: '',
  hostname: '',
  port: '',
  pathname: '/C:/Work/*redacted*/sync-bridge/server/abra_api/schema_postprocessed.json',
  search: '',
  searchParams: URLSearchParams {},
  hash: '' }

The / prefix is already there.

UPDATE2: Fixed by importing import { fileURLToPath } from 'url'; and changing const filePath = schema instanceof URL ? schema.pathname : schema; to const filePath = schema instanceof URL ? fileURLToPath(schema) : schema;.

Added a PR to hopefully save you a few minutes.

Thank you for the library, it is perfect with the caching now!

StepanMynarik avatar Sep 08 '25 08:09 StepanMynarik