schematic-kafka
Version:
Encode and decode kafka messages with Confluent Schema Registry (pure typescript implementation)
201 lines (200 loc) • 7.02 kB
TypeScript
import * as httpsRequest from "https";
import * as httpRequest from "http";
/**
* Enum of supported schema types / protocols.
* If this isn't provided the schema registry will assume AVRO.
*/
export declare enum SchemaType {
AVRO = "AVRO",// (default)
PROTOBUF = "PROTOBUF",
JSON = "JSON"
}
/**
* Enum of supported schema compatibility modes.
*/
export declare enum CompatibilityMode {
BACKWARD = "BACKWARD",
BACKWARD_TRANSITIVE = "BACKWARD_TRANSITIVE",
FORWARD = "FORWARD",
FORWARD_TRANSITIVE = "FORWARD_TRANSITIVE",
FULL = "FULL",
FULL_TRANSITIVE = "FULL_TRANSITIVE",
NONE = "NONE"
}
/**
* Custom error to be used by the schema registry client
*/
export declare class SchemaRegistryError extends Error {
errorCode: number;
constructor(errorCode: number, message: string);
}
/**
* Configuration properties for the schema api client
* TODO: allow/forward all configurations for http.request (not just the agent)
*/
export interface SchemaApiClientConfiguration {
baseUrl: string;
username?: string;
password?: string;
agent?: httpRequest.Agent | httpsRequest.Agent;
}
type RequestOptions = httpsRequest.RequestOptions | httpRequest.RequestOptions;
/**
* Full schema definition. Includes everything to know about a given schema version.
*/
export interface SchemaDefinition {
subject: string;
id: number;
version: number;
/**
* serialized schema representation
*/
schema?: string;
/**
* schema type (if not given schema registry will assume AVRO)
*/
schemaType?: SchemaType;
}
/**
* schema registry client to interact with confluent schema registry
* as specified here: https://docs.confluent.io/platform/current/schema-registry/develop/api.html
*
* Note: not all methods are implemented yet
*/
export declare class SchemaRegistryClient {
baseRequestOptions: RequestOptions;
requester: typeof httpRequest | typeof httpsRequest;
basePath: string;
/**
* create new SchemaRegistryClient instance
*/
constructor(options: SchemaApiClientConfiguration);
/**
* Get a schema by its id
* @param {number} schemaId id of the schema to fetch
* @returns serialized schema information and schema type
*/
getSchemaById(schemaId: number): Promise<{
schema: string;
schemaType: SchemaType;
}>;
/**
* Get types of possible schemas types
* @returns array of possible schema types (typically: AVRO, PROTOBUF and JSON)
*/
getSchemaTypes(): Promise<Array<string>>;
/**
* Get subject/version pairs for given id
* @param schemaId version of schema registered
* @returns subject and version number for the schema identified by the id
*/
listVersionsForId(schemaId: number): Promise<Array<{
subject: string;
version: number;
}>>;
/**
* Get list of available subjects
* @returns array of registered subjects
*/
listSubjects(): Promise<Array<string>>;
/**
* Get list of versions registered under the specified subject
* @param subject subject name
* @returns array of schema versions
*/
listVersionsForSubject(subject: string): Promise<Array<number>>;
/**
* Deletes a schema.
* Note: Should only be used in development mode. Don't delete schemas in production.
* @param subject subject name
* @returns list of deleted schema versions
*/
deleteSubject(subject: string, permanent?: boolean): Promise<Array<number>>;
/**
* Get schema for subject and version
* @param subject subject name
* @param version optional version to retrieve (if not provided the latest version will be fetched)
* @returns schema and its metadata
*/
getSchemaForSubjectAndVersion(subject: string, version?: number): Promise<SchemaDefinition>;
/**
* Alias for getSchemaForSubjectAndVersion with version = latest
* @param subject subject name
* @returns schema and its metadata
*/
getLatestVersionForSubject(subject: string): ReturnType<SchemaRegistryClient["getSchemaForSubjectAndVersion"]>;
/**
* Get schema for subject and version
* @param subject subject name
* @param version schema version
* @returns serialized schema
*/
getRawSchemaForSubjectAndVersion(subject: string, version: number): Promise<string>;
/**
* Register schema in registry
*
* Note: The specification says it will return subject, id, version and the schema itself.
* Factually it will only return the id of the newly created schema.
* @param subject subject name
* @param schema schema metadata and serialized schema representation
* @returns schema metadata (or just the id of the newly created schema)
*/
registerSchema(subject: string, schema: {
schema: string;
schemaType: string;
references?: any;
}): Promise<SchemaDefinition>;
/**
* Check if a schema has already been registered under the specified subject.
* If so, this returns the schema string along with its globally unique identifier, its version under this subject and the subject name.
* @param subject subject name
* @param schema serialized schema representation
* @returns schema metadata and serialized schema
*/
checkSchema(subject: string, schema: {
schema: string;
schemaType?: string;
references?: any;
}): Promise<SchemaDefinition>;
/**
* Check a schema for compatibility
* @param subject
* @param version
* @param schema
* @param verbose
* @returns whether or not the schema is compatible with the provided schema, given the compatibility mode for the subject
*/
testCompatibility(subject: string, version: number | "latest", schema: {
schema: string;
schemaType?: string;
references?: any;
}, verbose?: boolean): Promise<boolean>;
/**
* set schema registry default compatibility mode
* @param compatibility new default compatibility mode
* @returns new compatibility mode as echoed by the schema registry
*/
setConfig(compatibility: CompatibilityMode): Promise<any>;
/**
* get schema registry default compatibility mode
*/
getConfig(): Promise<CompatibilityMode>;
/**
* set compatibility mode for a subject
* @param subject name of the subject
* @param compatibility new compatibility mode
* @returns new compatibility mode as echoed by the schema registry
*/
setSubjectConfig(subject: string, compatibility: CompatibilityMode): Promise<CompatibilityMode>;
/**
* get compatibility mode for a subject
* @param subject name of the subject
* @returns current compatibility mode for the subject
*/
getSubjectConfig(subject: string): Promise<CompatibilityMode>;
/**
* Internal http request helper
*/
private request;
}
export {};