@stackbit/sdk
Version:
102 lines • 4.22 kB
TypeScript
import { Field, FieldListItems, FieldModelProps } from '@stackbit/types';
import { Model, YamlModel } from '../config/config-types';
/**
* This function invokes the `iteratee` function for every field of the `model`.
* It recursively traverses through fields of type `object` and `list` with
* items of type `object` and invokes the `iteratee` on their child fields,
* and so on. The traversal is a depth-first and the `iteratee` is invoked
* before traversing the field's child fields.
*
* The iteratee is invoked with two parameters, `field` and `fieldPath`. The
* `field` is the currently iterated field, and `fieldPath` is an array of
* strings indicating the path of the `field` relative to the model.
*
* @example
* model = {
* fields: [
* { name: "title", type: "string" },
* { name: "tags", type: "list" },
* { name: "banner", type: "object", fields: [{ name: "logo", type: "image" }] }
* {
* name: "actions",
* type: "list",
* items: { type: "object", fields: [{ name: "label", type: "string" }] }
* }
* ]
* }
* iterateModelFieldsRecursively(model, iteratee);
* // will call the iteratee 6 times with the following "field" and "fieldPath" arguments
* field = { name: "title", ... }, fieldPath = ['fields', 'title']
* field = { name: "tags", ... }, fieldPath = ['fields', 'tags']
* field = { name: "banner", ... }, fieldPath = ['fields', 'banner']
* field = { name: "logo", ... }, fieldPath = ['fields', 'banner', 'fields', 'logo']
* field = { name: "actions", ... }, fieldPath = ['fields', 'actions']
* field = { name: "label", ... }, fieldPath = ['fields', 'actions', 'items', 'fields', 'label']
*
* @param model The model to iterate fields
* @param iteratee The callback function
*/
export declare function iterateModelFieldsRecursively(model: Model | YamlModel, iteratee: (field: Field, modelKeyPath: string[]) => void): void;
export declare function mapModelFieldsRecursively<T extends Model>(model: T, iteratee: (field: Field, modelKeyPath: string[]) => Field): T;
export declare function iterateObjectFieldsWithModelRecursively(value: any, model: Model, modelsByName: Record<string, Model>, iteratee: (options: {
value: any;
model: Model | null;
field: Field | null;
fieldListItem: FieldListItems | null;
error: string | null;
valueKeyPath: (string | number)[];
modelKeyPath: string[];
objectStack: any[];
}) => void, { pageLayoutKey, objectTypeKey, valueId }?: {
pageLayoutKey?: string;
objectTypeKey?: string;
valueId?: string;
}): void;
export declare function mapObjectFieldsWithModelRecursively(value: any, model: Model, modelsByName: Record<string, Model>, iteratee: (options: {
value: any;
model: Model | null;
field: Field | null;
fieldListItem: FieldListItems | null;
error: string | null;
valueKeyPath: (string | number)[];
modelKeyPath: string[];
objectStack: any[];
}) => any, { pageLayoutKey, objectTypeKey, valueId }?: {
pageLayoutKey?: string;
objectTypeKey?: string;
valueId?: string;
}): any;
export declare function asyncMapObjectFieldsWithModelRecursively({ value, model, modelsByName, iteratee, pageLayoutKey, objectTypeKey, valueId }: {
value: any;
model: Model;
modelsByName: Record<string, Model>;
iteratee: (options: {
value: any;
model: Model | null;
field: Field | null;
fieldListItem: FieldListItems | null;
error: string | null;
valueKeyPath: (string | number)[];
modelKeyPath: string[];
objectStack: any[];
skipNested: () => void;
}) => Promise<any>;
pageLayoutKey?: string;
objectTypeKey?: string;
valueId?: string;
}): Promise<any>;
export declare function getModelOfObject({ object, field, modelsByName, pageLayoutKey, objectTypeKey, valueKeyPath, modelKeyPath }: {
object: any;
field: FieldModelProps;
modelsByName: Record<string, Model>;
pageLayoutKey: string;
objectTypeKey: string;
valueKeyPath: (string | number)[];
modelKeyPath: string[];
}): {
modelName: string;
model: Model;
} | {
error: string;
};
//# sourceMappingURL=model-iterators.d.ts.map