@player-tools/xlr-sdk 0.2.1-next.3 → 0.2.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.
- package/dist/index.cjs.js +236 -162
- package/dist/index.d.ts +66 -6
- package/dist/index.esm.js +237 -145
- package/package.json +4 -4
- package/src/registry/basic-registry.ts +0 -1
- package/src/sdk.ts +152 -27
- package/src/utils.ts +104 -43
- package/src/validator.ts +53 -76
package/dist/index.cjs.js
CHANGED
|
@@ -10,41 +10,23 @@ var ts = require('typescript');
|
|
|
10
10
|
|
|
11
11
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
12
12
|
|
|
13
|
-
function _interopNamespace(e) {
|
|
14
|
-
if (e && e.__esModule) return e;
|
|
15
|
-
var n = Object.create(null);
|
|
16
|
-
if (e) {
|
|
17
|
-
Object.keys(e).forEach(function (k) {
|
|
18
|
-
if (k !== 'default') {
|
|
19
|
-
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
20
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
21
|
-
enumerable: true,
|
|
22
|
-
get: function () { return e[k]; }
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
n["default"] = e;
|
|
28
|
-
return Object.freeze(n);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
13
|
var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
|
|
32
14
|
var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
|
|
33
15
|
var ts__default = /*#__PURE__*/_interopDefaultLegacy(ts);
|
|
34
16
|
|
|
35
|
-
var __defProp$
|
|
36
|
-
var __getOwnPropSymbols$
|
|
37
|
-
var __hasOwnProp$
|
|
38
|
-
var __propIsEnum$
|
|
39
|
-
var __defNormalProp$
|
|
40
|
-
var __spreadValues$
|
|
17
|
+
var __defProp$3 = Object.defineProperty;
|
|
18
|
+
var __getOwnPropSymbols$3 = Object.getOwnPropertySymbols;
|
|
19
|
+
var __hasOwnProp$3 = Object.prototype.hasOwnProperty;
|
|
20
|
+
var __propIsEnum$3 = Object.prototype.propertyIsEnumerable;
|
|
21
|
+
var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
22
|
+
var __spreadValues$3 = (a, b) => {
|
|
41
23
|
for (var prop in b || (b = {}))
|
|
42
|
-
if (__hasOwnProp$
|
|
43
|
-
__defNormalProp$
|
|
44
|
-
if (__getOwnPropSymbols$
|
|
45
|
-
for (var prop of __getOwnPropSymbols$
|
|
46
|
-
if (__propIsEnum$
|
|
47
|
-
__defNormalProp$
|
|
24
|
+
if (__hasOwnProp$3.call(b, prop))
|
|
25
|
+
__defNormalProp$3(a, prop, b[prop]);
|
|
26
|
+
if (__getOwnPropSymbols$3)
|
|
27
|
+
for (var prop of __getOwnPropSymbols$3(b)) {
|
|
28
|
+
if (__propIsEnum$3.call(b, prop))
|
|
29
|
+
__defNormalProp$3(a, prop, b[prop]);
|
|
48
30
|
}
|
|
49
31
|
return a;
|
|
50
32
|
};
|
|
@@ -56,7 +38,7 @@ class BasicXLRRegistry {
|
|
|
56
38
|
}
|
|
57
39
|
get(id) {
|
|
58
40
|
const value = this.typeMap.get(id);
|
|
59
|
-
return value ? __spreadValues$
|
|
41
|
+
return value ? __spreadValues$3({}, value) : void 0;
|
|
60
42
|
}
|
|
61
43
|
add(type, plugin, capability) {
|
|
62
44
|
this.typeMap.set(type.name, type);
|
|
@@ -96,28 +78,28 @@ class BasicXLRRegistry {
|
|
|
96
78
|
}
|
|
97
79
|
}
|
|
98
80
|
|
|
99
|
-
var __defProp = Object.defineProperty;
|
|
100
|
-
var __defProps = Object.defineProperties;
|
|
101
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
102
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
103
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
104
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
105
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
106
|
-
var __spreadValues = (a, b) => {
|
|
81
|
+
var __defProp$2 = Object.defineProperty;
|
|
82
|
+
var __defProps$2 = Object.defineProperties;
|
|
83
|
+
var __getOwnPropDescs$2 = Object.getOwnPropertyDescriptors;
|
|
84
|
+
var __getOwnPropSymbols$2 = Object.getOwnPropertySymbols;
|
|
85
|
+
var __hasOwnProp$2 = Object.prototype.hasOwnProperty;
|
|
86
|
+
var __propIsEnum$2 = Object.prototype.propertyIsEnumerable;
|
|
87
|
+
var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
88
|
+
var __spreadValues$2 = (a, b) => {
|
|
107
89
|
for (var prop in b || (b = {}))
|
|
108
|
-
if (__hasOwnProp.call(b, prop))
|
|
109
|
-
__defNormalProp(a, prop, b[prop]);
|
|
110
|
-
if (__getOwnPropSymbols)
|
|
111
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
112
|
-
if (__propIsEnum.call(b, prop))
|
|
113
|
-
__defNormalProp(a, prop, b[prop]);
|
|
90
|
+
if (__hasOwnProp$2.call(b, prop))
|
|
91
|
+
__defNormalProp$2(a, prop, b[prop]);
|
|
92
|
+
if (__getOwnPropSymbols$2)
|
|
93
|
+
for (var prop of __getOwnPropSymbols$2(b)) {
|
|
94
|
+
if (__propIsEnum$2.call(b, prop))
|
|
95
|
+
__defNormalProp$2(a, prop, b[prop]);
|
|
114
96
|
}
|
|
115
97
|
return a;
|
|
116
98
|
};
|
|
117
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
99
|
+
var __spreadProps$2 = (a, b) => __defProps$2(a, __getOwnPropDescs$2(b));
|
|
118
100
|
class XLRValidator {
|
|
119
|
-
constructor(
|
|
120
|
-
this.
|
|
101
|
+
constructor(resolveType) {
|
|
102
|
+
this.resolveType = resolveType;
|
|
121
103
|
this.regexCache = new Map();
|
|
122
104
|
}
|
|
123
105
|
validateType(rootNode, xlrNode) {
|
|
@@ -158,7 +140,7 @@ class XLRValidator {
|
|
|
158
140
|
message: `Does not match any of the expected types for type: '${xlrNode.name}'`
|
|
159
141
|
});
|
|
160
142
|
} else if (xlrNode.type === "and") {
|
|
161
|
-
const effectiveType = this.computeIntersectionType(xlrNode.and);
|
|
143
|
+
const effectiveType = __spreadValues$2(__spreadValues$2({}, this.computeIntersectionType(xlrNode.and)), xlrNode.name ? { name: xlrNode.name } : {});
|
|
162
144
|
validationIssues.push(...this.validateType(rootNode, effectiveType));
|
|
163
145
|
} else if (xlrNode.type === "record") {
|
|
164
146
|
(_a = rootNode.children) == null ? void 0 : _a.forEach((child) => {
|
|
@@ -194,11 +176,24 @@ class XLRValidator {
|
|
|
194
176
|
}
|
|
195
177
|
}
|
|
196
178
|
} else if (xlrNode.type === "conditional") {
|
|
197
|
-
|
|
198
|
-
if (
|
|
179
|
+
let { right, left } = xlrNode.check;
|
|
180
|
+
if (right.type === "ref") {
|
|
181
|
+
right = this.getRefType(right);
|
|
182
|
+
}
|
|
183
|
+
if (left.type === "ref") {
|
|
184
|
+
left = this.getRefType(left);
|
|
185
|
+
}
|
|
186
|
+
const resolvedXLRNode = __spreadProps$2(__spreadValues$2({}, xlrNode), {
|
|
187
|
+
check: {
|
|
188
|
+
left,
|
|
189
|
+
right
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
const resolvedConditional = xlrUtils.resolveConditional(resolvedXLRNode);
|
|
193
|
+
if (resolvedConditional === resolvedXLRNode) {
|
|
199
194
|
throw Error(`Unable to resolve conditional type at runtime: ${xlrNode.name}`);
|
|
200
195
|
}
|
|
201
|
-
validationIssues.push(...this.validateType(rootNode,
|
|
196
|
+
validationIssues.push(...this.validateType(rootNode, resolvedConditional));
|
|
202
197
|
} else {
|
|
203
198
|
throw Error(`Unknown type ${xlrNode.type}`);
|
|
204
199
|
}
|
|
@@ -234,7 +229,7 @@ class XLRValidator {
|
|
|
234
229
|
let effectiveXLRNode = xlrNode;
|
|
235
230
|
if (xlrNode.extends) {
|
|
236
231
|
const extendedNode = this.getRefType(xlrNode.extends);
|
|
237
|
-
effectiveXLRNode =
|
|
232
|
+
effectiveXLRNode = xlrUtils.computeEffectiveObject(extendedNode, xlrNode);
|
|
238
233
|
}
|
|
239
234
|
for (const prop in effectiveXLRNode.properties) {
|
|
240
235
|
const expectedType = effectiveXLRNode.properties[prop];
|
|
@@ -295,19 +290,14 @@ class XLRValidator {
|
|
|
295
290
|
}
|
|
296
291
|
getRefType(ref) {
|
|
297
292
|
let refName = ref.ref;
|
|
298
|
-
const { genericArguments } = ref;
|
|
299
293
|
if (refName.indexOf("<") > 0) {
|
|
300
294
|
[refName] = refName.split("<");
|
|
301
295
|
}
|
|
302
|
-
const actualType = this.
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
actualType.genericTokens.forEach((token, index) => {
|
|
306
|
-
var _a, _b;
|
|
307
|
-
genericMap.set(token.symbol, (_b = (_a = genericArguments[index]) != null ? _a : token.default) != null ? _b : token.constraints);
|
|
308
|
-
});
|
|
296
|
+
const actualType = this.resolveType(refName);
|
|
297
|
+
if (!actualType) {
|
|
298
|
+
throw new Error(`Error: can't resolve type reference ${refName}`);
|
|
309
299
|
}
|
|
310
|
-
return xlrUtils.
|
|
300
|
+
return xlrUtils.resolveReferenceNode(ref, actualType);
|
|
311
301
|
}
|
|
312
302
|
getRegex(expString) {
|
|
313
303
|
if (this.regexCache.has(expString)) {
|
|
@@ -325,6 +315,12 @@ class XLRValidator {
|
|
|
325
315
|
}
|
|
326
316
|
if (firstElement.type === "and") {
|
|
327
317
|
effectiveType = this.computeIntersectionType(firstElement.and);
|
|
318
|
+
} else if (firstElement.type === "record") {
|
|
319
|
+
effectiveType = {
|
|
320
|
+
type: "object",
|
|
321
|
+
properties: {},
|
|
322
|
+
additionalProperties: firstElement.valueType
|
|
323
|
+
};
|
|
328
324
|
} else if (firstElement.type !== "or" && firstElement.type !== "object") {
|
|
329
325
|
throw new Error(`Can't compute a union with a non-object type ${firstElement.type} (${firstElement.name})`);
|
|
330
326
|
} else {
|
|
@@ -332,6 +328,13 @@ class XLRValidator {
|
|
|
332
328
|
}
|
|
333
329
|
types.slice(1).forEach((type) => {
|
|
334
330
|
let typeToApply = type;
|
|
331
|
+
if (typeToApply.type === "record") {
|
|
332
|
+
typeToApply = {
|
|
333
|
+
type: "object",
|
|
334
|
+
properties: {},
|
|
335
|
+
additionalProperties: typeToApply.valueType
|
|
336
|
+
};
|
|
337
|
+
}
|
|
335
338
|
if (type.type === "ref") {
|
|
336
339
|
typeToApply = this.getRefType(type);
|
|
337
340
|
}
|
|
@@ -340,15 +343,15 @@ class XLRValidator {
|
|
|
340
343
|
}
|
|
341
344
|
if (typeToApply.type === "object") {
|
|
342
345
|
if (effectiveType.type === "object") {
|
|
343
|
-
effectiveType =
|
|
346
|
+
effectiveType = xlrUtils.computeEffectiveObject(effectiveType, typeToApply);
|
|
344
347
|
} else {
|
|
345
|
-
effectiveType = __spreadProps(__spreadValues({}, effectiveType), {
|
|
348
|
+
effectiveType = __spreadProps$2(__spreadValues$2({}, effectiveType), {
|
|
346
349
|
or: effectiveType.or.map((y) => this.computeIntersectionType([y, typeToApply]))
|
|
347
350
|
});
|
|
348
351
|
}
|
|
349
352
|
} else if (typeToApply.type === "or") {
|
|
350
353
|
if (effectiveType.type === "object") {
|
|
351
|
-
effectiveType = __spreadProps(__spreadValues({}, typeToApply), {
|
|
354
|
+
effectiveType = __spreadProps$2(__spreadValues$2({}, typeToApply), {
|
|
352
355
|
or: typeToApply.or.map((y) => this.computeIntersectionType([y, effectiveType]))
|
|
353
356
|
});
|
|
354
357
|
} else {
|
|
@@ -360,32 +363,130 @@ class XLRValidator {
|
|
|
360
363
|
});
|
|
361
364
|
return effectiveType;
|
|
362
365
|
}
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
var __defProp$1 = Object.defineProperty;
|
|
369
|
+
var __defProps$1 = Object.defineProperties;
|
|
370
|
+
var __getOwnPropDescs$1 = Object.getOwnPropertyDescriptors;
|
|
371
|
+
var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
|
|
372
|
+
var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
|
|
373
|
+
var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
|
|
374
|
+
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
375
|
+
var __spreadValues$1 = (a, b) => {
|
|
376
|
+
for (var prop in b || (b = {}))
|
|
377
|
+
if (__hasOwnProp$1.call(b, prop))
|
|
378
|
+
__defNormalProp$1(a, prop, b[prop]);
|
|
379
|
+
if (__getOwnPropSymbols$1)
|
|
380
|
+
for (var prop of __getOwnPropSymbols$1(b)) {
|
|
381
|
+
if (__propIsEnum$1.call(b, prop))
|
|
382
|
+
__defNormalProp$1(a, prop, b[prop]);
|
|
376
383
|
}
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
+
return a;
|
|
385
|
+
};
|
|
386
|
+
var __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b));
|
|
387
|
+
function simpleTransformGenerator(typeToTransform, capabilityToTransform, functionToRun) {
|
|
388
|
+
const walker = (n, capability) => {
|
|
389
|
+
var _a;
|
|
390
|
+
if (capability === capabilityToTransform) {
|
|
391
|
+
let node = __spreadValues$1({}, n);
|
|
392
|
+
if (node.type === typeToTransform) {
|
|
393
|
+
node = functionToRun(node);
|
|
394
|
+
}
|
|
395
|
+
if (node.type === "object") {
|
|
396
|
+
const newObjectProperties = {};
|
|
397
|
+
for (const key in node.properties) {
|
|
398
|
+
const value = node.properties[key];
|
|
399
|
+
newObjectProperties[key] = {
|
|
400
|
+
required: value.required,
|
|
401
|
+
node: walker(value.node, capability)
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
return __spreadProps$1(__spreadValues$1({}, node), {
|
|
405
|
+
properties: __spreadValues$1({}, newObjectProperties),
|
|
406
|
+
extends: node.extends ? walker(node.extends, capability) : void 0,
|
|
407
|
+
additionalProperties: node.additionalProperties ? walker(node.additionalProperties, capability) : false
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
if (node.type === "array") {
|
|
411
|
+
return __spreadProps$1(__spreadValues$1({}, node), {
|
|
412
|
+
elementType: walker(node.elementType, capability)
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
if (node.type === "and") {
|
|
416
|
+
return __spreadProps$1(__spreadValues$1({}, node), {
|
|
417
|
+
and: node.and.map((element) => walker(element, capability))
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
if (node.type === "or") {
|
|
421
|
+
return __spreadProps$1(__spreadValues$1({}, node), {
|
|
422
|
+
or: node.or.map((element) => walker(element, capability))
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
if (node.type === "ref") {
|
|
426
|
+
return __spreadProps$1(__spreadValues$1({}, node), {
|
|
427
|
+
genericArguments: (_a = node.genericArguments) == null ? void 0 : _a.map((arg) => walker(arg, capability))
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
if (node.type === "tuple") {
|
|
431
|
+
return __spreadProps$1(__spreadValues$1({}, node), {
|
|
432
|
+
elementTypes: node.elementTypes.map((type) => walker(type, capability)),
|
|
433
|
+
additionalItems: node.additionalItems ? walker(node.additionalItems, capability) : false
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
if (node.type === "function") {
|
|
437
|
+
return __spreadProps$1(__spreadValues$1({}, node), {
|
|
438
|
+
parameters: node.parameters.map((param) => {
|
|
439
|
+
return __spreadProps$1(__spreadValues$1({}, param), {
|
|
440
|
+
type: walker(param.type, capability),
|
|
441
|
+
default: param.default ? walker(param.default, capability) : void 0
|
|
442
|
+
});
|
|
443
|
+
}),
|
|
444
|
+
returnType: node.returnType ? walker(node.returnType, capability) : void 0
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
if (node.type === "record") {
|
|
448
|
+
return __spreadProps$1(__spreadValues$1({}, node), {
|
|
449
|
+
keyType: walker(node.keyType, capability),
|
|
450
|
+
valueType: walker(node.valueType, capability)
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
if (node.type === "conditional") {
|
|
454
|
+
return __spreadProps$1(__spreadValues$1({}, node), {
|
|
455
|
+
check: {
|
|
456
|
+
left: walker(node.check.left, capability),
|
|
457
|
+
right: walker(node.check.left, capability)
|
|
458
|
+
},
|
|
459
|
+
value: {
|
|
460
|
+
true: walker(node.value.true, capability),
|
|
461
|
+
false: walker(node.value.false, capability)
|
|
462
|
+
}
|
|
463
|
+
});
|
|
464
|
+
}
|
|
384
465
|
}
|
|
385
|
-
return
|
|
386
|
-
}
|
|
466
|
+
return n;
|
|
467
|
+
};
|
|
468
|
+
return walker;
|
|
387
469
|
}
|
|
388
470
|
|
|
471
|
+
var __defProp = Object.defineProperty;
|
|
472
|
+
var __defProps = Object.defineProperties;
|
|
473
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
474
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
475
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
476
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
477
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
478
|
+
var __spreadValues = (a, b) => {
|
|
479
|
+
for (var prop in b || (b = {}))
|
|
480
|
+
if (__hasOwnProp.call(b, prop))
|
|
481
|
+
__defNormalProp(a, prop, b[prop]);
|
|
482
|
+
if (__getOwnPropSymbols)
|
|
483
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
484
|
+
if (__propIsEnum.call(b, prop))
|
|
485
|
+
__defNormalProp(a, prop, b[prop]);
|
|
486
|
+
}
|
|
487
|
+
return a;
|
|
488
|
+
};
|
|
489
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
389
490
|
var __async = (__this, __arguments, generator) => {
|
|
390
491
|
return new Promise((resolve, reject) => {
|
|
391
492
|
var fulfilled = (value) => {
|
|
@@ -409,7 +510,7 @@ var __async = (__this, __arguments, generator) => {
|
|
|
409
510
|
class XLRSDK {
|
|
410
511
|
constructor(customRegistry) {
|
|
411
512
|
this.registry = customRegistry != null ? customRegistry : new BasicXLRRegistry();
|
|
412
|
-
this.validator = new XLRValidator(this.
|
|
513
|
+
this.validator = new XLRValidator(this.getType.bind(this));
|
|
413
514
|
this.tsWriter = new xlrConverters.TSWriter();
|
|
414
515
|
}
|
|
415
516
|
loadDefinitionsFromDisk(inputPath, filters, transforms) {
|
|
@@ -426,36 +527,39 @@ class XLRSDK {
|
|
|
426
527
|
if ((filters == null ? void 0 : filters.capabilityFilter) && capabilityName.match(filters == null ? void 0 : filters.capabilityFilter))
|
|
427
528
|
return;
|
|
428
529
|
capabilityList.forEach((extensionName) => {
|
|
530
|
+
var _a2;
|
|
429
531
|
if (!(filters == null ? void 0 : filters.typeFilter) || !extensionName.match(filters == null ? void 0 : filters.typeFilter)) {
|
|
430
532
|
const cType = JSON.parse(fs__default["default"].readFileSync(path__default["default"].join(inputPath, "xlr", `${extensionName}.json`)).toString());
|
|
431
|
-
transforms == null ? void 0 : transforms.
|
|
432
|
-
|
|
433
|
-
this.registry.add(resolvedType, manifest.pluginName, capabilityName);
|
|
533
|
+
const effectiveType = (_a2 = transforms == null ? void 0 : transforms.reduce((typeAccumulator, transformFn) => transformFn(typeAccumulator, capabilityName), cType)) != null ? _a2 : cType;
|
|
534
|
+
this.registry.add(effectiveType, manifest.pluginName, capabilityName);
|
|
434
535
|
}
|
|
435
536
|
});
|
|
436
537
|
});
|
|
437
538
|
}
|
|
438
|
-
loadDefinitionsFromModule(
|
|
539
|
+
loadDefinitionsFromModule(manifest, filters, transforms) {
|
|
439
540
|
return __async(this, null, function* () {
|
|
440
541
|
var _a;
|
|
441
|
-
const importManifest = yield (function (t) { return Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(t)); }); })(path__default["default"].join(inputPath, "xlr", "manifest.js"));
|
|
442
|
-
const manifest = importManifest.default;
|
|
443
542
|
(_a = Object.keys(manifest.capabilities)) == null ? void 0 : _a.forEach((capabilityName) => {
|
|
444
543
|
if ((filters == null ? void 0 : filters.capabilityFilter) && capabilityName.match(filters == null ? void 0 : filters.capabilityFilter))
|
|
445
544
|
return;
|
|
446
545
|
const capabilityList = manifest.capabilities[capabilityName];
|
|
447
546
|
capabilityList.forEach((extension) => {
|
|
547
|
+
var _a2;
|
|
448
548
|
if (!(filters == null ? void 0 : filters.typeFilter) || !extension.name.match(filters == null ? void 0 : filters.typeFilter)) {
|
|
449
|
-
transforms == null ? void 0 : transforms.
|
|
450
|
-
|
|
451
|
-
this.registry.add(resolvedType, manifest.pluginName, extension.name);
|
|
549
|
+
const effectiveType = (_a2 = transforms == null ? void 0 : transforms.reduce((typeAccumulator, transformFn) => transformFn(typeAccumulator, capabilityName), extension)) != null ? _a2 : extension;
|
|
550
|
+
this.registry.add(effectiveType, manifest.pluginName, capabilityName);
|
|
452
551
|
}
|
|
453
552
|
});
|
|
454
553
|
});
|
|
455
554
|
});
|
|
456
555
|
}
|
|
457
|
-
getType(id) {
|
|
458
|
-
|
|
556
|
+
getType(id, options) {
|
|
557
|
+
let type = this.registry.get(id);
|
|
558
|
+
if ((options == null ? void 0 : options.getRawType) === true || !type) {
|
|
559
|
+
return type;
|
|
560
|
+
}
|
|
561
|
+
type = this.resolveType(type);
|
|
562
|
+
return xlrUtils.fillInGenerics(type);
|
|
459
563
|
}
|
|
460
564
|
hasType(id) {
|
|
461
565
|
return this.registry.has(id);
|
|
@@ -463,20 +567,28 @@ class XLRSDK {
|
|
|
463
567
|
listTypes(filters) {
|
|
464
568
|
return this.registry.list(filters);
|
|
465
569
|
}
|
|
466
|
-
|
|
467
|
-
|
|
570
|
+
getTypeInfo(id) {
|
|
571
|
+
return this.registry.info(id);
|
|
572
|
+
}
|
|
573
|
+
validateByName(typeName, rootNode) {
|
|
574
|
+
const xlr = this.getType(typeName, { getRawType: true });
|
|
468
575
|
if (!xlr) {
|
|
469
576
|
throw new Error(`Type ${typeName} does not exist in registry, can't validate`);
|
|
470
577
|
}
|
|
471
578
|
return this.validator.validateType(rootNode, xlr);
|
|
472
579
|
}
|
|
580
|
+
validateByType(type, rootNode) {
|
|
581
|
+
return this.validator.validateType(rootNode, type);
|
|
582
|
+
}
|
|
473
583
|
exportRegistry(exportType, importMap, filters, transforms) {
|
|
474
584
|
const typesToExport = this.registry.list(filters).map((type) => {
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
585
|
+
var _a;
|
|
586
|
+
const resolvedType = this.resolveType(type);
|
|
587
|
+
const effectiveType = (_a = transforms == null ? void 0 : transforms.reduce((typeAccumulator, transformFn) => {
|
|
588
|
+
var _a2;
|
|
589
|
+
return transformFn(typeAccumulator, (_a2 = this.registry.info(type.name)) == null ? void 0 : _a2.capability);
|
|
590
|
+
}, resolvedType)) != null ? _a : resolvedType;
|
|
591
|
+
return effectiveType;
|
|
480
592
|
});
|
|
481
593
|
if (exportType === "TypeScript") {
|
|
482
594
|
const outputString = this.exportToTypeScript(typesToExport, importMap);
|
|
@@ -484,6 +596,23 @@ class XLRSDK {
|
|
|
484
596
|
}
|
|
485
597
|
throw new Error(`Unknown export format ${exportType}`);
|
|
486
598
|
}
|
|
599
|
+
resolveType(type) {
|
|
600
|
+
return simpleTransformGenerator("object", "any", (objectNode) => {
|
|
601
|
+
if (objectNode.extends) {
|
|
602
|
+
const refName = objectNode.extends.ref.split("<")[0];
|
|
603
|
+
let extendedType = this.getType(refName, { getRawType: true });
|
|
604
|
+
if (!extendedType) {
|
|
605
|
+
throw new Error(`Error resolving ${objectNode.name}: can't find extended type ${refName}`);
|
|
606
|
+
}
|
|
607
|
+
extendedType = xlrUtils.resolveReferenceNode(objectNode.extends, extendedType);
|
|
608
|
+
return __spreadProps(__spreadValues({}, xlrUtils.computeEffectiveObject(extendedType, objectNode, false)), {
|
|
609
|
+
name: objectNode.name,
|
|
610
|
+
description: objectNode.description
|
|
611
|
+
});
|
|
612
|
+
}
|
|
613
|
+
return objectNode;
|
|
614
|
+
})(type, "any");
|
|
615
|
+
}
|
|
487
616
|
exportToTypeScript(typesToExport, importMap) {
|
|
488
617
|
const referencedImports = new Set();
|
|
489
618
|
const exportedTypes = new Map();
|
|
@@ -500,7 +629,7 @@ class XLRSDK {
|
|
|
500
629
|
importMap.forEach((imports, packageName) => {
|
|
501
630
|
const applicableImports = imports.filter((i) => referencedImports.has(i));
|
|
502
631
|
resultFile = ts__default["default"].factory.updateSourceFile(resultFile, [
|
|
503
|
-
ts__default["default"].factory.createImportDeclaration(void 0,
|
|
632
|
+
ts__default["default"].factory.createImportDeclaration(void 0, ts__default["default"].factory.createImportClause(false, void 0, ts__default["default"].factory.createNamedImports(applicableImports.map((i) => ts__default["default"].factory.createImportSpecifier(false, void 0, ts__default["default"].factory.createIdentifier(i))))), ts__default["default"].factory.createStringLiteral(packageName)),
|
|
504
633
|
...resultFile.statements
|
|
505
634
|
]);
|
|
506
635
|
});
|
|
@@ -511,61 +640,6 @@ ${nodeText}`;
|
|
|
511
640
|
}
|
|
512
641
|
}
|
|
513
642
|
|
|
514
|
-
function simpleTransformGenerator(typeToTransform, capabilityToTransform, functionToRun) {
|
|
515
|
-
const walker = (node, capability) => {
|
|
516
|
-
var _a;
|
|
517
|
-
if (capability === capabilityToTransform) {
|
|
518
|
-
if (node.type === typeToTransform) {
|
|
519
|
-
functionToRun(node);
|
|
520
|
-
}
|
|
521
|
-
if (node.type === "object") {
|
|
522
|
-
if (node.extends) {
|
|
523
|
-
walker(node, capability);
|
|
524
|
-
}
|
|
525
|
-
for (const key in node.properties) {
|
|
526
|
-
const value = node.properties[key];
|
|
527
|
-
walker(value.node, capability);
|
|
528
|
-
}
|
|
529
|
-
if (node.additionalProperties) {
|
|
530
|
-
walker(node.additionalProperties, capability);
|
|
531
|
-
}
|
|
532
|
-
} else if (node.type === "array") {
|
|
533
|
-
walker(node.elementType, capability);
|
|
534
|
-
} else if (node.type === "and") {
|
|
535
|
-
node.and.forEach((element) => walker(element, capability));
|
|
536
|
-
} else if (node.type === "or") {
|
|
537
|
-
node.or.forEach((element) => walker(element, capability));
|
|
538
|
-
} else if (node.type === "ref") {
|
|
539
|
-
(_a = node.genericArguments) == null ? void 0 : _a.forEach((element) => walker(element, capability));
|
|
540
|
-
} else if (node.type === "tuple") {
|
|
541
|
-
if (node.additionalItems) {
|
|
542
|
-
walker(node.additionalItems, capability);
|
|
543
|
-
}
|
|
544
|
-
node.elementTypes.forEach((element) => walker(element, capability));
|
|
545
|
-
} else if (node.type === "function") {
|
|
546
|
-
node.parameters.forEach((param) => {
|
|
547
|
-
walker(param.type, capability);
|
|
548
|
-
if (param.default) {
|
|
549
|
-
walker(param.default, capability);
|
|
550
|
-
}
|
|
551
|
-
});
|
|
552
|
-
if (node.returnType) {
|
|
553
|
-
walker(node.returnType, capability);
|
|
554
|
-
}
|
|
555
|
-
} else if (node.type === "record") {
|
|
556
|
-
walker(node.keyType, capability);
|
|
557
|
-
walker(node.valueType, capability);
|
|
558
|
-
} else if (node.type === "conditional") {
|
|
559
|
-
walker(node.check.left, capability);
|
|
560
|
-
walker(node.check.right, capability);
|
|
561
|
-
walker(node.value.true, capability);
|
|
562
|
-
walker(node.value.false, capability);
|
|
563
|
-
}
|
|
564
|
-
}
|
|
565
|
-
};
|
|
566
|
-
return walker;
|
|
567
|
-
}
|
|
568
|
-
|
|
569
643
|
exports.BasicXLRRegistry = BasicXLRRegistry;
|
|
570
644
|
exports.XLRSDK = XLRSDK;
|
|
571
645
|
exports.simpleTransformGenerator = simpleTransformGenerator;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Node } from 'jsonc-parser';
|
|
2
|
-
import { NamedType, NodeType, TransformFunction, NodeTypeStrings, NodeTypeMap } from '@player-tools/xlr';
|
|
2
|
+
import { NamedType, NodeType, TransformFunction, TSManifest, NodeTypeStrings, NodeTypeMap } from '@player-tools/xlr';
|
|
3
3
|
|
|
4
4
|
interface ValidationError {
|
|
5
5
|
/** Error message text */
|
|
@@ -50,6 +50,10 @@ declare class BasicXLRRegistry implements XLRRegistry {
|
|
|
50
50
|
info(id: string): TypeMetadata | undefined;
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
+
interface GetTypeOptions {
|
|
54
|
+
/** Resolves `extends` fields in objects */
|
|
55
|
+
getRawType?: boolean;
|
|
56
|
+
}
|
|
53
57
|
/**
|
|
54
58
|
* Abstraction for interfacing with XLRs making it more approachable to use without understanding the inner workings of the types and how they are packaged
|
|
55
59
|
*/
|
|
@@ -58,12 +62,67 @@ declare class XLRSDK {
|
|
|
58
62
|
private validator;
|
|
59
63
|
private tsWriter;
|
|
60
64
|
constructor(customRegistry?: XLRRegistry);
|
|
65
|
+
/**
|
|
66
|
+
* Loads definitions from a path on the filesystem
|
|
67
|
+
*
|
|
68
|
+
* @param inputPath - path to the directory to load (above the xlr folder)
|
|
69
|
+
* @param filters - Any filters to apply when loading the types (a positive match will omit)
|
|
70
|
+
* @param transforms - any transforms to apply to the types being loaded
|
|
71
|
+
*/
|
|
61
72
|
loadDefinitionsFromDisk(inputPath: string, filters?: Omit<Filters, 'pluginFilter'>, transforms?: Array<TransformFunction>): void;
|
|
62
|
-
|
|
63
|
-
|
|
73
|
+
/**
|
|
74
|
+
* Load definitions from a js/ts file in memory
|
|
75
|
+
*
|
|
76
|
+
* @param manifest - The imported XLR manifest module
|
|
77
|
+
* @param filters - Any filters to apply when loading the types (a positive match will omit)
|
|
78
|
+
* @param transforms - any transforms to apply to the types being loaded
|
|
79
|
+
*/
|
|
80
|
+
loadDefinitionsFromModule(manifest: TSManifest, filters?: Omit<Filters, 'pluginFilter'>, transforms?: Array<TransformFunction>): Promise<void>;
|
|
81
|
+
/**
|
|
82
|
+
* Returns a Type that has been previously loaded
|
|
83
|
+
*
|
|
84
|
+
* @param id - Type to retrieve
|
|
85
|
+
* @param options - `GetTypeOptions`
|
|
86
|
+
* @returns `NamedType<NodeType>` | `undefined`
|
|
87
|
+
*/
|
|
88
|
+
getType(id: string, options?: GetTypeOptions): NamedType<NodeType> | undefined;
|
|
89
|
+
/**
|
|
90
|
+
* Returns if a Type with `id` has been loaded into the DSK
|
|
91
|
+
*
|
|
92
|
+
* @param id - Type to retrieve
|
|
93
|
+
* @returns `boolean`
|
|
94
|
+
*/
|
|
64
95
|
hasType(id: string): boolean;
|
|
96
|
+
/**
|
|
97
|
+
* Lists types that have been loaded into the SDK
|
|
98
|
+
*
|
|
99
|
+
* @param filters - Any filters to apply to the types returned (a positive match will omit)
|
|
100
|
+
* @returns `Array<NamedTypes>`
|
|
101
|
+
*/
|
|
65
102
|
listTypes(filters?: Filters): NamedType<NodeType>[];
|
|
66
|
-
|
|
103
|
+
/**
|
|
104
|
+
* Returns meta information around a registered type
|
|
105
|
+
*
|
|
106
|
+
* @param id - Name of Type to retrieve
|
|
107
|
+
* @returns `TypeMetaData` | `undefined`
|
|
108
|
+
*/
|
|
109
|
+
getTypeInfo(id: string): TypeMetadata | undefined;
|
|
110
|
+
/**
|
|
111
|
+
* Validates if a JSONC Node follows the XLR Type registered under the `typeName` specified
|
|
112
|
+
*
|
|
113
|
+
* @param typeName - Registered XLR Type to use for validation
|
|
114
|
+
* @param rootNode - Node to validate
|
|
115
|
+
* @returns `Array<ValidationErrors>`
|
|
116
|
+
*/
|
|
117
|
+
validateByName(typeName: string, rootNode: Node): ValidationError[];
|
|
118
|
+
/**
|
|
119
|
+
* Validates if a JSONC Node follows the supplied XLR Type
|
|
120
|
+
*
|
|
121
|
+
* @param type - Type to validate against
|
|
122
|
+
* @param rootNode - Node to validate
|
|
123
|
+
* @returns `Array<ValidationErrors>`
|
|
124
|
+
*/
|
|
125
|
+
validateByType(type: NodeType, rootNode: Node): ValidationError[];
|
|
67
126
|
/**
|
|
68
127
|
* Exports the types loaded into the registry to the specified format
|
|
69
128
|
*
|
|
@@ -74,6 +133,7 @@ declare class XLRSDK {
|
|
|
74
133
|
* @returns [filename, content][] - Tuples of filenames and content to write
|
|
75
134
|
*/
|
|
76
135
|
exportRegistry(exportType: ExportTypes, importMap: Map<string, string[]>, filters?: Filters, transforms?: Array<TransformFunction>): [string, string][];
|
|
136
|
+
private resolveType;
|
|
77
137
|
private exportToTypeScript;
|
|
78
138
|
}
|
|
79
139
|
|
|
@@ -81,6 +141,6 @@ declare class XLRSDK {
|
|
|
81
141
|
* Helper function for simple transforms
|
|
82
142
|
* Walks an XLR tree looking for the specified node type calls the supplied function when called
|
|
83
143
|
*/
|
|
84
|
-
declare function simpleTransformGenerator<T extends NodeTypeStrings = NodeTypeStrings>(typeToTransform: T, capabilityToTransform: string, functionToRun: (input: NodeTypeMap[T]) =>
|
|
144
|
+
declare function simpleTransformGenerator<T extends NodeTypeStrings = NodeTypeStrings>(typeToTransform: T, capabilityToTransform: string, functionToRun: (input: NodeTypeMap[T]) => NodeTypeMap[T]): TransformFunction;
|
|
85
145
|
|
|
86
|
-
export { BasicXLRRegistry, ExportTypes, Filters, TypeMetadata, ValidationError, XLRRegistry, XLRSDK, simpleTransformGenerator };
|
|
146
|
+
export { BasicXLRRegistry, ExportTypes, Filters, GetTypeOptions, TypeMetadata, ValidationError, XLRRegistry, XLRSDK, simpleTransformGenerator };
|