How to use quicktype-core - 10 common examples

To help you get started, we’ve selected a few quicktype-core examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github angular / angular-cli / tools / quicktype_runner.js View on Github external
async function generate(inPath) {
  // Best description of how to use the API was found at
  //   https://blog.quicktype.io/customizing-quicktype/
  const inputData = new InputData();
  const content = fs.readFileSync(inPath, 'utf-8');
  const source = { name: 'Schema', schema: appendDeprecatedDescription(content) };

  await inputData.addSource('schema', source, () => {
    return new JSONSchemaInput(new FetchingJSONSchemaStore(inPath));
  });

  const lang = new TypeScriptTargetLanguage();

  const { lines } = await quicktype({
    lang,
    inputData,
    alphabetizeProperties: true,
    rendererOptions: {
      'just-types': 'true',
      'explicit-unions': 'true',
github quicktype / quicktype-vscode / src / extension.ts View on Github external
const configuration = vscode.workspace.getConfiguration(configurationSection);
    const justTypes = forceJustTypes || configuration.justTypes;

    const rendererOptions: RendererOptions = {};
    if (justTypes) {
        // FIXME: The target language should have a property to return these options.
        if (lang.name === "csharp") {
            rendererOptions["features"] = "just-types";
        } else if (lang.name === "kotlin") {
            rendererOptions["framework"] = "just-types";
        } else {
            rendererOptions["just-types"] = "true";
        }
    }

    const inputData = new InputData();
    switch (kind) {
        case "json":
            await inputData.addSource("json", { name: topLevelName, samples: [content] }, () =>
                jsonInputForTargetLanguage(lang)
            );
            break;
        case "schema":
            await inputData.addSource(
                "schema",
                { name: topLevelName, schema: content },
                () => new JSONSchemaInput(undefined)
            );
            break;
        case "typescript":
            await inputData.addSource(
                "schema",
github angular / angular-cli / tools / quicktype_runner.js View on Github external
async function generate(inPath) {
  // Best description of how to use the API was found at
  //   https://blog.quicktype.io/customizing-quicktype/
  const inputData = new InputData();
  const content = fs.readFileSync(inPath, 'utf-8');
  const source = { name: 'Schema', schema: appendDeprecatedDescription(content) };

  await inputData.addSource('schema', source, () => {
    return new JSONSchemaInput(new FetchingJSONSchemaStore(inPath));
  });

  const lang = new TypeScriptTargetLanguage();

  const { lines } = await quicktype({
    lang,
    inputData,
    alphabetizeProperties: true,
    rendererOptions: {
      'just-types': 'true',
      'explicit-unions': 'true',
      'acronym-style': 'camel',
    },
  });

  return header + lines.join('\n') + footer;
}
github segmentio / typewriter / src / commands / gen-js / typescript.ts View on Github external
this.forEachTopLevel('leading-and-interposing', (t, name) => {
      const camelCaseName = modifySource(camelCase, name)
      this.emitDescription(this.descriptionForType(t))

      // Override sourceFor behavior to disable `{ [key: string]: any }`
      // if no properties are set for an event.
      let type = this.sourceFor(t).source
      if (t instanceof MapType) {
        type = '{}'
      }

      if (this.ajsOptions.client === Client.js) {
        this.emitLine([
          camelCaseName,
          '(props?: ',
          type,
          ', options?: SegmentOptions, callback?: AnalyticsJSCallback): void'
        ])
github segmentio / typewriter / src / commands / gen-android.ts View on Github external
withOptions: boolean
  ): void {
    // TODO: Emit a function description, once we support top-level event descriptions in JSON Schema
    const description: Sourcelike = [
      ['@see <a href="https://segment.com/docs/spec/track/">Track Documentation</a>']
    ]
    if (hasProperties) {
      description.unshift([
        '@param props {@link ',
        name,
        '} to add extra information to this call.'
      ])
    }
    this.emitDescriptionBlock(description)

    const camelCaseName = modifySource(camelCase, name)
    this.emitBlock(
      [
        'public void ',
        camelCaseName,
        '(',
        ...(hasProperties ? ['final @Nullable ', name, ' props'] : []),
        hasProperties &amp;&amp; withOptions ? ', ' : '',
        withOptions ? 'final @Nullable Options options' : '',
        ')'
      ],
      () =&gt; {
        this.emitLine([
          'this.analytics.track("',
          getRawName(name),
          '", ',
          hasProperties ? 'props.toProperties()' : 'new Properties()',
github angular / angular-cli / tools / quicktype_runner.js View on Github external
async function generate(inPath) {
  // Best description of how to use the API was found at
  //   https://blog.quicktype.io/customizing-quicktype/
  const inputData = new InputData();
  const content = fs.readFileSync(inPath, 'utf-8');
  const source = { name: 'Schema', schema: appendDeprecatedDescription(content) };

  await inputData.addSource('schema', source, () => {
    return new JSONSchemaInput(new FetchingJSONSchemaStore(inPath));
  });

  const lang = new TypeScriptTargetLanguage();

  const { lines } = await quicktype({
    lang,
    inputData,
    alphabetizeProperties: true,
    rendererOptions: {
      'just-types': 'true',
      'explicit-unions': 'true',
      'acronym-style': 'camel',
    },
  });

  return header + lines.join('\n') + footer;
}
github quicktype / quicktype-vscode / src / extension.ts View on Github external
openForEditor(editor, "json")
        ),
        vscode.commands.registerTextEditorCommand(Command.OpenQuicktypeForJSONSchema, editor =>
            openForEditor(editor, "schema")
        ),
        vscode.commands.registerTextEditorCommand(Command.OpenQuicktypeForTypeScript, editor =>
            openForEditor(editor, "typescript")
        ),
        vscode.commands.registerCommand(Command.ChangeTargetLanguage, changeTargetLanguage)
    );

    await persist.init({ dir: path.join(homedir(), ".quicktype-vscode") });

    const maybeName = await persist.getItem(lastTargetLanguageUsedKey);
    if (typeof maybeName === "string") {
        explicitlySetTargetLanguage = languageNamed(maybeName);
    }
}
github segmentio / typewriter / src / commands / gen-ts.ts View on Github external
export async function genTS(events: TrackedEvent[]) {
  const inputData = new InputData()

  events.forEach(({ name, rules }) => {
    const schema = {
      $schema: 'http://json-schema.org/draft-04/schema#',
      title: rules.title,
      description: rules.description,
      ...rules.properties.properties
    }

    inputData.addSource(
      'schema',
      { name, uris: [name], schema: JSON.stringify(schema) },
      () => new JSONSchemaInput(undefined)
    )
  })
github segmentio / typewriter / src / lib / rules.ts View on Github external
export const processEventsForQuickType = (events: TrackedEvent[]) => {
  const inputData = new InputData()

  events.forEach(({ name, rules }) => {
    const schema = {
      $schema: rules.$schema || 'http://json-schema.org/draft-07/schema#',
      title: rules.title,
      description: rules.description,
      ...get(rules, 'properties.properties', {})
    }

    inputData.addSource(
      'schema',
      { name, uris: [name], schema: JSON.stringify(schema) },
      () => new JSONSchemaInput(undefined)
    )
  })
github servall / app-config / src / generate.ts View on Github external
augmentModule: boolean,
  leadingComments: string[] | undefined,
  rendererOptions: RendererOptions,
) =&gt; {
  const src: JSONSchemaSourceData = {
    name,
    schema: JSON.stringify(schema),
  };

  class FetchingJSONSchemaStore extends JSONSchemaStore {
    async fetch(address: string): Promise {
      return (schemaRefs as any)[address];
    }
  }

  const inputData = new InputData();
  await inputData.addSource(
    'schema',
    src,
    () =&gt; new JSONSchemaInput(new FetchingJSONSchemaStore(), []),
  );

  const { lines } = await quicktype({
    inputData,
    lang: type,
    indentation: '  ',
    leadingComments: (leadingComments || [
      'AUTO GENERATED CODE',
      'Run app-config with \'generate\' command to regenerate this file',
    ]),
    rendererOptions: {
      'just-types': 'true',