UNPKG

arvo-core

Version:

The core Arvo package which provides application tier core primitives and contract system for building production-grade event-driven application. Provides ArvoEvent (CloudEvents-compliant), ArvoContract for type-safe service interfaces, event factories, O

69 lines (68 loc) 2.95 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createArvoContract = void 0; var _1 = __importDefault(require(".")); var typegen_1 = require("../ArvoOrchestratorContract/typegen"); var utils_1 = require("../utils"); /** * Creates a validated ArvoContract instance with full control over event types and schemas. * * @param contract - Contract specification object * * @throws {Error} If the event types contain reserved prefix ({@link ArvoOrchestratorEventTypeGen}) * @throws {Error} If any of the ArvoContract's internal validations fail. See {@link ArvoContract} * * @returns A fully typed and validated ArvoContract instance * * @example * ```typescript * const contract = createArvoContract({ * uri: 'com.example.contract', * type: 'input.event', * description: "Some example contract", * metadata: { * owner: 'team-a', * priority: 'high' * }, * versions: { * '1.0.0': { * accepts: z.object({ data: z.string() }), * emits: { * 'output.event': z.object({ result: z.number() }) * } * } * } * }); * ``` */ var createArvoContract = function (contract) { var _a, _b, _c; var createErrorMessage = function (source, type, version) { var versionString = version ? ", version=".concat(version) : ''; return (0, utils_1.cleanString)("\n In contract (uri=".concat(contract.uri).concat(versionString, "), the '").concat(source, "' event (type=").concat(type, ") must not start\n with '").concat(typegen_1.ArvoOrchestratorEventTypeGen.prefix, "' because this is a reserved pattern\n for Arvo orchestrators.\n ")); }; if (typegen_1.ArvoOrchestratorEventTypeGen.isOrchestratorEventType(contract.type)) { throw new Error(createErrorMessage('accepts', contract.type, null)); } for (var _i = 0, _d = Object.entries(contract.versions); _i < _d.length; _i++) { var _e = _d[_i], version = _e[0], versionContract = _e[1]; for (var _f = 0, _g = Object.keys(versionContract['emits']); _f < _g.length; _f++) { var emitType = _g[_f]; if (typegen_1.ArvoOrchestratorEventTypeGen.isOrchestratorEventType(emitType)) { throw new Error(createErrorMessage('emits', emitType, version)); } } } return new _1.default({ uri: contract.uri, type: contract.type, domain: (_a = contract === null || contract === void 0 ? void 0 : contract.domain) !== null && _a !== void 0 ? _a : null, description: (_b = contract.description) !== null && _b !== void 0 ? _b : null, metadata: (_c = contract.metadata) !== null && _c !== void 0 ? _c : {}, versions: contract.versions, }); }; exports.createArvoContract = createArvoContract;