shyft
Version:
Model driven GraphQL API framework
136 lines (135 loc) • 6.75 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildObjectDataType = exports.isObjectDataType = exports.ObjectDataType = void 0;
var util_1 = require("../util");
var Entity_1 = require("../entity/Entity");
var DataType_1 = require("./DataType");
var ComplexDataType_1 = require("./ComplexDataType");
var ObjectDataType = /** @class */ (function (_super) {
__extends(ObjectDataType, _super);
function ObjectDataType(setup) {
if (setup === void 0) { setup = {}; }
var _this =
// constructor(setup: ObjectDataTypeSetupType = <ObjectDataTypeSetupType>{}) {
_super.call(this) || this;
_this.validate = function (_a) {
var value = _a.value;
if (value) {
if (!util_1.isMap(value)) {
throw new Error("Object data type '" + _this.name + "' expects an object");
}
}
};
var name = setup.name, description = setup.description, attributes = setup.attributes;
util_1.passOrThrow(name, function () { return 'Missing object data type name'; });
util_1.passOrThrow(description, function () { return "Missing description for object data type '" + name + "'"; });
util_1.passOrThrow(attributes, function () { return "Missing attributes for object data type '" + name + "'"; });
util_1.passOrThrow(util_1.isMap(attributes) || util_1.isFunction(attributes), function () {
return "Object data type '" + name + "' needs an attribute definition as a map or a function returning such a map";
});
_this.name = name;
_this.description = description;
_this._attributesMap = attributes;
return _this;
}
ObjectDataType.prototype.getAttributes = function () {
if (this._attributes) {
return this._attributes;
}
var ret = (this._attributes = this._processAttributeMap());
return ret;
};
ObjectDataType.prototype._processAttribute = function (rawAttribute, attributeName) {
var _this = this;
if (util_1.isFunction(rawAttribute.type)) {
var rawAttributeTypeFn = rawAttribute.type;
rawAttribute.type = rawAttributeTypeFn({
setup: {
name: attributeName,
description: rawAttribute.description,
},
});
}
var attribute = __assign(__assign({}, rawAttribute), { required: !!rawAttribute.required, name: attributeName });
util_1.passOrThrow(attribute.description, function () { return "Missing description for '" + _this.name + "." + attributeName + "'"; });
util_1.passOrThrow(DataType_1.isDataType(attribute.type) ||
ComplexDataType_1.isComplexDataType(attribute.type) ||
Entity_1.isEntity(attribute.type), function () {
return "'" + _this.name + "." + attributeName + "' has invalid data type '" + String(attribute.type) + "'";
});
util_1.passOrThrow(!attribute.resolve || util_1.isFunction(attribute.resolve), function () { return "'" + _this.name + "." + attributeName + "' has an invalid resolve function'"; });
if (attribute.defaultValue) {
// enforce mandatory param if defaultValue provided
attribute.required = true;
util_1.passOrThrow(util_1.isFunction(attribute.defaultValue), function () {
return "'" + _this.name + "." + attributeName + "' has an invalid defaultValue function'";
});
util_1.passOrThrow(!ComplexDataType_1.isComplexDataType(attribute.type), function () {
return "Complex data type '" + _this.name + "." + attributeName + "' cannot have a defaultValue function'";
});
}
util_1.passOrThrow(!attribute.validate || util_1.isFunction(attribute.validate), function () { return "'" + _this.name + "." + attributeName + "' has an invalid validate function'"; });
return attribute;
};
ObjectDataType.prototype._processAttributeMap = function () {
var _this = this;
// if it's a function, resolve it to get that map
var attributeMap = util_1.resolveFunctionMap(this._attributesMap);
util_1.passOrThrow(util_1.isMap(attributeMap), function () {
return "Attribute definition function for object data type '" + _this.name + "' does not return a map";
});
var attributeNames = Object.keys(attributeMap);
util_1.passOrThrow(attributeNames.length > 0, function () { return "Object data type '" + _this.name + "' has no attributes defined"; });
var resultAttributes = {};
attributeNames.forEach(function (attributeName) {
resultAttributes[attributeName] = _this._processAttribute(attributeMap[attributeName], attributeName);
});
return resultAttributes;
};
ObjectDataType.prototype.toString = function () {
return this.name;
};
return ObjectDataType;
}(ComplexDataType_1.ComplexDataType));
exports.ObjectDataType = ObjectDataType;
var isObjectDataType = function (obj) {
return obj instanceof ObjectDataType;
};
exports.isObjectDataType = isObjectDataType;
var buildObjectDataType = function (obj) {
return function (_a) {
var _b = _a.setup, name = _b.name, description = _b.description;
return new ObjectDataType({
description: description,
attributes: obj.attributes,
name: name,
});
};
};
exports.buildObjectDataType = buildObjectDataType;