@ovotech/avro-ts
Advanced tools
Comparing version 5.3.0 to 6.0.0
@@ -1,6 +0,4 @@ | ||
import { Schema } from 'avsc'; | ||
import { Schema, schema } from 'avsc'; | ||
import { Convert, Context } from './types'; | ||
export declare const firstUpperCase: (name: string) => string; | ||
export declare const convertNamespace: (namespace: string) => string; | ||
export declare const nameParts: (fullName: string) => [string] | [string, string]; | ||
export declare const addRef: (type: schema.RecordType | schema.EnumType, context: Context) => Context; | ||
export declare const collectRefs: (type: Schema, context: Context) => Context; | ||
@@ -7,0 +5,0 @@ export declare const convertType: Convert; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.toExternalContext = exports.toTypeScript = exports.convertType = exports.collectRefs = exports.nameParts = exports.convertNamespace = exports.firstUpperCase = void 0; | ||
exports.toExternalContext = exports.toTypeScript = exports.convertType = exports.collectRefs = exports.addRef = void 0; | ||
const ts_compose_1 = require("@ovotech/ts-compose"); | ||
@@ -15,11 +15,14 @@ const wrapped_union_1 = require("./types/wrapped-union"); | ||
const document_1 = require("@ovotech/ts-compose/dist/document"); | ||
const helpers_1 = require("./helpers"); | ||
const ts = require("typescript"); | ||
const helpers_1 = require("./helpers"); | ||
exports.firstUpperCase = (name) => name ? name[0].toUpperCase() + name.slice(1) : name; | ||
exports.convertNamespace = (namespace) => namespace.split('.').map(exports.firstUpperCase).join(''); | ||
exports.nameParts = (fullName) => { | ||
const parts = fullName.split('.'); | ||
return parts.length > 1 | ||
? [parts.slice(parts.length - 1)[0], parts.slice(0, parts.length - 1).join('.')] | ||
: [parts[0]]; | ||
exports.addRef = (type, context) => { | ||
var _a, _b; | ||
return ({ | ||
...context, | ||
namespace: (_a = type.namespace) !== null && _a !== void 0 ? _a : context.namespace, | ||
refs: { | ||
...context.refs, | ||
[helpers_1.fullName(context, type)]: { ...type, namespace: (_b = type.namespace) !== null && _b !== void 0 ? _b : context.namespace }, | ||
}, | ||
}); | ||
}; | ||
@@ -36,14 +39,7 @@ exports.collectRefs = (type, context) => { | ||
} | ||
else if (enum_1.isEnumType(type)) { | ||
return exports.addRef(type, context); | ||
} | ||
else if (record_1.isRecordType(type)) { | ||
return type.fields.reduce((all, item) => { | ||
var _a, _b; | ||
return exports.collectRefs(item.type, { | ||
...all, | ||
namespace: (_a = type.namespace) !== null && _a !== void 0 ? _a : all.namespace, | ||
refs: { | ||
...all.refs, | ||
[helpers_1.fullName(all, type)]: { ...type, namespace: (_b = type.namespace) !== null && _b !== void 0 ? _b : all.namespace }, | ||
}, | ||
}); | ||
}, context); | ||
return type.fields.reduce((all, item) => exports.collectRefs(item.type, all), exports.addRef(type, context)); | ||
} | ||
@@ -84,3 +80,3 @@ else { | ||
else if (typeof type === 'string') { | ||
const [name, nameNamespace] = exports.nameParts(type); | ||
const [name, nameNamespace] = helpers_1.nameParts(type); | ||
const namespace = nameNamespace !== null && nameNamespace !== void 0 ? nameNamespace : context.namespace; | ||
@@ -90,9 +86,9 @@ if (namespace && context.external && !((_a = context.refs) === null || _a === void 0 ? void 0 : _a[type])) { | ||
if (context.external[module][type]) { | ||
const externalNamespace = exports.convertNamespace(namespace); | ||
const alias = `${externalNamespace}${exports.firstUpperCase(name)}`; | ||
const externalNamespace = helpers_1.convertNamespace(namespace); | ||
const alias = `${externalNamespace}${helpers_1.firstUpperCase(name)}`; | ||
const externalContext = document_1.withImports(context, { | ||
named: [{ name: exports.convertNamespace(namespace), as: alias }], | ||
named: [{ name: helpers_1.convertNamespace(namespace), as: alias }], | ||
module, | ||
}); | ||
const ref = ts_compose_1.Type.Referance([alias, exports.firstUpperCase(name)]); | ||
const ref = ts_compose_1.Type.Referance([alias, helpers_1.firstUpperCase(name)]); | ||
return ts_compose_1.document(externalContext, ref); | ||
@@ -103,4 +99,4 @@ } | ||
const ref = namespace | ||
? ts_compose_1.Type.Referance([exports.convertNamespace(namespace), exports.firstUpperCase(name)]) | ||
: ts_compose_1.Type.Referance(exports.firstUpperCase(name)); | ||
? ts_compose_1.Type.Referance([helpers_1.convertNamespace(namespace), helpers_1.firstUpperCase(name)]) | ||
: ts_compose_1.Type.Referance(helpers_1.firstUpperCase(name)); | ||
return ts_compose_1.document(context, ref); | ||
@@ -107,0 +103,0 @@ } |
import { Context } from './types'; | ||
import { schema as avroSchema } from 'avsc'; | ||
export declare const fullName: (context: Context, schema: avroSchema.RecordType) => string; | ||
import { Document } from '@ovotech/ts-compose'; | ||
import * as ts from 'typescript'; | ||
export declare const fullName: (context: Context, schema: avroSchema.RecordType | avroSchema.EnumType) => string; | ||
export declare const firstUpperCase: (name: string) => string; | ||
export declare const convertNamespace: (namespace: string) => string; | ||
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>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.fullName = void 0; | ||
exports.namedType = exports.nameParts = exports.convertNamespace = exports.firstUpperCase = exports.fullName = void 0; | ||
const ts_compose_1 = require("@ovotech/ts-compose"); | ||
exports.fullName = (context, schema) => { var _a; return `${(_a = schema.namespace) !== null && _a !== void 0 ? _a : context.namespace}.${schema.name}`; }; | ||
exports.firstUpperCase = (name) => name ? name[0].toUpperCase() + name.slice(1) : name; | ||
exports.convertNamespace = (namespace) => namespace.split('.').map(exports.firstUpperCase).join(''); | ||
exports.nameParts = (fullName) => { | ||
const parts = fullName.split('.'); | ||
return parts.length > 1 | ||
? [parts.slice(parts.length - 1)[0], parts.slice(0, parts.length - 1).join('.')] | ||
: [parts[0]]; | ||
}; | ||
exports.namedType = (type, context, schema, namespace) => { | ||
const name = exports.firstUpperCase(schema.name); | ||
const namespaceName = namespace ? exports.convertNamespace(namespace) : undefined; | ||
const fullName = namespaceName ? [namespaceName, name] : name; | ||
const contextWithRef = namespace | ||
? ts_compose_1.withIdentifier(context, ts_compose_1.Node.Const({ | ||
name: `${name}Name`, | ||
isExport: true, | ||
value: `${namespace}.${schema.name}`, | ||
}), namespaceName) | ||
: context; | ||
return ts_compose_1.document(ts_compose_1.withIdentifier(contextWithRef, type, namespaceName), ts_compose_1.Type.Referance(fullName)); | ||
}; |
@@ -5,6 +5,14 @@ "use strict"; | ||
const ts_compose_1 = require("@ovotech/ts-compose"); | ||
const helpers_1 = require("../helpers"); | ||
exports.isEnumType = (type) => typeof type === 'object' && 'type' in type && type.type === 'enum'; | ||
exports.convertEnumType = (context, type) => ({ | ||
context, | ||
type: ts_compose_1.Type.Union(type.symbols.map((symbol) => ts_compose_1.Type.Literal(symbol))), | ||
}); | ||
exports.convertEnumType = (context, schema) => { | ||
var _a; | ||
const namespace = (_a = schema.namespace) !== null && _a !== void 0 ? _a : context.namespace; | ||
const type = ts_compose_1.Type.Alias({ | ||
name: 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); | ||
}; |
@@ -1,5 +0,5 @@ | ||
import { schema, Schema } from 'avsc'; | ||
import { schema as avroSchema, Schema } from 'avsc'; | ||
import { Convert } from '../types'; | ||
export declare const isRecordType: (type: Schema) => type is schema.RecordType; | ||
export declare const isRecordType: (type: Schema) => type is avroSchema.RecordType; | ||
export declare const withDefault: (def: unknown, doc: string | undefined) => string | undefined; | ||
export declare const convertRecordType: Convert<schema.RecordType>; | ||
export declare const convertRecordType: Convert<avroSchema.RecordType>; |
@@ -6,2 +6,3 @@ "use strict"; | ||
const convert_1 = require("../convert"); | ||
const helpers_1 = require("../helpers"); | ||
exports.isRecordType = (type) => typeof type === 'object' && 'type' in type && type.type === 'record'; | ||
@@ -27,7 +28,4 @@ exports.withDefault = (def, doc) => { | ||
}); | ||
const name = convert_1.firstUpperCase(schema.name); | ||
const namespaceName = namespace ? convert_1.convertNamespace(namespace) : undefined; | ||
const fullName = namespaceName ? [namespaceName, name] : name; | ||
const record = ts_compose_1.Type.Interface({ | ||
name: convert_1.firstUpperCase(schema.name), | ||
name: helpers_1.firstUpperCase(schema.name), | ||
props: fields.items, | ||
@@ -37,10 +35,3 @@ isExport: true, | ||
}); | ||
const contextWithRef = namespace | ||
? ts_compose_1.withIdentifier(fields.context, ts_compose_1.Node.Const({ | ||
name: `${name}Name`, | ||
isExport: true, | ||
value: `${namespace}.${schema.name}`, | ||
}), namespaceName) | ||
: fields.context; | ||
return ts_compose_1.document(ts_compose_1.withIdentifier(contextWithRef, record, namespaceName), ts_compose_1.Type.Referance(fullName)); | ||
return helpers_1.namedType(record, fields.context, schema, namespace); | ||
}; |
{ | ||
"name": "@ovotech/avro-ts", | ||
"description": "Convert avro schemas into typescript interfaces", | ||
"version": "5.3.0", | ||
"version": "6.0.0", | ||
"main": "dist/index.js", | ||
@@ -40,3 +40,3 @@ "source": "src/index.ts", | ||
}, | ||
"gitHead": "2851345b2d18036bdb1ab7ab8a27f267fed3c3f0" | ||
"gitHead": "7977e2fe2bf979d21c6ad5d323382e85886312d7" | ||
} |
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
29699
414