UNPKG

@prismicio/mock

Version:

Generate mock Prismic documents, fields, Slices, and models for development and testing environments

77 lines (76 loc) 2.61 kB
import * as prismic from "@prismicio/client"; import { valueForModel } from "./valueForModel.js"; import { createFaker } from "./createFaker.js"; const getValueConfigType = (model) => { var _a; switch (model.type) { case prismic.CustomTypeModelFieldType.Boolean: return "boolean"; case prismic.CustomTypeModelFieldType.Color: return "color"; case prismic.CustomTypeModelFieldType.Date: return "date"; case prismic.CustomTypeModelFieldType.Embed: return "embed"; case prismic.CustomTypeModelFieldType.GeoPoint: return "geoPoint"; case prismic.CustomTypeModelFieldType.Group: return "group"; case prismic.CustomTypeModelFieldType.Image: return "image"; case prismic.CustomTypeModelFieldType.Link: { switch ((_a = model.config) == null ? void 0 : _a.select) { case prismic.CustomTypeModelLinkSelectType.Document: return "contentRelationship"; case prismic.CustomTypeModelLinkSelectType.Media: return "linkToMedia"; default: return "link"; } } case prismic.CustomTypeModelFieldType.Number: return "number"; case prismic.CustomTypeModelFieldType.Select: return "select"; case prismic.CustomTypeModelFieldType.StructuredText: { if (model.config && "single" in model.config && model.config.single && model.config.single.split(",").every((element) => /heading{1,6}/.test(element.trim()))) { return "title"; } else { return "richText"; } } case prismic.CustomTypeModelFieldType.Text: return "keyText"; case prismic.CustomTypeModelFieldType.Timestamp: return "timestamp"; case prismic.CustomTypeModelFieldType.UID: return "uid"; case prismic.CustomTypeModelFieldType.Integration: return "integration"; case prismic.CustomTypeModelFieldType.Slices: return "sliceZone"; default: { throw new Error(`The "${model.type}" field type is not supported in @prismicio/mock.`); } } }; const valueForModelMap = (config) => { var _a; const faker = config.faker || createFaker(config.seed); const result = {}; for (const fieldId in config.map) { const fieldModel = config.map[fieldId]; const fieldConfigType = getValueConfigType(fieldModel); const fieldConfig = (_a = config.configs) == null ? void 0 : _a[fieldConfigType]; result[fieldId] = valueForModel({ faker, model: fieldModel, config: fieldConfig }); } return result; }; export { valueForModelMap }; //# sourceMappingURL=valueForModelMap.js.map