@apideck/portman
Version:
Port OpenAPI Spec to Postman Collection, with contract & variation tests included
74 lines • 4.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.hasTpl = exports.parseTpl = void 0;
var tslib_1 = require("tslib");
var openapi_format_1 = require("openapi-format");
var parseTpl = function (dto) {
var _a, _b, _c, _d, _e;
var _f = dto.template, template = _f === void 0 ? '<opsRef>.<varProp>' : _f, oaOperation = dto.oaOperation, dynamicValues = dto.dynamicValues, options = dto.options;
if (!template || typeof template !== 'string')
return template;
var varName = template;
var partsCounter = 0;
var pathParts = (_b = (_a = oaOperation === null || oaOperation === void 0 ? void 0 : oaOperation.path) === null || _a === void 0 ? void 0 : _a.split('/')) !== null && _b !== void 0 ? _b : [];
var pathPartsObj = pathParts.reduce(function (acc, pathPart) {
if (pathPart.trim() !== '') {
partsCounter++;
acc["pathPart".concat(partsCounter)] = pathPart;
}
return acc;
}, {});
var tags = (_c = oaOperation === null || oaOperation === void 0 ? void 0 : oaOperation.tags) !== null && _c !== void 0 ? _c : [];
var tagsObj = tags.reduce(function (acc, tag, index) {
acc["tag".concat(index + 1)] = tag;
return acc;
}, {});
var openApiInfo = {
operationId: oaOperation === null || oaOperation === void 0 ? void 0 : oaOperation.id,
path: oaOperation === null || oaOperation === void 0 ? void 0 : oaOperation.path,
pathRef: oaOperation === null || oaOperation === void 0 ? void 0 : oaOperation.pathRef,
method: oaOperation === null || oaOperation === void 0 ? void 0 : oaOperation.method,
opsRef: (_d = oaOperation === null || oaOperation === void 0 ? void 0 : oaOperation.id) !== null && _d !== void 0 ? _d : oaOperation === null || oaOperation === void 0 ? void 0 : oaOperation.pathRef,
tag: (_e = oaOperation === null || oaOperation === void 0 ? void 0 : oaOperation.tags) === null || _e === void 0 ? void 0 : _e[0]
} || {};
var tplValues = tslib_1.__assign(tslib_1.__assign(tslib_1.__assign(tslib_1.__assign({}, pathPartsObj), tagsObj), openApiInfo), dynamicValues);
var startSymbol = '<';
var endSymbol = '>';
var placeholderRegex = new RegExp("".concat(startSymbol, "(.*?)").concat(endSymbol), 'g');
varName = varName.replace(placeholderRegex, function (_, placeholder) {
placeholder = placeholder.trim();
var tplValue = tplValues[placeholder];
var casedTplValue = (options === null || options === void 0 ? void 0 : options.caseOnlyExpressions) === true && (options === null || options === void 0 ? void 0 : options.casing)
? (0, openapi_format_1.changeCase)(tplValue, options === null || options === void 0 ? void 0 : options.casing)
: tplValue;
return casedTplValue ? casedTplValue : '';
});
if (options === null || options === void 0 ? void 0 : options.prefix) {
varName = options.prefix.trim() + varName;
}
if (options === null || options === void 0 ? void 0 : options.suffix) {
varName = varName + options.suffix.trim();
}
if ((options === null || options === void 0 ? void 0 : options.casing) && varName && (options === null || options === void 0 ? void 0 : options.caseOnlyExpressions) !== true) {
var placeholderRegex_1 = new RegExp("({{{?|{{)(.*?)(}}}?|}})", 'g');
varName = varName.replace(placeholderRegex_1, function (_, openingBraces, placeholder, closingBraces) {
if (options.casing) {
return "".concat(openingBraces).concat((0, openapi_format_1.changeCase)(placeholder.trim(), options.casing)).concat(closingBraces);
}
return "".concat(openingBraces).concat(placeholder).concat(closingBraces);
});
if (!varName.includes('{{') && !varName.includes('}}')) {
varName = (0, openapi_format_1.changeCase)(varName, options.casing);
}
}
return varName;
};
exports.parseTpl = parseTpl;
var hasTpl = function (template) {
if (!template)
return false;
var symbolsRegex = /<|>/;
return symbolsRegex.test(template);
};
exports.hasTpl = hasTpl;
//# sourceMappingURL=parseTpl.js.map