arvo-core
Version:
This core package contains all the core classes and components of the Arvo Event Driven System
84 lines (83 loc) • 3 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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createSimpleArvoContract = void 0;
var helpers_1 = require("../helpers");
/**
* Creates an ArvoContract with standardized naming conventions and a simplified event pattern.
* Use this to create contracts with one emit type only.
*
* @param param - Contract configuration
* @param param.uri - Contract identifier URI
* @param param.type - Base event type (will be prefixed with "com.")
* @param param.versions - Version-specific schema definitions
* @param param.metadata - Optional metadata for the contract
* @param param.description - Optional contract description
*
* @returns ArvoContract with standardized type formatting and metadata
*
* @throws {Error} If any of the validations in {@link ArvoContract} or {@link createArvoContract} fail
*
* @example
* ```typescript
* const contract = createSimpleArvoContract({
* uri: 'api.example/contracts/user',
* type: 'user.create',
* description: 'User creation contract',
* versions: {
* '1.0.0': {
* accepts: z.object({
* name: z.string(),
* email: z.string().email()
* }),
* emits: z.object({
* userId: z.string(),
* timestamp: z.date()
* })
* }
* }
* });
* ```
*
* @remarks
* Provides a simplified contract creation pattern with standardized conventions:
* - Automatically prefixes accept types with "com."
* - Creates a single emit type with "evt." prefix and ".success" suffix
* - Adds standard metadata identifying it as a SimpleArvoContract
*/
var createSimpleArvoContract = function (param) {
var _a;
var mergedMetadata = __assign(__assign({}, ((_a = param.metadata) !== null && _a !== void 0 ? _a : {})), { contractType: 'SimpleArvoContract', rootType: param.type });
var emitType = "evt.".concat(param.type, ".success");
return (0, helpers_1.createArvoContract)({
uri: param.uri,
domain: param.domain,
type: "com.".concat(param.type),
description: param.description,
metadata: mergedMetadata,
versions: Object.fromEntries(Object.entries(param.versions).map(function (_a) {
var _b;
var version = _a[0], contract = _a[1];
return [
version,
{
accepts: contract.accepts,
emits: (_b = {},
_b[emitType] = contract.emits,
_b),
},
];
})),
});
};
exports.createSimpleArvoContract = createSimpleArvoContract;