glideapps / quicktype

Generate types and converters from JSON, Schema, and GraphQL
https://app.quicktype.io
Apache License 2.0
12.31k stars 1.07k forks source link

One TS file from multiple schemas #2487

Open codan84 opened 8 months ago

codan84 commented 8 months ago

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 commented 7 months ago

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 commented 6 months ago

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 commented 6 months ago

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

Error: Option parsing failed: UNKNOWN_OPTION: Unknown option: --combine-files.
rodcloutier commented 5 months ago

@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.