@biorate/schema-registry 0.27.3 → 0.29.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/.nyc_output/267de769-9ac6-4dd6-a344-31855bf8eef0.json +1 -0
  2. package/.nyc_output/processinfo/267de769-9ac6-4dd6-a344-31855bf8eef0.json +1 -0
  3. package/.nyc_output/processinfo/index.json +1 -0
  4. package/CHANGELOG.md +35 -0
  5. package/coverage/lcov-report/api.ts.html +745 -0
  6. package/coverage/lcov-report/base.css +224 -0
  7. package/coverage/lcov-report/block-navigation.js +87 -0
  8. package/coverage/lcov-report/cache.ts.html +118 -0
  9. package/coverage/lcov-report/errors.ts.html +130 -0
  10. package/coverage/lcov-report/favicon.png +0 -0
  11. package/coverage/lcov-report/index.html +146 -0
  12. package/coverage/lcov-report/index.ts.html +337 -0
  13. package/coverage/lcov-report/prettify.css +1 -0
  14. package/coverage/lcov-report/prettify.js +2 -0
  15. package/coverage/lcov-report/schema-registry/index.html +116 -0
  16. package/coverage/lcov-report/schema-registry/index.ts.html +88 -0
  17. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  18. package/coverage/lcov-report/sorter.js +196 -0
  19. package/coverage/lcov.info +175 -0
  20. package/dist/tsconfig.build.tsbuildinfo +1 -1
  21. package/package.json +7 -7
  22. package/dist/src/api.js +0 -190
  23. package/dist/src/api.js.map +0 -1
  24. package/dist/src/errors.js +0 -19
  25. package/dist/src/errors.js.map +0 -1
  26. package/dist/src/index.js +0 -51
  27. package/dist/src/index.js.map +0 -1
  28. package/dist/src/interfaces.js +0 -3
  29. package/dist/src/interfaces.js.map +0 -1
  30. package/index.ts +0 -1
  31. package/src/api.ts +0 -220
  32. package/src/errors.ts +0 -15
  33. package/src/index.ts +0 -84
  34. package/src/interfaces.ts +0 -13
  35. package/tests/__mocks__/index.ts +0 -22
package/src/api.ts DELETED
@@ -1,220 +0,0 @@
1
- import { Axios } from '@biorate/axios';
2
- import { Type } from 'avsc';
3
- import { ISchemaRegistryConfig } from './interfaces';
4
- import { SchemaRegistryAvroSchemaParseError } from './errors';
5
-
6
- export const create = (config: ISchemaRegistryConfig) => {
7
- const cache = new Map<number, Type>();
8
-
9
- class SchemaRegistryApi extends Axios {
10
- public baseURL = config.baseURL;
11
- }
12
-
13
- class Ping extends SchemaRegistryApi {
14
- public url = '/';
15
- public method = 'get';
16
-
17
- public static fetch() {
18
- return this._fetch<{}>({});
19
- }
20
- }
21
-
22
- class GetSchemasById extends SchemaRegistryApi {
23
- public url = '/schemas/ids/:id';
24
- public method = 'get';
25
-
26
- public static fetch(id: number) {
27
- return this._fetch<{ schema: string }>({ path: { id } });
28
- }
29
- }
30
-
31
- class GetSchemasTypes extends SchemaRegistryApi {
32
- public url = '/schemas/types';
33
- public method = 'get';
34
-
35
- public static fetch() {
36
- return this._fetch<string[]>({});
37
- }
38
- }
39
-
40
- class GetSchemasVersionsById extends SchemaRegistryApi {
41
- public url = '/schemas/ids/:id/versions';
42
- public method = 'get';
43
-
44
- public static fetch(id: number) {
45
- return this._fetch<{ subject: string; version: number }[]>({ path: { id } });
46
- }
47
- }
48
-
49
- class GetSubjects extends SchemaRegistryApi {
50
- public url = '/subjects';
51
- public method = 'get';
52
-
53
- public static fetch() {
54
- return this._fetch<string[]>({});
55
- }
56
- }
57
-
58
- class GetSubjectsVersions extends SchemaRegistryApi {
59
- public url = '/subjects/:subject/versions';
60
- public method = 'get';
61
-
62
- public static fetch(subject: string) {
63
- return this._fetch<number[]>({ path: { subject } });
64
- }
65
- }
66
-
67
- class DeleteSubjects extends SchemaRegistryApi {
68
- public url = '/subjects/:subject';
69
- public method = 'delete';
70
-
71
- public static fetch(data: { subject: string; permanent?: boolean }) {
72
- return this._fetch<number[]>({
73
- path: { subject: data.subject },
74
- params: { permanent: !!data.permanent },
75
- });
76
- }
77
- }
78
-
79
- class GetSubjectsByVersion extends SchemaRegistryApi {
80
- public url = '/subjects/:subject/versions/:version';
81
- public method = 'get';
82
-
83
- public static fetch(data: { subject: string; version: number | string }) {
84
- return this._fetch<{
85
- subject: string;
86
- id: number;
87
- version: number;
88
- schemaType: string;
89
- schema: string;
90
- }>({ path: data });
91
- }
92
- }
93
-
94
- class GetSchemaBySubjectsAndVersion extends SchemaRegistryApi {
95
- public url = '/subjects/:subject/versions/:version/schema';
96
- public method = 'get';
97
-
98
- public static fetch(data: { subject: string; version: number | string }) {
99
- return this._fetch<unknown>({ path: data });
100
- }
101
- }
102
-
103
- class PostSubjectsVersions extends SchemaRegistryApi {
104
- public url = '/subjects/:subject/versions';
105
- public method = 'post';
106
-
107
- public static fetch(data: {
108
- subject: string;
109
- schema: string | Record<string, any>;
110
- schemaType?: string;
111
- reference?: string;
112
- normalize?: boolean;
113
- }) {
114
- return this._fetch<{ id: number }>({
115
- path: { subject: data.subject },
116
- params: { normalize: !!data.normalize },
117
- data: {
118
- schema: toStringData(data.schema),
119
- schemaType: data.schemaType,
120
- reference: data.reference,
121
- },
122
- });
123
- }
124
- }
125
-
126
- class PostSubjects extends SchemaRegistryApi {
127
- public url = '/subjects/:subject';
128
- public method = 'post';
129
-
130
- public static fetch(data: {
131
- subject: string;
132
- schema: string | Record<string, any>;
133
- schemaType?: string;
134
- reference?: string;
135
- normalize?: boolean;
136
- }) {
137
- return this._fetch<{
138
- subject: string;
139
- id: number;
140
- version: number;
141
- schema: string;
142
- }>({
143
- path: { subject: data.subject },
144
- params: { normalize: !!data.normalize },
145
- data: {
146
- schema: toStringData(data.schema),
147
- schemaType: data.schemaType,
148
- reference: data.reference,
149
- },
150
- });
151
- }
152
- }
153
-
154
- async function encode(
155
- subject: string,
156
- data: Record<string, any>,
157
- version: string | number = 'latest',
158
- ) {
159
- const errors = [];
160
- const response = await GetSubjectsByVersion.fetch({ subject, version });
161
- const header = Buffer.alloc(5);
162
- const schema = Type.forSchema(JSON.parse(response.data.schema));
163
- schema.isValid(data, {
164
- errorHook: (path: string[], value: unknown) => {
165
- errors.push(`${path.join('.')}: ${value} (${typeof value})`);
166
- },
167
- });
168
- if (errors.length) throw new SchemaRegistryAvroSchemaParseError(errors);
169
- header.writeInt32BE(response.data.id, 1);
170
- return Buffer.concat([header, schema.toBuffer(data)]);
171
- }
172
-
173
- async function decode(buffer: Buffer) {
174
- const id = buffer.readInt32BE(1);
175
- let data: Type = cache.get(id);
176
- if (!data) {
177
- const response = await GetSchemasById.fetch(id);
178
- data = Type.forSchema(JSON.parse(response.data.schema));
179
- cache.set(id, data);
180
- }
181
- const schema = Type.forSchema(data);
182
- return schema.fromBuffer(buffer.slice(5));
183
- }
184
-
185
- function toStringData(data: string | Record<string, any>) {
186
- return typeof data === 'string' ? data : JSON.stringify(data);
187
- }
188
-
189
- return {
190
- ping: <typeof Ping.fetch>Ping.fetch.bind(Ping),
191
- getSchemasById: <typeof GetSchemasById.fetch>(
192
- GetSchemasById.fetch.bind(GetSchemasById)
193
- ),
194
- getSchemasTypes: <typeof GetSchemasTypes.fetch>(
195
- GetSchemasTypes.fetch.bind(GetSchemasTypes)
196
- ),
197
- getSchemasVersionsById: <typeof GetSchemasVersionsById.fetch>(
198
- GetSchemasVersionsById.fetch.bind(GetSchemasVersionsById)
199
- ),
200
- getSubjects: <typeof GetSubjects.fetch>GetSubjects.fetch.bind(GetSubjects),
201
- getSubjectsVersions: <typeof GetSubjectsVersions.fetch>(
202
- GetSubjectsVersions.fetch.bind(GetSubjectsVersions)
203
- ),
204
- deleteSubjects: <typeof DeleteSubjects.fetch>(
205
- DeleteSubjects.fetch.bind(DeleteSubjects)
206
- ),
207
- getSubjectsByVersion: <typeof GetSubjectsByVersion.fetch>(
208
- GetSubjectsByVersion.fetch.bind(GetSubjectsByVersion)
209
- ),
210
- getSchemaBySubjectsAndVersion: <typeof GetSchemaBySubjectsAndVersion.fetch>(
211
- GetSchemaBySubjectsAndVersion.fetch.bind(GetSchemaBySubjectsAndVersion)
212
- ),
213
- postSubjects: <typeof PostSubjects.fetch>PostSubjects.fetch.bind(PostSubjects),
214
- postSubjectsVersions: <typeof PostSubjectsVersions.fetch>(
215
- PostSubjectsVersions.fetch.bind(PostSubjectsVersions)
216
- ),
217
- encode,
218
- decode,
219
- };
220
- };
package/src/errors.ts DELETED
@@ -1,15 +0,0 @@
1
- import { BaseError } from '@biorate/errors';
2
-
3
- export class SchemaRegistryCantConnectError extends BaseError {
4
- public constructor(e: Error) {
5
- super(`Can't connect to schema registry: [%s]`, [e.message]);
6
- }
7
- }
8
-
9
- export class SchemaRegistryAvroSchemaParseError extends BaseError {
10
- public constructor(errors: string[]) {
11
- super('%s', [errors.join('; ')], {
12
- status: 400,
13
- });
14
- }
15
- }
package/src/index.ts DELETED
@@ -1,84 +0,0 @@
1
- import { injectable } from '@biorate/inversion';
2
- import { ISchemaRegistryConfig, ISchemaRegistryConnection } from './interfaces';
3
- import { Connector } from '@biorate/connector';
4
- import { SchemaRegistryCantConnectError } from './errors';
5
- import { create } from './api';
6
- export * from './api';
7
- export * from './errors';
8
- export * from './interfaces';
9
-
10
- /**
11
- * @description Schema registry connector
12
- *
13
- * ### Features:
14
- * - connector manager for schema registry
15
- *
16
- * @example
17
- * ```
18
- * import { inject, container, Types, Core } from '@biorate/inversion';
19
- * import { IConfig, Config } from '@biorate/config';
20
- * import { IConnector } from '@biorate/connector';
21
- * import { SchemaRegistryConnector, ISchemaRegistryConnector } from '@biorate/schema-registry';
22
- *
23
- * export class Root extends Core() {
24
- * @inject(SchemaRegistryConnector) public connector: ISchemaRegistryConnector;
25
- * }
26
- *
27
- * container.bind<IConfig>(Types.Config).to(Config).inSingletonScope();
28
- * container
29
- * .bind<ISchemaRegistryConnector>(SchemaRegistryConnector)
30
- * .toSelf()
31
- * .inSingletonScope();
32
- * container.bind<Root>(Root).toSelf().inSingletonScope();
33
- *
34
- * container.get<IConfig>(Types.Config).merge({
35
- * SchemaRegistry: [{ name: 'connection', baseURL: 'http://localhost:8085' }],
36
- * });
37
- *
38
- * (async () => {
39
- * const root = container.get<Root>(Root);
40
- * await root.$run();
41
- *
42
- * const { PostSubjectsVersions } = root.connector.connection('connection');
43
- * const { data } = await PostSubjectsVersions.fetch({
44
- * subject: 'test',
45
- * schema: {
46
- * type: 'record',
47
- * name: 'Test',
48
- * namespace: 'test',
49
- * fields: [
50
- * {
51
- * name: 'firstName',
52
- * type: 'string',
53
- * },
54
- * {
55
- * name: 'lastName',
56
- * type: 'string',
57
- * },
58
- * {
59
- * name: 'age',
60
- * type: 'int',
61
- * },
62
- * ],
63
- * },
64
- * });
65
- * console.log(data); // { id: 1 }
66
- * })();
67
- * ```
68
- */
69
- @injectable()
70
- export class SchemaRegistryConnector extends Connector<
71
- ISchemaRegistryConfig,
72
- ISchemaRegistryConnection
73
- > {
74
- protected readonly namespace = 'SchemaRegistry';
75
- protected async connect(config) {
76
- const connection = create(config);
77
- try {
78
- await connection.ping();
79
- } catch (e) {
80
- throw new SchemaRegistryCantConnectError(e);
81
- }
82
- return connection;
83
- }
84
- }
package/src/interfaces.ts DELETED
@@ -1,13 +0,0 @@
1
- import { IConnectorConfig, IConnector } from '@biorate/connector';
2
- import { create } from './api';
3
-
4
- export type ISchemaRegistryConnection = ReturnType<typeof create>;
5
-
6
- export interface ISchemaRegistryConfig extends IConnectorConfig {
7
- baseURL: string;
8
- }
9
-
10
- export type ISchemaRegistryConnector = IConnector<
11
- ISchemaRegistryConfig,
12
- ISchemaRegistryConnection
13
- >;
@@ -1,22 +0,0 @@
1
- import { use } from 'chai';
2
- import { jestSnapshotPlugin } from 'mocha-chai-jest-snapshot';
3
- import { inject, container, Types, Core } from '@biorate/inversion';
4
- import { IConfig, Config } from '@biorate/config';
5
- import { SchemaRegistryConnector, ISchemaRegistryConnector } from '../../src';
6
-
7
- use(jestSnapshotPlugin());
8
-
9
- export class Root extends Core() {
10
- @inject(SchemaRegistryConnector) public connector: ISchemaRegistryConnector;
11
- }
12
-
13
- container.bind<IConfig>(Types.Config).to(Config).inSingletonScope();
14
- container
15
- .bind<ISchemaRegistryConnector>(SchemaRegistryConnector)
16
- .toSelf()
17
- .inSingletonScope();
18
- container.bind<Root>(Root).toSelf().inSingletonScope();
19
-
20
- container.get<IConfig>(Types.Config).merge({
21
- SchemaRegistry: [{ name: 'connection', baseURL: 'http://localhost:8085' }],
22
- });