UNPKG

@deliverr/serverless-offline-step-functions

Version:

Serverless Offline Plugin to Support Step Functions for Local Development

69 lines (68 loc) 2.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ParameterPayloadTemplate = void 0; const jsonpath_plus_1 = require("jsonpath-plus"); const ContextToJson_1 = require("../Context/ContextToJson"); const Logger_1 = require("../utils/Logger"); const PayloadTemplate_1 = require("./PayloadTemplate"); class ParameterPayloadTemplate extends PayloadTemplate_1.PayloadTemplate { constructor(_payload, _context) { super(); this._payload = _payload; this._context = _context; } static create(payload, context) { return new ParameterPayloadTemplate(payload, context); } hasJsonStructure(str) { if (typeof str !== 'string') return false; try { const result = JSON.parse(str); const type = Object.prototype.toString.call(result); return type === '[object Object]' || type === '[object Array]'; } catch (err) { return false; } } processPathKey(key, path, inputJson) { if (typeof path !== 'string') { throw new Error(`The path for the field '${key}' must be a STRING that contains a JSONPath but was an ${typeof path}`); } const newKey = key.substring(0, key.length - 2); let result; if (this.isContextObjectPath(path)) { const jsonContext = ContextToJson_1.ContextToJson(this._context); result = jsonpath_plus_1.JSONPath({ path: path.substring(1, path.length), json: jsonContext, }); if (result.length < 1) { const message = `Could not process key "${key}", with path "${path}" for json ${jsonContext}.`; Logger_1.Logger.getInstance().error(message); throw new Error(message); } if (this.hasJsonStructure(result[0])) { result[0] = JSON.parse(result[0]); } } else { result = jsonpath_plus_1.JSONPath({ path: path === undefined ? '$' : path, json: JSON.parse(inputJson), }); if (result.length < 1) { const message = `Could not process key "${key}", with path "${path}" for json ${inputJson}.`; Logger_1.Logger.getInstance().error(message); throw new Error(message); } } return { [newKey]: result[0] }; } process(inputJson) { return this.processPayloadTemplate(inputJson, this._payload); } } exports.ParameterPayloadTemplate = ParameterPayloadTemplate; ParameterPayloadTemplate.acceptableParameterProperties = ['FunctionName', 'Payload'];