@webiny/api-headless-cms 5.40.1 → 5.40.2-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/crud/contentEntry/referenceFieldsMapping.js +8 -8
  2. package/crud/contentEntry/referenceFieldsMapping.js.map +1 -1
  3. package/crud/contentModel/validation.d.ts +21 -21
  4. package/crud/contentModel/validation.js +1 -1
  5. package/crud/contentModel/validation.js.map +1 -1
  6. package/fieldConverters/CmsModelDynamicZoneFieldConverterPlugin.js +22 -54
  7. package/fieldConverters/CmsModelDynamicZoneFieldConverterPlugin.js.map +1 -1
  8. package/graphql/schema/createManageResolvers.js +21 -48
  9. package/graphql/schema/createManageResolvers.js.map +1 -1
  10. package/graphql/schema/createPreviewResolvers.js +4 -2
  11. package/graphql/schema/createPreviewResolvers.js.map +1 -1
  12. package/graphql/schema/createReadResolvers.js +4 -2
  13. package/graphql/schema/createReadResolvers.js.map +1 -1
  14. package/graphql/schema/resolvers/manage/normalizeGraphQlInput.d.ts +7 -0
  15. package/graphql/schema/resolvers/manage/normalizeGraphQlInput.js +59 -0
  16. package/graphql/schema/resolvers/manage/normalizeGraphQlInput.js.map +1 -0
  17. package/graphqlFields/dynamicZone/dynamicZoneField.js +43 -15
  18. package/graphqlFields/dynamicZone/dynamicZoneField.js.map +1 -1
  19. package/graphqlFields/dynamicZone/index.d.ts +0 -1
  20. package/graphqlFields/dynamicZone/index.js +0 -7
  21. package/graphqlFields/dynamicZone/index.js.map +1 -1
  22. package/index.d.ts +2 -1
  23. package/index.js +13 -2
  24. package/index.js.map +1 -1
  25. package/package.json +20 -20
  26. package/types/modelAst.d.ts +1 -1
  27. package/types/modelAst.js.map +1 -1
  28. package/types/modelField.d.ts +3 -2
  29. package/types/modelField.js.map +1 -1
  30. package/types/plugins.d.ts +2 -1
  31. package/types/plugins.js.map +1 -1
  32. package/types/types.d.ts +6 -0
  33. package/types/types.js.map +1 -1
  34. package/utils/contentEntryTraverser/ContentEntryTraverser.d.ts +1 -1
  35. package/utils/contentEntryTraverser/ContentEntryTraverser.js +28 -15
  36. package/utils/contentEntryTraverser/ContentEntryTraverser.js.map +1 -1
  37. package/graphqlFields/dynamicZone/dynamicZoneStorage.d.ts +0 -3
  38. package/graphqlFields/dynamicZone/dynamicZoneStorage.js +0 -111
  39. package/graphqlFields/dynamicZone/dynamicZoneStorage.js.map +0 -1
@@ -10,47 +10,60 @@ const nodeHasChildren = node => {
10
10
  const childrenAreCollections = node => {
11
11
  return node.children.every(node => node.type === "collection");
12
12
  };
13
+ const emptyValues = [null, undefined];
13
14
  class ContentEntryTraverser {
14
15
  constructor(modelAst) {
15
16
  this.modelAst = modelAst;
16
17
  }
17
- traverse(values, visitor) {
18
- this.visitTree(this.modelAst, values, [], visitor);
18
+ async traverse(values, visitor) {
19
+ await this.visitTree(this.modelAst, values, [], visitor);
19
20
  }
20
- visitTree(root, values, path, visitor) {
21
+ async visitTree(root, values, path, visitor) {
21
22
  for (const node of root.children) {
22
23
  const context = {
23
24
  node,
24
25
  parent: root
25
26
  };
26
27
  const field = this.getFieldFromNode(context);
27
- const value = values[field.fieldId];
28
- if (!value) {
28
+ let value = values[field.fieldId];
29
+
30
+ // We do not descend into nodes if they're `null` or `undefined`.
31
+ if (nodeHasChildren(node) && emptyValues.includes(value)) {
32
+ continue;
33
+ }
34
+
35
+ // We do not visit leaf nodes that are `undefined`.
36
+ if (!nodeHasChildren(node) && value === undefined) {
29
37
  continue;
30
38
  }
31
39
  const fieldPath = [...path, field.fieldId];
32
- visitor({
40
+ await visitor({
33
41
  field,
34
42
  value,
35
43
  path: fieldPath.join(".")
36
44
  }, context);
45
+
46
+ // Refetch the value from the original input, in case the value changed within the visitor.
47
+ value = values[field.fieldId];
37
48
  if (nodeHasChildren(node) && childrenAreCollections(node)) {
38
49
  if (field.multipleValues) {
39
- this.ensureArray(value).forEach((value, index) => {
40
- this.findCollectionAndVisit(node, value, [...fieldPath, index.toString()], visitor);
41
- });
50
+ const arrayValue = this.ensureArray(value);
51
+ for (let i = 0; i < arrayValue.length; i++) {
52
+ await this.findCollectionAndVisit(node, arrayValue[i], [...fieldPath, i.toString()], visitor);
53
+ }
42
54
  } else {
43
- this.findCollectionAndVisit(node, value, path, visitor);
55
+ await this.findCollectionAndVisit(node, value, fieldPath, visitor);
44
56
  }
45
57
  continue;
46
58
  }
47
59
  if (field.multipleValues) {
48
- this.ensureArray(value).forEach((value, index) => {
49
- this.visitTree(node, value, [...fieldPath, index.toString()], visitor);
50
- });
60
+ const arrayValue = this.ensureArray(value);
61
+ for (let i = 0; i < arrayValue.length; i++) {
62
+ await this.visitTree(node, arrayValue[i], [...fieldPath, i.toString()], visitor);
63
+ }
51
64
  continue;
52
65
  }
53
- this.visitTree(node, value, fieldPath, visitor);
66
+ await this.visitTree(node, value, fieldPath, visitor);
54
67
  }
55
68
  }
56
69
  ensureArray(value) {
@@ -67,7 +80,7 @@ class ContentEntryTraverser {
67
80
  if (!collection) {
68
81
  return;
69
82
  }
70
- this.visitTree(collection, values, path, visitor);
83
+ return this.visitTree(collection, values, path, visitor);
71
84
  }
72
85
  getFieldFromNode({
73
86
  node,
@@ -1 +1 @@
1
- {"version":3,"names":["nodeHasChildren","node","children","length","childrenAreCollections","every","type","ContentEntryTraverser","constructor","modelAst","traverse","values","visitor","visitTree","root","path","context","parent","field","getFieldFromNode","value","fieldId","fieldPath","join","multipleValues","ensureArray","forEach","index","findCollectionAndVisit","toString","Array","isArray","collection","find","child","discriminator","id","exports"],"sources":["ContentEntryTraverser.ts"],"sourcesContent":["import {\n CmsModelAst,\n CmsModelFieldAstNodeField,\n CmsModelFieldAstNode,\n ContentEntryValueVisitor,\n CmsModelFieldAstNodeCollection,\n CmsEntryValues\n} from \"~/types\";\n\ntype ParentNode = CmsModelAst | CmsModelFieldAstNode | null;\n\ntype VisitorContext = {\n node: CmsModelFieldAstNode;\n parent: ParentNode;\n};\n\nconst nodeHasChildren = (node: CmsModelFieldAstNode) => {\n return node.children.length > 0;\n};\n\ninterface NodeWithCollections extends CmsModelFieldAstNodeField {\n children: CmsModelFieldAstNodeCollection[];\n}\n\nconst childrenAreCollections = (node: CmsModelFieldAstNode): node is NodeWithCollections => {\n return node.children.every(node => node.type === \"collection\");\n};\n\nexport class ContentEntryTraverser {\n private readonly modelAst: CmsModelAst;\n\n constructor(modelAst: CmsModelAst) {\n this.modelAst = modelAst;\n }\n\n traverse(values: CmsEntryValues, visitor: ContentEntryValueVisitor) {\n this.visitTree(this.modelAst, values, [], visitor);\n }\n\n private visitTree(\n root: CmsModelAst | CmsModelFieldAstNode,\n values: CmsEntryValues,\n path: string[],\n visitor: ContentEntryValueVisitor\n ) {\n for (const node of root.children) {\n const context: VisitorContext = { node, parent: root };\n const field = this.getFieldFromNode(context);\n const value = values[field.fieldId];\n\n if (!value) {\n continue;\n }\n\n const fieldPath = [...path, field.fieldId];\n\n visitor(\n {\n field,\n value,\n path: fieldPath.join(\".\")\n },\n context\n );\n\n if (nodeHasChildren(node) && childrenAreCollections(node)) {\n if (field.multipleValues) {\n this.ensureArray(value).forEach((value, index) => {\n this.findCollectionAndVisit(\n node,\n value,\n [...fieldPath, index.toString()],\n visitor\n );\n });\n } else {\n this.findCollectionAndVisit(node, value, path, visitor);\n }\n continue;\n }\n\n if (field.multipleValues) {\n this.ensureArray(value).forEach((value, index) => {\n this.visitTree(node, value, [...fieldPath, index.toString()], visitor);\n });\n continue;\n }\n\n this.visitTree(node, value, fieldPath, visitor);\n }\n }\n\n private ensureArray(value: any) {\n if (!Array.isArray(value)) {\n return [];\n }\n\n return value;\n }\n\n private findCollectionAndVisit(\n node: NodeWithCollections,\n values: CmsEntryValues,\n path: string[],\n visitor: ContentEntryValueVisitor\n ) {\n const collection = node.children.find(child => {\n // Use the `discriminator` to find the correct value.\n return values[child.collection.discriminator] === child.collection.id;\n });\n\n if (!collection) {\n return;\n }\n\n this.visitTree(collection, values, path, visitor);\n }\n\n private getFieldFromNode({ node, parent }: VisitorContext) {\n if (node.type === \"collection\") {\n return (parent as CmsModelFieldAstNodeField).field;\n }\n\n return (node as CmsModelFieldAstNodeField).field;\n }\n}\n"],"mappings":";;;;;;AAgBA,MAAMA,eAAe,GAAIC,IAA0B,IAAK;EACpD,OAAOA,IAAI,CAACC,QAAQ,CAACC,MAAM,GAAG,CAAC;AACnC,CAAC;AAMD,MAAMC,sBAAsB,GAAIH,IAA0B,IAAkC;EACxF,OAAOA,IAAI,CAACC,QAAQ,CAACG,KAAK,CAACJ,IAAI,IAAIA,IAAI,CAACK,IAAI,KAAK,YAAY,CAAC;AAClE,CAAC;AAEM,MAAMC,qBAAqB,CAAC;EAG/BC,WAAWA,CAACC,QAAqB,EAAE;IAC/B,IAAI,CAACA,QAAQ,GAAGA,QAAQ;EAC5B;EAEAC,QAAQA,CAACC,MAAsB,EAAEC,OAAiC,EAAE;IAChE,IAAI,CAACC,SAAS,CAAC,IAAI,CAACJ,QAAQ,EAAEE,MAAM,EAAE,EAAE,EAAEC,OAAO,CAAC;EACtD;EAEQC,SAASA,CACbC,IAAwC,EACxCH,MAAsB,EACtBI,IAAc,EACdH,OAAiC,EACnC;IACE,KAAK,MAAMX,IAAI,IAAIa,IAAI,CAACZ,QAAQ,EAAE;MAC9B,MAAMc,OAAuB,GAAG;QAAEf,IAAI;QAAEgB,MAAM,EAAEH;MAAK,CAAC;MACtD,MAAMI,KAAK,GAAG,IAAI,CAACC,gBAAgB,CAACH,OAAO,CAAC;MAC5C,MAAMI,KAAK,GAAGT,MAAM,CAACO,KAAK,CAACG,OAAO,CAAC;MAEnC,IAAI,CAACD,KAAK,EAAE;QACR;MACJ;MAEA,MAAME,SAAS,GAAG,CAAC,GAAGP,IAAI,EAAEG,KAAK,CAACG,OAAO,CAAC;MAE1CT,OAAO,CACH;QACIM,KAAK;QACLE,KAAK;QACLL,IAAI,EAAEO,SAAS,CAACC,IAAI,CAAC,GAAG;MAC5B,CAAC,EACDP,OACJ,CAAC;MAED,IAAIhB,eAAe,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AAa,CAAC,KAAKD,KAAK,CAACF,UAAU,CAACI,EAAE;IACzE,CAAC,CAAC;IAEF,IAAI,CAACJ,UAAU,EAAE;MACb;IACJ;IAEA,IAAI,CAACnB,SAAS,CAACmB,UAAU,EAAErB,MAAM,EAAEI,IAAI,EAAEH,OAAO,CAAC;EACrD;EAEQO,gBAAgBA,CAAC;IAAElB,IAAI;IAAEgB;EAAuB,CAAC,EAAE;IACvD,IAAIhB,IAAI,CAACK,IAAI,KAAK,YAAY,EAAE;MAC5B,OAAQW,MAAM,CAA+BC,KAAK;IACtD;IAEA,OAAQjB,IAAI,CAA+BiB,KAAK;EACpD;AACJ;AAACmB,OAAA,CAAA9B,qBAAA,GAAAA,qBAAA","ignoreList":[]}
1
+ {"version":3,"names":["nodeHasChildren","node","children","length","childrenAreCollections","every","type","emptyValues","undefined","ContentEntryTraverser","constructor","modelAst","traverse","values","visitor","visitTree","root","path","context","parent","field","getFieldFromNode","value","fieldId","includes","fieldPath","join","multipleValues","arrayValue","ensureArray","i","findCollectionAndVisit","toString","Array","isArray","collection","find","child","discriminator","id","exports"],"sources":["ContentEntryTraverser.ts"],"sourcesContent":["import {\n CmsModelAst,\n CmsModelFieldAstNodeField,\n CmsModelFieldAstNode,\n ContentEntryValueVisitor,\n CmsModelFieldAstNodeCollection,\n CmsEntryValues\n} from \"~/types\";\n\ntype ParentNode = CmsModelAst | CmsModelFieldAstNode | null;\n\ntype VisitorContext = {\n node: CmsModelFieldAstNode;\n parent: ParentNode;\n};\n\nconst nodeHasChildren = (node: CmsModelFieldAstNode) => {\n return node.children.length > 0;\n};\n\ninterface NodeWithCollections extends CmsModelFieldAstNodeField {\n children: CmsModelFieldAstNodeCollection[];\n}\n\nconst childrenAreCollections = (node: CmsModelFieldAstNode): node is NodeWithCollections => {\n return node.children.every(node => node.type === \"collection\");\n};\n\nconst emptyValues = [null, undefined];\n\nexport class ContentEntryTraverser {\n private readonly modelAst: CmsModelAst;\n\n constructor(modelAst: CmsModelAst) {\n this.modelAst = modelAst;\n }\n\n async traverse(values: CmsEntryValues, visitor: ContentEntryValueVisitor) {\n await this.visitTree(this.modelAst, values, [], visitor);\n }\n\n private async visitTree(\n root: CmsModelAst | CmsModelFieldAstNode,\n values: CmsEntryValues,\n path: string[],\n visitor: ContentEntryValueVisitor\n ) {\n for (const node of root.children) {\n const context: VisitorContext = { node, parent: root };\n const field = this.getFieldFromNode(context);\n let value = values[field.fieldId];\n\n // We do not descend into nodes if they're `null` or `undefined`.\n if (nodeHasChildren(node) && emptyValues.includes(value)) {\n continue;\n }\n\n // We do not visit leaf nodes that are `undefined`.\n if (!nodeHasChildren(node) && value === undefined) {\n continue;\n }\n\n const fieldPath = [...path, field.fieldId];\n\n await visitor(\n {\n field,\n value,\n path: fieldPath.join(\".\")\n },\n context\n );\n\n // Refetch the value from the original input, in case the value changed within the visitor.\n value = values[field.fieldId];\n\n if (nodeHasChildren(node) && childrenAreCollections(node)) {\n if (field.multipleValues) {\n const arrayValue = this.ensureArray(value);\n for (let i = 0; i < arrayValue.length; i++) {\n await this.findCollectionAndVisit(\n node,\n arrayValue[i],\n [...fieldPath, i.toString()],\n visitor\n );\n }\n } else {\n await this.findCollectionAndVisit(node, value, fieldPath, visitor);\n }\n continue;\n }\n\n if (field.multipleValues) {\n const arrayValue = this.ensureArray(value);\n for (let i = 0; i < arrayValue.length; i++) {\n await this.visitTree(\n node,\n arrayValue[i],\n [...fieldPath, i.toString()],\n visitor\n );\n }\n continue;\n }\n\n await this.visitTree(node, value, fieldPath, visitor);\n }\n }\n\n private ensureArray(value: any) {\n if (!Array.isArray(value)) {\n return [];\n }\n\n return value;\n }\n\n private findCollectionAndVisit(\n node: NodeWithCollections,\n values: CmsEntryValues,\n path: string[],\n visitor: ContentEntryValueVisitor\n ) {\n const collection = node.children.find(child => {\n // Use the `discriminator` to find the correct value.\n return values[child.collection.discriminator] === child.collection.id;\n });\n\n if (!collection) {\n return;\n }\n\n return this.visitTree(collection, values, path, visitor);\n }\n\n private getFieldFromNode({ node, parent }: VisitorContext) {\n if (node.type === \"collection\") {\n return (parent as CmsModelFieldAstNodeField).field;\n }\n\n return (node as CmsModelFieldAstNodeField).field;\n }\n}\n"],"mappings":";;;;;;AAgBA,MAAMA,eAAe,GAAIC,IAA0B,IAAK;EACpD,OAAOA,IAAI,CAACC,QAAQ,CAACC,MAAM,GAAG,CAAC;AACnC,CAAC;AAMD,MAAMC,sBAAsB,GAAIH,IAA0B,IAAkC;EACxF,OAAOA,IAAI,CAACC,QAAQ,CAACG,KAAK,CAACJ,IAAI,IAAIA,IAAI,CAACK,IAAI,KAAK,YAAY,CAAC;AAClE,CAAC;AAED,MAAMC,WAAW,GAAG,CAAC,IAAI,EAAEC,SAAS,CAAC;AAE9B,MAAMC,qBAAqB,CAAC;EAG/BC,WAAWA,CAACC,QAAqB,EAAE;IAC/B,IAAI,CAACA,QAAQ,GAAGA,QAAQ;EAC5B;EAEA,MAAMC,QAAQA,CAACC,MAAsB,EAAEC,OAAiC,EAAE;IACtE,MAAM,IAAI,CAACC,SAAS,CAAC,IAAI,CAACJ,QAAQ,EAAEE,MAAM,EAAE,EAAE,EAAEC,OAAO,CAAC;EAC5D;EAEA,MAAcC,SAASA,CACnBC,IAAwC,EACxCH,MAAsB,EACtBI,IAAc,EACdH,OAAiC,EACnC;IACE,KAAK,MAAMb,IAAI,IAAIe,IAAI,CAACd,QAAQ,EAAE;MAC9B,MAAMgB,OAAuB,GAAG;QAAEjB,IAAI;QAAEkB,MAAM,EAAEH;MAAK,CAAC;MACtD,MAAMI,KAAK,GAAG,IAAI,CAACC,gBAAgB,CAACH,OAAO,CAAC;MAC5C,IAAII,KAAK,GAAGT,MAAM,CAACO,KAAK,CAACG,OAAO,CAAC;;MAEjC;MACA,IAAIvB,eAAe,CAACC,IAAI,CAAC,IAAIM,WAAW,CAACiB,QAAQ,CAACF,KAAK,CAAC,EAAE;QACtD;MACJ;;MAEA;MACA,IAAI,CAACtB,eAAe,CAACC,IAAI,CAAC,IAAI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AAC,CAAC,CAAC,EAC5BlB,OACJ,CAAC;QACL;QACA;MACJ;MAEA,MAAM,IAAI,CAACC,SAAS,CAACd,IAAI,EAAEqB,KAAK,EAAEG,SAAS,EAAEX,OAAO,CAAC;IACzD;EACJ;EAEQe,WAAWA,CAACP,KAAU,EAAE;IAC5B,IAAI,CAACW,KAAK,CAACC,OAAO,CAACZ,KAAK,CAAC,EAAE;MACvB,OAAO,EAAE;IACb;IAEA,OAAOA,KAAK;EAChB;EAEQS,sBAAsBA,CAC1B9B,IAAyB,EACzBY,MAAsB,EACtBI,IAAc,EACdH,OAAiC,EACnC;IACE,MAAMqB,UAAU,GAAGlC,IAAI,CAACC,QAAQ,CAACkC,IAAI,CAACC,KAAK,IAAI;MAC3C;MACA,OAAOxB,MAAM,CAACwB,KAAK,CAACF,UAAU,CAACG,aAAa,CAAC,KAAKD,KAAK,CAACF,UAAU,CAACI,EAAE;IACzE,CAAC,CAAC;IAEF,IAAI,CAACJ,UAAU,EAAE;MACb;IACJ;IAEA,OAAO,IAAI,CAACpB,SAAS,CAACoB,UAAU,EAAEtB,MAAM,EAAEI,IAAI,EAAEH,OAAO,CAAC;EAC5D;EAEQO,gBAAgBA,CAAC;IAAEpB,IAAI;IAAEkB;EAAuB,CAAC,EAAE;IACvD,IAAIlB,IAAI,CAACK,IAAI,KAAK,YAAY,EAAE;MAC5B,OAAQa,MAAM,CAA+BC,KAAK;IACtD;IAEA,OAAQnB,IAAI,CAA+BmB,KAAK;EACpD;AACJ;AAACoB,OAAA,CAAA/B,qBAAA,GAAAA,qBAAA","ignoreList":[]}
@@ -1,3 +0,0 @@
1
- import { CmsModelDynamicZoneField } from "../../types";
2
- import { StorageTransformPlugin } from "../../plugins";
3
- export declare const createDynamicZoneStorageTransform: () => StorageTransformPlugin<any, any, CmsModelDynamicZoneField>;
@@ -1,111 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.createDynamicZoneStorageTransform = void 0;
7
- var _plugins = require("../../plugins");
8
- function valueWithTemplateId(value, {
9
- id,
10
- gqlTypeName
11
- }) {
12
- return {
13
- [gqlTypeName]: {
14
- ...value[gqlTypeName],
15
- _templateId: id
16
- }
17
- };
18
- }
19
- const convertToStorage = (value, templates) => {
20
- // Only one key is allowed in the input object.
21
- const inputType = Object.keys(value)[0];
22
- const template = templates.find(tpl => tpl.gqlTypeName === inputType);
23
- if (template) {
24
- return {
25
- ...value[inputType],
26
- _templateId: template.id
27
- };
28
- }
29
- /**
30
- * There is a possibility that the value is already in the storage format, so there is no need to transform it again.
31
- * We are going to check:
32
- * 1. value is an object
33
- * 2. it contains a _templateId key
34
- * 3. the key is a valid template id - at this point we know it is already converted
35
- */
36
- if (!value || typeof value !== "object") {
37
- return undefined;
38
- } else if (!value._templateId) {
39
- return undefined;
40
- }
41
- const tpl = templates.find(tpl => tpl.id === value._templateId);
42
- return tpl ? value : undefined;
43
- };
44
- const convertFromStorage = (value, templates) => {
45
- const template = templates.find(tpl => value._templateId === tpl.id);
46
- if (template) {
47
- // We keep the `_templateId` property, to simplify further processing.
48
- return {
49
- [template.gqlTypeName]: value
50
- };
51
- }
52
-
53
- /**
54
- * When the `value` is in the original input format (during GraphQL mutations), `_templateId` will not be present
55
- * in the `value` object (because this internal property is added by `toStorage` storage transform method, and since
56
- * we simply return the input from the CRUD methods, this property will be missing).
57
- * For that reason, we need to run some extra logic, to acquire the `_templateId`.
58
- */
59
-
60
- if (!value || typeof value !== "object") {
61
- return undefined;
62
- }
63
-
64
- /**
65
- * `value` object must have exactly one none-empty key.
66
- */
67
- const keys = Object.keys(value);
68
- if (keys.length !== 1 || !keys[0]) {
69
- return undefined;
70
- }
71
-
72
- /**
73
- * Find a template that matches the first (and only) key of the `value` object by template's `gqlTypeName`.
74
- */
75
- const tpl = templates.find(tpl => tpl.gqlTypeName === keys[0]);
76
- return tpl ? valueWithTemplateId(value, tpl) : undefined;
77
- };
78
- const createDynamicZoneStorageTransform = () => {
79
- return new _plugins.StorageTransformPlugin({
80
- fieldType: "dynamicZone",
81
- fromStorage: async ({
82
- value,
83
- field
84
- }) => {
85
- if (!value) {
86
- return null;
87
- }
88
- const templates = field.settings.templates;
89
- if (Array.isArray(value) && field.multipleValues) {
90
- return value.map(value => convertFromStorage(value, templates)).filter(Boolean);
91
- }
92
- return convertFromStorage(value, templates);
93
- },
94
- toStorage: async ({
95
- value,
96
- field
97
- }) => {
98
- if (!value) {
99
- return value;
100
- }
101
- const templates = field.settings.templates;
102
- if (Array.isArray(value) && field.multipleValues) {
103
- return value.map(value => convertToStorage(value, templates)).filter(Boolean);
104
- }
105
- return convertToStorage(value, templates);
106
- }
107
- });
108
- };
109
- exports.createDynamicZoneStorageTransform = createDynamicZoneStorageTransform;
110
-
111
- //# sourceMappingURL=dynamicZoneStorage.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["_plugins","require","valueWithTemplateId","value","id","gqlTypeName","_templateId","convertToStorage","templates","inputType","Object","keys","template","find","tpl","undefined","convertFromStorage","length","createDynamicZoneStorageTransform","StorageTransformPlugin","fieldType","fromStorage","field","settings","Array","isArray","multipleValues","map","filter","Boolean","toStorage","exports"],"sources":["dynamicZoneStorage.ts"],"sourcesContent":["import { CmsDynamicZoneTemplate, CmsModelDynamicZoneField } from \"~/types\";\nimport { StorageTransformPlugin } from \"~/plugins\";\n\nfunction valueWithTemplateId(\n value: Record<string, any>,\n { id, gqlTypeName }: CmsDynamicZoneTemplate\n) {\n return { [gqlTypeName]: { ...value[gqlTypeName], _templateId: id } };\n}\n\nconst convertToStorage = (value: Record<string, any>, templates: CmsDynamicZoneTemplate[]) => {\n // Only one key is allowed in the input object.\n const inputType = Object.keys(value)[0];\n const template = templates.find(tpl => tpl.gqlTypeName === inputType);\n if (template) {\n return { ...value[inputType], _templateId: template.id };\n }\n /**\n * There is a possibility that the value is already in the storage format, so there is no need to transform it again.\n * We are going to check:\n * 1. value is an object\n * 2. it contains a _templateId key\n * 3. the key is a valid template id - at this point we know it is already converted\n */\n if (!value || typeof value !== \"object\") {\n return undefined;\n } else if (!value._templateId) {\n return undefined;\n }\n const tpl = templates.find(tpl => tpl.id === value._templateId);\n return tpl ? value : undefined;\n};\n\ninterface TemplateValueFromStorage {\n _templateId: string;\n [key: string]: any;\n}\n\nconst convertFromStorage = (\n value: TemplateValueFromStorage,\n templates: CmsDynamicZoneTemplate[]\n) => {\n const template = templates.find(tpl => value._templateId === tpl.id);\n\n if (template) {\n // We keep the `_templateId` property, to simplify further processing.\n return { [template.gqlTypeName]: value };\n }\n\n /**\n * When the `value` is in the original input format (during GraphQL mutations), `_templateId` will not be present\n * in the `value` object (because this internal property is added by `toStorage` storage transform method, and since\n * we simply return the input from the CRUD methods, this property will be missing).\n * For that reason, we need to run some extra logic, to acquire the `_templateId`.\n */\n\n if (!value || typeof value !== \"object\") {\n return undefined;\n }\n\n /**\n * `value` object must have exactly one none-empty key.\n */\n const keys = Object.keys(value);\n if (keys.length !== 1 || !keys[0]) {\n return undefined;\n }\n\n /**\n * Find a template that matches the first (and only) key of the `value` object by template's `gqlTypeName`.\n */\n const tpl = templates.find(tpl => tpl.gqlTypeName === keys[0]);\n return tpl ? valueWithTemplateId(value, tpl) : undefined;\n};\n\nexport const createDynamicZoneStorageTransform = () => {\n return new StorageTransformPlugin<any, any, CmsModelDynamicZoneField>({\n fieldType: \"dynamicZone\",\n fromStorage: async ({ value, field }) => {\n if (!value) {\n return null;\n }\n\n const templates = field.settings.templates;\n\n if (Array.isArray(value) && field.multipleValues) {\n return value.map(value => convertFromStorage(value, templates)).filter(Boolean);\n }\n\n return convertFromStorage(value, templates);\n },\n toStorage: async ({ value, field }) => {\n if (!value) {\n return value;\n }\n\n const templates = field.settings.templates;\n\n if (Array.isArray(value) && field.multipleValues) {\n return value.map(value => convertToStorage(value, templates)).filter(Boolean);\n }\n\n return convertToStorage(value, templates);\n }\n });\n};\n"],"mappings":";;;;;;AACA,IAAAA,QAAA,GAAAC,OAAA;AAEA,SAASC,mBA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