Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
@ovotech/avro-ts
Advanced tools
Generate typescript from avro types.
Uses typescript's compiler api to convert avro to typescript AST, and pretty prints the results.
yarn add @ovotech/avro-ts
And then you can use the function to get typescript types:
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;
}
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.
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.
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;
}
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.
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' } },
};
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.
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);
By default AVRO enums are converted to a string union. If you prefer to use Typescript enums instead, you can use withTypescriptEnums option.
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);
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.
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);
Then you can run the tests with:
yarn test
Style is maintained with prettier and tslint
yarn lint
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.
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).
This project is licensed under Apache 2 - see the LICENSE file for details
FAQs
Convert avro schemas into typescript interfaces
We found that @ovotech/avro-ts demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 36 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.