schematic-kafka
Version:
Encode and decode kafka messages with Confluent Schema Registry (pure typescript implementation)
102 lines (101 loc) • 4.91 kB
TypeScript
import { SchemaRegistryClient, SchemaType } from "./schema-registry-client";
import { FunctionCacher } from "./function-cacher";
interface DecodedValueWithSubjectVersionInformation {
message: any;
subjects?: Array<{
subject: string;
version: number;
}>;
}
/**
* Helper class to cache calls to the SchemaRegistryClient
*/
declare class CachedSchemaRegistryClient extends SchemaRegistryClient {
cacher: FunctionCacher;
constructor(...schemaRegistryClientOptions: ConstructorParameters<typeof SchemaRegistryClient>);
checkSchema: (subject: string, schema: {
schema: string;
schemaType?: string;
references?: any;
}) => Promise<import("./schema-registry-client").SchemaDefinition>;
getLatestVersionForSubject: (subject: string) => Promise<import("./schema-registry-client").SchemaDefinition>;
getSchemaById: (schemaId: number) => Promise<{
schema: string;
schemaType: SchemaType;
}>;
listVersionsForId: (schemaId: number) => Promise<{
subject: string;
version: number;
}[]>;
}
/**
* This is a "convenient" method to provide different schema
* handlers (i.e. AVRO, JSON, PROTOBUF) into the ??
*/
type SchemaHandlerFactory = (schema: string) => {
decode(message: Buffer): any;
encode(message: any): Buffer;
};
export declare class KafkaRegistryHelper {
schemaHandlers: Map<SchemaType, SchemaHandlerFactory>;
readonly schemaRegistryClient: CachedSchemaRegistryClient;
constructor(...schemaRegistryOptions: ConstructorParameters<typeof SchemaRegistryClient>);
/**
* Helper function to add a schema Handler in a syntactic sugary way
* Note: The registry helper will call the factory every time a message is encoded/decoded
* It is your responsibility to handle caching of the schema handlers
* @param schemaType
* @param schemaHandlerFactory
* @returns the KafkaRegistryHelper instance
*/
withSchemaHandler(schemaType: SchemaType, schemaHandlerFactory: SchemaHandlerFactory): this;
/**
* Checks whether a schema is available in the schema registry, if not tries to register it
* @param subject
* @param schemaType
* @param schema the registry needs the schema to be a string
* @param references
* @returns schema string and schemaId
*/
private makeSureSchemaIsRegistered;
/**
* Encode a message with a given schema id
* @param schemaId id of the schema
* @param message formatted in whatever type SchemaHandler works with
* @param schemaType (optional, default = AVRO) schema type can be given, mostly for safety reason
* @returns message encoded with schema associated with schema id
*/
encodeForId(schemaId: number, message: any, schemaType?: SchemaType): Promise<Buffer<ArrayBufferLike>>;
/**
* Encode a message by subject name
* @param subject subject name (for kafka messages, don't forget the -key/-value postfix)
* @param message formatted in whatever type SchemaHandler works with
* @param schemaType (optional, default = AVRO) type of schema (e.g. PROTOBUG)
* @param schema serialized representation of the message schema (this will be registered with registry if it doesn't exist there yet)
* @param references additional schema references
* @returns message encoded for given subject / schema (schema id will be determined automatically)
*/
encodeForSubject(subject: string, message: any, schemaType?: SchemaType, schema?: string, references?: any): Promise<Buffer>;
/**
* decode a kafka event message
* @param message message with or without schema preamble. Note: If the preamble is missing the payload will be passed through
* @returns message in whatever format the SchemaHandler for the schemaType produces
*/
decode(message: Buffer): Promise<any>;
/**
* decode a kafka event message, but queries the schema for possible subject name and versions
* @param message message with or without schema preamble. Note: If the preamble is missing the payload will be passed through
* @returns decoded message in whatever format the SchemaHandler for the schemaType produces plus a list of possible subject names and versions matching the schema id used for encoding
*/
decodeWithSubjectAndVersionInformation(message: Buffer): Promise<DecodedValueWithSubjectVersionInformation>;
/**
* handle message encoding with schemaHandler
* @param schemaId id of the schema
* @param message formatted in whatever type SchemaHandler works with
* @param schemaType schema type, schema handler for this type has to be registered
* @param schema the schema as provided by the protocol handler
* @returns message encoded with handler corresponding to the schemaType
*/
private encodeMessage;
}
export {};