UNPKG

openapi-to-postmanv2

Version:

Convert a given OpenAPI specification to Postman Collection v2.0

97 lines 3.84 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.mergeRequestAndResponseBodyRaw = mergeRequestAndResponseBodyRaw; exports.mergeRequestBodyData = mergeRequestBodyData; const lodash_1 = __importDefault(require("lodash")); const shared_1 = require("../../shared"); /** * Deep merges two objects, preserving values from current when keys exist in both. * Adds keys from target that don't exist in source. * Removes keys from source that don't exist in target. * If source is an empty object {}, uses all keys from target. * @param {unknown} target - Target object (source of truth for structure) * @param {unknown} source - Source object (source of truth for values) * @returns {Record<string, unknown> | unknown} Merged object */ function deepMergeBodyObjects(target, source) { if (!lodash_1.default.isPlainObject(target) || !lodash_1.default.isPlainObject(source)) { return target; } // Cast to Record<string, unknown> after type guard const targetObj = target, sourceObj = source; if (Object.keys(sourceObj).length === 0) { return targetObj; } const result = {}; Object.keys(targetObj).forEach((key) => { if (Object.hasOwn(sourceObj, key)) { const targetValue = targetObj[key], sourceValue = sourceObj[key]; // Recursively merge if both values are objects if (lodash_1.default.isPlainObject(targetValue) && lodash_1.default.isPlainObject(sourceValue)) { result[key] = deepMergeBodyObjects(targetValue, sourceValue); } else if (lodash_1.default.isPlainObject(targetValue) && !lodash_1.default.isEmpty(targetValue)) { result[key] = targetValue; } else { result[key] = sourceValue; } } else { result[key] = targetObj[key]; } }); return result; } /** * Merges body.raw from target request into source request. * Preserves current values for existing keys, adds new keys from target, * and removes keys that don't exist in target. * @param {string} targetBodyRaw - Body.raw from the target request (JSON string) * @param {string} sourceBodyRaw - Body.raw from the source request (JSON string) * @returns {string} The merged body.raw as a JSON string */ function mergeRequestAndResponseBodyRaw(targetBodyRaw, sourceBodyRaw) { if (!targetBodyRaw) { return undefined; } if (!sourceBodyRaw) { return targetBodyRaw; } try { const targetBody = JSON.parse(targetBodyRaw), sourceBody = JSON.parse(sourceBodyRaw), merged = deepMergeBodyObjects(targetBody, sourceBody); return JSON.stringify(merged, null, shared_1.SPACE_COUNT); } catch (error) { return targetBodyRaw; } } /** * Merges body.raw data in request JSON * @param {RequestBodyDefinition | undefined} targetBody - Target request body * @param {RequestBodyDefinition | undefined} sourceBody - Source request body * @returns {RequestBodyDefinition | undefined} Merged request body */ function mergeRequestBodyData(targetBody, sourceBody) { if (!targetBody) { return undefined; } if (!sourceBody) { return targetBody; } if (targetBody.mode === 'raw' && sourceBody.mode === 'raw') { return { ...targetBody, raw: mergeRequestAndResponseBodyRaw(targetBody.raw, sourceBody.raw) }; } else if (targetBody.mode === 'raw') { return targetBody; } // Preserve current body when mode is not raw (formdata, urlencoded, file, graphql) return sourceBody; } //# sourceMappingURL=BodyMerger.js.map