@ovotech/avro-ts
Advanced tools
Comparing version 1.1.2 to 1.2.0
@@ -16,2 +16,3 @@ import { Schema, schema } from 'avsc'; | ||
}; | ||
visitedLogicalTypes: Array<string>; | ||
} | ||
@@ -41,4 +42,10 @@ export interface Result<TsType = ts.TypeNode> { | ||
export declare const printAstNode: (node: Result<ts.TypeNode>) => string; | ||
declare type LogicalTypeWithImport = { | ||
import: string; | ||
type: string; | ||
}; | ||
declare type LogicalTypeDefinition = string | LogicalTypeWithImport; | ||
export declare function avroTs(recordType: schema.RecordType, logicalTypes?: { | ||
[key: string]: string; | ||
[key: string]: LogicalTypeDefinition; | ||
}): string; | ||
export {}; |
@@ -103,5 +103,10 @@ "use strict"; | ||
const convertEnum = (context, enumType) => exports.result(context, ts.createUnionTypeNode(enumType.symbols.map(symbol => ts.createLiteralTypeNode(ts.createLiteral(symbol))))); | ||
const convertLogicalType = (context, type) => context.logicalTypes[type.logicalType] | ||
? exports.result(context, context.logicalTypes[type.logicalType]) | ||
: convertPrimitive(context, type.type); | ||
const convertLogicalType = (context, type) => { | ||
if (context.logicalTypes[type.logicalType]) { | ||
if (!context.visitedLogicalTypes.includes(type.logicalType)) | ||
context.visitedLogicalTypes.push(type.logicalType); | ||
return exports.result(context, context.logicalTypes[type.logicalType]); | ||
} | ||
return convertPrimitive(context, type.type); | ||
}; | ||
const convertPredefinedType = (context, type) => context.namespaces[type] ? exports.result(context, context.namespaces[type]) : convertPrimitive(context, type); | ||
@@ -150,2 +155,6 @@ const convertArrayType = (context, type) => { | ||
exports.printAstNode = (node) => { | ||
console.error('DEPRECATED', 'printAstNode() will soon not be exported anymore. See the official Typescript documentation for steps to write your own. https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#creating-and-printing-a-typescript-ast'); | ||
return printAstNodeFullyFeatured(node); | ||
}; | ||
const printAstNodeFullyFeatured = (node, extras = {}) => { | ||
const resultFile = ts.createSourceFile('someFileName.ts', '', ts.ScriptTarget.Latest); | ||
@@ -155,6 +164,6 @@ const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed }); | ||
const fullSourceFile = ts.updateSourceFileNode(resultFile, entries); | ||
return [ | ||
printer.printNode(ts.EmitHint.Unspecified, node.type, fullSourceFile), | ||
...entries.map(entry => printer.printNode(ts.EmitHint.Unspecified, entry, fullSourceFile)), | ||
].join('\n\n'); | ||
const importLines = extras.importLines || []; | ||
return importLines | ||
.concat(printer.printNode(ts.EmitHint.Unspecified, node.type, fullSourceFile), entries.map(entry => printer.printNode(ts.EmitHint.Unspecified, entry, fullSourceFile))) | ||
.join('\n\n'); | ||
}; | ||
@@ -166,7 +175,18 @@ function avroTs(recordType, logicalTypes = {}) { | ||
namespaces: {}, | ||
logicalTypes: Object.entries(logicalTypes).reduce((all, [name, type]) => ({ ...all, [name]: ts.createTypeReferenceNode(type, undefined) }), {}), | ||
visitedLogicalTypes: [], | ||
logicalTypes: Object.entries(logicalTypes).reduce((all, [name, type]) => { | ||
const typeStr = type.type ? type.type : type; | ||
return { | ||
...all, | ||
[name]: ts.createTypeReferenceNode(typeStr, undefined), | ||
}; | ||
}, {}), | ||
}; | ||
return exports.printAstNode(convertRecord(context, recordType)); | ||
const node = convertRecord(context, recordType); | ||
const importLines = context.visitedLogicalTypes | ||
.map(visitedType => logicalTypes[visitedType].import) | ||
.filter(Boolean); | ||
return printAstNodeFullyFeatured(node, { importLines }); | ||
} | ||
exports.avroTs = avroTs; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@ovotech/avro-ts", | ||
"description": "Convert avro schemas into typescript interfaces", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"main": "dist/index.js", | ||
@@ -36,3 +36,3 @@ "source": "src/index.ts", | ||
}, | ||
"gitHead": "08920fa2c6cc9a25f01f9de4c1b2283291d93b1b" | ||
"gitHead": "082b7fb2d47cfdfee25eadf492532e41385c9f47" | ||
} |
@@ -20,3 +20,10 @@ # Avro TS | ||
const avro: schema.RecordType = JSON.parse(String(readFileSync(join(__dirname, 'avro', file)))); | ||
const ts = avroTs(avro, { 'timestamp-millis': 'string', date: 'string' }); | ||
const ts = avroTs(avro, { | ||
'timestamp-millis': 'string', | ||
date: 'string', | ||
decimal: { | ||
type: 'Decimal', | ||
import: "import { Decimal } from 'decimal.js'", | ||
}, | ||
}); | ||
@@ -23,0 +30,0 @@ console.log(ts); |
@@ -14,2 +14,3 @@ import { Schema, schema } from 'avsc'; | ||
logicalTypes: { [key: string]: ts.TypeReferenceNode }; | ||
visitedLogicalTypes: Array<string>; | ||
} | ||
@@ -167,6 +168,9 @@ | ||
const convertLogicalType: Convert<schema.LogicalType> = (context, type) => | ||
context.logicalTypes[type.logicalType] | ||
? result(context, context.logicalTypes[type.logicalType]) | ||
: convertPrimitive(context, type.type); | ||
const convertLogicalType: Convert<schema.LogicalType> = (context, type) => { | ||
if (context.logicalTypes[type.logicalType]) { | ||
if (!context.visitedLogicalTypes.includes(type.logicalType)) context.visitedLogicalTypes.push(type.logicalType); | ||
return result(context, context.logicalTypes[type.logicalType]); | ||
} | ||
return convertPrimitive(context, type.type); | ||
}; | ||
@@ -257,2 +261,10 @@ const convertPredefinedType: Convert<string> = (context, type) => | ||
export const printAstNode = (node: Result): string => { | ||
console.error( | ||
'DEPRECATED', | ||
'printAstNode() will soon not be exported anymore. See the official Typescript documentation for steps to write your own. https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#creating-and-printing-a-typescript-ast', | ||
); | ||
return printAstNodeFullyFeatured(node); | ||
}; | ||
const printAstNodeFullyFeatured = (node: Result, extras: { importLines?: Array<string> } = {}): string => { | ||
const resultFile = ts.createSourceFile('someFileName.ts', '', ts.ScriptTarget.Latest); | ||
@@ -263,9 +275,19 @@ const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed }); | ||
return [ | ||
printer.printNode(ts.EmitHint.Unspecified, node.type, fullSourceFile), | ||
...entries.map(entry => printer.printNode(ts.EmitHint.Unspecified, entry, fullSourceFile)), | ||
].join('\n\n'); | ||
const importLines = extras.importLines || []; | ||
return importLines | ||
.concat( | ||
printer.printNode(ts.EmitHint.Unspecified, node.type, fullSourceFile), | ||
entries.map(entry => printer.printNode(ts.EmitHint.Unspecified, entry, fullSourceFile)), | ||
) | ||
.join('\n\n'); | ||
}; | ||
export function avroTs(recordType: schema.RecordType, logicalTypes: { [key: string]: string } = {}): string { | ||
type LogicalTypeWithImport = { import: string; type: string }; | ||
type LogicalTypeDefinition = string | LogicalTypeWithImport; | ||
export function avroTs( | ||
recordType: schema.RecordType, | ||
logicalTypes: { [key: string]: LogicalTypeDefinition } = {}, | ||
): string { | ||
const context: Context = { | ||
@@ -275,9 +297,18 @@ root: true, | ||
namespaces: {}, | ||
logicalTypes: Object.entries(logicalTypes).reduce( | ||
(all, [name, type]) => ({ ...all, [name]: ts.createTypeReferenceNode(type, undefined) }), | ||
{}, | ||
), | ||
visitedLogicalTypes: [], | ||
logicalTypes: Object.entries(logicalTypes).reduce((all, [name, type]) => { | ||
const typeStr = (type as LogicalTypeWithImport).type ? (type as LogicalTypeWithImport).type : (type as string); | ||
return { | ||
...all, | ||
[name]: ts.createTypeReferenceNode(typeStr, undefined), | ||
}; | ||
}, {}), | ||
}; | ||
return printAstNode(convertRecord(context, recordType)); | ||
const node = convertRecord(context, recordType); | ||
const importLines = context.visitedLogicalTypes | ||
.map(visitedType => (logicalTypes[visitedType] as LogicalTypeWithImport).import) | ||
.filter(Boolean); | ||
return printAstNodeFullyFeatured(node, { importLines }); | ||
} |
Sorry, the diff of this file is not supported yet
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
33047
503
73