UNPKG

gatsby-source-prismic

Version:

Gatsby source plugin for building websites using Prismic as a data source

1 lines 4.97 kB
{"version":3,"file":"resolveModels.cjs","sources":["../../../src/lib/resolveModels.ts"],"sourcesContent":["import * as prismicCustomTypesClient from \"@prismicio/custom-types-client\";\nimport type { CustomTypeModel, SharedSliceModel } from \"@prismicio/client\";\n\nimport type { PluginOptions } from \"../types\";\n\ntype GetModelsArgs = {\n\tpluginOptions: PluginOptions;\n};\n\n/**\n * Collects all Custom Type and Shared Slice models. Models may come from plugin\n * options or the Custom Types API.\n *\n * @param args - Arguments for `resolveModels`.\n *\n * @returns A collection of Custom Type and Shared Slice models.\n */\nexport const resolveModels = async (\n\targs: GetModelsArgs,\n): Promise<{\n\tcustomTypeModels: CustomTypeModel[];\n\tsharedSliceModels: SharedSliceModel[];\n}> => {\n\t// These \"keyed\" records store the resulting models. By using the\n\t// models' IDs as keys, we can easily manage precedence by overwriting\n\t// a ID's model.\n\tconst keyedCustomTypeModels: Record<string, CustomTypeModel> = {};\n\tconst keyedSharedSliceModels: Record<string, SharedSliceModel> = {};\n\n\t// If a Custom Type API token is provided, fetch all Custom Type and\n\t// Shared Slice models. They will be used as the lowest priority\n\t// models. If any models are given locally as a plugin option, they\n\t// will take priority over models fetched from the Custom Types API.\n\tif (args.pluginOptions.customTypesApiToken) {\n\t\tconst client = prismicCustomTypesClient.createClient({\n\t\t\trepositoryName: args.pluginOptions.repositoryName,\n\t\t\ttoken: args.pluginOptions.customTypesApiToken,\n\t\t\tfetch: args.pluginOptions.fetch || (await import(\"node-fetch\")).default,\n\t\t\tendpoint: args.pluginOptions.customTypesApiEndpoint,\n\t\t});\n\n\t\tconst [customTypeModels, sharedSliceModels] = await Promise.all([\n\t\t\tclient.getAllCustomTypes(),\n\t\t\tclient.getAllSharedSlices(),\n\t\t]);\n\n\t\tfor (const customTypeModel of customTypeModels) {\n\t\t\tkeyedCustomTypeModels[customTypeModel.id] = customTypeModel;\n\t\t}\n\n\t\tfor (const sharedSliceModel of sharedSliceModels) {\n\t\t\tkeyedSharedSliceModels[sharedSliceModel.id] = sharedSliceModel;\n\t\t}\n\t}\n\n\t// If Custom Type definitions are provided, add them to the result.\n\t// They must be converted from a JSON definition to a full Custom Type\n\t// model (Model = Metadata + JSON Definition).\n\tif (args.pluginOptions.schemas) {\n\t\tfor (const id in args.pluginOptions.schemas) {\n\t\t\tconst customTypeModel: CustomTypeModel = {\n\t\t\t\tid,\n\t\t\t\tjson: args.pluginOptions.schemas[id],\n\t\t\t\t// The following values are \"fake\". They are\n\t\t\t\t// not part of the given definition so we must\n\t\t\t\t// fill them in to promote the type to a Custom\n\t\t\t\t// Type model.\n\t\t\t\tlabel: \"label\",\n\t\t\t\tstatus: true,\n\t\t\t\trepeatable: true,\n\t\t\t};\n\n\t\t\tkeyedCustomTypeModels[customTypeModel.id] = customTypeModel;\n\t\t}\n\t}\n\n\t// If Custom Type models are provided, add them to the result. They\n\t// take priority over anything that was added before this block.\n\tif (args.pluginOptions.customTypeModels) {\n\t\tfor (const customTypeModel of args.pluginOptions.customTypeModels) {\n\t\t\tkeyedCustomTypeModels[customTypeModel.id] = customTypeModel;\n\t\t}\n\t}\n\n\t// If Shared Slice models are provided, add them to the result. They\n\t// take priority over anything that was added before this block.\n\tif (args.pluginOptions.sharedSliceModels) {\n\t\tfor (const sharedSliceModel of args.pluginOptions.sharedSliceModels) {\n\t\t\tkeyedSharedSliceModels[sharedSliceModel.id] = sharedSliceModel;\n\t\t}\n\t}\n\n\treturn {\n\t\tcustomTypeModels: Object.values(keyedCustomTypeModels),\n\t\tsharedSliceModels: Object.values(keyedSharedSliceModels),\n\t};\n};\n"],"names":["prismicCustomTypesClient"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiBa,MAAA,gBAAgB,OAC5B,SAII;AAIJ,QAAM,wBAAyD,CAAA;AAC/D,QAAM,yBAA2D,CAAA;AAM7D,MAAA,KAAK,cAAc,qBAAqB;AACrC,UAAA,SAAS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