@datocms/cma-client
Version:
JS client for DatoCMS REST Content Management API
157 lines • 11 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { isItemWithOptionalIdAndMeta, } from '../fieldTypes';
import { nonRecursiveFilterBlocksInNonLocalizedFieldValueAsync, nonRecursiveFindAllBlocksInNonLocalizedFieldValueAsync, nonRecursiveMapBlocksInNonLocalizedFieldValueAsync, nonRecursiveReduceBlocksInNonLocalizedFieldValueAsync, nonRecursiveSomeBlocksInNonLocalizedFieldValueAsync, nonRecursiveVisitBlocksInNonLocalizedFieldValueAsync, } from './nonRecursiveBlocks';
export function visitBlocksInNonLocalizedFieldValue(nonLocalizedFieldValue, fieldType, schemaRepository, visitor, path = []) {
return __awaiter(this, void 0, void 0, function* () {
yield nonRecursiveVisitBlocksInNonLocalizedFieldValueAsync(fieldType, nonLocalizedFieldValue, (block, innerPath) => __awaiter(this, void 0, void 0, function* () {
yield visitor(block, [...path, ...innerPath]);
if (!isItemWithOptionalIdAndMeta(block)) {
return;
}
const itemType = yield schemaRepository.getRawItemTypeById(block.relationships.item_type.data.id);
const fields = yield schemaRepository.getRawItemTypeFields(itemType);
for (const field of fields) {
yield visitBlocksInNonLocalizedFieldValue(block.attributes[field.attributes.api_key], field.attributes.field_type, schemaRepository, visitor, [...path, ...innerPath, 'attributes', field.attributes.api_key]);
}
}));
});
}
export function findAllBlocksInNonLocalizedFieldValue(nonLocalizedFieldValue, fieldType, schemaRepository, predicate, path = []) {
return __awaiter(this, void 0, void 0, function* () {
const results = [];
const directMatches = yield nonRecursiveFindAllBlocksInNonLocalizedFieldValueAsync(fieldType, nonLocalizedFieldValue, (block, innerPath) => __awaiter(this, void 0, void 0, function* () { return yield predicate(block, [...path, ...innerPath]); }));
results.push(...directMatches.map(({ item, path: innerPath }) => ({
item,
path: [...path, ...innerPath],
})));
yield nonRecursiveVisitBlocksInNonLocalizedFieldValueAsync(fieldType, nonLocalizedFieldValue, (block, innerPath) => __awaiter(this, void 0, void 0, function* () {
if (!isItemWithOptionalIdAndMeta(block)) {
return;
}
const itemType = yield schemaRepository.getRawItemTypeById(block.relationships.item_type.data.id);
const fields = yield schemaRepository.getRawItemTypeFields(itemType);
for (const field of fields) {
const nestedResults = yield findAllBlocksInNonLocalizedFieldValue(block.attributes[field.attributes.api_key], field.attributes.field_type, schemaRepository, predicate, [...path, ...innerPath, 'attributes', field.attributes.api_key]);
results.push(...nestedResults);
}
}));
return results;
});
}
export function filterBlocksInNonLocalizedFieldValue(nonLocalizedFieldValue, fieldType, schemaRepository, predicate, options = {}, path = []) {
return __awaiter(this, void 0, void 0, function* () {
const { traversalDirection = 'top-down' } = options;
const mapperFunc = (block, innerPath) => __awaiter(this, void 0, void 0, function* () {
const blockPath = [...path, ...innerPath];
if (!isItemWithOptionalIdAndMeta(block)) {
return block;
}
const itemType = yield schemaRepository.getRawItemTypeById(block.relationships.item_type.data.id);
const fields = yield schemaRepository.getRawItemTypeFields(itemType);
if (traversalDirection === 'top-down') {
const blockCopy = Object.assign(Object.assign({}, block), { attributes: Object.assign({}, block.attributes) });
for (const field of fields) {
blockCopy.attributes[field.attributes.api_key] =
yield filterBlocksInNonLocalizedFieldValue(blockCopy.attributes[field.attributes.api_key], field.attributes.field_type, schemaRepository, predicate, options, [...blockPath, 'attributes', field.attributes.api_key]);
}
return blockCopy;
}
const blockCopy = Object.assign(Object.assign({}, block), { attributes: Object.assign({}, block.attributes) });
for (const field of fields) {
blockCopy.attributes[field.attributes.api_key] =
yield filterBlocksInNonLocalizedFieldValue(blockCopy.attributes[field.attributes.api_key], field.attributes.field_type, schemaRepository, predicate, options, [...blockPath, 'attributes', field.attributes.api_key]);
}
return blockCopy;
});
const mappedValue = yield nonRecursiveMapBlocksInNonLocalizedFieldValueAsync(fieldType, nonLocalizedFieldValue, mapperFunc);
return nonRecursiveFilterBlocksInNonLocalizedFieldValueAsync(fieldType, mappedValue, (block, innerPath) => __awaiter(this, void 0, void 0, function* () {
const blockPath = [...path, ...innerPath];
return yield predicate(block, blockPath);
}));
});
}
export function reduceBlocksInNonLocalizedFieldValue(nonLocalizedFieldValue, fieldType, schemaRepository, reducer, initialValue, path = []) {
return __awaiter(this, void 0, void 0, function* () {
let accumulator = yield nonRecursiveReduceBlocksInNonLocalizedFieldValueAsync(fieldType, nonLocalizedFieldValue, (acc, block, innerPath) => __awaiter(this, void 0, void 0, function* () { return yield reducer(acc, block, [...path, ...innerPath]); }), initialValue);
yield nonRecursiveVisitBlocksInNonLocalizedFieldValueAsync(fieldType, nonLocalizedFieldValue, (block, innerPath) => __awaiter(this, void 0, void 0, function* () {
if (!isItemWithOptionalIdAndMeta(block)) {
return;
}
const itemType = yield schemaRepository.getRawItemTypeById(block.relationships.item_type.data.id);
const fields = yield schemaRepository.getRawItemTypeFields(itemType);
for (const field of fields) {
accumulator = yield reduceBlocksInNonLocalizedFieldValue(block.attributes[field.attributes.api_key], field.attributes.field_type, schemaRepository, reducer, accumulator, [...path, ...innerPath, 'attributes', field.attributes.api_key]);
}
}));
return accumulator;
});
}
export function someBlocksInNonLocalizedFieldValue(nonLocalizedFieldValue, fieldType, schemaRepository, predicate, path = []) {
return __awaiter(this, void 0, void 0, function* () {
const directMatch = yield nonRecursiveSomeBlocksInNonLocalizedFieldValueAsync(fieldType, nonLocalizedFieldValue, (block, innerPath) => __awaiter(this, void 0, void 0, function* () { return yield predicate(block, [...path, ...innerPath]); }));
if (directMatch) {
return true;
}
let found = false;
yield nonRecursiveVisitBlocksInNonLocalizedFieldValueAsync(fieldType, nonLocalizedFieldValue, (block, innerPath) => __awaiter(this, void 0, void 0, function* () {
if (found || !isItemWithOptionalIdAndMeta(block)) {
return;
}
const itemType = yield schemaRepository.getRawItemTypeById(block.relationships.item_type.data.id);
const fields = yield schemaRepository.getRawItemTypeFields(itemType);
for (const field of fields) {
if (found)
break;
const nestedMatch = yield someBlocksInNonLocalizedFieldValue(block.attributes[field.attributes.api_key], field.attributes.field_type, schemaRepository, predicate, [...path, ...innerPath, 'attributes', field.attributes.api_key]);
if (nestedMatch) {
found = true;
}
}
}));
return found;
});
}
export function everyBlockInNonLocalizedFieldValue(nonLocalizedFieldValue, fieldType, schemaRepository, predicate, path = []) {
return __awaiter(this, void 0, void 0, function* () {
return !(yield someBlocksInNonLocalizedFieldValue(nonLocalizedFieldValue, fieldType, schemaRepository, (item, path) => __awaiter(this, void 0, void 0, function* () { return !(yield predicate(item, path)); }), path));
});
}
export function mapBlocksInNonLocalizedFieldValue(nonLocalizedFieldValue, fieldType, schemaRepository, mapper, options = {}, path = []) {
return __awaiter(this, void 0, void 0, function* () {
const { traversalDirection = 'top-down' } = options;
return nonRecursiveMapBlocksInNonLocalizedFieldValueAsync(fieldType, nonLocalizedFieldValue, (block, innerPath) => __awaiter(this, void 0, void 0, function* () {
const blockPath = [...path, ...innerPath];
if (!isItemWithOptionalIdAndMeta(block)) {
return yield mapper(block, blockPath);
}
const itemType = yield schemaRepository.getRawItemTypeById(block.relationships.item_type.data.id);
const fields = yield schemaRepository.getRawItemTypeFields(itemType);
if (traversalDirection === 'top-down') {
const newBlock = yield mapper(block, blockPath);
if (!isItemWithOptionalIdAndMeta(newBlock)) {
return newBlock;
}
for (const field of fields) {
newBlock.attributes[field.attributes.api_key] =
yield mapBlocksInNonLocalizedFieldValue(newBlock.attributes[field.attributes.api_key], field.attributes.field_type, schemaRepository, mapper, options, [...blockPath, 'attributes', field.attributes.api_key]);
}
return newBlock;
}
const blockCopy = Object.assign({}, block);
for (const field of fields) {
blockCopy.attributes[field.attributes.api_key] =
yield mapBlocksInNonLocalizedFieldValue(blockCopy.attributes[field.attributes.api_key], field.attributes.field_type, schemaRepository, mapper, options, [...blockPath, 'attributes', field.attributes.api_key]);
}
return yield mapper(blockCopy, blockPath);
}));
});
}
//# sourceMappingURL=recursiveBlocks.js.map