@ovotech/avro-ts
Advanced tools
Comparing version 6.0.9 to 6.1.0
@@ -9,2 +9,2 @@ import { Document } from '@ovotech/ts-compose'; | ||
export declare const nameParts: (fullName: string) => [string] | [string, string]; | ||
export declare const namedType: (type: ts.InterfaceDeclaration | ts.TypeAliasDeclaration, context: Context, schema: avroSchema.RecordType | avroSchema.EnumType, namespace?: string | undefined) => Document<ts.TypeNode, Context>; | ||
export declare const namedType: (type: ts.InterfaceDeclaration | ts.TypeAliasDeclaration | ts.EnumDeclaration, context: Context, schema: avroSchema.RecordType | avroSchema.EnumType, namespace?: string | undefined) => Document<ts.TypeNode, Context>; |
@@ -28,3 +28,4 @@ import * as ts from 'typescript'; | ||
defaultsAsOptional?: boolean; | ||
withTypescriptEnums?: boolean; | ||
} | ||
export declare type Convert<TSchema = Schema, TType = ts.TypeNode> = (context: Context, type: TSchema) => Document<TType, Context>; |
@@ -6,2 +6,3 @@ "use strict"; | ||
const helpers_1 = require("../helpers"); | ||
const record_1 = require("./record"); | ||
const isEnumType = (type) => typeof type === 'object' && 'type' in type && type.type === 'enum'; | ||
@@ -12,10 +13,17 @@ exports.isEnumType = isEnumType; | ||
const namespace = (_a = schema.namespace) !== null && _a !== void 0 ? _a : context.namespace; | ||
const type = ts_compose_1.Type.Alias({ | ||
name: helpers_1.convertName(helpers_1.firstUpperCase(schema.name)), | ||
type: ts_compose_1.Type.Union(schema.symbols.map((symbol) => ts_compose_1.Type.Literal(symbol))), | ||
isExport: true, | ||
jsDoc: schema.doc, | ||
}); | ||
const type = context.withTypescriptEnums === true | ||
? ts_compose_1.Node.Enum({ | ||
name: helpers_1.convertName(helpers_1.firstUpperCase(schema.name)), | ||
members: schema.symbols.map((val) => ts_compose_1.Node.EnumMember({ name: val, value: val })), | ||
isExport: true, | ||
jsDoc: schema.default ? record_1.withDefault(schema.default, schema.doc) : schema.doc, | ||
}) | ||
: ts_compose_1.Type.Alias({ | ||
name: helpers_1.convertName(helpers_1.firstUpperCase(schema.name)), | ||
type: ts_compose_1.Type.Union(schema.symbols.map((symbol) => ts_compose_1.Type.Literal(symbol))), | ||
isExport: true, | ||
jsDoc: schema.doc, | ||
}); | ||
return helpers_1.namedType(type, context, schema, namespace); | ||
}; | ||
exports.convertEnumType = convertEnumType; |
{ | ||
"name": "@ovotech/avro-ts", | ||
"description": "Convert avro schemas into typescript interfaces", | ||
"version": "6.0.9", | ||
"version": "6.1.0", | ||
"main": "dist/index.js", | ||
@@ -25,3 +25,3 @@ "source": "src/index.ts", | ||
"@types/node": "^14.14.28", | ||
"avsc": "^5.5.6", | ||
"avsc": "^5.7.5", | ||
"eslint-config-prettier": "^7.2.0", | ||
@@ -41,3 +41,3 @@ "jest": "^26.6.3", | ||
}, | ||
"gitHead": "4920d20f220ad5fe20acbbe736b7c7062e680dd7" | ||
"gitHead": "1d98485e4f00a99d22e0ef0c49414668e0c7c486" | ||
} |
@@ -255,2 +255,30 @@ # Avro 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](examples/with-typescript-enums.ts) | ||
```typescript | ||
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 | ||
@@ -257,0 +285,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33922
481
335