@contentstack/utils
Version:
Contentstack utilities for Javascript
63 lines • 2.55 kB
JavaScript
export function updateAssetURLForGQL(gqlResponse) {
try {
var response = gqlResponse === null || gqlResponse === void 0 ? void 0 : gqlResponse.data;
for (var contentType in response) {
if ("items" in response[contentType]) {
var entries = response[contentType].items;
entries.forEach(function (entry) {
processEntry(entry);
});
}
else {
processEntry(response[contentType]);
}
}
}
catch (error) {
console.error('Error in updating asset URL for GQL response', error);
}
}
function processEntry(entry) {
for (var field in entry) {
var fieldData = entry[field];
if (fieldData instanceof Array) {
fieldData.forEach(function (data) {
findRTEFieldAndUpdateURL(data);
});
}
else if (fieldData && typeof fieldData === 'object') {
findRTEFieldAndUpdateURL(fieldData);
}
}
}
function findRTEFieldAndUpdateURL(fieldData) {
var _a;
var rteField = findRTEField(fieldData);
if (!rteField)
return;
var edges = (_a = rteField === null || rteField === void 0 ? void 0 : rteField.embedded_itemsConnection) === null || _a === void 0 ? void 0 : _a.edges;
edges.forEach(function (edge) {
var _a, _b, _c;
var node = edge.node;
if ((node === null || node === void 0 ? void 0 : node.url) && (node === null || node === void 0 ? void 0 : node.filename)) {
if (!((_a = node === null || node === void 0 ? void 0 : node.system) === null || _a === void 0 ? void 0 : _a.uid))
throw new Error('Asset UID not found in the response');
var correspondingAsset = (_c = (_b = rteField === null || rteField === void 0 ? void 0 : rteField.json) === null || _b === void 0 ? void 0 : _b.children) === null || _c === void 0 ? void 0 : _c.find(function (child) { return child.attrs['asset-uid'] === node.system.uid; });
correspondingAsset.attrs['asset-link'] = node.url;
}
});
}
function findRTEField(fieldData) {
if (fieldData && fieldData.embedded_itemsConnection) {
return fieldData;
}
for (var key in fieldData) {
if (fieldData[key] && typeof fieldData[key] === 'object') {
var found = findRTEField(fieldData[key]);
if (found) {
return found;
}
}
}
}
//# sourceMappingURL=updateAssetURLForGQL.js.map