npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ovotech/avro-ts

v6.2.0

Published

Convert avro schemas into typescript interfaces

Downloads

152,616

Readme

Avro TS

Generate typescript from avro types.

Uses typescript's compiler api to convert avro to typescript AST, and pretty prints the results.

Using

yarn add @ovotech/avro-ts

And then you can use the function to get typescript types:

examples/simple.ts

import { toTypeScript } from '@ovotech/avro-ts';
import { Schema } from 'avsc';

const avro: Schema = {
  type: 'record',
  name: 'User',
  fields: [
    { name: 'id', type: 'int' },
    { name: 'username', type: 'string' },
  ],
};

const ts = toTypeScript(avro);

console.log(ts);

Resulting TypeScript:

export type AvroType = User;

export interface User {
  id: number;
  username: string;
}

Logical Types

Avro has logical types. In their docs:

The built-in types provided by Avro are sufficient for many use-cases, but it can often be much more convenient to work with native JavaScript objects.

To support them we need to modify the typescript generation to use the typescript type instead of the logical type. If we don't avro-ts will fall back on the original underlying type.

examples/logical-types.ts

import { toTypeScript } from '@ovotech/avro-ts';
import { Schema } from 'avsc';

const avro: Schema = {
  type: 'record',
  name: 'Event',
  fields: [
    { name: 'id', type: 'int' },
    { name: 'createdAt', type: { type: 'long', logicalType: 'timestamp-millis' } },
  ],
};

const ts = toTypeScript(avro, {
  logicalTypes: {
    'timestamp-millis': 'string',
  },
});

console.log(ts);

Resulting TypeScript:

export type AvroType = Event;

export interface Event {
  id: number;
  createdAt: string;
}

We can also use custom classes for our logical types. It will also add the code to import the module.

examples/custom-logical-types.ts

import { toTypeScript } from '@ovotech/avro-ts';
import { Schema } from 'avsc';

const avro: Schema = {
  type: 'record',
  name: 'Event',
  fields: [
    { name: 'id', type: 'int' },
    { name: 'decimalValue', type: { type: 'long', logicalType: 'decimal' } },
    { name: 'anotherDecimal', type: { type: 'long', logicalType: 'decimal' } },
  ],
};

const ts = toTypeScript(avro, {
  logicalTypes: {
    decimal: { module: 'decimal.js', named: 'Decimal' },
  },
});

console.log(ts);

Resulting TypeScript:

import { Decimal } from 'decimal.js';

export type AvroType = Event;

export interface Event {
  id: number;
  decimalValue: Decimal;
  anotherDecimal: Decimal;
}

Wrapped Unions

Avro Ts attempts to generate the types of the "auto" setting for wrapped unions. https://github.com/mtth/avsc/wiki/API#typeforschemaschema-opts This would mean that unions of records would be wrapped in an object with namespaced keys.

The typescript interfaces are also namespaced appropriately. Avro namespaces like 'com.example.avro' are converted into ComExampleAvro namespaces in TS.

examples/wrapped-union.ts

import { toTypeScript } from '@ovotech/avro-ts';
import { Schema } from 'avsc';

const avro: Schema = {
  type: 'record',
  name: 'Event',
  namespace: 'com.example.avro',
  fields: [
    { name: 'id', type: 'int' },
    {
      name: 'event',
      type: [
        {
          type: 'record',
          name: 'ElectricityEvent',
          fields: [
            { name: 'accountId', type: 'string' },
            { name: 'MPAN', type: 'string' },
          ],
        },
        {
          type: 'record',
          name: 'GasEvent',
          fields: [
            { name: 'accountId', type: 'string' },
            { name: 'MPRN', type: 'string' },
          ],
        },
      ],
    },
  ],
};

const ts = toTypeScript(avro);

console.log(ts);

Which would result in this typescript:

/* eslint-disable @typescript-eslint/no-namespace */

export type Event = ComExampleAvro.Event;

export namespace ComExampleAvro {
  export const ElectricityEventName = 'com.example.avro.ElectricityEvent';
  export interface ElectricityEvent {
    accountId: string;
    MPAN: string;
  }
  export const GasEventName = 'com.example.avro.GasEvent';
  export interface GasEvent {
    accountId: string;
    MPRN: string;
  }
  export const EventName = 'com.example.avro.Event';
  export interface Event {
    id: number;
    event:
      | {
          'com.example.avro.ElectricityEvent': ComExampleAvro.ElectricityEvent;
          'com.example.avro.GasEvent'?: never;
        }
      | {
          'com.example.avro.ElectricityEvent'?: never;
          'com.example.avro.GasEvent': ComExampleAvro.GasEvent;
        };
  }
}

Notice that not only the interfaces themselves are exported, but their fully qualified names as well. This should help to improve readability.

We also breakout the root type from its namespace for ease of use.

import { ComExampleAvro as NS, Event } from '...';

const elecEvent: Event = {
  id: 10,
  event: { [NS.ElectricityEventName]: { MPAN: '111', accountId: '123' } },
};

const gasEvent: Event = {
  id: 10,
  event: { [NS.GasEventName]: { MPRN: '222', accountId: '123' } },
};

Defaults as optional

If you are creating avro objects, that have defaults in their schema, then by definition they are optional. It is not possible to express those default values in TypeScript so that one interface works for both creating and reading objects with default values. That's why we provide a flag to specify that we want the values that have defaults to be optional.

You can generate 2 sets of types - one for consuming one for producing avro objects this way.

examples/defaults-as-optional.ts

import { toTypeScript } from '@ovotech/avro-ts';
import { Schema } from 'avsc';

const avro: Schema = {
  type: 'record',
  name: 'User',
  fields: [
    { name: 'id', type: 'int' },
    { name: 'username', type: 'string', default: 'Simon' },
  ],
};

const ts = toTypeScript(avro, { defaultsAsOptional: true });

console.log(ts);

Typescript Enums

By default AVRO enums are converted to a string union. If you prefer to use Typescript enums instead, you can use withTypescriptEnums option.

examples/with-typescript-enums.ts

import { toTypeScript } from '@ovotech/avro-ts';
import { Schema } from 'avsc';

const avro: Schema = {
  type: 'record',
  name: 'User',
  fields: [
    { name: 'id', type: 'int' },
    {
      "name": "status",
      "type": { "type": "enum", "name": "Status", "symbols": ["Active", "Inactive"] },
      "doc": "The status of the user account"
    },
  ],
};

const ts = toTypeScript(avro, { withTypescriptEnums: true });

console.log(ts);

External references

AvroTs supports external references to schemas in other files. In order to do that you'll need to convert the external schemas first, and then pass them as "external" in the initial context. This can be used as a building blocks to process multiple schemas at once.

examples/external.ts

import { toTypeScript, toExternalContext } from '@ovotech/avro-ts';
import { readFileSync } from 'fs';
import { join } from 'path';

const createUserSchema = JSON.parse(
  readFileSync(join(__dirname, 'external-CreateUser.json'), 'utf-8'),
);
const addressSchema = JSON.parse(readFileSync(join(__dirname, 'external-Address.json'), 'utf-8'));

const addressContext = toExternalContext(addressSchema);

const ts = toTypeScript(createUserSchema, { external: { './external-Address': addressContext } });

console.log(ts);

Running the tests

Then you can run the tests with:

yarn test

Coding style (linting, etc) tests

Style is maintained with prettier and tslint

yarn lint

Deployment

Deployment is preferment by lerna automatically on merge / push to main, but you'll need to bump the package version numbers yourself. Only updated packages with newer versions will be pushed to the npm registry.

Contributing

Have a bug? File an issue with a simple example that reproduces this so we can take a look & confirm.

Want to make a change? Submit a PR, explain why it's useful, and make sure you've updated the docs (this file) and the tests (see test folder).

License

This project is licensed under Apache 2 - see the LICENSE file for details