arvo-core
Version:
This core package contains all the core classes and components of the Arvo Event Driven System
124 lines (123 loc) • 4.96 kB
JavaScript
;
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 __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createArvoOrchestratorContract = void 0;
var ArvoContract_1 = __importDefault(require("../ArvoContract"));
var schema_1 = require("./schema");
var typegen_1 = require("./typegen");
/**
* Validates if a string contains only uppercase or lowercase alphanumeric characters.
*
* This function checks if the input string consists solely of:
* - Lowercase letters (a-z)
* - Numbers (0-9)
* - Dot (.)
*
* It does not allow any special characters, spaces, or other non-alphanumeric characters.
*
* @param input - The string to be validated.
* @returns True if the string contains only alphanumeric characters, false otherwise.
*/
function isLowerAlphanumeric(input) {
var alphanumericRegex = /^[a-z0-9.]+$/;
return alphanumericRegex.test(input);
}
/**
* Creates an ArvoOrchestratorContract with specified parameters.
*
* The ArvoOrchestratorContract is a specialized contract designed to manage the lifecycle
* of orchestration processes within the Arvo framework. It creates a contract with an init event
* type and a corresponding complete event type.
*
* Key features:
* 1. Type Validation: Ensures the type parameter follows lowercase alphanumeric with dots format
* 2. Event Type Generation: Automatically generates init and complete event types based on the provided type
* 3. Schema Merging: Merges provided init schemas with the OrchestrationInitEventBaseSchema
* 4. Version Support: Handles multiple versions of the contract with their respective schemas
*
* @param contract - Configuration object for the orchestrator contract
*
* @throws {Error} If the type parameter contains invalid characters (must be lowercase alphanumeric with dots)
*
* @returns An ArvoOrchestratorContract instance configured with the specified parameters
*
* @example
* ```typescript
* const contract = createArvoOrchestratorContract({
* uri: '#/orchestrators/data/processor',
* name: 'data.processor',
* versions: {
* '1.0.0': {
* init: z.object({
* data: z.string(),
* options: z.object({
* format: z.string()
* })
* }),
* complete: z.object({
* processedData: z.string(),
* metadata: z.record(z.string())
* })
* },
* '1.1.0': {
* init: z.object({
* data: z.string(),
* options: z.object({
* format: z.string(),
* compression: z.boolean().optional()
* })
* }),
* complete: z.object({
* processedData: z.string(),
* metadata: z.record(z.string()),
* performance: z.object({
* duration: z.number(),
* bytesProcessed: z.number()
* })
* })
* }
* }
* });
* ```
*/
var createArvoOrchestratorContract = function (contract) {
var _a, _b, _c;
if (!isLowerAlphanumeric(contract.name)) {
throw new Error("Invalid 'name' = '".concat(contract.name, "'. The 'name' must only contain alphanumeric characters. e.g. test.orchestrator"));
}
var mergedMetaData = __assign(__assign({}, ((_a = contract.metadata) !== null && _a !== void 0 ? _a : {})), { contractType: 'ArvoOrchestratorContract', rootType: contract.name, completeEventType: typegen_1.ArvoOrchestratorEventTypeGen.complete(contract.name), initEventType: typegen_1.ArvoOrchestratorEventTypeGen.init(contract.name) });
return new ArvoContract_1.default({
uri: contract.uri,
domain: (_b = contract.domain) !== null && _b !== void 0 ? _b : null,
type: typegen_1.ArvoOrchestratorEventTypeGen.init(contract.name),
description: (_c = contract.description) !== null && _c !== void 0 ? _c : null,
metadata: mergedMetaData,
versions: Object.fromEntries(Object.entries(contract.versions).map(function (_a) {
var _b;
var version = _a[0], versionContract = _a[1];
return [
version,
{
accepts: schema_1.OrchestrationInitEventBaseSchema.merge(versionContract.init),
emits: (_b = {},
_b[typegen_1.ArvoOrchestratorEventTypeGen.complete(contract.name)] = versionContract.complete,
_b),
},
];
})),
});
};
exports.createArvoOrchestratorContract = createArvoOrchestratorContract;