UNPKG

shyft

Version:

Model driven GraphQL API framework

67 lines (66 loc) 3.09 kB
"use strict"; 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.isDataTypeEnum = exports.DataTypeEnum = void 0; var lodash_1 = require("lodash"); var util_1 = require("../util"); var constants_1 = require("../constants"); var DataType_1 = require("./DataType"); var DataTypeEnum = /** @class */ (function (_super) { __extends(DataTypeEnum, _super); function DataTypeEnum(setup) { if (setup === void 0) { setup = {}; } var _this = this; var name = setup.name, description = setup.description, values = setup.values; util_1.passOrThrow(util_1.isMap(values, true), function () { return "'Missing enum values for data type '" + name + "'"; }); var valueNames = Object.keys(values); var uniqueIds = []; valueNames.map(function (valueName) { var valueId = values[valueName]; uniqueIds.push(valueId); util_1.passOrThrow(constants_1.enumValueRegex.test(valueName), function () { return "Invalid value name '" + valueName + "' for data type '" + name + "' (Regex: /" + constants_1.ENUM_VALUE_PATTERN + "/)"; }); util_1.passOrThrow(valueId === Number(valueId) && valueId > 0, function () { return "Value '" + valueName + "' 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 value defined for data type '" + name + "' needs to have a unique ID"; }); _this = _super.call(this, { name: name, description: description || "Enumeration set: " + valueNames.join(', '), enforceIndex: true, mock: function () { var randomPos = Math.floor(Math.random() * uniqueIds.length); return uniqueIds[randomPos]; }, }) || this; _this.values = values; return _this; } DataTypeEnum.prototype.toString = function () { return this.name; }; return DataTypeEnum; }(DataType_1.DataType)); exports.DataTypeEnum = DataTypeEnum; var isDataTypeEnum = function (obj) { return obj instanceof DataTypeEnum; }; exports.isDataTypeEnum = isDataTypeEnum;