fix: load OpenAPI spec via static JSON require so bundlers include it#2
Open
max-programming wants to merge 1 commit into
Open
fix: load OpenAPI spec via static JSON require so bundlers include it#2max-programming wants to merge 1 commit into
max-programming wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1
what's happening
the sdk passes a yaml file path to
openapi-client-axios, which reads and parses it from disk oninit(). bundlers can't see a runtime string-concat file dependency, so the yaml never makes it into the deployed bundle and the first api call throwsENOENTin production (full details + repro in #1).the fix
converted the spec to json and load it with a static require instead:
bundlers statically inline json requires, so the spec always ships inside the bundle.
definitionalready accepts a pre-parsed document object (Document | stringin the v5 types), so nothing else changes.the yaml stays as the source of truth — the json is generated from it. added
npm run spec:buildso you can regenerate after editing the yaml (uses js-yaml, which is already around as a peer dep of openapi-client-axios).side benefit: js-yaml isn't needed at runtime anymore for consumers, and the spec file read on cold start goes away.
tests
added
test/openapi-spec.test.js(runs withnpx jest openapi-spec, no api token needed — left thenpm testscript untouched):verified
bundled the package with
npx esbuild --bundle --platform=nodeand ran the output from a directory with nonode_modules:Error opening file ".../Clearout-OpenAPI-Spec-V3.yaml" ENOENT— same failure as ENOENT reading Clearout-OpenAPI-Spec-V3.yaml when the SDK is bundled (Next.js on Vercel) #1