UNPKG

@sprucelabs/spruce-event-utils

Version:

Some helpful utilities to speed up working with Mercury! 🚅

50 lines (49 loc) • 1.91 kB
import { namesUtil } from '@sprucelabs/spruce-skill-utils'; import eventSourceSchema from './../.spruce/schemas/spruceEventUtils/v2021_09_13/eventSource.schema.js'; function buildEmitTargetAndPayloadSchema(options) { var _a, _b; const { eventName, payloadSchema: emitPayloadSchema, targetSchema, } = options; const targetField = { type: 'schema', isRequired: hasAnyRequiredFields(targetSchema), options: { schema: targetSchema, }, }; const schema = { id: `${namesUtil.toCamel(eventName)}EmitTargetAndPayload`, fields: { source: { type: 'schema', label: 'Source', options: { schema: eventSourceSchema, }, }, }, }; const hasTargetFields = targetSchema && Object.keys((_a = targetSchema.fields) !== null && _a !== void 0 ? _a : {}).length > 0; if (hasTargetFields) { //@ts-ignore schema.fields.target = targetField; } const hasPayloadFields = emitPayloadSchema && Object.keys((_b = emitPayloadSchema.fields) !== null && _b !== void 0 ? _b : {}).length > 0; if (hasPayloadFields) { const isRequired = hasAnyRequiredFields(emitPayloadSchema); //@ts-ignore schema.fields.payload = { type: 'schema', isRequired, options: { schema: emitPayloadSchema, }, }; } return schema; } export default buildEmitTargetAndPayloadSchema; function hasAnyRequiredFields(schema) { var _a; return !!Object.keys((_a = schema === null || schema === void 0 ? void 0 : schema.fields) !== null && _a !== void 0 ? _a : {}).find((f) => { var _a; return (_a = schema === null || schema === void 0 ? void 0 : schema.fields) === null || _a === void 0 ? void 0 : _a[f].isRequired; }); }