nuxt-open-fetch
nuxt-open-fetch copied to clipboard
Optionally skipping schema based codegen on startup
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
- [x] Read the contribution guide.
- [x] Check existing discussions and issues.
Same problem here, great suggestion
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.
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 implement a feature that should cache your generated
openapiTSfiles. 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*
},
},
},
},
Maybe there is an issue with resolving relative payhs? I'll try looking into that today and if not this week for sure.
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
addCachedSchemaTemplatein 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
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.
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!