@trayio/tray-schema 1.2.0 → 1.4.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,11 @@
1
+ import { DynamicObject } from '@trayio/commons/dynamictype/DynamicType';
2
+ export interface LegacyTraySchemaConverterInterface {
3
+ convertToLegacySchema(schema: DynamicObject): DynamicObject;
4
+ }
5
+ export declare class LegacyTraySchemaConverter implements LegacyTraySchemaConverterInterface {
6
+ convertToLegacySchema(schema: DynamicObject): DynamicObject;
7
+ private transformLookup;
8
+ private transformLookupInput;
9
+ private transformEnums;
10
+ }
11
+ //# sourceMappingURL=LegacyTraySchemaConverter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LegacyTraySchemaConverter.d.ts","sourceRoot":"","sources":["../../src/schema/LegacyTraySchemaConverter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,yCAAyC,CAAC;AAExE,MAAM,WAAW,kCAAkC;IAClD,qBAAqB,CAAC,MAAM,EAAE,aAAa,GAAG,aAAa,CAAC;CAC5D;AAED,qBAAa,yBACZ,YAAW,kCAAkC;IAE7C,qBAAqB,CAAC,MAAM,EAAE,aAAa,GAAG,aAAa;IAY3D,OAAO,CAAC,eAAe,CAsBrB;IAEF,OAAO,CAAC,oBAAoB,CAc1B;IAEF,OAAO,CAAC,cAAc,CAoBpB;CACF"}
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LegacyTraySchemaConverter = void 0;
4
+ class LegacyTraySchemaConverter {
5
+ constructor() {
6
+ this.transformLookup = (object) => {
7
+ if (typeof object !== 'object' || object === null)
8
+ return object;
9
+ if (object.lookup &&
10
+ object.lookup.operation &&
11
+ object.lookup.input) {
12
+ const lookup = object.lookup;
13
+ object.lookup = {
14
+ url: '{{{step.ephemeral_url}}}',
15
+ body: {
16
+ step_settings: this.transformLookupInput(lookup.input),
17
+ // auth_id: '{{{step.auth_id}}}', //TODO add this conditionally, only needs to be added for connectors that use authentications
18
+ message: lookup.operation,
19
+ },
20
+ };
21
+ }
22
+ return object;
23
+ };
24
+ this.transformLookupInput = (input) => {
25
+ if (typeof input !== 'object' || input === null)
26
+ return input;
27
+ const result = {};
28
+ Object.keys(input).forEach((key) => {
29
+ result[key] = {
30
+ value: typeof input[key] === 'object'
31
+ ? this.transformLookupInput(input[key])
32
+ : input[key],
33
+ type: typeof input[key],
34
+ };
35
+ });
36
+ return result;
37
+ };
38
+ this.transformEnums = (object) => {
39
+ if (typeof object !== 'object' || object === null)
40
+ return object;
41
+ if (object.enum &&
42
+ object.enumLabels &&
43
+ Array.isArray(object.enum) &&
44
+ Array.isArray(object.enumLabels)) {
45
+ object.enum = object.enum.map((enumItem, index) => ({
46
+ value: enumItem,
47
+ text: object.enumLabels[index],
48
+ }));
49
+ delete object.enumLabels;
50
+ }
51
+ return object;
52
+ };
53
+ }
54
+ convertToLegacySchema(schema) {
55
+ for (const [key, value] of Object.entries(schema)) {
56
+ if (typeof value === 'object' && value !== null) {
57
+ this.transformLookup(value);
58
+ this.transformEnums(value);
59
+ this.convertToLegacySchema(value);
60
+ }
61
+ }
62
+ return schema;
63
+ }
64
+ }
65
+ exports.LegacyTraySchemaConverter = LegacyTraySchemaConverter;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=LegacyTraySchemaConverter.unit.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LegacyTraySchemaConverter.unit.test.d.ts","sourceRoot":"","sources":["../../src/schema/LegacyTraySchemaConverter.unit.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,399 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const LegacyTraySchemaConverter_1 = require("./LegacyTraySchemaConverter");
4
+ describe('LegacyTraySchemaConverter', () => {
5
+ const converter = new LegacyTraySchemaConverter_1.LegacyTraySchemaConverter();
6
+ describe('convertToLegacySchema', () => {
7
+ it('should convert a connector schema with a lookup to legacy schema', () => {
8
+ const schema = {
9
+ type: 'object',
10
+ properties: {
11
+ id: {
12
+ type: 'number',
13
+ lookup: {
14
+ operation: 'list_tables_ddl',
15
+ input: {
16
+ workspace_id: '{{{workspace_id}}}',
17
+ project_id: '{{{project_id}}}',
18
+ service: {
19
+ name: '{{{service_name}}}',
20
+ version: 1,
21
+ },
22
+ },
23
+ },
24
+ },
25
+ },
26
+ additionalProperties: false,
27
+ required: ['id'],
28
+ $schema: 'http://json-schema.org/draft-07/schema#',
29
+ advanced: [],
30
+ };
31
+ const expected = {
32
+ type: 'object',
33
+ properties: {
34
+ id: {
35
+ type: 'number',
36
+ lookup: {
37
+ url: '{{{step.ephemeral_url}}}',
38
+ body: {
39
+ step_settings: {
40
+ workspace_id: {
41
+ value: '{{{workspace_id}}}',
42
+ type: 'string',
43
+ },
44
+ project_id: {
45
+ value: '{{{project_id}}}',
46
+ type: 'string',
47
+ },
48
+ service: {
49
+ type: 'object',
50
+ value: {
51
+ name: {
52
+ value: '{{{service_name}}}',
53
+ type: 'string',
54
+ },
55
+ version: {
56
+ value: 1,
57
+ type: 'number',
58
+ },
59
+ },
60
+ },
61
+ },
62
+ message: 'list_tables_ddl',
63
+ },
64
+ },
65
+ },
66
+ },
67
+ additionalProperties: false,
68
+ required: ['id'],
69
+ $schema: 'http://json-schema.org/draft-07/schema#',
70
+ advanced: [],
71
+ };
72
+ const result = converter.convertToLegacySchema(schema);
73
+ expect(result).toEqual(expected);
74
+ });
75
+ it('should convert a connector schema with a lookup to legacy schema and also has a lookup field', () => {
76
+ const schema = {
77
+ type: 'object',
78
+ properties: {
79
+ id: {
80
+ type: 'number',
81
+ lookup: {
82
+ operation: 'list_tables_ddl',
83
+ input: {
84
+ workspace_id: '{{{workspace_id}}}',
85
+ project_id: '{{{project_id}}}',
86
+ },
87
+ },
88
+ },
89
+ lookup: {
90
+ type: 'string',
91
+ lookup: {
92
+ operation: 'list_tables_ddl',
93
+ input: {
94
+ workspace_id: '{{{workspace_id}}}',
95
+ project_id: '{{{project_id}}}',
96
+ },
97
+ },
98
+ },
99
+ },
100
+ additionalProperties: false,
101
+ required: ['id'],
102
+ $schema: 'http://json-schema.org/draft-07/schema#',
103
+ advanced: [],
104
+ };
105
+ const expected = {
106
+ type: 'object',
107
+ properties: {
108
+ id: {
109
+ type: 'number',
110
+ lookup: {
111
+ url: '{{{step.ephemeral_url}}}',
112
+ body: {
113
+ step_settings: {
114
+ workspace_id: {
115
+ value: '{{{workspace_id}}}',
116
+ type: 'string',
117
+ },
118
+ project_id: {
119
+ value: '{{{project_id}}}',
120
+ type: 'string',
121
+ },
122
+ },
123
+ message: 'list_tables_ddl',
124
+ },
125
+ },
126
+ },
127
+ lookup: {
128
+ type: 'string',
129
+ lookup: {
130
+ url: '{{{step.ephemeral_url}}}',
131
+ body: {
132
+ step_settings: {
133
+ workspace_id: {
134
+ value: '{{{workspace_id}}}',
135
+ type: 'string',
136
+ },
137
+ project_id: {
138
+ value: '{{{project_id}}}',
139
+ type: 'string',
140
+ },
141
+ },
142
+ message: 'list_tables_ddl',
143
+ },
144
+ },
145
+ },
146
+ },
147
+ additionalProperties: false,
148
+ required: ['id'],
149
+ $schema: 'http://json-schema.org/draft-07/schema#',
150
+ advanced: [],
151
+ };
152
+ const result = converter.convertToLegacySchema(schema);
153
+ expect(result).toEqual(expected);
154
+ });
155
+ it('should convert a schema with an enum', () => {
156
+ const schema = {
157
+ type: 'object',
158
+ properties: {
159
+ actions: {
160
+ description: 'example dropdown',
161
+ type: 'array',
162
+ items: {
163
+ type: 'object',
164
+ properties: {
165
+ text: { type: 'string' },
166
+ type: {
167
+ type: 'string',
168
+ enum: ['default always', 'primary-one', 'the_danger'],
169
+ enumLabels: ['Default Always', 'Primary One', 'The Danger'],
170
+ },
171
+ },
172
+ additionalProperties: false,
173
+ required: ['text', 'type'],
174
+ advanced: [],
175
+ },
176
+ },
177
+ name: { type: 'string' },
178
+ },
179
+ additionalProperties: false,
180
+ required: ['actions', 'name'],
181
+ $schema: 'http://json-schema.org/draft-07/schema#',
182
+ advanced: [],
183
+ };
184
+ const expected = {
185
+ type: 'object',
186
+ properties: {
187
+ actions: {
188
+ description: 'example dropdown',
189
+ type: 'array',
190
+ items: {
191
+ type: 'object',
192
+ properties: {
193
+ text: { type: 'string' },
194
+ type: {
195
+ type: 'string',
196
+ enum: [
197
+ {
198
+ value: 'default always',
199
+ text: 'Default Always',
200
+ },
201
+ {
202
+ value: 'primary-one',
203
+ text: 'Primary One',
204
+ },
205
+ {
206
+ value: 'the_danger',
207
+ text: 'The Danger',
208
+ },
209
+ ],
210
+ },
211
+ },
212
+ additionalProperties: false,
213
+ required: ['text', 'type'],
214
+ advanced: [],
215
+ },
216
+ },
217
+ name: { type: 'string' },
218
+ },
219
+ required: ['actions', 'name'],
220
+ additionalProperties: false,
221
+ $schema: 'http://json-schema.org/draft-07/schema#',
222
+ advanced: [],
223
+ };
224
+ const result = converter.convertToLegacySchema(schema);
225
+ expect(result).toEqual(expected);
226
+ });
227
+ it('should convert a schema with an enum and a field name as enum', () => {
228
+ const schema = {
229
+ type: 'object',
230
+ properties: {
231
+ actions: {
232
+ description: 'example dropdown',
233
+ type: 'array',
234
+ items: {
235
+ type: 'object',
236
+ properties: {
237
+ text: { type: 'string' },
238
+ type: {
239
+ type: 'string',
240
+ enum: ['default always', 'primary-one', 'the_danger'],
241
+ enumLabels: ['Default Always', 'Primary One', 'The Danger'],
242
+ },
243
+ },
244
+ additionalProperties: false,
245
+ required: ['text', 'type'],
246
+ advanced: [],
247
+ },
248
+ },
249
+ enum: { type: 'string' },
250
+ },
251
+ additionalProperties: false,
252
+ required: ['actions', 'enum'],
253
+ $schema: 'http://json-schema.org/draft-07/schema#',
254
+ advanced: [],
255
+ };
256
+ const expected = {
257
+ type: 'object',
258
+ properties: {
259
+ actions: {
260
+ description: 'example dropdown',
261
+ type: 'array',
262
+ items: {
263
+ type: 'object',
264
+ properties: {
265
+ text: { type: 'string' },
266
+ type: {
267
+ type: 'string',
268
+ enum: [
269
+ {
270
+ value: 'default always',
271
+ text: 'Default Always',
272
+ },
273
+ {
274
+ value: 'primary-one',
275
+ text: 'Primary One',
276
+ },
277
+ {
278
+ value: 'the_danger',
279
+ text: 'The Danger',
280
+ },
281
+ ],
282
+ },
283
+ },
284
+ additionalProperties: false,
285
+ required: ['text', 'type'],
286
+ advanced: [],
287
+ },
288
+ },
289
+ enum: { type: 'string' },
290
+ },
291
+ additionalProperties: false,
292
+ required: ['actions', 'enum'],
293
+ $schema: 'http://json-schema.org/draft-07/schema#',
294
+ advanced: [],
295
+ };
296
+ const result = converter.convertToLegacySchema(schema);
297
+ expect(result).toEqual(expected);
298
+ });
299
+ it('should convert lookups and enums in nested objects', () => {
300
+ const schema = {
301
+ properties: {
302
+ id: {
303
+ type: 'object',
304
+ properties: {
305
+ lookup: {
306
+ input: {
307
+ input1: 'value1',
308
+ input2: 'value2',
309
+ },
310
+ operation: 'operation',
311
+ },
312
+ },
313
+ },
314
+ nested: {
315
+ properties: {
316
+ sort_by: {
317
+ type: {
318
+ type: 'string',
319
+ enum: ['value1', 'value2'],
320
+ enumLabels: ['Value 1', 'Value 2'],
321
+ },
322
+ },
323
+ },
324
+ },
325
+ },
326
+ };
327
+ const expected = {
328
+ properties: {
329
+ id: {
330
+ type: 'object',
331
+ properties: {
332
+ lookup: {
333
+ url: '{{{step.ephemeral_url}}}',
334
+ body: {
335
+ step_settings: {
336
+ input1: {
337
+ value: 'value1',
338
+ type: 'string',
339
+ },
340
+ input2: {
341
+ value: 'value2',
342
+ type: 'string',
343
+ },
344
+ },
345
+ message: 'operation',
346
+ },
347
+ },
348
+ },
349
+ },
350
+ nested: {
351
+ properties: {
352
+ sort_by: {
353
+ type: {
354
+ type: 'string',
355
+ enum: [
356
+ {
357
+ value: 'value1',
358
+ text: 'Value 1',
359
+ },
360
+ {
361
+ value: 'value2',
362
+ text: 'Value 2',
363
+ },
364
+ ],
365
+ },
366
+ },
367
+ },
368
+ },
369
+ },
370
+ };
371
+ const result = converter.convertToLegacySchema(schema);
372
+ expect(result).toEqual(expected);
373
+ });
374
+ it('should preserve the schema without a lookup and an enum', () => {
375
+ const schema = {
376
+ type: 'object',
377
+ properties: {
378
+ name: { type: 'string' },
379
+ },
380
+ additionalProperties: false,
381
+ required: ['actions'],
382
+ $schema: 'http://json-schema.org/draft-07/schema#',
383
+ advanced: [],
384
+ };
385
+ const expected = {
386
+ type: 'object',
387
+ properties: {
388
+ name: { type: 'string' },
389
+ },
390
+ additionalProperties: false,
391
+ required: ['actions'],
392
+ $schema: 'http://json-schema.org/draft-07/schema#',
393
+ advanced: [],
394
+ };
395
+ const result = converter.convertToLegacySchema(schema);
396
+ expect(result).toEqual(expected);
397
+ });
398
+ });
399
+ });
@@ -1 +1 @@
1
- {"version":3,"file":"TJSTraySchemaGenerator.d.ts","sourceRoot":"","sources":["../../src/schema/TJSTraySchemaGenerator.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,GAAG,MAAM,wBAAwB,CAAC;AAC9C,OAAO,EAAE,MAAM,EAAe,MAAM,cAAc,CAAC;AAEnD,OAAO,EAAE,aAAa,EAAE,MAAM,yCAAyC,CAAC;AACxE,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAE5D,qBAAa,sBAAuB,YAAW,mBAAmB;IACjE,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC,KAAK,EAAE,aAAa,CAAC;IAgC1E,sBAAsB,CAAC,MAAM,EAAE,GAAG,CAAC,UAAU,GAAG,GAAG,CAAC,UAAU;CAqG9D"}
1
+ {"version":3,"file":"TJSTraySchemaGenerator.d.ts","sourceRoot":"","sources":["../../src/schema/TJSTraySchemaGenerator.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,GAAG,MAAM,wBAAwB,CAAC;AAC9C,OAAO,EAAE,MAAM,EAAe,MAAM,cAAc,CAAC;AAEnD,OAAO,EAAE,aAAa,EAAE,MAAM,yCAAyC,CAAC;AACxE,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAE5D,qBAAa,sBAAuB,YAAW,mBAAmB;IACjE,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC,KAAK,EAAE,aAAa,CAAC;IAgC1E,sBAAsB,CAAC,MAAM,EAAE,GAAG,CAAC,UAAU,GAAG,GAAG,CAAC,UAAU;CAsG9D"}
@@ -76,7 +76,7 @@ class TJSTraySchemaGenerator {
76
76
  }
77
77
  };
78
78
  const generateEnumLabels = (object) => {
79
- if (object.enum && Array.isArray(object.enum)) {
79
+ if (object.enum && !object.enumLabels && Array.isArray(object.enum)) {
80
80
  const enumLabels = [];
81
81
  const enums = object.enum;
82
82
  enums.forEach((enumItem) => {
@@ -142,6 +142,7 @@ class TJSTraySchemaGenerator {
142
142
  for (const [key, value] of Object.entries(object)) {
143
143
  if (typeof value === 'object' && value !== null) {
144
144
  transformLookup(value);
145
+ transformEnumLabels(value);
145
146
  generateEnumLabels(value);
146
147
  transformOneOf(value);
147
148
  recursiveObjectSearch(value);
@@ -195,8 +195,46 @@ describe('TJSTraySchemaGenerator', () => {
195
195
  properties: {
196
196
  text: { type: 'string' },
197
197
  type: {
198
- enumLabels: ['Default Always', 'Primary One', 'The Danger'],
199
- enum: ['default always', 'primary-one', 'the_danger'],
198
+ enumLabels: ['Default Always', 'The Danger', 'Primary One'],
199
+ enum: ['default always', 'the_danger', 'primary-one'],
200
+ type: 'string',
201
+ },
202
+ },
203
+ advanced: [],
204
+ required: ['text', 'type'],
205
+ additionalProperties: false,
206
+ },
207
+ },
208
+ },
209
+ advanced: [],
210
+ required: ['actions'],
211
+ additionalProperties: false,
212
+ $schema: 'http://json-schema.org/draft-07/schema#',
213
+ };
214
+ expect(actual).toEqual(expected);
215
+ });
216
+ it('Generates enums and custom enumLabels', () => {
217
+ const type = 'Input';
218
+ const dirPath = __dirname;
219
+ const path = (0, path_1.join)(dirPath, 'enumLabels.test.data.ts');
220
+ const result = new TJSTraySchemaGenerator_1.TJSTraySchemaGenerator().generateFromType(type, path);
221
+ const actual = E.getOrElse((error) => {
222
+ console.warn(error);
223
+ throw new Error(`Failed to generate JSON schema from type`);
224
+ })(result);
225
+ const expected = {
226
+ type: 'object',
227
+ properties: {
228
+ actions: {
229
+ description: 'example dropdown',
230
+ type: 'array',
231
+ items: {
232
+ type: 'object',
233
+ properties: {
234
+ text: { type: 'string' },
235
+ type: {
236
+ enumLabels: ['The Default', 'The Danger', 'The Primary'],
237
+ enum: ['default', 'danger', 'primary'],
200
238
  type: 'string',
201
239
  },
202
240
  },
@@ -0,0 +1,20 @@
1
+ export type Input = {
2
+ /**
3
+ * @description example dropdown
4
+ */
5
+ actions: Action[];
6
+ };
7
+ type Action = {
8
+ text: string;
9
+ type: ActionType;
10
+ };
11
+ /**
12
+ * @enumLabels The Default, The Danger, The Primary
13
+ */
14
+ declare enum ActionType {
15
+ default = "default",
16
+ danger = "danger",
17
+ primary = "primary"
18
+ }
19
+ export {};
20
+ //# sourceMappingURL=enumLabels.test.data.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"enumLabels.test.data.d.ts","sourceRoot":"","sources":["../../src/schema/enumLabels.test.data.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,KAAK,GAAG;IACnB;;OAEG;IACH,OAAO,EAAE,MAAM,EAAE,CAAC;CAClB,CAAC;AAEF,KAAK,MAAM,GAAG;IACb,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,UAAU,CAAC;CACjB,CAAC;AACF;;GAEG;AACH,aAAK,UAAU;IACd,OAAO,YAAY;IACnB,MAAM,WAAW;IACjB,OAAO,YAAY;CACnB"}
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /**
4
+ * @enumLabels The Default, The Danger, The Primary
5
+ */
6
+ var ActionType;
7
+ (function (ActionType) {
8
+ ActionType["default"] = "default";
9
+ ActionType["danger"] = "danger";
10
+ ActionType["primary"] = "primary";
11
+ })(ActionType || (ActionType = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trayio/tray-schema",
3
- "version": "1.2.0",
3
+ "version": "1.4.0",
4
4
  "description": "Schema for Tray connectors and authentications",
5
5
  "exports": {
6
6
  "./*": "./dist/*.js"
@@ -11,10 +11,10 @@
11
11
  "access": "public"
12
12
  },
13
13
  "dependencies": {
14
- "@trayio/commons": "1.2.0",
14
+ "@trayio/commons": "1.4.0",
15
15
  "@types/node": "18.11.9",
16
16
  "lodash": "4.17.5",
17
- "typescript-json-schema": "0.61.0"
17
+ "typescript-json-schema": "0.62.0"
18
18
  },
19
19
  "typesVersions": {
20
20
  "*": {