@confluentinc/schemaregistry
Version:
Node.js client for Confluent Schema Registry
80 lines (79 loc) • 3.56 kB
TypeScript
import { Deserializer, DeserializerConfig, FieldTransform, RuleContext, SerdeType, Serializer, SerializerConfig } from "./serde";
import { Client, SchemaInfo } from "../schemaregistry-client";
import Ajv, { ErrorObject } from "ajv";
import { DereferencedJSONSchemaDraft07, DereferencedJSONSchemaDraft2020_12 } from '@criteria/json-schema';
import { LRUCache } from "lru-cache";
import { RuleRegistry } from "./rule-registry";
import { IHeaders } from "../../types/kafkajs";
export declare const JSON_TYPE = "JSON";
export interface ValidateFunction {
(this: any, data: any): boolean;
errors?: null | ErrorObject[];
}
export type DereferencedJSONSchema = DereferencedJSONSchemaDraft07 | DereferencedJSONSchemaDraft2020_12;
export type JsonSerdeConfig = ConstructorParameters<typeof Ajv>[0] & {
validate?: boolean;
};
export interface JsonSerde {
schemaToTypeCache: LRUCache<string, DereferencedJSONSchema>;
schemaToValidateCache: LRUCache<string, ValidateFunction>;
}
/**
* JsonSerializerConfig is the configuration for the JsonSerializer.
*/
export type JsonSerializerConfig = SerializerConfig & JsonSerdeConfig;
/**
* JsonSerializer is a serializer for JSON messages.
*/
export declare class JsonSerializer extends Serializer implements JsonSerde {
schemaToTypeCache: LRUCache<string, DereferencedJSONSchema>;
schemaToValidateCache: LRUCache<string, ValidateFunction>;
/**
* Creates a new JsonSerializer.
* @param client - the schema registry client
* @param serdeType - the serializer type
* @param conf - the serializer configuration
* @param ruleRegistry - the rule registry
*/
constructor(client: Client, serdeType: SerdeType, conf: JsonSerializerConfig, ruleRegistry?: RuleRegistry);
/**
* Serializes a message.
* @param topic - the topic
* @param msg - the message
* @param headers - optional headers
*/
serialize(topic: string, msg: any, headers?: IHeaders): Promise<Buffer>;
fieldTransform(ctx: RuleContext, fieldTransform: FieldTransform, msg: any): Promise<any>;
toType(info: SchemaInfo): Promise<DereferencedJSONSchema>;
toValidateFunction(info: SchemaInfo): Promise<ValidateFunction | undefined>;
static messageToSchema(msg: any): DereferencedJSONSchema;
}
/**
* JsonDeserializerConfig is the configuration for the JsonDeserializer.
*/
export type JsonDeserializerConfig = DeserializerConfig & JsonSerdeConfig;
/**
* JsonDeserializer is a deserializer for JSON messages.
*/
export declare class JsonDeserializer extends Deserializer implements JsonSerde {
schemaToTypeCache: LRUCache<string, DereferencedJSONSchema>;
schemaToValidateCache: LRUCache<string, ValidateFunction>;
/**
* Creates a new JsonDeserializer.
* @param client - the schema registry client
* @param serdeType - the deserializer type
* @param conf - the deserializer configuration
* @param ruleRegistry - the rule registry
*/
constructor(client: Client, serdeType: SerdeType, conf: JsonDeserializerConfig, ruleRegistry?: RuleRegistry);
/**
* Deserializes a message.
* @param topic - the topic
* @param payload - the message payload
* @param headers - optional headers
*/
deserialize(topic: string, payload: Buffer, headers?: IHeaders): Promise<any>;
fieldTransform(ctx: RuleContext, fieldTransform: FieldTransform, msg: any): Promise<any>;
toType(info: SchemaInfo): DereferencedJSONSchema;
toValidateFunction(info: SchemaInfo): Promise<ValidateFunction | undefined>;
}