Skip to content

Commit

Permalink
patch typings
Browse files Browse the repository at this point in the history
  • Loading branch information
erikengervall committed Feb 11, 2020
1 parent 70b30de commit 7a453ff
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
31 changes: 25 additions & 6 deletions src/utils/avdlToAVSC.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 7a453ff

Please sign in to comment.