quicktype json schema to typescript just-type in custom function

1.1k Views Asked by At

I have a function that use the quicktype API. It works but I'd like to use "just-types" option to keep only the typescript interfaces (without the classes and function).

Here is the code I have:

async function* getFiles(dir) {
  const dirents = await readdir(dir, { withFileTypes: true });
  for (const dirent of dirents) {
    const res = resolve(dir, dirent.name);
    if (dirent.isDirectory()) {
      yield* getFiles(res);
    } else {
      yield res;
    }
  }
}

(async () => {
  const directoryPath = join(__dirname, "XML")

  for await (const file of getFiles(directoryPath)) {
    const filename = basename(file);
    const filePathArray = file.split('\\');
    const foldername = filePathArray[filePathArray.length - 2];

    if (!fs.existsSync(join(__dirname, 'interfaces', foldername))){
      fs.mkdirSync(join(__dirname, 'interfaces', foldername));
    }
    const interfaceName = filename.split('.')[0];

    const schemaInput = new JSONSchemaInput(new q.JSONSchemaStore());
    schemaInput.addSource({ name: interfaceName, schema: fs.readFileSync(file, "utf8")});

    const inputData = new InputData();
    inputData.addInput(schemaInput);

    quicktype({inputData, lang: new TypeScriptTargetLanguage()}).then(function (data) {
      fs.writeFile(join(__dirname, 'interfaces', foldername, interfaceName + '.ts'), data.lines.join('\n'), function () {});
    });
  }
})()

I've seen the TypeScriptRenderer class who can takes as third parameter a boolean "justTypes" but I don't know how to make it works.

I've tried passing all the arguments:

TypeScriptRenderer(new TypeScriptTargetLanguage(), ??, { justTypes: true });

The second argument is the RenderContext. I don't know how to instance it.

Somebody knows how to make it works?

1

There are 1 best solutions below

1
paul at stepupsoftware On

If anyone else stumbles upon this looking for a similar answer, you can do

quicktype({ lang: new TypeScriptTargetLanguage(), inputData, rendererOptions: { 'just-types': 'true' } });