UNPKG

@prismicio/mock

Version:

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

1 lines 2.99 kB
{"version":3,"file":"customType.cjs","names":["createFaker","capitalCase","snakeCase"],"sources":["../../src/model/customType.ts"],"sourcesContent":["import type * as prismic from \"@prismicio/client\"\n\nimport { capitalCase, snakeCase } from \"../lib/changeCase\"\nimport { createFaker } from \"../lib/createFaker\"\nimport type { MockModelConfig } from \"../types\"\n\nexport type MockCustomTypeModelConfig<\n\tDefinition extends prismic.CustomTypeModelTab | 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 prismic.CustomTypeModelTab | 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 prismic.CustomTypeModelTab | prismic.CustomTypeModelDefinition,\n>(\n\tconfig: MockCustomTypeModelConfig<Definition>,\n): MockCustomTypeModel<Definition> => {\n\tconst faker = config.faker || createFaker(config.seed)\n\n\tlet label: string = config.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.id) {\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"],"mappings":";;;AAsCA,MAAa,cAGZ,WACqC;CACrC,MAAM,QAAQ,OAAO,SAASA,oBAAAA,YAAY,OAAO,KAAK;CAEtD,IAAI,QAAgB,OAAO,SAASC,mBAAAA,YAAY,MAAM,MAAM,MAAM,MAAM,GAAG,EAAE,CAAC,CAAC;CAC/E,IAAI,KAAa,OAAO,MAAMC,mBAAAA,UAAU,MAAM;AAE9C,KAAI,OAAO,MAAM,CAAC,OAAO,MACxB,SAAQD,mBAAAA,YAAY,OAAO,GAAG;UACpB,OAAO,SAAS,CAAC,OAAO,GAClC,MAAKC,mBAAAA,UAAU,OAAO,MAAM;CAG7B,MAAM,SAAS,OAAO,UAAU,MAAM,cAAc,CAAC,QAAQ,SAAS,CAAC;CAEvE,IAAI,OAAO,EAAE;AAEb,KAAI,YAAY,UAAU,OAAO,OAChC,QAAO,EAAE,MAAM,OAAO,QAAQ;UACpB,UAAU,UAAU,OAAO,KACrC,QAAO,OAAO;AAGf,QAAO;EACN;EACA;EACA,QAAQ,OAAO,UAAU,MAAM,SAAS;EACxC,YAAY,OAAO,cAAc,MAAM,SAAS;EAChD;EACA;EACA"}