UNPKG

@deliverr/serverless-offline-step-functions

Version:

Serverless Offline Plugin to Support Step Functions for Local Development

131 lines (130 loc) 5.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StateProcessor = void 0; const jsonpath_plus_1 = require("jsonpath-plus"); const LambdaWaitFotTokenPayloadTemplate_1 = require("./PayloadTemplates/LambdaWaitFotTokenPayloadTemplate"); const ParameterPayloadTemplate_1 = require("./PayloadTemplates/ParameterPayloadTemplate"); const Logger_1 = require("./utils/Logger"); class StateProcessor { static processItemsPath(dataJson, itemsPath) { this.logger.debug(`StateProcessor - processItemsPath - ${dataJson}`); if (itemsPath === null) { return '[]'; } const inputJson = dataJson || '{}'; const result = jsonpath_plus_1.JSONPath({ path: itemsPath === undefined ? '$' : itemsPath, json: JSON.parse(inputJson), }); if (!result || result.length === 0) { throw new Error(`Could not find itemsPath "${itemsPath}" in JSON "${dataJson}"`); } return JSON.stringify(result[0]); } static processInputPath(dataJson, inputPath) { this.logger.debug(`StateProcessor - processInputPath - ${dataJson}`); if (inputPath === null) { return '{}'; } const inputJson = dataJson || '{}'; const result = jsonpath_plus_1.JSONPath({ path: inputPath === undefined ? '$' : inputPath, json: JSON.parse(inputJson), }); if (!result || result.length === 0) { throw new Error(''); } return JSON.stringify(result[0]); } static processWaitForTokenParameters(dataJson, parameters, context) { const inputJson = dataJson || '{}'; const payloadTemplate = LambdaWaitFotTokenPayloadTemplate_1.LambdaWaitFotTokenPayloadTemplate.create(parameters, context); return JSON.stringify(payloadTemplate.process(inputJson)); } static processParameters(dataJson, payloadTemplateInput, context) { this.logger.debug('Starting processParameters'); this.logger.debug(JSON.stringify(dataJson)); this.logger.debug(JSON.stringify(payloadTemplateInput)); if (!payloadTemplateInput) { return dataJson || '{}'; } const inputJson = dataJson || '{}'; const payloadTemplate = ParameterPayloadTemplate_1.ParameterPayloadTemplate.create(payloadTemplateInput, context); return JSON.stringify(payloadTemplate.process(inputJson)); } static processResultPath(input, result, resultPath) { this.logger.debug('processResultPath'); if (!resultPath || resultPath === '$') { this.logger.debug('No result path defined'); return JSON.stringify(result); } this.logger.debug(resultPath); let resultPathArray = jsonpath_plus_1.JSONPath.toPathArray(resultPath); if (resultPathArray[0] !== '$') { resultPathArray = ['$', ...resultPathArray]; } this.logger.debug('resultPathArray'); this.logger.debug(resultPathArray); this.logger.debug('input'); this.logger.debug(JSON.stringify(input)); let temp = input; for (let i = 1; i < resultPathArray.length; i++) { const key = resultPathArray[i]; if (i === resultPathArray.length - 1) { temp[key] = result; } else { if (!temp[key]) { temp[key] = {}; } temp = temp[key]; } } return JSON.stringify(input); } static processOutputPath(json, outputPath) { if (!json) { throw new Error('Output JSON of lambda was undefined'); } const result = jsonpath_plus_1.JSONPath({ path: outputPath || '$', json: JSON.parse(json), }); if (!result || result.length === 0) { throw new Error(''); } return JSON.stringify(result[0]); } static processResultSelector(dataJson, resultSelector) { this.logger.debug('Starting processResultSelector'); this.logger.debug(JSON.stringify(dataJson)); this.logger.debug(JSON.stringify(resultSelector)); if (!resultSelector) { this.logger.debug('No ResultSelector Defined. skipping.'); return dataJson; } const parsedDataJson = JSON.parse(dataJson); const resultSelectorObject = Object.entries(resultSelector).reduce((projectedObject, [key, pathOrValue]) => { if (key.endsWith('.$')) { const result = jsonpath_plus_1.JSONPath({ path: pathOrValue, json: parsedDataJson, }); if (!result || result.length === 0) { throw new Error(`Could not find "${pathOrValue}" in JSON "${dataJson}"`); } const jsonPathResult = result.length === 1 ? result[0] : result; projectedObject[key.replace(/\.\$$/giu, '')] = jsonPathResult; } else { projectedObject[key] = pathOrValue; } return projectedObject; }, {}); this.logger.debug('finished processResultSelector'); this.logger.debug(JSON.stringify(resultSelectorObject)); return JSON.stringify(resultSelectorObject); } } exports.StateProcessor = StateProcessor; StateProcessor.logger = Logger_1.Logger.getInstance();