feat: embed specHash from spec-metadata.json in published package#91
feat: embed specHash from spec-metadata.json in published package#91
Conversation
Add post-gen hook (050-emit-spec-hash.ts) that reads specHash from
spec-metadata.json and writes src/gen/specHash.ts with an exported
SPEC_HASH constant. This constant is re-exported from src/index.ts
and included in the dist/ output.
Consumers can import { SPEC_HASH } to audit which API spec version
was used to generate a given SDK release.
Closes #90
Add post-gen hook (050-emit-spec-hash.ts) that reads specHash from
spec-metadata.json and writes src/gen/specHash.ts with an exported
SPEC_HASH constant. This constant is re-exported from src/index.ts
and included in the dist/ output.
Consumers can import { SPEC_HASH } to audit which API spec version
was used to generate a given SDK release.
Closes #90
There was a problem hiding this comment.
Pull request overview
This PR embeds the bundled OpenAPI specHash into the generated SDK output and re-exports it from the package entrypoint so consumers can audit which upstream spec a published SDK build was generated from.
Changes:
- Added a post-generation hook to emit
src/gen/specHash.tsfromexternal-spec/bundled/spec-metadata.json. - Re-exported
SPEC_HASHfromsrc/index.tsso it’s included indist/. - Added a Vitest unit test validating the exported hash format.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| hooks/post/050-emit-spec-hash.ts | New post-hook that reads specHash from spec metadata and writes src/gen/specHash.ts. |
| src/gen/specHash.ts | Generated constant file exporting SPEC_HASH. |
| src/index.ts | Re-exports SPEC_HASH from the package entrypoint. |
| tests/spec-hash.test.ts | Unit test asserting SPEC_HASH is present and matches the expected sha256 format. |
| const metadata = JSON.parse(fs.readFileSync(METADATA_PATH, 'utf8')); | ||
| const specHash: string = metadata.specHash ?? ''; | ||
|
|
||
| if (!specHash.startsWith('sha256:')) { | ||
| console.warn(`[emit-spec-hash] Unexpected specHash format: ${specHash}`); | ||
| } |
There was a problem hiding this comment.
The hook defaults specHash to '' and only warns on unexpected formats, but still writes SPEC_HASH. That can result in publishing an empty/invalid hash (or failing tests later) without a clear failure cause. Consider validating against the expected /^sha256:[0-9a-f]{64}$/ and aborting (non-zero exit) if specHash is missing or invalid.
- Fail fast (throw) when spec-metadata.json is missing instead of skipping
- Validate specHash against /^sha256:[0-9a-f]{64}$/ and abort if invalid
- Use JSON.stringify for safe string literal emission
- Remove duplicate SPEC_HASH export in src/index.ts
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Released in |
Summary
Embeds the
specHashfromspec-metadata.jsonas an exported constant in the published npm package.Changes
hooks/post/050-emit-spec-hash.ts— readsspecHashfromexternal-spec/bundled/spec-metadata.jsonand writessrc/gen/specHash.tsSPEC_HASHre-exported fromsrc/index.ts→ included indist/tests/spec-hash.test.ts— verifies non-empty string withsha256:prefixUsage
Verification
npm run buildpassesSPEC_HASHaccessible via both ESM and CJS imports/^sha256:[0-9a-f]{64}$/Closes #90