shyft
Version:
Model driven GraphQL API framework
90 lines (89 loc) • 5.03 kB
JavaScript
;
/* eslint-disable @typescript-eslint/no-explicit-any */
Object.defineProperty(exports, "__esModule", { value: true });
exports.isProtocolType = exports.ProtocolType = void 0;
var util_1 = require("../util");
var DataType_1 = require("../datatype/DataType");
var ProtocolConfiguration_1 = require("./ProtocolConfiguration");
var ProtocolType = /** @class */ (function () {
function ProtocolType(setup) {
if (setup === void 0) { setup = {}; }
var name = setup.name, description = setup.description, isProtocolDataType = setup.isProtocolDataType;
util_1.passOrThrow(name, function () { return 'Missing protocol type name'; });
util_1.passOrThrow(description, function () { return "Missing description for protocol type '" + name + "'"; });
util_1.passOrThrow(util_1.isFunction(isProtocolDataType), function () { return "Protocol type '" + name + "' needs to implement isProtocolDataType()"; });
this.name = name;
this.description = description;
this.isProtocolDataType = isProtocolDataType;
this._dataTypeMap = {};
this._dynamicDataTypeMap = [];
}
ProtocolType.prototype.addDataTypeMap = function (schemaDataType, protocolDataType) {
var _this = this;
util_1.passOrThrow(DataType_1.isDataType(schemaDataType), function () {
return "Provided schemaDataType is not a valid data type in '" + _this.name + "'";
});
util_1.passOrThrow(this.isProtocolDataType(protocolDataType), function () {
return "Provided protocolDataType for '" + String(schemaDataType) + "' is not a valid protocol data type in '" + _this.name + "'";
});
util_1.passOrThrow(!this._dataTypeMap[schemaDataType.name], function () {
return "Data type mapping for '" + schemaDataType.name + "' already registered with protocol type '" + _this.name + "'";
});
this._dataTypeMap[schemaDataType.name] = protocolDataType;
};
ProtocolType.prototype.addDynamicDataTypeMap = function (schemaDataTypeDetector, protocolDataType) {
var _this = this;
util_1.passOrThrow(util_1.isFunction(schemaDataTypeDetector), function () {
return "Provided schemaDataTypeDetector is not a valid function in '" + _this.name + "', " +
("got this instead: " + String(schemaDataTypeDetector));
});
util_1.passOrThrow(this.isProtocolDataType(protocolDataType) || util_1.isFunction(protocolDataType), function () {
return "Provided protocolDataType for '" + String(schemaDataTypeDetector) + "' is not a valid protocol data type or function in '" + _this.name + "', " +
("got this instead: " + String(protocolDataType));
});
this._dynamicDataTypeMap.push({
schemaDataTypeDetector: schemaDataTypeDetector,
protocolDataType: protocolDataType,
});
};
ProtocolType.prototype.convertToProtocolDataType = function (schemaDataType, sourceName, asInput) {
var _this = this;
var foundDynamicDataType = this._dynamicDataTypeMap.find(function (_a) {
var schemaDataTypeDetector = _a.schemaDataTypeDetector;
return schemaDataTypeDetector(schemaDataType);
});
if (foundDynamicDataType) {
var protocolDataType = foundDynamicDataType.protocolDataType;
if (util_1.isFunction(protocolDataType)) {
var attributeType = schemaDataType;
var protocolDataTypeFn = protocolDataType;
return protocolDataTypeFn(attributeType, sourceName, asInput);
}
return protocolDataType;
}
if (DataType_1.isDataType(schemaDataType)) {
util_1.passOrThrow(this._dataTypeMap[schemaDataType.name], function () {
return "No data type mapping found for '" + schemaDataType.name + "' in protocol type '" + _this.name + "'";
});
return this._dataTypeMap[schemaDataType.name];
}
throw new Error("Provided schemaDataType is not a valid data type in protocol type '" + this.name + "'");
};
ProtocolType.prototype.setProtocolConfiguration = function (protocolConfiguration) {
util_1.passOrThrow(ProtocolConfiguration_1.isProtocolConfiguration(protocolConfiguration), function () { return 'ProtocolType expects a valid protocolConfiguration'; });
this.protocolConfiguration = protocolConfiguration;
};
ProtocolType.prototype.getProtocolConfiguration = function () {
util_1.passOrThrow(this.protocolConfiguration, function () { return 'ProtocolType is missing a valid protocolConfiguration'; });
return this.protocolConfiguration;
};
ProtocolType.prototype.toString = function () {
return this.name;
};
return ProtocolType;
}());
exports.ProtocolType = ProtocolType;
var isProtocolType = function (obj) {
return obj instanceof ProtocolType;
};
exports.isProtocolType = isProtocolType;