gatsby-source-prismic
Version:
Gatsby source plugin for building websites using Prismic as a data source
1 lines • 12.2 kB
Source Map (JSON)
{"version":3,"file":"pluginOptionsSchema.cjs","sources":["../../../src/gatsby-node/pluginOptionsSchema.ts"],"sourcesContent":["// TODO: Validate that all Shared Slices referenced in all Custom Type's Slice Zones are provided.\n//\n// This is necessary because we will be blindly referencing Shared Slice types within Slice Zones.\nimport * as prismic from \"@prismicio/client\";\nimport * as prismicCustomTypesClient from \"@prismicio/custom-types-client\";\nimport type { PluginOptionsSchemaArgs } from \"gatsby\";\nimport type { ObjectSchema } from \"gatsby-plugin-utils\";\n\nimport type { PluginOptions } from \"../types\";\n\nexport const pluginOptionsSchema = ({\n\tJoi,\n}: PluginOptionsSchemaArgs): ObjectSchema => {\n\treturn Joi.object({\n\t\trepositoryName: Joi.string().required(),\n\t\taccessToken: Joi.string().allow(\"\"),\n\t\tapiEndpoint: Joi.string(),\n\t\troutes: Joi.array().items(\n\t\t\tJoi.object({\n\t\t\t\ttype: Joi.string().required(),\n\t\t\t\tuid: Joi.string(),\n\t\t\t\tlang: Joi.string(),\n\t\t\t\tpath: Joi.string().required(),\n\t\t\t\tresolvers: Joi.object().pattern(Joi.string(), Joi.string().required()),\n\t\t\t}).required(),\n\t\t),\n\t\tlinkResolver: Joi.function().arity(1),\n\t\thtmlSerializer: Joi.alternatives(\n\t\t\tJoi.object().pattern(\n\t\t\t\tJoi.allow(...Object.keys(prismic.Element)),\n\t\t\t\tJoi.function(),\n\t\t\t),\n\t\t\tJoi.function(),\n\t\t),\n\t\tlang: Joi.string(),\n\t\tfetchLinks: Joi.array().items(Joi.string()),\n\t\tgraphQuery: Joi.string(),\n\t\tpredicates: Joi.alternatives(Joi.string(), Joi.array().items(Joi.string())),\n\n\t\treleaseID: Joi.string(),\n\t\treleaseLabel: Joi.string(),\n\n\t\ttypePrefix: Joi.string(),\n\n\t\tcustomTypesApiToken: Joi.string(),\n\t\tcustomTypesApiEndpoint: Joi.string(),\n\t\tschemas: Joi.object().pattern(Joi.string(), Joi.object().required()),\n\t\tcustomTypeModels: Joi.array().items(\n\t\t\tJoi.object({\n\t\t\t\tid: Joi.string().required(),\n\t\t\t\tjson: Joi.object().required(),\n\t\t\t}).unknown(),\n\t\t),\n\t\tsharedSliceModels: Joi.array().items(\n\t\t\tJoi.object({\n\t\t\t\tid: Joi.string().required(),\n\t\t\t\tvariations: Joi.array()\n\t\t\t\t\t.items(\n\t\t\t\t\t\tJoi.object({\n\t\t\t\t\t\t\tid: Joi.string().required(),\n\t\t\t\t\t\t\tprimary: Joi.object(),\n\t\t\t\t\t\t\titems: Joi.object(),\n\t\t\t\t\t\t}).unknown(),\n\t\t\t\t\t)\n\t\t\t\t\t.required(),\n\t\t\t}).unknown(),\n\t\t),\n\n\t\timageImgixParams: Joi.object().pattern(Joi.string(), Joi.any()),\n\t\timagePlaceholderImgixParams: Joi.object().pattern(Joi.string(), Joi.any()),\n\n\t\ttransformFieldName: Joi.function().arity(1),\n\t\tshouldDownloadFiles: Joi.alternatives(\n\t\t\tJoi.boolean(),\n\t\t\tJoi.function().arity(1),\n\t\t\tJoi.object().pattern(\n\t\t\t\tJoi.string(),\n\t\t\t\tJoi.alternatives(Joi.boolean(), Joi.function().arity(1)),\n\t\t\t),\n\t\t),\n\n\t\twebhookSecret: Joi.string(),\n\n\t\tfetch: Joi.function(),\n\t})\n\t\t.or(\"customTypesApiToken\", \"customTypeModels\", \"schemas\")\n\t\t.oxor(\"fetchLinks\", \"graphQuery\")\n\t\t.oxor(\"releaseID\", \"releaseLabel\")\n\t\t.external(async (options: Omit<PluginOptions, \"plugins\">) => {\n\t\t\tconst client = prismic.createClient(\n\t\t\t\toptions.apiEndpoint || options.repositoryName,\n\t\t\t\t{\n\t\t\t\t\taccessToken: options.accessToken,\n\t\t\t\t\tfetch: options.fetch || (await import(\"node-fetch\")).default,\n\t\t\t\t},\n\t\t\t);\n\n\t\t\tlet repository: prismic.Repository;\n\n\t\t\t// Check access to the Prismic repository.\n\t\t\ttry {\n\t\t\t\trepository = await client.getRepository();\n\t\t\t} catch (error) {\n\t\t\t\tif (error instanceof prismic.NotFoundError) {\n\t\t\t\t\tthrow new Joi.ValidationError(\n\t\t\t\t\t\t\"repositoryName\",\n\t\t\t\t\t\t[\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tmessage: `Could not access the \"${options.repositoryName}\" Prismic repository. Check that the \\`repositoryName\\` option is correct and try again.`,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t\toptions,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tif (error instanceof prismic.ForbiddenError) {\n\t\t\t\t\tif (options.accessToken) {\n\t\t\t\t\t\tthrow new Joi.ValidationError(\n\t\t\t\t\t\t\t\"accessToken\",\n\t\t\t\t\t\t\t[\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tmessage: `The provided accessToken for the \"${options.repositoryName}\" repository is incorrect. Check that the \\`accessToken\\` option is correct and try again.`,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\toptions,\n\t\t\t\t\t\t);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tthrow new Joi.ValidationError(\n\t\t\t\t\t\t\t\"accessToken\",\n\t\t\t\t\t\t\t[\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tmessage: `An access token is required for the \"${options.repositoryName}\" Prismic repository, but one was not given. Check that the \\`accessToken\\` option is correct and try again.`,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\toptions,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tthrow error;\n\t\t\t}\n\n\t\t\t// Check that the Release exists (if the plugin is\n\t\t\t// configured to query from one).\n\t\t\tif (options.releaseID || options.releaseLabel) {\n\t\t\t\tif (options.releaseID) {\n\t\t\t\t\tconst ref = repository.refs.find(\n\t\t\t\t\t\t(ref) => ref.id === options.releaseID,\n\t\t\t\t\t);\n\n\t\t\t\t\tif (!ref) {\n\t\t\t\t\t\tif (options.accessToken) {\n\t\t\t\t\t\t\tthrow new Joi.ValidationError(\n\t\t\t\t\t\t\t\t\"releaseID\",\n\t\t\t\t\t\t\t\t[\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\tmessage: `A Release with the ID \"${options.releaseID}\" could not be found. Check that the \\`releaseID\\` option is correct and try again. Also check that the access token has access to Releases.`,\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\toptions,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tthrow new Joi.ValidationError(\n\t\t\t\t\t\t\t\t\"releaseID\",\n\t\t\t\t\t\t\t\t[\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\tmessage: `A Release with the ID \"${options.releaseID}\" could not be found. Check that the \\`releaseID\\` option is correct and try again. You may also need to provide an access token to query Releases.`,\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\toptions,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t} else if (options.releaseLabel) {\n\t\t\t\t\tconst ref = repository.refs.find(\n\t\t\t\t\t\t(ref) => ref.label === options.releaseLabel,\n\t\t\t\t\t);\n\n\t\t\t\t\tif (!ref) {\n\t\t\t\t\t\tif (options.accessToken) {\n\t\t\t\t\t\t\tthrow new Joi.ValidationError(\n\t\t\t\t\t\t\t\t\"releaseLabel\",\n\t\t\t\t\t\t\t\t[\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\tmessage: `A Release with the label \"${options.releaseLabel}\" could not be found. Check that the \\`releaseLabel\\` option is correct and try again. Also check that the access token has access to Releases.`,\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\toptions,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tthrow new Joi.ValidationError(\n\t\t\t\t\t\t\t\t\"releaseLabel\",\n\t\t\t\t\t\t\t\t[\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\tmessage: `A Release with the label \"${options.releaseLabel}\" could not be found. Check that the \\`releaseLabel\\` option is correct and try again. You may also need to provide an access token to query Releases.`,\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\toptions,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Check access to the Custom Types API (if the plugin\n\t\t\t// is configured to fetch models from it).\n\t\t\tif (options.customTypesApiToken) {\n\t\t\t\tconst client = prismicCustomTypesClient.createClient({\n\t\t\t\t\trepositoryName: options.repositoryName,\n\t\t\t\t\tendpoint: options.customTypesApiEndpoint,\n\t\t\t\t\ttoken: options.customTypesApiToken,\n\t\t\t\t\tfetch: options.fetch || (await import(\"node-fetch\")).default,\n\t\t\t\t});\n\n\t\t\t\ttry {\n\t\t\t\t\tawait client.getAllCustomTypes();\n\t\t\t\t} catch (error) {\n\t\t\t\t\tif (error instanceof prismicCustomTypesClient.ForbiddenError) {\n\t\t\t\t\t\tthrow new Joi.ValidationError(\n\t\t\t\t\t\t\t\"customTypesApiToken\",\n\t\t\t\t\t\t\t[\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tmessage: `The provided Custom Types API token is incorrect. Check that the \\`customTypesApiToken\\` option is correct and try again.`,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\toptions,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow error;\n\t\t\t\t}\n\t\t\t}\n\t\t});\n};\n"],"names":["prismic","ref","client","prismicCustomTypesClient"],"mappings":";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