shyft
Version:
Model driven GraphQL API framework
67 lines (66 loc) • 3.09 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 __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.isDataTypeState = exports.DataTypeState = void 0;
var lodash_1 = require("lodash");
var util_1 = require("../util");
var constants_1 = require("../constants");
var DataType_1 = require("./DataType");
var DataTypeState = /** @class */ (function (_super) {
__extends(DataTypeState, _super);
function DataTypeState(setup) {
if (setup === void 0) { setup = {}; }
var _this = this;
var name = setup.name, description = setup.description, states = setup.states;
util_1.passOrThrow(util_1.isMap(states, true), function () { return "'Missing states for data type '" + name + "'"; });
var stateNames = Object.keys(states);
var uniqueIds = [];
stateNames.map(function (stateName) {
var stateId = states[stateName];
uniqueIds.push(stateId);
util_1.passOrThrow(constants_1.stateNameRegex.test(stateName), function () {
return "Invalid state name '" + stateName + "' for data type '" + name + "' (Regex: /" + constants_1.STATE_NAME_PATTERN + "/)";
});
util_1.passOrThrow(stateId === Number(stateId) && stateId > 0, function () {
return "State '" + stateName + "' for data type '" + name + "' has an invalid unique ID (needs to be a positive integer)";
});
});
util_1.passOrThrow(uniqueIds.length === lodash_1.uniq(uniqueIds).length, function () {
return "Each state defined for data type '" + name + "' needs to have a unique ID";
});
_this = _super.call(this, {
name: name,
description: description || "States: " + stateNames.join(', '),
enforceIndex: true,
mock: function () {
var randomPos = Math.floor(Math.random() * uniqueIds.length);
return uniqueIds[randomPos];
},
}) || this;
_this.states = states;
return _this;
}
DataTypeState.prototype.toString = function () {
return this.name;
};
return DataTypeState;
}(DataType_1.DataType));
exports.DataTypeState = DataTypeState;
var isDataTypeState = function (obj) {
return obj instanceof DataTypeState;
};
exports.isDataTypeState = isDataTypeState;