Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

One TS file from multiple schemas #2487

Open
codan84 opened this issue Feb 7, 2024 · 4 comments
Open

One TS file from multiple schemas #2487

codan84 opened this issue Feb 7, 2024 · 4 comments

Comments

@codan84
Copy link

codan84 commented Feb 7, 2024

I have a directory with multiple schemas, some of them $ref others:

schemas/
  draft.json
  new-draft.json
  enums/
    some-enum.json

Both draft and new-draft reference the enums/some-enum.json.

Is there a way I can convert this to 1 typescript file with interfaces/enums declared for each?

@SaintPepsi
Copy link

Yes, you can use Quicktype to generate TypeScript interfaces and enums for your JSON schemas. Quicktype supports resolving $ref references to external files, so it should work well for your scenario.

Here's how you can do it:

  1. Install Quicktype if you haven't already:

    npm install -g quicktype
    
  2. Use Quicktype to generate TypeScript interfaces and enums:

    quicktype --src-lang schema --lang ts --combine-files schemas/draft.json schemas/new-draft.json schemas/enums/some-enum.json
    

    This command tells Quicktype to generate TypeScript code (--lang ts) by combining multiple schema files (--combine-files). Replace the file paths with your actual file structure.

Quicktype will then generate a single TypeScript file containing interfaces and enums for each schema, including the referenced enums from enums/some-enum.json. You can then use these TypeScript definitions in your code.

@codan84
Copy link
Author

codan84 commented Mar 19, 2024

Thanks, this unfortunately will explode out of proportion with the number of schemas I am dealing with. Is there no way to automaticaly include all schemas in a directory (recursively)?

@codan84
Copy link
Author

codan84 commented Mar 20, 2024

Furthermore - what version of quicktype are you using @SaintPepsi ? I am getting:

Error: Option parsing failed: UNKNOWN_OPTION: Unknown option: --combine-files.

@rodcloutier
Copy link

@codan84 It seems that you don't need the --combine-files option. Just specifying the files at the command line will combine them in the output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants