@ply-ct/ply
Version:
REST API Automated Testing
61 lines • 2.79 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.replaceLine = exports.replace = void 0;
const ply_values_1 = require("@ply-ct/ply-values");
const util = __importStar(require("./util"));
const names_1 = require("./names");
/**
* Replaces template expressions with values from context (per line), with template literal
* syntax. Untrusted supports a limited subset of template literal. Ignores regular expressions
* starting with ${~. * Resulting newlines are always \n.
*/
const replace = (template, context, options) => {
return util
.lines(template)
.map((l) => (0, exports.replaceLine)(l, context, options))
.join('\n');
};
exports.replace = replace;
const replaceLine = (line, context, options) => {
var _a, _b;
try {
let l = line.replace(/\${@\[/g, '${' + names_1.RESULTS + '[');
l = l.replace(/\${@/g, '${' + names_1.RESULTS + '.');
return l.replace(/\$\{.+?}/g, (expr) => {
return (0, ply_values_1.resolve)(expr, context, options === null || options === void 0 ? void 0 : options.trusted, options === null || options === void 0 ? void 0 : options.logger);
});
}
catch (err) {
if (err.message === `${names_1.RESULTS} is not defined`) {
err.message = 'No previous results found';
}
(_a = options === null || options === void 0 ? void 0 : options.logger) === null || _a === void 0 ? void 0 : _a.error(`Error in expression:\n${line}\n** ${err.message} **`);
(_b = options === null || options === void 0 ? void 0 : options.logger) === null || _b === void 0 ? void 0 : _b.debug(`${err}`, err);
return line;
}
};
exports.replaceLine = replaceLine;
//# sourceMappingURL=replace.js.map