@ovotech/avro-ts
Advanced tools
Comparing version 1.1.0 to 1.1.1
@@ -27,2 +27,3 @@ import { Schema, schema } from 'avsc'; | ||
}; | ||
export declare const withContexts: (context: Context, items: Context[]) => Context; | ||
export declare const withEntry: (context: Context, entry: ts.InterfaceDeclaration) => Context; | ||
@@ -29,0 +30,0 @@ export declare const withNamespace: (context: Context, record: schema.RecordType) => Context; |
@@ -15,2 +15,7 @@ "use strict"; | ||
}, { items: [], context }); | ||
exports.withContexts = (context, items) => items.reduce((all, itemContext) => ({ | ||
...all, | ||
registry: { ...all.registry, ...itemContext.registry }, | ||
namespaces: { ...all.namespaces, ...itemContext.namespaces }, | ||
}), context); | ||
exports.withEntry = (context, entry) => ({ | ||
@@ -34,3 +39,4 @@ ...context, | ||
const namespaceContext = type.namespace ? exports.withNamespace(context, type) : context; | ||
const fields = exports.mapContext({ ...namespaceContext, root: false }, type.fields, (fieldContext, fieldType) => { | ||
const fieldContext = { ...namespaceContext, root: false }; | ||
const fields = type.fields.map(fieldType => { | ||
const field = convertType(fieldContext, fieldType.type); | ||
@@ -43,8 +49,9 @@ const prop = ts.createPropertySignature(undefined, fieldType.name, isOptional(fieldType.type) ? ts.createToken(ts.SyntaxKind.QuestionToken) : undefined, field.type, undefined); | ||
}); | ||
const interfaceType = ts.createInterfaceDeclaration(undefined, [ts.createToken(ts.SyntaxKind.ExportKeyword)], type.name, undefined, undefined, fields.items); | ||
const interfaceType = ts.createInterfaceDeclaration(undefined, [ts.createToken(ts.SyntaxKind.ExportKeyword)], type.name, undefined, undefined, fields.map(field => field.type)); | ||
const recordContext = exports.withContexts(fieldContext, fields.map(item => item.context)); | ||
if (context.root) { | ||
return exports.result(fields.context, interfaceType); | ||
return exports.result(recordContext, interfaceType); | ||
} | ||
else { | ||
return exports.result(exports.withEntry(fields.context, interfaceType), ts.createTypeReferenceNode(type.name, undefined)); | ||
return exports.result(exports.withEntry(recordContext, interfaceType), ts.createTypeReferenceNode(type.name, undefined)); | ||
} | ||
@@ -51,0 +58,0 @@ }; |
{ | ||
"name": "@ovotech/avro-ts", | ||
"description": "Convert avro schemas into typescript interfaces", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"main": "dist/index.js", | ||
@@ -36,3 +36,3 @@ "source": "src/index.ts", | ||
}, | ||
"gitHead": "33fbdb8057520cf1d3f229059a0231089febbaee" | ||
"gitHead": "b932b8948421d3f28fa6b2ffd49ea9163447d18d" | ||
} |
@@ -44,2 +44,12 @@ import { Schema, schema } from 'avsc'; | ||
export const withContexts = (context: Context, items: Context[]) => | ||
items.reduce( | ||
(all, itemContext) => ({ | ||
...all, | ||
registry: { ...all.registry, ...itemContext.registry }, | ||
namespaces: { ...all.namespaces, ...itemContext.namespaces }, | ||
}), | ||
context, | ||
); | ||
export const withEntry = (context: Context, entry: ts.InterfaceDeclaration): Context => ({ | ||
@@ -73,4 +83,5 @@ ...context, | ||
const namespaceContext = type.namespace ? withNamespace(context, type) : context; | ||
const fieldContext = { ...namespaceContext, root: false }; | ||
const fields = mapContext({ ...namespaceContext, root: false }, type.fields, (fieldContext, fieldType) => { | ||
const fields = type.fields.map(fieldType => { | ||
const field = convertType(fieldContext, fieldType.type); | ||
@@ -98,9 +109,11 @@ const prop = ts.createPropertySignature( | ||
undefined, | ||
fields.items, | ||
fields.map(field => field.type), | ||
); | ||
const recordContext = withContexts(fieldContext, fields.map(item => item.context)); | ||
if (context.root) { | ||
return result(fields.context, interfaceType); | ||
return result(recordContext, interfaceType); | ||
} else { | ||
return result(withEntry(fields.context, interfaceType), ts.createTypeReferenceNode(type.name, undefined)); | ||
return result(withEntry(recordContext, interfaceType), ts.createTypeReferenceNode(type.name, undefined)); | ||
} | ||
@@ -107,0 +120,0 @@ }; |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
29856
449
0