chore: Auto-generate schema type aliases alongside API schema#3580
chore: Auto-generate schema type aliases alongside API schema#3580
Conversation
Extends generate-schemas.js to produce a companion *Types.generated.ts
file that re-exports all OpenAPI schema types as named exports. This
allows importing types directly (e.g. `import type { QaIssueModel }
from 'tg.service/apiSchemaTypes.generated'`) instead of extracting them
from the components interface in every file.
📝 WalkthroughWalkthroughReplaces async command execution with synchronous runs in the schema generator, adds fetching and validation of the OpenAPI JSON (specUrl), derives sorted schema names, writes a new generated type-alias file, improves CLI error messaging, and changes execution order to generate schemas then types, then log completion. Changes
Sequence Diagram(s)sequenceDiagram
participant Script as Schema Generation Script
participant Tool as openapi-typescript Tool
participant Spec as OpenAPI Spec Server
participant FS as File System
Script->>Script: prepare command & specUrl
Script->>Tool: run openapi-typescript (sync, stdio: inherit)
Tool->>FS: write `apiSchema.generated.ts`
Script->>Spec: fetch OpenAPI JSON from specUrl
Spec-->>Script: return OpenAPI JSON (response.ok)
Script->>Script: validate components.schemas exists & extract keys
Script->>Script: sort schema names & build type aliases
Script->>FS: write `apiSchemaTypes.generated.ts`
Script->>Script: log "Schema generation completed."
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
webapp/scripts/generate-schemas.js (1)
38-79: Refactor to top-down flow to satisfy the Stepdown Rule.The high-level orchestration (Lines 77-79) appears after helper details. Prefer a
main()defined near the top, with helper functions below it.As per coding guidelines
**/*.{ts,tsx,js,jsx,kt,kts}: The Stepdown Rule... Functions should be ordered so that a caller appears before the functions it calls.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@webapp/scripts/generate-schemas.js` around lines 38 - 79, Define a top-level async function main() near the top of the file that orchestrates the flow (call generateSchema(); await generateSchemaTypes(); console.log('Schema generation completed.')), move the helper functions generateSchema and generateSchemaTypes below it, and replace the existing top-level calls (generateSchema(); await generateSchemaTypes(); console.log(...)) with a single call to main(); this removes the top-level await and satisfies the Stepdown Rule so callers (main) appear before the functions they invoke (generateSchema, generateSchemaTypes).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@webapp/scripts/generate-schemas.js`:
- Line 65: The emitted import uses a relative path string `import type {
components } from './${schemaBasename}';` in generate-schemas.js; change the
template to use the Tolgee alias form `tg.service/${schemaBasename}` so
generated files use the repo's path alias. Update the string/template that
constructs that import (where `schemaBasename` is interpolated) in
generate-schemas.js to emit `tg.service/${schemaBasename}` instead of a leading
"./", and ensure any surrounding code that writes the import line still formats
correctly for TypeScript type imports.
- Around line 51-53: The code assumes spec.components.schemas exists before
calling Object.keys, which causes a generic crash when missing; update the logic
around spec (the variable parsed from response.json) to guard that
spec.components and spec.components.schemas are present and are objects before
calling Object.keys, and if not, throw or log a clear, fast-fail error message
referencing spec.components.schemas (e.g., in the block that assigns
schemaNames) so the script exits with an explicit message instead of a generic
runtime error.
---
Nitpick comments:
In `@webapp/scripts/generate-schemas.js`:
- Around line 38-79: Define a top-level async function main() near the top of
the file that orchestrates the flow (call generateSchema(); await
generateSchemaTypes(); console.log('Schema generation completed.')), move the
helper functions generateSchema and generateSchemaTypes below it, and replace
the existing top-level calls (generateSchema(); await generateSchemaTypes();
console.log(...)) with a single call to main(); this removes the top-level await
and satisfies the Stepdown Rule so callers (main) appear before the functions
they invoke (generateSchema, generateSchemaTypes).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: f2e2bbcf-f7c6-4818-b7db-9cba4c32c20d
📒 Files selected for processing (2)
webapp/scripts/generate-schemas.jswebapp/src/service/apiSchemaTypes.generated.ts
I wanted to add something like this for a while. Premise:
Summary
webapp/scripts/generate-schemas.jsto produce a companion*Types.generated.tsfile alongside the existingapiSchema.generated.tsimport type { QaIssueModel } from 'tg.service/apiSchemaTypes.generated'instead oftype QaIssueModel = components['schemas']['QaIssueModel']Summary by CodeRabbit
New Features
Chores