{"version":3,"file":"transform.mjs","sources":["../../../src/core-api/controller/transform.ts"],"sourcesContent":["import { isNil, isPlainObject } from 'lodash/fp';\nimport type { UID, Struct, Data } from '@strapi/types';\nimport { async } from '@strapi/utils';\n\ntype TransformedEntry = {\n  id: string;\n  documentId?: Data.DocumentID | null;\n  attributes: Record<string, unknown>;\n};\n\ntype TransformedComponent = {\n  id: string;\n  [key: string]: unknown;\n};\n\ntype Entry = {\n  id: string;\n  documentId: Data.DocumentID | null;\n  [key: string]: Entry | Entry[] | string | number | null | boolean | Date;\n};\n\nfunction isEntry(property: unknown): property is Entry | Entry[] {\n  return property === null || isPlainObject(property) || Array.isArray(property);\n}\n\nfunction isDZEntries(property: unknown): property is (Entry & { __component: UID.Component })[] {\n  return Array.isArray(property);\n}\n\ninterface TransformOptions {\n  contentType?: Struct.ContentTypeSchema | Struct.ComponentSchema;\n  /**\n   * @deprecated this option is deprecated and will be removed in the next major version\n   */\n  useJsonAPIFormat?: boolean;\n  encodeSourceMaps?: boolean;\n}\n\nconst transformResponse = async (\n  resource: any,\n  meta: unknown = {},\n  opts: TransformOptions = {\n    useJsonAPIFormat: false,\n    encodeSourceMaps: false,\n  }\n) => {\n  if (isNil(resource)) {\n    return resource;\n  }\n\n  if (!isPlainObject(resource) && !Array.isArray(resource)) {\n    throw new Error('Entry must be an object or an array of objects');\n  }\n\n  // Transform pipeline functions\n  const applyJsonApiFormat = async (data: any) =>\n    opts.useJsonAPIFormat ? transformEntry(data, opts?.contentType) : data;\n\n  const applySourceMapEncoding = async (data: any) =>\n    opts.encodeSourceMaps && opts.contentType\n      ? strapi.get('content-source-maps').encodeSourceMaps({ data, schema: opts.contentType })\n      : data;\n\n  // Process data through transformation pipeline\n  const data = await async.pipe(applyJsonApiFormat, applySourceMapEncoding)(resource);\n\n  return {\n    data,\n    meta,\n  };\n};\n\nfunction transformComponent<T extends Entry | Entry[] | null>(\n  data: T,\n  component: Struct.ComponentSchema\n): T extends Entry[] ? TransformedComponent[] : T extends Entry ? TransformedComponent : null;\nfunction transformComponent(\n  data: Entry | Entry[] | null,\n  component: Struct.ComponentSchema\n): TransformedComponent | TransformedComponent[] | null {\n  if (Array.isArray(data)) {\n    return data.map((datum) => transformComponent(datum, component));\n  }\n\n  const res = transformEntry(data, component);\n\n  if (isNil(res)) {\n    return res;\n  }\n\n  const { id, attributes } = res;\n  return { id, ...attributes };\n}\n\nfunction transformEntry<T extends Entry | Entry[] | null>(\n  entry: T,\n  type?: Struct.Schema\n): T extends Entry[] ? TransformedEntry[] : T extends Entry ? TransformedEntry : null;\nfunction transformEntry(\n  entry: Entry | Entry[] | null,\n  type?: Struct.Schema\n): TransformedEntry | TransformedEntry[] | null {\n  if (isNil(entry)) {\n    return entry;\n  }\n\n  if (Array.isArray(entry)) {\n    return entry.map((singleEntry) => transformEntry(singleEntry, type));\n  }\n\n  if (!isPlainObject(entry)) {\n    throw new Error('Entry must be an object');\n  }\n\n  const { id, documentId, ...properties } = entry;\n\n  const attributeValues: Record<string, unknown> = {};\n\n  for (const key of Object.keys(properties)) {\n    const property = properties[key];\n    const attribute = type && type.attributes[key];\n\n    if (attribute && attribute.type === 'relation' && isEntry(property) && 'target' in attribute) {\n      const data = transformEntry(property, strapi.contentType(attribute.target));\n\n      attributeValues[key] = { data };\n    } else if (attribute && attribute.type === 'component' && isEntry(property)) {\n      attributeValues[key] = transformComponent(property, strapi.components[attribute.component]);\n    } else if (attribute && attribute.type === 'dynamiczone' && isDZEntries(property)) {\n      if (isNil(property)) {\n        attributeValues[key] = property;\n      }\n\n      attributeValues[key] = property.map((subProperty) => {\n        return transformComponent(subProperty, strapi.components[subProperty.__component]);\n      });\n    } else if (attribute && attribute.type === 'media' && isEntry(property)) {\n      const data = transformEntry(property, strapi.contentType('plugin::upload.file'));\n\n      attributeValues[key] = { data };\n    } else {\n      attributeValues[key] = property;\n    }\n  }\n\n  return {\n    id,\n    documentId,\n    attributes: attributeValues,\n  };\n}\n\nexport { transformResponse };\n"],"names":["isEntry","property","isPlainObject","Array","isArray","isDZEntries","transformResponse","resource","meta","opts","useJsonAPIFormat","encodeSourceMaps","isNil","Error","applyJsonApiFormat","data","transformEntry","contentType","applySourceMapEncoding","strapi","get","schema","async","pipe","transformComponent","component","map","datum","res","id","attributes","entry","type","singleEntry","documentId","properties","attributeValues","key","Object","keys","attribute","target","components","subProperty","__component"],"mappings":";;;AAqBA,SAASA,QAAQC,QAAiB,EAAA;AAChC,IAAA,OAAOA,aAAa,IAAA,IAAQC,aAAAA,CAAcD,QAAAA,CAAAA,IAAaE,KAAAA,CAAMC,OAAO,CAACH,QAAAA,CAAAA;AACvE;AAEA,SAASI,YAAYJ,QAAiB,EAAA;IACpC,OAAOE,KAAAA,CAAMC,OAAO,CAACH,QAAAA,CAAAA;AACvB;AAWA,MAAMK,oBAAoB,OACxBC,QAAAA,EACAC,OAAgB,EAAE,EAClBC,IAAAA,GAAyB;IACvBC,gBAAAA,EAAkB,KAAA;IAClBC,gBAAAA,EAAkB;AACpB,CAAC,GAAA;AAED,IAAA,IAAIC,MAAML,QAAAA,CAAAA,EAAW;QACnB,OAAOA,QAAAA;AACT,IAAA;AAEA,IAAA,IAAI,CAACL,aAAAA,CAAcK,QAAAA,CAAAA,IAAa,CAACJ,KAAAA,CAAMC,OAAO,CAACG,QAAAA,CAAAA,EAAW;AACxD,QAAA,MAAM,IAAIM,KAAAA,CAAM,gDAAA,CAAA;AAClB,IAAA;;IAGA,MAAMC,kBAAAA,GAAqB,OAAOC,IAAAA,GAChCN,IAAAA,CAAKC,gBAAgB,GAAGM,cAAAA,CAAeD,IAAAA,EAAMN,IAAAA,EAAMQ,WAAAA,CAAAA,GAAeF,IAAAA;AAEpE,IAAA,MAAMG,sBAAAA,GAAyB,OAAOH,IAAAA,GACpCN,IAAAA,CAAKE,gBAAgB,IAAIF,IAAAA,CAAKQ,WAAW,GACrCE,MAAAA,CAAOC,GAAG,CAAC,qBAAA,CAAA,CAAuBT,gBAAgB,CAAC;AAAEI,YAAAA,IAAAA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