UNPKG

@prismicio/mock

Version:

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

1 lines 3.14 kB
{"version":3,"file":"customType.cjs","sources":["../../../src/model/customType.ts"],"sourcesContent":["import * as prismic from \"@prismicio/client\";\n\nimport { capitalCase, snakeCase } from \"../lib/changeCase\";\nimport { createFaker } from \"../lib/createFaker\";\n\nimport { MockModelConfig } from \"../types\";\n\nexport type MockCustomTypeModelConfig<\n\tDefinition extends\n\t\t| prismic.CustomTypeModelTab\n\t\t| prismic.CustomTypeModelDefinition =\n\t\t| prismic.CustomTypeModelTab\n\t\t| prismic.CustomTypeModelDefinition,\n> = {\n\tid?: string;\n\tlabel?: string;\n\tstatus?: boolean;\n\trepeatable?: boolean;\n\tformat?: prismic.CustomTypeModel[\"format\"];\n} & (\n\t| {\n\t\t\tfields?: Definition;\n\t\t\ttabs?: never;\n\t }\n\t| {\n\t\t\ttabs?: Definition;\n\t\t\tfields?: never;\n\t }\n) &\n\tMockModelConfig;\n\ntype MockCustomTypeModel<\n\tDefinition extends\n\t\t| prismic.CustomTypeModelTab\n\t\t| prismic.CustomTypeModelDefinition,\n> = (Definition extends prismic.CustomTypeModelTab\n\t? prismic.CustomTypeModel<string, Record<\"Main\", Definition>>\n\t: Definition extends prismic.CustomTypeModelDefinition\n\t\t? prismic.CustomTypeModel<string, Definition>\n\t\t: never) & {\n\tlabel: NonNullable<prismic.CustomTypeModel[\"label\"]>;\n};\n\nexport const customType = <\n\tDefinition extends\n\t\t| prismic.CustomTypeModelTab\n\t\t| prismic.CustomTypeModelDefinition,\n>(\n\tconfig: MockCustomTypeModelConfig<Definition>,\n): MockCustomTypeModel<Definition> => {\n\tconst faker = config.faker || createFaker(config.seed);\n\n\tlet label: string =\n\t\tconfig.label || capitalCase(faker.words(faker.range(1, 2)));\n\tlet id: string = config.id || snakeCase(label);\n\n\tif (config.id && !config.label) {\n\t\tlabel = capitalCase(config.id);\n\t} else if (config.label && !config.label) {\n\t\tid = snakeCase(config.label);\n\t}\n\n\tconst format = config.format ?? faker.randomElement([\"page\", \"custom\"]);\n\n\tlet json = {} as prismic.CustomTypeModelDefinition;\n\n\tif (\"fields\" in config && config.fields) {\n\t\tjson = { Main: config.fields } as typeof json;\n\t} else if (\"tabs\" in config && config.tabs) {\n\t\tjson = config.tabs as typeof json;\n\t}\n\n\treturn {\n\t\tid,\n\t\tlabel,\n\t\tstatus: config.status ?? faker.boolean(),\n\t\trepeatable: config.repeatable ?? faker.boolean(),\n\t\tformat,\n\t\tjson,\n\t} as MockCustomTypeModel<Definition>;\n};\n"],"names":["createFaker","capitalCase","snakeCase"],"mappings":";;;;AA2Ca,MAAA,aAAa,CAKzB,WACoC;AACpC,QAAM,QAAQ,OAAO,SAASA,YAAAA,YAAY,OAAO,IAAI;AAEjD,MAAA,QACH,OAAO,SAASC,WAAY,YAAA,MAAM,MAAM,MAAM,MAAM,GAAG,CAAC,CAAC,CAAC;AAC3D,MAAI,KAAa,OAAO,MAAMC,WAAA,UAAU,KAAK;AAE7C,MAAI,OAAO,MAAM,CAAC,OAAO,OAAO;AACvB,YAAAD,WAAA,YAAY,OAAO,EAAE;AAAA,EACnB,WAAA,OAAO,SAAS,CAAC,OAAO,OAAO;AACpC,SAAAC,WAAA,UAAU,OAAO,KAAK;AAAA,EAC5B;AAEM,QAAA,SAAS,OAAO,UAAU,MAAM,cAAc,CAAC,QAAQ,QAAQ,CAAC;AAEtE,MAAI,OAAO,CAAA;AAEP,MAAA,YAAY,UAAU,OAAO,QAAQ;AACjC,WAAA,EAAE,MAAM,OAAO;EACZ,WAAA,UAAU,UAAU,OAAO,MAAM;AAC3C,WAAO,OAAO;AAAA,EACf;AAEO,SAAA;AAAA,IACN;AAAA,IACA;AAAA,IACA,QAAQ,OAAO,UAAU,MAAM,QAAS;AAAA,IACxC,YAAY,OAAO,cAAc,MAAM,QAAS;AAAA,IAChD;AAAA,IACA;AAAA,EAAA;AAEF;;"}