{"version":3,"file":"preview.mjs","sources":["../../../../../server/src/preview/controllers/validation/preview.ts"],"sourcesContent":["import * as yup from 'yup';\nimport { pick } from 'lodash/fp';\n\nimport type { Core, UID } from '@strapi/types';\nimport { validateYupSchema, errors } from '@strapi/utils';\n\nimport { Preview } from '../../../../../shared/contracts';\nimport type { HandlerParams } from '../../services/preview-config';\n\nconst getPreviewUrlSchema = yup\n  .object()\n  .shape({\n    // Will be undefined for single types\n    documentId: yup.string(),\n    locale: yup.string().nullable(),\n    status: yup.string(),\n  })\n  .required();\n\nexport const validatePreviewUrl = async (\n  strapi: Core.Strapi,\n  uid: UID.ContentType,\n  params: Preview.GetPreviewUrl.Request['query']\n): Promise<HandlerParams> => {\n  // Validate the request parameters format\n  await validateYupSchema(getPreviewUrlSchema)(params);\n\n  const newParams = pick(['documentId', 'locale', 'status'], params) as HandlerParams;\n  const model = strapi.getModel(uid);\n\n  // If it's not a collection type or single type\n  if (!model || model.modelType !== 'contentType') {\n    throw new errors.ValidationError('Invalid content type');\n  }\n\n  // Document id is not required for single types\n  const isSingleType = model?.kind === 'singleType';\n  if (!isSingleType && !params.documentId) {\n    throw new errors.ValidationError('documentId is required for Collection Types');\n  }\n\n  // Fill the documentId if it's a single type\n  if (isSingleType) {\n    const doc = await strapi.documents(uid).findFirst();\n\n    if (!doc) {\n      throw new errors.NotFoundError('Document not found');\n    }\n\n    newParams.documentId = doc?.documentId;\n  }\n\n  /**\n   * If status is not specified, follow the following rules:\n   * - D&P disabled: status is considered published\n   * - D&P enabled: status is considered draft\n   */\n  if (!newParams.status) {\n    const isDPEnabled = model?.options?.draftAndPublish;\n    newParams.status = isDPEnabled ? 'draft' : 'published';\n  }\n\n  return newParams;\n};\n"],"names":["getPreviewUrlSchema","yup","object","shape","documentId","string","locale","nullable","status","required","validatePreviewUrl","strapi","uid","params","validateYupSchema","newParams","pick","model","getModel","modelType","errors","ValidationError","isSingleType","kind","doc","documents","findFirst","NotFoundError","isDPEnabled","options","draftAndPublish"],"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