UNPKG

shyft

Version:

Model driven GraphQL API framework

311 lines (310 loc) 18.3 kB
"use strict"; 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); }; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __spreadArray = (this && this.__spreadArray) || function (to, from) { for (var i = 0, il = from.length, j = to.length; i < il; i++, j++) to[j] = from[i]; return to; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateMutationPayload = exports.validateActionPayload = void 0; var _ = __importStar(require("lodash")); var ObjectDataType_1 = require("./datatype/ObjectDataType"); var ListDataType_1 = require("./datatype/ListDataType"); var ComplexDataType_1 = require("./datatype/ComplexDataType"); var util_1 = require("./util"); var Mutation_1 = require("./mutation/Mutation"); var DataType_1 = require("./datatype/DataType"); var validateDataTypePayload = function (_a) { var dataType = _a.dataType, payload = _a.payload, context = _a.context, source = _a.source; return __awaiter(void 0, void 0, void 0, function () { var dataTypeValidator; return __generator(this, function (_b) { switch (_b.label) { case 0: if (!(DataType_1.isDataType(dataType) || ObjectDataType_1.isObjectDataType(dataType) || ListDataType_1.isListDataType(dataType))) return [3 /*break*/, 2]; dataTypeValidator = dataType.validate; if (!dataTypeValidator) return [3 /*break*/, 2]; return [4 /*yield*/, dataTypeValidator({ value: payload, context: context, source: source })]; case 1: _b.sent(); _b.label = 2; case 2: return [2 /*return*/]; } }); }); }; var validatePayload = function (param, payload, source, context, path) { if (path === void 0) { path = []; } return __awaiter(void 0, void 0, void 0, function () { var paramName_1, paramType_1, attributes_1, payloadList, attribute, attributeName, attributeValidator; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!(typeof payload !== 'undefined' && payload !== null)) return [3 /*break*/, 8]; paramName_1 = param.name; paramType_1 = ListDataType_1.isListDataType(param.type) ? param.type.getItemType() : param.type; return [4 /*yield*/, validateDataTypePayload({ dataType: param.type, payload: payload[paramName_1], context: context, source: source, })]; case 1: _a.sent(); if (!ObjectDataType_1.isObjectDataType(paramType_1)) return [3 /*break*/, 3]; attributes_1 = paramType_1.getAttributes(); return [4 /*yield*/, util_1.asyncForEach(Object.keys(attributes_1), function (attributeName) { return __awaiter(void 0, void 0, void 0, function () { var attribute, newPath; return __generator(this, function (_a) { switch (_a.label) { case 0: attribute = attributes_1[attributeName]; newPath = __spreadArray(__spreadArray([], path), [attribute['name']]); return [4 /*yield*/, validatePayload(attribute, payload[paramName_1], source, context, newPath)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); })]; case 2: _a.sent(); _a.label = 3; case 3: if (!ListDataType_1.isListDataType(param.type)) return [3 /*break*/, 5]; payloadList = payload[paramName_1]; if (!(typeof payloadList !== 'undefined' && payloadList !== null)) return [3 /*break*/, 5]; return [4 /*yield*/, Promise.all(payloadList.map(function (itemPayload) { return __awaiter(void 0, void 0, void 0, function () { var attributes_2, pathString_1; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!ObjectDataType_1.isObjectDataType(paramType_1)) return [3 /*break*/, 3]; return [4 /*yield*/, validateDataTypePayload({ dataType: paramType_1, payload: itemPayload, context: context, source: source, })]; case 1: _a.sent(); attributes_2 = paramType_1.getAttributes(); pathString_1 = path.length ? path.join('.') + "." : ''; return [4 /*yield*/, util_1.asyncForEach(Object.keys(attributes_2), function (attributeName) { return __awaiter(void 0, void 0, void 0, function () { var attribute, newPath; return __generator(this, function (_a) { switch (_a.label) { case 0: attribute = attributes_2[attributeName]; util_1.passOrThrow(!attribute.required || // TODO: name does not exist on AttributeBase // eslint-disable-next-line dot-notation util_1.isDefined(itemPayload[attribute['name']]), function () { // TODO: name does not exist on AttributeBase // eslint-disable-next-line dot-notation return "Missing required input attribute '" + pathString_1 + attribute['name'] + "'"; }); newPath = __spreadArray(__spreadArray([], path), [attribute['name']]); return [4 /*yield*/, validatePayload(attribute, itemPayload, source, context, newPath)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); })]; case 2: _a.sent(); _a.label = 3; case 3: return [2 /*return*/]; } }); }); }))]; case 4: _a.sent(); _a.label = 5; case 5: if (!!ComplexDataType_1.isComplexDataType(paramType_1)) return [3 /*break*/, 8]; attribute = param; attributeName = attribute.name; attributeValidator = attribute.validate; return [4 /*yield*/, validateDataTypePayload({ dataType: paramType_1, payload: payload[attributeName], context: context, source: source, })]; case 6: _a.sent(); if (!attributeValidator) return [3 /*break*/, 8]; if (!(attribute.isSystemAttribute || typeof payload[attributeName] !== 'undefined')) return [3 /*break*/, 8]; return [4 /*yield*/, attributeValidator({ value: payload[attributeName], attributeName: attributeName, input: payload, source: source, context: context, })]; case 7: _a.sent(); _a.label = 8; case 8: return [2 /*return*/]; } }); }); }; var validateActionPayload = function (param, payload, action, context) { return __awaiter(void 0, void 0, void 0, function () { var newParam, newPayload; return __generator(this, function (_a) { switch (_a.label) { case 0: newParam = __assign(__assign({}, param), { name: 'input' }); newPayload = { input: __assign({}, payload), }; if (!util_1.isMap(payload)) { newPayload.input = payload; } // why ts-ignore? because action does not exist on the graphQl/Source class type as public member // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore return [4 /*yield*/, validatePayload(newParam, newPayload, { action: action }, context)]; case 1: // why ts-ignore? because action does not exist on the graphQl/Source class type as public member // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore _a.sent(); return [2 /*return*/]; } }); }); }; exports.validateActionPayload = validateActionPayload; var validateMutationPayload = function (entity, mutation, payload, context) { return __awaiter(void 0, void 0, void 0, function () { var attributes, systemAttributes, attributesToValidate; return __generator(this, function (_a) { switch (_a.label) { case 0: attributes = entity.getAttributes(); systemAttributes = _.filter(attributes, function (attribute) { return attribute.isSystemAttribute && attribute.defaultValue; }).map(function (attribute) { return attribute.name; }); return [4 /*yield*/, Promise.all(systemAttributes.map(function (attributeName) { return __awaiter(void 0, void 0, void 0, function () { var attribute; return __generator(this, function (_a) { switch (_a.label) { case 0: attribute = attributes[attributeName]; // why ts-ignore? because mutation does not exist on the graphQl/Source class type as public member // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore return [4 /*yield*/, validatePayload(attribute, payload, { mutation: mutation, entity: entity }, context)]; case 1: // why ts-ignore? because mutation does not exist on the graphQl/Source class type as public member // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore _a.sent(); return [2 /*return*/]; } }); }); }))]; case 1: _a.sent(); attributesToValidate = mutation.attributes || []; return [4 /*yield*/, Promise.all(attributesToValidate.map(function (attributeName) { return __awaiter(void 0, void 0, void 0, function () { var attribute; return __generator(this, function (_a) { switch (_a.label) { case 0: attribute = attributes[attributeName]; if (mutation.type === Mutation_1.MUTATION_TYPE_CREATE && !attribute.i18n) { util_1.passOrThrow(!attribute.required || util_1.isDefined(payload[attributeName]), function () { return "Missing required input attribute '" + attributeName + "'"; }); } // why ts-ignore? because mutation does not exist on the graphQl/Source class type as public member // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore return [4 /*yield*/, validatePayload(attribute, payload, { mutation: mutation, entity: entity }, context, [ attributeName, ])]; case 1: // why ts-ignore? because mutation does not exist on the graphQl/Source class type as public member // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore _a.sent(); return [2 /*return*/]; } }); }); }))]; case 2: _a.sent(); return [2 /*return*/]; } }); }); }; exports.validateMutationPayload = validateMutationPayload;