@trayio/commons 5.13.0 → 5.15.0

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.
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Utility for converting runtime JavaScript objects to JSON Schema
3
+ * Used for dynamic output schema generation in CDK connectors
4
+ *
5
+ * This implementation uses the generate-schema library to convert raw JSON
6
+ * responses into valid JSON Schema representations, matching the behavior
7
+ * of the legacy connector framework.
8
+ */
9
+ export declare class JsonSchemaIntrospector {
10
+ /**
11
+ * Introspects a JavaScript object and generates a JSON Schema representation
12
+ *
13
+ * @param obj - The object to introspect
14
+ * @returns A JSON Schema object describing the structure
15
+ */
16
+ static introspectToJsonSchema(obj: any): any;
17
+ }
18
+ //# sourceMappingURL=JsonSchemaIntrospector.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"JsonSchemaIntrospector.d.ts","sourceRoot":"","sources":["../../src/schema/JsonSchemaIntrospector.ts"],"names":[],"mappings":"AAEA;;;;;;;GAOG;AACH,qBAAa,sBAAsB;IAClC;;;;;OAKG;IACH,MAAM,CAAC,sBAAsB,CAAC,GAAG,EAAE,GAAG,GAAG,GAAG;CAS5C"}
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.JsonSchemaIntrospector = void 0;
27
+ const generateSchema = __importStar(require("generate-schema"));
28
+ /**
29
+ * Utility for converting runtime JavaScript objects to JSON Schema
30
+ * Used for dynamic output schema generation in CDK connectors
31
+ *
32
+ * This implementation uses the generate-schema library to convert raw JSON
33
+ * responses into valid JSON Schema representations, matching the behavior
34
+ * of the legacy connector framework.
35
+ */
36
+ class JsonSchemaIntrospector {
37
+ /**
38
+ * Introspects a JavaScript object and generates a JSON Schema representation
39
+ *
40
+ * @param obj - The object to introspect
41
+ * @returns A JSON Schema object describing the structure
42
+ */
43
+ static introspectToJsonSchema(obj) {
44
+ // If object already has $schema, assume it's a JSON schema and return as-is
45
+ // This matches the old connector framework behavior
46
+ if (obj && typeof obj === 'object' && obj.$schema) {
47
+ return obj;
48
+ }
49
+ return generateSchema.json('Schema', obj);
50
+ }
51
+ }
52
+ exports.JsonSchemaIntrospector = JsonSchemaIntrospector;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=JsonSchemaIntrospector.unit.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"JsonSchemaIntrospector.unit.test.d.ts","sourceRoot":"","sources":["../../src/schema/JsonSchemaIntrospector.unit.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,134 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const JsonSchemaIntrospector_1 = require("./JsonSchemaIntrospector");
4
+ describe('JsonSchemaIntrospector', () => {
5
+ describe('introspectToJsonSchema', () => {
6
+ it('should handle primitive types', () => {
7
+ const stringSchema = JsonSchemaIntrospector_1.JsonSchemaIntrospector.introspectToJsonSchema('test');
8
+ expect(stringSchema.$schema).toBe('http://json-schema.org/draft-04/schema#');
9
+ expect(stringSchema.type).toBeDefined();
10
+ const numberSchema = JsonSchemaIntrospector_1.JsonSchemaIntrospector.introspectToJsonSchema(42);
11
+ expect(numberSchema.$schema).toBe('http://json-schema.org/draft-04/schema#');
12
+ expect(numberSchema.type).toBe('number');
13
+ const boolSchema = JsonSchemaIntrospector_1.JsonSchemaIntrospector.introspectToJsonSchema(true);
14
+ expect(boolSchema.$schema).toBe('http://json-schema.org/draft-04/schema#');
15
+ expect(boolSchema.type).toBe('boolean');
16
+ const nullSchema = JsonSchemaIntrospector_1.JsonSchemaIntrospector.introspectToJsonSchema(null);
17
+ expect(nullSchema.$schema).toBe('http://json-schema.org/draft-04/schema#');
18
+ expect(nullSchema.type).toBe('null');
19
+ });
20
+ it('should handle simple objects', () => {
21
+ const obj = {
22
+ id: 1,
23
+ name: 'John',
24
+ active: true,
25
+ };
26
+ const schema = JsonSchemaIntrospector_1.JsonSchemaIntrospector.introspectToJsonSchema(obj);
27
+ expect(schema.$schema).toBe('http://json-schema.org/draft-04/schema#');
28
+ expect(schema.type).toBe('object');
29
+ expect(schema.properties).toEqual({
30
+ id: { type: 'number' },
31
+ name: { type: 'string' },
32
+ active: { type: 'boolean' },
33
+ });
34
+ // Note: generate-schema doesn't add 'required' array, which is fine for path referencing
35
+ });
36
+ it('should handle nested objects', () => {
37
+ const obj = {
38
+ user: {
39
+ id: 1,
40
+ profile: {
41
+ name: 'John',
42
+ age: 30,
43
+ },
44
+ },
45
+ };
46
+ const schema = JsonSchemaIntrospector_1.JsonSchemaIntrospector.introspectToJsonSchema(obj);
47
+ expect(schema.$schema).toBe('http://json-schema.org/draft-04/schema#');
48
+ expect(schema.type).toBe('object');
49
+ expect(schema.properties.user.type).toBe('object');
50
+ expect(schema.properties.user.properties.profile.type).toBe('object');
51
+ expect(schema.properties.user.properties.profile.properties.name).toEqual({
52
+ type: 'string',
53
+ });
54
+ });
55
+ it('should handle arrays', () => {
56
+ const obj = {
57
+ items: [1, 2, 3],
58
+ };
59
+ const schema = JsonSchemaIntrospector_1.JsonSchemaIntrospector.introspectToJsonSchema(obj);
60
+ expect(schema.properties.items).toEqual({
61
+ type: 'array',
62
+ items: { type: 'number' },
63
+ });
64
+ });
65
+ it('should handle arrays of objects', () => {
66
+ const obj = {
67
+ users: [
68
+ { id: 1, name: 'John' },
69
+ { id: 2, name: 'Jane' },
70
+ ],
71
+ };
72
+ const schema = JsonSchemaIntrospector_1.JsonSchemaIntrospector.introspectToJsonSchema(obj);
73
+ expect(schema.properties.users.type).toBe('array');
74
+ expect(schema.properties.users.items.type).toBe('object');
75
+ expect(schema.properties.users.items.properties).toEqual({
76
+ id: { type: 'number' },
77
+ name: { type: 'string' },
78
+ });
79
+ });
80
+ it('should handle empty arrays', () => {
81
+ const obj = { items: [] };
82
+ const schema = JsonSchemaIntrospector_1.JsonSchemaIntrospector.introspectToJsonSchema(obj);
83
+ expect(schema.properties.items).toEqual({
84
+ type: 'array',
85
+ items: {},
86
+ });
87
+ });
88
+ it('should return as-is if object already has $schema', () => {
89
+ // This matches the old connector framework behavior
90
+ const existingSchema = {
91
+ $schema: 'http://json-schema.org/draft-07/schema#',
92
+ type: 'object',
93
+ properties: {
94
+ custom: { type: 'string' },
95
+ },
96
+ };
97
+ const schema = JsonSchemaIntrospector_1.JsonSchemaIntrospector.introspectToJsonSchema(existingSchema);
98
+ // Should return the input unchanged
99
+ expect(schema).toBe(existingSchema);
100
+ expect(schema.$schema).toBe('http://json-schema.org/draft-07/schema#');
101
+ });
102
+ it('should handle complex real-world example', () => {
103
+ const obj = {
104
+ id: 1,
105
+ title: 'Post Title',
106
+ body: 'Post content',
107
+ userId: 10,
108
+ metadata: {
109
+ created: '2024-01-01',
110
+ tags: ['tech', 'tutorial'],
111
+ stats: {
112
+ views: 100,
113
+ likes: 25,
114
+ },
115
+ },
116
+ comments: [
117
+ {
118
+ id: 1,
119
+ text: 'Great post!',
120
+ author: 'Alice',
121
+ },
122
+ ],
123
+ };
124
+ const schema = JsonSchemaIntrospector_1.JsonSchemaIntrospector.introspectToJsonSchema(obj);
125
+ expect(schema.$schema).toBe('http://json-schema.org/draft-04/schema#');
126
+ expect(schema.type).toBe('object');
127
+ expect(schema.properties.id.type).toBe('number');
128
+ expect(schema.properties.metadata.type).toBe('object');
129
+ expect(schema.properties.metadata.properties.tags.type).toBe('array');
130
+ expect(schema.properties.comments.type).toBe('array');
131
+ expect(schema.properties.comments.items.type).toBe('object');
132
+ });
133
+ });
134
+ });
@@ -0,0 +1,2 @@
1
+ export { JsonSchemaIntrospector } from './JsonSchemaIntrospector';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/schema/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC"}
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JsonSchemaIntrospector = void 0;
4
+ var JsonSchemaIntrospector_1 = require("./JsonSchemaIntrospector");
5
+ Object.defineProperty(exports, "JsonSchemaIntrospector", { enumerable: true, get: function () { return JsonSchemaIntrospector_1.JsonSchemaIntrospector; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trayio/commons",
3
- "version": "5.13.0",
3
+ "version": "5.15.0",
4
4
  "description": "Extensions to the standard/core libraries and basic features",
5
5
  "exports": {
6
6
  "./*": "./dist/*.js"
@@ -25,6 +25,7 @@
25
25
  "formidable": "3.5.1",
26
26
  "fp-ts": "2.12.3",
27
27
  "fs-extra": "11.1.0",
28
+ "generate-schema": "^2.6.0",
28
29
  "io-ts": "2.2.20",
29
30
  "io-ts-reporters": "2.0.1",
30
31
  "io-ts-types": "0.5.19",