From 7a453ff51ae3d3799e11f8423d005f4b19c2c86f Mon Sep 17 00:00:00 2001 From: Erik Engervall Date: Tue, 11 Feb 2020 23:09:14 +0100 Subject: [PATCH] patch typings --- src/cache.ts | 3 ++- src/utils/avdlToAVSC.ts | 31 +++++++++++++++++++++++++------ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/cache.ts b/src/cache.ts index d15fe62..70f2e5b 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -21,7 +21,8 @@ export default class Cache { getSchema = (registryId: number): Schema => this.schemasByRegistryId[registryId] - setSchema = (registryId: number, schema: Schema): Schema => { + setSchema = (registryId: number, schema: Schema) => { + // @ts-ignore TODO: Fix typings for Schema... this.schemasByRegistryId[registryId] = avro.Type.forSchema(schema) return this.schemasByRegistryId[registryId] diff --git a/src/utils/avdlToAVSC.ts b/src/utils/avdlToAVSC.ts index a96917d..1ff753b 100644 --- a/src/utils/avdlToAVSC.ts +++ b/src/utils/avdlToAVSC.ts @@ -1,12 +1,31 @@ import * as fs from 'fs' -import { AssembleProtocolError, assembleProtocol, readProtocol } from 'avsc' +import { assembleProtocol, readProtocol } from 'avsc' + import { ConfluentSchemaRegistryError } from '../errors' +interface AssembleProtocolError extends Error { + path: string +} +interface Obj { + [key: string]: any +} +interface Iterable extends Obj { + map: any +} +interface Field { + type: { + type: string + items: any + } +} + let cache: any const merge = Object.assign -const isObject = (obj: any) => obj && typeof obj === 'object' -const isIterable = (obj: any) => isObject(obj) && typeof obj.map !== 'undefined' -const isFieldArray = (field: any) => isObject(field.type) && field.type.type === 'array' +const isObject = (obj: unknown): obj is Obj => obj && typeof obj === 'object' +const isIterable = (obj: unknown): obj is Iterable => + isObject(obj) && typeof obj.map !== 'undefined' +const isFieldArray = (field: unknown): field is Field => + isObject(field) && isObject(field.type) && field.type.type === 'array' const combine = (rootType: any, types: any) => { if (!rootType.fields) { @@ -73,8 +92,8 @@ export function avdlToAVSC(path: any) { export async function avdlToAVSCAsync(path: string) { cache = {} - const protocol: any = await new Promise((resolve, reject) => { - assembleProtocol(path, (err: AssembleProtocolError, schema: object) => { + const protocol: { [key: string]: any } = await new Promise((resolve, reject) => { + assembleProtocol(path, (err: AssembleProtocolError, schema) => { if (err) { reject(new ConfluentSchemaRegistryError(`${err.message}. Caused by: ${err.path}`)) } else {