{"version":3,"file":"preview.mjs","sources":["../../../../../server/src/preview/controllers/validation/preview.ts"],"sourcesContent":["import { pick } from 'lodash/fp';\n\nimport type { Core, UID } from '@strapi/types';\nimport { z, validateZodSchema, errors } from '@strapi/utils';\n\nimport { Preview } from '../../../../../shared/contracts';\nimport type { HandlerParams } from '../../services/preview-config';\n\nconst getPreviewUrlSchema = z.object({\n  // Will be undefined for single types\n  documentId: z.string().optional(),\n  locale: z.string().nullable().optional(),\n  status: z.string().optional(),\n});\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  validateZodSchema(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","z","object","documentId","string","optional","locale","nullable","status","validatePreviewUrl","strapi","uid","params","validateZodSchema","newParams","pick","model","getModel","modelType","errors","ValidationError","isSingleType","kind","doc","documents","findFirst","NotFoundError","isDPEnabled","options","draftAndPublish"],"mappings":";;;AAQA,MAAMA,mBAAAA,GAA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