@biorate/schema-registry
Version:
Schema registry connector
205 lines • 7.08 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.create = void 0;
const axios_prometheus_1 = require("@biorate/axios-prometheus");
const avsc_1 = require("avsc");
const errors_1 = require("./errors");
const create = (config) => {
const cache = new Map();
class SchemaRegistryApi extends axios_prometheus_1.AxiosPrometheus {
constructor() {
super(...arguments);
this.baseURL = config.baseURL;
this.headers = config.headers;
}
}
class Ping extends SchemaRegistryApi {
constructor() {
super(...arguments);
this.url = '/';
this.method = 'get';
}
static fetch() {
return this._fetch({});
}
}
class GetSchemasById extends SchemaRegistryApi {
constructor() {
super(...arguments);
this.url = '/schemas/ids/:id';
this.method = 'get';
}
static fetch(id) {
return this._fetch({ path: { id } });
}
}
class GetSchemasTypes extends SchemaRegistryApi {
constructor() {
super(...arguments);
this.url = '/schemas/types';
this.method = 'get';
}
static fetch() {
return this._fetch({});
}
}
class GetSchemasVersionsById extends SchemaRegistryApi {
constructor() {
super(...arguments);
this.url = '/schemas/ids/:id/versions';
this.method = 'get';
}
static fetch(id) {
return this._fetch({ path: { id } });
}
}
class GetSubjects extends SchemaRegistryApi {
constructor() {
super(...arguments);
this.url = '/subjects';
this.method = 'get';
}
static fetch() {
return this._fetch({});
}
}
class GetSubjectsVersions extends SchemaRegistryApi {
constructor() {
super(...arguments);
this.url = '/subjects/:subject/versions';
this.method = 'get';
}
static fetch(subject) {
return this._fetch({ path: { subject } });
}
}
class DeleteSubjects extends SchemaRegistryApi {
constructor() {
super(...arguments);
this.url = '/subjects/:subject';
this.method = 'delete';
}
static fetch(data) {
return this._fetch({
path: { subject: data.subject },
params: { permanent: !!data.permanent },
});
}
}
class GetSubjectsByVersion extends SchemaRegistryApi {
constructor() {
super(...arguments);
this.url = '/subjects/:subject/versions/:version';
this.method = 'get';
}
static fetch(data) {
return this._fetch({ path: data });
}
}
class GetSchemaBySubjectsAndVersion extends SchemaRegistryApi {
constructor() {
super(...arguments);
this.url = '/subjects/:subject/versions/:version/schema';
this.method = 'get';
}
static fetch(data) {
return this._fetch({ path: data });
}
}
class PostSubjectsVersions extends SchemaRegistryApi {
constructor() {
super(...arguments);
this.url = '/subjects/:subject/versions';
this.method = 'post';
}
static fetch(data) {
return this._fetch({
path: { subject: data.subject },
params: { normalize: !!data.normalize },
data: {
schema: toStringData(data.schema),
schemaType: data.schemaType,
reference: data.reference,
},
});
}
}
class PostSubjects extends SchemaRegistryApi {
constructor() {
super(...arguments);
this.url = '/subjects/:subject';
this.method = 'post';
}
static fetch(data) {
return this._fetch({
path: { subject: data.subject },
params: { normalize: !!data.normalize },
data: {
schema: toStringData(data.schema),
schemaType: data.schemaType,
reference: data.reference,
},
});
}
}
class PutConfig extends SchemaRegistryApi {
constructor() {
super(...arguments);
this.url = '/config/:subject';
this.method = 'put';
}
static fetch(data) {
return this._fetch({
path: { subject: data.subject },
data: { compatibility: data.compatibility },
});
}
}
async function encode(subject, data, version = 'latest') {
const errors = [];
const response = await GetSubjectsByVersion.fetch({ subject, version });
const header = Buffer.alloc(5);
const schema = avsc_1.Type.forSchema(JSON.parse(response.data.schema));
schema.isValid(data, {
errorHook: (path, value) => {
errors.push(`${path.join('.')}: ${value} (${typeof value})`);
},
});
if (errors.length)
throw new errors_1.SchemaRegistryAvroSchemaParseError(errors);
header.writeInt32BE(response.data.id, 1);
return Buffer.concat([header, schema.toBuffer(data)]);
}
async function decode(buffer) {
const id = buffer.readInt32BE(1);
let data = cache.get(id);
if (!data) {
const response = await GetSchemasById.fetch(id);
data = avsc_1.Type.forSchema(JSON.parse(response.data.schema));
cache.set(id, data);
}
const schema = avsc_1.Type.forSchema(data);
return schema.fromBuffer(buffer.slice(5));
}
function toStringData(data) {
return typeof data === 'string' ? data : JSON.stringify(data);
}
return {
ping: Ping.fetch.bind(Ping),
getSchemasById: (GetSchemasById.fetch.bind(GetSchemasById)),
getSchemasTypes: (GetSchemasTypes.fetch.bind(GetSchemasTypes)),
getSchemasVersionsById: (GetSchemasVersionsById.fetch.bind(GetSchemasVersionsById)),
getSubjects: GetSubjects.fetch.bind(GetSubjects),
getSubjectsVersions: (GetSubjectsVersions.fetch.bind(GetSubjectsVersions)),
deleteSubjects: (DeleteSubjects.fetch.bind(DeleteSubjects)),
getSubjectsByVersion: (GetSubjectsByVersion.fetch.bind(GetSubjectsByVersion)),
getSchemaBySubjectsAndVersion: (GetSchemaBySubjectsAndVersion.fetch.bind(GetSchemaBySubjectsAndVersion)),
postSubjects: PostSubjects.fetch.bind(PostSubjects),
postSubjectsVersions: (PostSubjectsVersions.fetch.bind(PostSubjectsVersions)),
putConfig: PutConfig.fetch.bind(PutConfig),
encode,
decode,
};
};
exports.create = create;
//# sourceMappingURL=api.js.map