@ovotech/avro-ts
Advanced tools
Comparing version 5.0.0 to 5.1.0
@@ -9,1 +9,4 @@ import { Schema } from 'avsc'; | ||
export declare const toTypeScript: (schema: Schema, initial?: Context) => string; | ||
export declare const toExternalContext: (schema: Schema, initial?: Context) => { | ||
[key: string]: Schema; | ||
}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.toTypeScript = exports.convertType = exports.collectRefs = exports.nameParts = exports.convertNamespace = exports.firstUpperCase = void 0; | ||
exports.toExternalContext = exports.toTypeScript = exports.convertType = exports.collectRefs = exports.nameParts = exports.convertNamespace = exports.firstUpperCase = void 0; | ||
const ts_compose_1 = require("@ovotech/ts-compose"); | ||
@@ -53,2 +53,3 @@ const wrapped_union_1 = require("./types/wrapped-union"); | ||
exports.convertType = (context, type) => { | ||
var _a; | ||
if (wrapped_union_1.isWrappedUnion(type, context)) { | ||
@@ -84,2 +85,16 @@ return wrapped_union_1.convertWrappedUnionType(context, type); | ||
const namespace = nameNamespace !== null && nameNamespace !== void 0 ? nameNamespace : context.namespace; | ||
if (namespace && context.external && !((_a = context.refs) === null || _a === void 0 ? void 0 : _a[type])) { | ||
for (const module in context.external) { | ||
if (context.external[module][type]) { | ||
const externalNamespace = exports.convertNamespace(namespace); | ||
const alias = `${externalNamespace}${exports.firstUpperCase(name)}`; | ||
const externalContext = document_1.withImports(context, { | ||
named: [{ name: exports.convertNamespace(namespace), as: alias }], | ||
module, | ||
}); | ||
const ref = ts_compose_1.Type.Referance([alias, exports.firstUpperCase(name)]); | ||
return ts_compose_1.document(externalContext, ref); | ||
} | ||
} | ||
} | ||
const ref = namespace | ||
@@ -105,1 +120,7 @@ ? ts_compose_1.Type.Referance([exports.convertNamespace(namespace), exports.firstUpperCase(name)]) | ||
}; | ||
exports.toExternalContext = (schema, initial = {}) => { | ||
var _a; | ||
const contextWithRefs = exports.collectRefs(schema, initial); | ||
const { context } = exports.convertType(contextWithRefs, schema); | ||
return (_a = context.refs) !== null && _a !== void 0 ? _a : {}; | ||
}; |
@@ -1,2 +0,2 @@ | ||
export { toTypeScript, convertType } from './convert'; | ||
export { toTypeScript, convertType, toExternalContext } from './convert'; | ||
export { Context, CustomLogicalType } from './types'; |
@@ -6,1 +6,2 @@ "use strict"; | ||
Object.defineProperty(exports, "convertType", { enumerable: true, get: function () { return convert_1.convertType; } }); | ||
Object.defineProperty(exports, "toExternalContext", { enumerable: true, get: function () { return convert_1.toExternalContext; } }); |
@@ -22,3 +22,8 @@ import * as ts from 'typescript'; | ||
}; | ||
external?: { | ||
[file: string]: { | ||
[key: string]: Schema; | ||
}; | ||
}; | ||
} | ||
export declare type Convert<TSchema = Schema, TType = ts.TypeNode> = (context: Context, type: TSchema) => Document<TType, Context>; |
{ | ||
"name": "@ovotech/avro-ts", | ||
"description": "Convert avro schemas into typescript interfaces", | ||
"version": "5.0.0", | ||
"version": "5.1.0", | ||
"main": "dist/index.js", | ||
@@ -38,3 +38,3 @@ "source": "src/index.ts", | ||
}, | ||
"gitHead": "fe95a576eb2366110b3bf478cbe587aff3fc63fa" | ||
"gitHead": "394448c15bf0b386163650b9f17df6b1e4162164" | ||
} |
@@ -229,2 +229,25 @@ # Avro TS | ||
## External references | ||
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. | ||
> [examples/external.ts](examples/external.ts) | ||
```typescript | ||
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); | ||
``` | ||
## Running the tests | ||
@@ -231,0 +254,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
27307
391
281