@azure/schema-registry-json 1.0.1-alpha.20241206.1 → 1.0.1-alpha.20241210.2
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.
- package/dist/browser/errors.d.ts +5 -0
- package/dist/browser/errors.d.ts.map +1 -0
- package/dist/browser/index.d.ts +3 -0
- package/dist/browser/index.d.ts.map +1 -0
- package/dist/browser/index.js +5 -0
- package/{dist-esm/src → dist/browser}/index.js.map +1 -1
- package/dist/browser/jsonSchemaSerializer.d.ts +46 -0
- package/dist/browser/jsonSchemaSerializer.d.ts.map +1 -0
- package/{dist-esm/src → dist/browser}/jsonSchemaSerializer.js +3 -3
- package/dist/browser/jsonSchemaSerializer.js.map +1 -0
- package/dist/browser/logger.d.ts +5 -0
- package/dist/browser/logger.d.ts.map +1 -0
- package/dist/browser/models.d.ts +51 -0
- package/dist/browser/models.d.ts.map +1 -0
- package/dist/browser/package.json +3 -0
- package/dist/browser/utility.d.ts +3 -0
- package/dist/browser/utility.d.ts.map +1 -0
- package/{dist-esm/src → dist/browser}/utility.js.map +1 -1
- package/dist/commonjs/errors.d.ts +5 -0
- package/dist/commonjs/errors.d.ts.map +1 -0
- package/dist/commonjs/errors.js +27 -0
- package/dist/commonjs/errors.js.map +1 -0
- package/dist/commonjs/index.d.ts +3 -0
- package/dist/commonjs/index.d.ts.map +1 -0
- package/dist/commonjs/index.js +10 -0
- package/dist/commonjs/index.js.map +1 -0
- package/dist/commonjs/jsonSchemaSerializer.d.ts +46 -0
- package/dist/commonjs/jsonSchemaSerializer.d.ts.map +1 -0
- package/dist/commonjs/jsonSchemaSerializer.js +177 -0
- package/dist/commonjs/jsonSchemaSerializer.js.map +1 -0
- package/dist/commonjs/logger.d.ts +5 -0
- package/dist/commonjs/logger.d.ts.map +1 -0
- package/dist/commonjs/logger.js +11 -0
- package/dist/commonjs/logger.js.map +1 -0
- package/dist/commonjs/models.d.ts +51 -0
- package/dist/commonjs/models.d.ts.map +1 -0
- package/dist/commonjs/models.js +5 -0
- package/dist/commonjs/models.js.map +1 -0
- package/dist/commonjs/package.json +3 -0
- package/dist/commonjs/tsdoc-metadata.json +11 -0
- package/dist/commonjs/utility.d.ts +3 -0
- package/dist/commonjs/utility.d.ts.map +1 -0
- package/dist/commonjs/utility.js +10 -0
- package/dist/commonjs/utility.js.map +1 -0
- package/dist/esm/errors.d.ts +5 -0
- package/dist/esm/errors.d.ts.map +1 -0
- package/dist/esm/errors.js +23 -0
- package/dist/esm/errors.js.map +1 -0
- package/dist/esm/index.d.ts +3 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/jsonSchemaSerializer.d.ts +46 -0
- package/dist/esm/jsonSchemaSerializer.d.ts.map +1 -0
- package/dist/{index.js → esm/jsonSchemaSerializer.js} +10 -52
- package/dist/esm/jsonSchemaSerializer.js.map +1 -0
- package/dist/esm/logger.d.ts +5 -0
- package/dist/esm/logger.d.ts.map +1 -0
- package/dist/esm/logger.js +8 -0
- package/dist/esm/logger.js.map +1 -0
- package/dist/esm/models.d.ts +51 -0
- package/dist/esm/models.d.ts.map +1 -0
- package/dist/esm/models.js +4 -0
- package/dist/esm/models.js.map +1 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/utility.d.ts +3 -0
- package/dist/esm/utility.d.ts.map +1 -0
- package/dist/esm/utility.js +7 -0
- package/dist/esm/utility.js.map +1 -0
- package/dist/react-native/errors.d.ts +5 -0
- package/dist/react-native/errors.d.ts.map +1 -0
- package/dist/react-native/errors.js +23 -0
- package/dist/react-native/errors.js.map +1 -0
- package/dist/react-native/index.d.ts +3 -0
- package/dist/react-native/index.d.ts.map +1 -0
- package/dist/react-native/index.js +5 -0
- package/dist/react-native/index.js.map +1 -0
- package/dist/react-native/jsonSchemaSerializer.d.ts +46 -0
- package/dist/react-native/jsonSchemaSerializer.d.ts.map +1 -0
- package/dist/react-native/jsonSchemaSerializer.js +173 -0
- package/dist/react-native/jsonSchemaSerializer.js.map +1 -0
- package/dist/react-native/logger.d.ts +5 -0
- package/dist/react-native/logger.d.ts.map +1 -0
- package/dist/react-native/logger.js +8 -0
- package/dist/react-native/logger.js.map +1 -0
- package/dist/react-native/models.d.ts +51 -0
- package/dist/react-native/models.d.ts.map +1 -0
- package/dist/react-native/models.js +4 -0
- package/dist/react-native/models.js.map +1 -0
- package/dist/react-native/package.json +3 -0
- package/dist/react-native/utility.d.ts +3 -0
- package/dist/react-native/utility.d.ts.map +1 -0
- package/dist/react-native/utility.js +7 -0
- package/dist/react-native/utility.js.map +1 -0
- package/package.json +65 -36
- package/dist/index.js.map +0 -1
- package/dist-esm/src/index.js +0 -5
- package/dist-esm/src/jsonSchemaSerializer.js.map +0 -1
- package/types/schema-registry-json.d.ts +0 -101
- /package/{dist-esm/src → dist/browser}/errors.js +0 -0
- /package/{dist-esm/src → dist/browser}/errors.js.map +0 -0
- /package/{dist-esm/src → dist/browser}/logger.js +0 -0
- /package/{dist-esm/src → dist/browser}/logger.js.map +0 -0
- /package/{dist-esm/src → dist/browser}/models.js +0 -0
- /package/{dist-esm/src → dist/browser}/models.js.map +0 -0
- /package/{dist-esm/src → dist/browser}/utility.js +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"models.js","sourceRoot":"","sources":["../../src/models.ts"],"names":[],"mappings":";AAAA,uCAAuC;AACvC,kCAAkC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\n/**\n * A message that contains binary data and a content type.\n */\nexport interface MessageContent {\n /**\n * The message's binary data\n */\n data: Uint8Array;\n /**\n * The message's content type\n */\n contentType: string;\n}\n\n/**\n * MessageAdapter is an interface that converts to/from a concrete message type\n * to a MessageContent\n */\nexport interface MessageAdapter<MessageT> {\n /**\n * defines how to create a message from a payload and a content type\n */\n produce: (messageContent: MessageContent) => MessageT;\n /**\n * defines how to access the payload and the content type of a message\n */\n consume: (message: MessageT) => MessageContent;\n}\n\n/**\n * Options for Schema\n */\nexport interface JsonSchemaSerializerOptions<MessageT> {\n /**\n * The group name to be used when registering/looking up a schema. Must be specified\n * if `serialize` will be called.\n */\n groupName?: string;\n /**\n * Message Adapter enables the serializer to produce and consume custom messages.\n */\n messageAdapter?: MessageAdapter<MessageT>;\n}\n\n/**\n * The options to the deserialize method.\n */\nexport interface DeserializeOptions {\n /**\n * Validate the value against the schema. Raise an error if the validation is not successful.\n */\n validateCallback?: (value: unknown, schema: string) => void;\n}\n"]}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// This file is read by tools that parse documentation comments conforming to the TSDoc standard.
|
|
2
|
+
// It should be published with your NPM package. It should not be tracked by Git.
|
|
3
|
+
{
|
|
4
|
+
"tsdocVersion": "0.12",
|
|
5
|
+
"toolPackages": [
|
|
6
|
+
{
|
|
7
|
+
"packageName": "@microsoft/api-extractor",
|
|
8
|
+
"packageVersion": "7.48.0"
|
|
9
|
+
}
|
|
10
|
+
]
|
|
11
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utility.d.ts","sourceRoot":"","sources":["../../src/utility.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAElD,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,IAAI,cAAc,CAG5E"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright (c) Microsoft Corporation.
|
|
3
|
+
// Licensed under the MIT License.
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.isMessageContent = isMessageContent;
|
|
6
|
+
function isMessageContent(message) {
|
|
7
|
+
const castMessage = message;
|
|
8
|
+
return castMessage.data !== undefined && castMessage.contentType !== undefined;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=utility.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utility.js","sourceRoot":"","sources":["../../src/utility.ts"],"names":[],"mappings":";AAAA,uCAAuC;AACvC,kCAAkC;;AAIlC,4CAGC;AAHD,SAAgB,gBAAgB,CAAC,OAAgB;IAC/C,MAAM,WAAW,GAAG,OAAyB,CAAC;IAC9C,OAAO,WAAW,CAAC,IAAI,KAAK,SAAS,IAAI,WAAW,CAAC,WAAW,KAAK,SAAS,CAAC;AACjF,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport type { MessageContent } from \"./models.js\";\n\nexport function isMessageContent(message: unknown): message is MessageContent {\n const castMessage = message as MessageContent;\n return castMessage.data !== undefined && castMessage.contentType !== undefined;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAGA,gBAAgB;AAChB,wBAAgB,SAAS,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,CAAC,EAAE,OAAO,EAAE,MAAM,GAAG,CAAC,CAQ3D;AAED,gBAAgB;AAChB,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,GAAG,KAAK,CASnE"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation.
|
|
2
|
+
// Licensed under the MIT License.
|
|
3
|
+
/** @internal */
|
|
4
|
+
export function wrapError(f, message) {
|
|
5
|
+
let result;
|
|
6
|
+
try {
|
|
7
|
+
result = f();
|
|
8
|
+
}
|
|
9
|
+
catch (cause) {
|
|
10
|
+
throw errorWithCause(message, cause);
|
|
11
|
+
}
|
|
12
|
+
return result;
|
|
13
|
+
}
|
|
14
|
+
/** @internal */
|
|
15
|
+
export function errorWithCause(message, cause) {
|
|
16
|
+
return new Error(message,
|
|
17
|
+
// TS v4.6 and below do not yet recognize the cause option in the Error constructor
|
|
18
|
+
// see https://medium.com/ovrsea/power-up-your-node-js-debugging-and-error-handling-with-the-new-error-cause-feature-4136c563126a
|
|
19
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
20
|
+
// @ts-ignore
|
|
21
|
+
{ cause });
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,gBAAgB;AAChB,MAAM,UAAU,SAAS,CAAI,CAAU,EAAE,OAAe;IACtD,IAAI,MAAS,CAAC;IACd,IAAI,CAAC;QACH,MAAM,GAAG,CAAC,EAAE,CAAC;IACf,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,cAAc,CAAC,OAAO,EAAE,KAAc,CAAC,CAAC;IAChD,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,gBAAgB;AAChB,MAAM,UAAU,cAAc,CAAC,OAAe,EAAE,KAAY;IAC1D,OAAO,IAAI,KAAK,CACd,OAAO;IACP,mFAAmF;IACnF,iIAAiI;IACjI,6DAA6D;IAC7D,aAAa;IACb,EAAE,KAAK,EAAE,CACV,CAAC;AACJ,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\n/** @internal */\nexport function wrapError<T>(f: () => T, message: string): T {\n let result: T;\n try {\n result = f();\n } catch (cause) {\n throw errorWithCause(message, cause as Error);\n }\n return result;\n}\n\n/** @internal */\nexport function errorWithCause(message: string, cause: Error): Error {\n return new Error(\n message,\n // TS v4.6 and below do not yet recognize the cause option in the Error constructor\n // see https://medium.com/ovrsea/power-up-your-node-js-debugging-and-error-handling-with-the-new-error-cause-feature-4136c563126a\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n { cause },\n );\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAEjE,cAAc,aAAa,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAEjE,cAAc,aAAa,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nexport { JsonSchemaSerializer } from \"./jsonSchemaSerializer.js\";\n\nexport * from \"./models.js\";\n"]}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { DeserializeOptions, JsonSchemaSerializerOptions, MessageContent } from "./models.js";
|
|
2
|
+
import type { SchemaRegistry } from "@azure/schema-registry";
|
|
3
|
+
/**
|
|
4
|
+
* Json serializer that obtains schemas from a schema registry and does not
|
|
5
|
+
* pack schemas into its payloads.
|
|
6
|
+
*/
|
|
7
|
+
export declare class JsonSchemaSerializer<MessageT = MessageContent> {
|
|
8
|
+
/**
|
|
9
|
+
* Creates a new serializer.
|
|
10
|
+
*
|
|
11
|
+
* @param client - Schema Registry where schemas are registered and obtained.
|
|
12
|
+
* Usually this is a SchemaRegistryClient instance.
|
|
13
|
+
*/
|
|
14
|
+
constructor(client: SchemaRegistry, options?: JsonSchemaSerializerOptions<MessageT>);
|
|
15
|
+
private readonly schemaGroup?;
|
|
16
|
+
private readonly registry;
|
|
17
|
+
private readonly messageAdapter?;
|
|
18
|
+
private readonly cacheIdByDefinition;
|
|
19
|
+
private readonly cacheById;
|
|
20
|
+
/**
|
|
21
|
+
* serializes the value parameter according to the input schema and creates a message
|
|
22
|
+
* with the serialized data.
|
|
23
|
+
*
|
|
24
|
+
* @param value - The value to serialize.
|
|
25
|
+
* @param schema - The Json schema to use.
|
|
26
|
+
* @returns A new message with the serialized value. The structure of message is
|
|
27
|
+
* constrolled by the message factory option.
|
|
28
|
+
* @throws {@link Error}
|
|
29
|
+
* Thrown if the schema can not be parsed or the value does not match the schema.
|
|
30
|
+
*/
|
|
31
|
+
serialize(value: unknown, schema: string): Promise<MessageT>;
|
|
32
|
+
/**
|
|
33
|
+
* Deserializes the payload of the message using the schema ID in the content type
|
|
34
|
+
* field if no schema was provided.
|
|
35
|
+
*
|
|
36
|
+
* @param message - The message with the payload to be deserialized.
|
|
37
|
+
* @returns The deserialized value.
|
|
38
|
+
* @throws {@link Error}
|
|
39
|
+
* Thrown if the deserialization failed, e.g. because reader and writer schemas are incompatible.
|
|
40
|
+
*/
|
|
41
|
+
deserialize<T>(message: MessageT, options?: DeserializeOptions): Promise<T>;
|
|
42
|
+
private getSchemaById;
|
|
43
|
+
private getSchemaByDefinition;
|
|
44
|
+
private cache;
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=jsonSchemaSerializer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jsonSchemaSerializer.d.ts","sourceRoot":"","sources":["../../src/jsonSchemaSerializer.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,kBAAkB,EAClB,2BAA2B,EAE3B,cAAc,EACf,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EAAqB,cAAc,EAAE,MAAM,wBAAwB,CAAC;AA0ChF;;;GAGG;AACH,qBAAa,oBAAoB,CAAC,QAAQ,GAAG,cAAc;IACzD;;;;;OAKG;gBACS,MAAM,EAAE,cAAc,EAAE,OAAO,CAAC,EAAE,2BAA2B,CAAC,QAAQ,CAAC;IAMnF,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAS;IACtC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAiB;IAC1C,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAC,CAA2B;IAC3D,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAA8C;IAClF,OAAO,CAAC,QAAQ,CAAC,SAAS,CAA8C;IAExE;;;;;;;;;;OAUG;IACG,SAAS,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC;IAuBlE;;;;;;;;OAQG;IACG,WAAW,CAAC,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,CAAC,EAAE,kBAAkB,GAAG,OAAO,CAAC,CAAC,CAAC;YAkBnE,aAAa;YAkBb,qBAAqB;IAmCnC,OAAO,CAAC,KAAK;CASd"}
|
|
@@ -1,50 +1,10 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var schemaRegistry = require('@azure/schema-registry');
|
|
6
|
-
var lruCache = require('lru-cache');
|
|
7
|
-
var logger$1 = require('@azure/logger');
|
|
8
|
-
|
|
9
|
-
// Copyright (c) Microsoft Corporation.
|
|
10
|
-
// Licensed under the MIT License.
|
|
11
|
-
function isMessageContent(message) {
|
|
12
|
-
const castMessage = message;
|
|
13
|
-
return castMessage.data !== undefined && castMessage.contentType !== undefined;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
// Copyright (c) Microsoft Corporation.
|
|
17
|
-
// Licensed under the MIT License.
|
|
18
|
-
/** @internal */
|
|
19
|
-
function wrapError(f, message) {
|
|
20
|
-
let result;
|
|
21
|
-
try {
|
|
22
|
-
result = f();
|
|
23
|
-
}
|
|
24
|
-
catch (cause) {
|
|
25
|
-
throw errorWithCause(message, cause);
|
|
26
|
-
}
|
|
27
|
-
return result;
|
|
28
|
-
}
|
|
29
|
-
/** @internal */
|
|
30
|
-
function errorWithCause(message, cause) {
|
|
31
|
-
return new Error(message,
|
|
32
|
-
// TS v4.6 and below do not yet recognize the cause option in the Error constructor
|
|
33
|
-
// see https://medium.com/ovrsea/power-up-your-node-js-debugging-and-error-handling-with-the-new-error-cause-feature-4136c563126a
|
|
34
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
35
|
-
// @ts-ignore
|
|
36
|
-
{ cause });
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// Copyright (c) Microsoft Corporation.
|
|
40
|
-
// Licensed under the MIT License.
|
|
41
|
-
/**
|
|
42
|
-
* The \@azure/logger configuration for the schema-registry-json package.
|
|
43
|
-
*/
|
|
44
|
-
const logger = logger$1.createClientLogger("schema-registry-json");
|
|
45
|
-
|
|
46
1
|
// Copyright (c) Microsoft Corporation.
|
|
47
2
|
// Licensed under the MIT License.
|
|
3
|
+
import { KnownSchemaFormats } from "@azure/schema-registry";
|
|
4
|
+
import { isMessageContent } from "./utility.js";
|
|
5
|
+
import { errorWithCause, wrapError } from "./errors.js";
|
|
6
|
+
import { LRUCache } from "lru-cache";
|
|
7
|
+
import { logger } from "./logger.js";
|
|
48
8
|
const jsonMimeType = "application/json";
|
|
49
9
|
const encoder = new TextEncoder();
|
|
50
10
|
const decoder = new TextDecoder();
|
|
@@ -66,7 +26,7 @@ const cacheOptions = {
|
|
|
66
26
|
* Json serializer that obtains schemas from a schema registry and does not
|
|
67
27
|
* pack schemas into its payloads.
|
|
68
28
|
*/
|
|
69
|
-
class JsonSchemaSerializer {
|
|
29
|
+
export class JsonSchemaSerializer {
|
|
70
30
|
/**
|
|
71
31
|
* Creates a new serializer.
|
|
72
32
|
*
|
|
@@ -74,8 +34,8 @@ class JsonSchemaSerializer {
|
|
|
74
34
|
* Usually this is a SchemaRegistryClient instance.
|
|
75
35
|
*/
|
|
76
36
|
constructor(client, options) {
|
|
77
|
-
this.cacheIdByDefinition = new
|
|
78
|
-
this.cacheById = new
|
|
37
|
+
this.cacheIdByDefinition = new LRUCache(cacheOptions);
|
|
38
|
+
this.cacheById = new LRUCache(cacheOptions);
|
|
79
39
|
this.registry = client;
|
|
80
40
|
this.schemaGroup = options === null || options === void 0 ? void 0 : options.groupName;
|
|
81
41
|
this.messageAdapter = options === null || options === void 0 ? void 0 : options.messageAdapter;
|
|
@@ -156,7 +116,7 @@ class JsonSchemaSerializer {
|
|
|
156
116
|
const description = {
|
|
157
117
|
groupName: this.schemaGroup,
|
|
158
118
|
name: getSchemaName(schemaObj),
|
|
159
|
-
format:
|
|
119
|
+
format: KnownSchemaFormats.Json,
|
|
160
120
|
definition,
|
|
161
121
|
};
|
|
162
122
|
let id;
|
|
@@ -210,6 +170,4 @@ function getSchemaName(schema) {
|
|
|
210
170
|
}
|
|
211
171
|
return id;
|
|
212
172
|
}
|
|
213
|
-
|
|
214
|
-
exports.JsonSchemaSerializer = JsonSchemaSerializer;
|
|
215
|
-
//# sourceMappingURL=index.js.map
|
|
173
|
+
//# sourceMappingURL=jsonSchemaSerializer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jsonSchemaSerializer.js","sourceRoot":"","sources":["../../src/jsonSchemaSerializer.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AASlC,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAC5D,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxD,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAErC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,MAAM,YAAY,GAAG,kBAAkB,CAAC;AACxC,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;AAClC,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;AAalC,SAAS,eAAe,CAAC,MAAc;IACrC,OAAO,SAAS,CACd,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,EACxB,oCAAoC,MAAM,mCAAmC,CAC9E,CAAC;AACJ,CAAC;AAED,MAAM,YAAY,GAA0C;IAC1D,GAAG,EAAE,GAAG;IACR;;;OAGG;IACH,OAAO,EAAE,MAAM,CAAC,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AAE,GAAG,MAAM,CAAC,GAAG,IAAI,MAAM,CAAC,EAAE,CAAC;IACnC,IAAI,CAAC,EAAE,EAAE,CAAC;QACR,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;IAC7C,CAAC;IACD,OAAO,EAAE,CAAC;AACZ,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport type {\n DeserializeOptions,\n JsonSchemaSerializerOptions,\n MessageAdapter,\n MessageContent,\n} from \"./models.js\";\nimport type { SchemaDescription, SchemaRegistry } from \"@azure/schema-registry\";\nimport { KnownSchemaFormats } from \"@azure/schema-registry\";\nimport { isMessageContent } from \"./utility.js\";\nimport { errorWithCause, wrapError } from \"./errors.js\";\nimport { LRUCache } from \"lru-cache\";\nimport LRUCacheOptions = LRUCache.Options;\nimport { logger } from \"./logger.js\";\n\nconst jsonMimeType = \"application/json\";\nconst encoder = new TextEncoder();\nconst decoder = new TextDecoder();\n\ninterface CacheEntry {\n /** Schema ID */\n id: string;\n /** Schema string */\n schema: string;\n}\ninterface SchemaObject {\n id?: string;\n $id?: string;\n $schema?: string;\n}\nfunction getSchemaObject(schema: string): SchemaObject {\n return wrapError(\n () => JSON.parse(schema),\n `Parsing Json schema failed:\\n\\n\\t${schema}\\n\\nSee 'cause' for more details.`,\n );\n}\n\nconst cacheOptions: LRUCacheOptions<string, any, unknown> = {\n max: 128,\n /**\n * This is needed in order to specify `sizeCalculation` but we do not intend\n * to limit the size just yet.\n */\n maxSize: Number.MAX_VALUE,\n sizeCalculation: (_value: any, key: string) => {\n return key.length;\n },\n};\n\n/**\n * Json serializer that obtains schemas from a schema registry and does not\n * pack schemas into its payloads.\n */\nexport class JsonSchemaSerializer<MessageT = MessageContent> {\n /**\n * Creates a new serializer.\n *\n * @param client - Schema Registry where schemas are registered and obtained.\n * Usually this is a SchemaRegistryClient instance.\n */\n constructor(client: SchemaRegistry, options?: JsonSchemaSerializerOptions<MessageT>) {\n this.registry = client;\n this.schemaGroup = options?.groupName;\n this.messageAdapter = options?.messageAdapter;\n }\n\n private readonly schemaGroup?: string;\n private readonly registry: SchemaRegistry;\n private readonly messageAdapter?: MessageAdapter<MessageT>;\n private readonly cacheIdByDefinition = new LRUCache<string, string>(cacheOptions);\n private readonly cacheById = new LRUCache<string, string>(cacheOptions);\n\n /**\n * serializes the value parameter according to the input schema and creates a message\n * with the serialized data.\n *\n * @param value - The value to serialize.\n * @param schema - The Json schema to use.\n * @returns A new message with the serialized value. The structure of message is\n * constrolled by the message factory option.\n * @throws {@link Error}\n * Thrown if the schema can not be parsed or the value does not match the schema.\n */\n async serialize(value: unknown, schema: string): Promise<MessageT> {\n const entry = await this.getSchemaByDefinition(schema);\n const data = wrapError(\n () => encoder.encode(JSON.stringify(value)),\n `Json serialization failed. See 'cause' for more details. Schema ID: ${entry.id}`,\n );\n const contentType = `${jsonMimeType}+${entry.id}`;\n return this.messageAdapter\n ? this.messageAdapter.produce({\n contentType,\n data,\n })\n : /**\n * If no message consumer was provided, then a MessageContent will be\n * returned. This should work because the MessageT type parameter defaults\n * to MessageContent.\n */\n ({\n data,\n contentType,\n } as MessageContent as unknown as MessageT);\n }\n\n /**\n * Deserializes the payload of the message using the schema ID in the content type\n * field if no schema was provided.\n *\n * @param message - The message with the payload to be deserialized.\n * @returns The deserialized value.\n * @throws {@link Error}\n * Thrown if the deserialization failed, e.g. because reader and writer schemas are incompatible.\n */\n async deserialize<T>(message: MessageT, options?: DeserializeOptions): Promise<T> {\n const { data, contentType } = convertMessage(message, this.messageAdapter);\n const schemaId = getSchemaId(contentType);\n const schema = await this.getSchemaById(schemaId);\n const returnedMessage = wrapError(\n () => JSON.parse(decoder.decode(data)),\n `Json deserialization failed with schema ID (${schemaId}). See 'cause' for more details.`,\n );\n const validate = options?.validateCallback;\n if (validate) {\n wrapError(\n () => validate(returnedMessage, schema),\n `Json validation failed. See 'cause' for more details. Schema ID: ${schemaId}`,\n );\n }\n return returnedMessage as T;\n }\n\n private async getSchemaById(schemaId: string): Promise<string> {\n const cached = this.cacheById.get(schemaId);\n if (cached) {\n return cached;\n }\n const schemaResponse = await this.registry.getSchema(schemaId);\n if (!schemaResponse) {\n throw new Error(`Schema with ID '${schemaId}' not found.`);\n }\n\n if (!schemaResponse.properties.format.match(/^json$/i)) {\n throw new Error(\n `Schema with ID '${schemaResponse.properties.id}' has format '${schemaResponse.properties.format}', not 'json'.`,\n );\n }\n return this.cache(schemaResponse.definition, schemaId).schema;\n }\n\n private async getSchemaByDefinition(definition: string): Promise<CacheEntry> {\n const schemaId = this.cacheIdByDefinition.get(definition);\n if (schemaId) {\n return { id: schemaId, schema: definition };\n }\n if (!this.schemaGroup) {\n throw new Error(\n \"Schema group must have been specified in the constructor options when the client was created in order to serialize.\",\n );\n }\n const schemaObj = getSchemaObject(definition);\n const description: SchemaDescription = {\n groupName: this.schemaGroup,\n name: getSchemaName(schemaObj),\n format: KnownSchemaFormats.Json,\n definition,\n };\n let id: string;\n\n try {\n id = (await this.registry.getSchemaProperties(description)).id;\n } catch (e) {\n if ((e as any).statusCode === 404) {\n throw errorWithCause(\n `Schema '${description.name}' not found in registry group '${description.groupName}', or not found to have matching definition.`,\n e as Error,\n );\n } else {\n throw e;\n }\n }\n\n return this.cache(definition, id);\n }\n\n private cache(schema: string, id: string): CacheEntry {\n const entry = { schema, id };\n this.cacheIdByDefinition.set(schema, id);\n this.cacheById.set(id, schema);\n logger.verbose(\n `Cache entry added or updated. Total number of entries: ${this.cacheIdByDefinition.size}; Total schema length ${this.cacheIdByDefinition.calculatedSize}`,\n );\n return entry;\n }\n}\n\nfunction getSchemaId(contentType: string): string {\n const contentTypeParts = contentType.split(\"+\");\n if (contentTypeParts.length !== 2) {\n throw new Error(\"Content type was not in the expected format of MIME type + schema ID\");\n }\n if (contentTypeParts[0] !== jsonMimeType) {\n throw new Error(\n `Received content of type ${contentTypeParts[0]} but an json serializer may only be used on content that is of '${jsonMimeType}' type`,\n );\n }\n return contentTypeParts[1];\n}\n\nfunction convertMessage<MessageT>(\n message: MessageT,\n adapter?: MessageAdapter<MessageT>,\n): MessageContent {\n const messageConsumer = adapter?.consume;\n if (messageConsumer) {\n return messageConsumer(message);\n } else if (isMessageContent(message)) {\n return message;\n } else {\n throw new Error(\n `Expected either a message adapter to be provided to the serializer or the input message to have data and contentType fields`,\n );\n }\n}\n\nfunction getSchemaName(schema: SchemaObject): string {\n const id = schema.$id || schema.id;\n if (!id) {\n throw new Error(\"Schema must have an ID.\");\n }\n return id;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../src/logger.ts"],"names":[],"mappings":"AAKA;;GAEG;AACH,eAAO,MAAM,MAAM,qCAA6C,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation.
|
|
2
|
+
// Licensed under the MIT License.
|
|
3
|
+
import { createClientLogger } from "@azure/logger";
|
|
4
|
+
/**
|
|
5
|
+
* The \@azure/logger configuration for the schema-registry-json package.
|
|
6
|
+
*/
|
|
7
|
+
export const logger = createClientLogger("schema-registry-json");
|
|
8
|
+
//# sourceMappingURL=logger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.js","sourceRoot":"","sources":["../../src/logger.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAEnD;;GAEG;AACH,MAAM,CAAC,MAAM,MAAM,GAAG,kBAAkB,CAAC,sBAAsB,CAAC,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport { createClientLogger } from \"@azure/logger\";\n\n/**\n * The \\@azure/logger configuration for the schema-registry-json package.\n */\nexport const logger = createClientLogger(\"schema-registry-json\");\n"]}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A message that contains binary data and a content type.
|
|
3
|
+
*/
|
|
4
|
+
export interface MessageContent {
|
|
5
|
+
/**
|
|
6
|
+
* The message's binary data
|
|
7
|
+
*/
|
|
8
|
+
data: Uint8Array;
|
|
9
|
+
/**
|
|
10
|
+
* The message's content type
|
|
11
|
+
*/
|
|
12
|
+
contentType: string;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* MessageAdapter is an interface that converts to/from a concrete message type
|
|
16
|
+
* to a MessageContent
|
|
17
|
+
*/
|
|
18
|
+
export interface MessageAdapter<MessageT> {
|
|
19
|
+
/**
|
|
20
|
+
* defines how to create a message from a payload and a content type
|
|
21
|
+
*/
|
|
22
|
+
produce: (messageContent: MessageContent) => MessageT;
|
|
23
|
+
/**
|
|
24
|
+
* defines how to access the payload and the content type of a message
|
|
25
|
+
*/
|
|
26
|
+
consume: (message: MessageT) => MessageContent;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Options for Schema
|
|
30
|
+
*/
|
|
31
|
+
export interface JsonSchemaSerializerOptions<MessageT> {
|
|
32
|
+
/**
|
|
33
|
+
* The group name to be used when registering/looking up a schema. Must be specified
|
|
34
|
+
* if `serialize` will be called.
|
|
35
|
+
*/
|
|
36
|
+
groupName?: string;
|
|
37
|
+
/**
|
|
38
|
+
* Message Adapter enables the serializer to produce and consume custom messages.
|
|
39
|
+
*/
|
|
40
|
+
messageAdapter?: MessageAdapter<MessageT>;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The options to the deserialize method.
|
|
44
|
+
*/
|
|
45
|
+
export interface DeserializeOptions {
|
|
46
|
+
/**
|
|
47
|
+
* Validate the value against the schema. Raise an error if the validation is not successful.
|
|
48
|
+
*/
|
|
49
|
+
validateCallback?: (value: unknown, schema: string) => void;
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=models.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"models.d.ts","sourceRoot":"","sources":["../../src/models.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B;;OAEG;IACH,IAAI,EAAE,UAAU,CAAC;IACjB;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC;CACrB;AAED;;;GAGG;AACH,MAAM,WAAW,cAAc,CAAC,QAAQ;IACtC;;OAEG;IACH,OAAO,EAAE,CAAC,cAAc,EAAE,cAAc,KAAK,QAAQ,CAAC;IACtD;;OAEG;IACH,OAAO,EAAE,CAAC,OAAO,EAAE,QAAQ,KAAK,cAAc,CAAC;CAChD;AAED;;GAEG;AACH,MAAM,WAAW,2BAA2B,CAAC,QAAQ;IACnD;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;OAEG;IACH,cAAc,CAAC,EAAE,cAAc,CAAC,QAAQ,CAAC,CAAC;CAC3C;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC;;OAEG;IACH,gBAAgB,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC;CAC7D"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"models.js","sourceRoot":"","sources":["../../src/models.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\n/**\n * A message that contains binary data and a content type.\n */\nexport interface MessageContent {\n /**\n * The message's binary data\n */\n data: Uint8Array;\n /**\n * The message's content type\n */\n contentType: string;\n}\n\n/**\n * MessageAdapter is an interface that converts to/from a concrete message type\n * to a MessageContent\n */\nexport interface MessageAdapter<MessageT> {\n /**\n * defines how to create a message from a payload and a content type\n */\n produce: (messageContent: MessageContent) => MessageT;\n /**\n * defines how to access the payload and the content type of a message\n */\n consume: (message: MessageT) => MessageContent;\n}\n\n/**\n * Options for Schema\n */\nexport interface JsonSchemaSerializerOptions<MessageT> {\n /**\n * The group name to be used when registering/looking up a schema. Must be specified\n * if `serialize` will be called.\n */\n groupName?: string;\n /**\n * Message Adapter enables the serializer to produce and consume custom messages.\n */\n messageAdapter?: MessageAdapter<MessageT>;\n}\n\n/**\n * The options to the deserialize method.\n */\nexport interface DeserializeOptions {\n /**\n * Validate the value against the schema. Raise an error if the validation is not successful.\n */\n validateCallback?: (value: unknown, schema: string) => void;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utility.d.ts","sourceRoot":"","sources":["../../src/utility.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAElD,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,IAAI,cAAc,CAG5E"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation.
|
|
2
|
+
// Licensed under the MIT License.
|
|
3
|
+
export function isMessageContent(message) {
|
|
4
|
+
const castMessage = message;
|
|
5
|
+
return castMessage.data !== undefined && castMessage.contentType !== undefined;
|
|
6
|
+
}
|
|
7
|
+
//# sourceMappingURL=utility.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utility.js","sourceRoot":"","sources":["../../src/utility.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAIlC,MAAM,UAAU,gBAAgB,CAAC,OAAgB;IAC/C,MAAM,WAAW,GAAG,OAAyB,CAAC;IAC9C,OAAO,WAAW,CAAC,IAAI,KAAK,SAAS,IAAI,WAAW,CAAC,WAAW,KAAK,SAAS,CAAC;AACjF,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport type { MessageContent } from \"./models.js\";\n\nexport function isMessageContent(message: unknown): message is MessageContent {\n const castMessage = message as MessageContent;\n return castMessage.data !== undefined && castMessage.contentType !== undefined;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAGA,gBAAgB;AAChB,wBAAgB,SAAS,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,CAAC,EAAE,OAAO,EAAE,MAAM,GAAG,CAAC,CAQ3D;AAED,gBAAgB;AAChB,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,GAAG,KAAK,CASnE"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation.
|
|
2
|
+
// Licensed under the MIT License.
|
|
3
|
+
/** @internal */
|
|
4
|
+
export function wrapError(f, message) {
|
|
5
|
+
let result;
|
|
6
|
+
try {
|
|
7
|
+
result = f();
|
|
8
|
+
}
|
|
9
|
+
catch (cause) {
|
|
10
|
+
throw errorWithCause(message, cause);
|
|
11
|
+
}
|
|
12
|
+
return result;
|
|
13
|
+
}
|
|
14
|
+
/** @internal */
|
|
15
|
+
export function errorWithCause(message, cause) {
|
|
16
|
+
return new Error(message,
|
|
17
|
+
// TS v4.6 and below do not yet recognize the cause option in the Error constructor
|
|
18
|
+
// see https://medium.com/ovrsea/power-up-your-node-js-debugging-and-error-handling-with-the-new-error-cause-feature-4136c563126a
|
|
19
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
20
|
+
// @ts-ignore
|
|
21
|
+
{ cause });
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,gBAAgB;AAChB,MAAM,UAAU,SAAS,CAAI,CAAU,EAAE,OAAe;IACtD,IAAI,MAAS,CAAC;IACd,IAAI,CAAC;QACH,MAAM,GAAG,CAAC,EAAE,CAAC;IACf,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,cAAc,CAAC,OAAO,EAAE,KAAc,CAAC,CAAC;IAChD,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,gBAAgB;AAChB,MAAM,UAAU,cAAc,CAAC,OAAe,EAAE,KAAY;IAC1D,OAAO,IAAI,KAAK,CACd,OAAO;IACP,mFAAmF;IACnF,iIAAiI;IACjI,6DAA6D;IAC7D,aAAa;IACb,EAAE,KAAK,EAAE,CACV,CAAC;AACJ,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\n/** @internal */\nexport function wrapError<T>(f: () => T, message: string): T {\n let result: T;\n try {\n result = f();\n } catch (cause) {\n throw errorWithCause(message, cause as Error);\n }\n return result;\n}\n\n/** @internal */\nexport function errorWithCause(message: string, cause: Error): Error {\n return new Error(\n message,\n // TS v4.6 and below do not yet recognize the cause option in the Error constructor\n // see https://medium.com/ovrsea/power-up-your-node-js-debugging-and-error-handling-with-the-new-error-cause-feature-4136c563126a\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n { cause },\n );\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAEjE,cAAc,aAAa,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAEjE,cAAc,aAAa,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nexport { JsonSchemaSerializer } from \"./jsonSchemaSerializer.js\";\n\nexport * from \"./models.js\";\n"]}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { DeserializeOptions, JsonSchemaSerializerOptions, MessageContent } from "./models.js";
|
|
2
|
+
import type { SchemaRegistry } from "@azure/schema-registry";
|
|
3
|
+
/**
|
|
4
|
+
* Json serializer that obtains schemas from a schema registry and does not
|
|
5
|
+
* pack schemas into its payloads.
|
|
6
|
+
*/
|
|
7
|
+
export declare class JsonSchemaSerializer<MessageT = MessageContent> {
|
|
8
|
+
/**
|
|
9
|
+
* Creates a new serializer.
|
|
10
|
+
*
|
|
11
|
+
* @param client - Schema Registry where schemas are registered and obtained.
|
|
12
|
+
* Usually this is a SchemaRegistryClient instance.
|
|
13
|
+
*/
|
|
14
|
+
constructor(client: SchemaRegistry, options?: JsonSchemaSerializerOptions<MessageT>);
|
|
15
|
+
private readonly schemaGroup?;
|
|
16
|
+
private readonly registry;
|
|
17
|
+
private readonly messageAdapter?;
|
|
18
|
+
private readonly cacheIdByDefinition;
|
|
19
|
+
private readonly cacheById;
|
|
20
|
+
/**
|
|
21
|
+
* serializes the value parameter according to the input schema and creates a message
|
|
22
|
+
* with the serialized data.
|
|
23
|
+
*
|
|
24
|
+
* @param value - The value to serialize.
|
|
25
|
+
* @param schema - The Json schema to use.
|
|
26
|
+
* @returns A new message with the serialized value. The structure of message is
|
|
27
|
+
* constrolled by the message factory option.
|
|
28
|
+
* @throws {@link Error}
|
|
29
|
+
* Thrown if the schema can not be parsed or the value does not match the schema.
|
|
30
|
+
*/
|
|
31
|
+
serialize(value: unknown, schema: string): Promise<MessageT>;
|
|
32
|
+
/**
|
|
33
|
+
* Deserializes the payload of the message using the schema ID in the content type
|
|
34
|
+
* field if no schema was provided.
|
|
35
|
+
*
|
|
36
|
+
* @param message - The message with the payload to be deserialized.
|
|
37
|
+
* @returns The deserialized value.
|
|
38
|
+
* @throws {@link Error}
|
|
39
|
+
* Thrown if the deserialization failed, e.g. because reader and writer schemas are incompatible.
|
|
40
|
+
*/
|
|
41
|
+
deserialize<T>(message: MessageT, options?: DeserializeOptions): Promise<T>;
|
|
42
|
+
private getSchemaById;
|
|
43
|
+
private getSchemaByDefinition;
|
|
44
|
+
private cache;
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=jsonSchemaSerializer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jsonSchemaSerializer.d.ts","sourceRoot":"","sources":["../../src/jsonSchemaSerializer.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,kBAAkB,EAClB,2BAA2B,EAE3B,cAAc,EACf,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EAAqB,cAAc,EAAE,MAAM,wBAAwB,CAAC;AA0ChF;;;GAGG;AACH,qBAAa,oBAAoB,CAAC,QAAQ,GAAG,cAAc;IACzD;;;;;OAKG;gBACS,MAAM,EAAE,cAAc,EAAE,OAAO,CAAC,EAAE,2BAA2B,CAAC,QAAQ,CAAC;IAMnF,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAS;IACtC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAiB;IAC1C,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAC,CAA2B;IAC3D,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAA8C;IAClF,OAAO,CAAC,QAAQ,CAAC,SAAS,CAA8C;IAExE;;;;;;;;;;OAUG;IACG,SAAS,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC;IAuBlE;;;;;;;;OAQG;IACG,WAAW,CAAC,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,CAAC,EAAE,kBAAkB,GAAG,OAAO,CAAC,CAAC,CAAC;YAkBnE,aAAa;YAkBb,qBAAqB;IAmCnC,OAAO,CAAC,KAAK;CASd"}
|