@bright.global/arboretum-sdk 0.0.9 → 0.1.0-rc.0

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.
@@ -108,9 +108,9 @@ var arboretumConfigOptions = function (config) {
108
108
  };
109
109
  export var createArboretumClient = function (config) { return __awaiter(void 0, void 0, void 0, function () {
110
110
  var clientApi, cmaPreviewClientApi, includeEntryStatus, options, sitemapDataCtx, dataE, _a, c, sitemap, pagesByTagIdRecord, ctx_1;
111
- var _b, _c, _d, _e, _f, _g, _h;
112
- return __generator(this, function (_j) {
113
- switch (_j.label) {
111
+ var _b, _c, _d, _e, _f, _g, _h, _j;
112
+ return __generator(this, function (_k) {
113
+ switch (_k.label) {
114
114
  case 0:
115
115
  clientApi = createContentfulClient(config);
116
116
  cmaPreviewClientApi = config.type === "cma-client"
@@ -119,11 +119,11 @@ export var createArboretumClient = function (config) { return __awaiter(void 0,
119
119
  includeEntryStatus = config.type === "cma-client" ? !!((_b = config.options) === null || _b === void 0 ? void 0 : _b.includeEntryStatus) : false;
120
120
  return [4 /*yield*/, arboretumConfigOptions(config)];
121
121
  case 1:
122
- options = _j.sent();
122
+ options = _k.sent();
123
123
  sitemapDataCtx = {
124
124
  preview: config.preview,
125
125
  clientApi: clientApi,
126
- options: __assign(__assign({}, options), { includeEntryStatus: includeEntryStatus }),
126
+ options: __assign(__assign({}, options), { includeEntryStatus: includeEntryStatus, sitemapRepresentation: (_c = config.options) === null || _c === void 0 ? void 0 : _c.sitemapRepresentation }),
127
127
  localeTagIdPrefix: localeTagIdPrefix,
128
128
  pageHomeTagId: options.homePageTagId || pageHomeTagId,
129
129
  contentfulClientType: config.type === "cda-client" && config.contentful.client.withAllLocales
@@ -132,25 +132,25 @@ export var createArboretumClient = function (config) { return __awaiter(void 0,
132
132
  pageTagIdPrefix: pageTagIdPrefix,
133
133
  cmaPreviewClientApi: cmaPreviewClientApi,
134
134
  };
135
- if (!((_c = config.options) === null || _c === void 0 ? void 0 : _c.data)) return [3 /*break*/, 2];
136
- _a = right({ data: (_d = config.options) === null || _d === void 0 ? void 0 : _d.data.data, warnings: undefined });
135
+ if (!((_d = config.options) === null || _d === void 0 ? void 0 : _d.data)) return [3 /*break*/, 2];
136
+ _a = right({ data: (_e = config.options) === null || _e === void 0 ? void 0 : _e.data.data, warnings: undefined });
137
137
  return [3 /*break*/, 4];
138
138
  case 2: return [4 /*yield*/, sitemapData(sitemapDataCtx)];
139
139
  case 3:
140
- _a = _j.sent();
141
- _j.label = 4;
140
+ _a = _k.sent();
141
+ _k.label = 4;
142
142
  case 4:
143
143
  dataE = _a;
144
144
  if (dataE._tag === "Right") {
145
145
  c = __assign(__assign({}, sitemapDataCtx), { data: dataE.right.data });
146
- sitemap = ((_e = config.options) === null || _e === void 0 ? void 0 : _e.data)
146
+ sitemap = ((_f = config.options) === null || _f === void 0 ? void 0 : _f.data)
147
147
  ? config.options.data.sitemap
148
- : ((_f = config.options) === null || _f === void 0 ? void 0 : _f.eagerly)
148
+ : ((_g = config.options) === null || _g === void 0 ? void 0 : _g.eagerly)
149
149
  ? buildSitemapEagerly(c, dataE.right.data.locales)
150
150
  : new Map();
151
- pagesByTagIdRecord = ((_g = config.options) === null || _g === void 0 ? void 0 : _g.data)
151
+ pagesByTagIdRecord = ((_h = config.options) === null || _h === void 0 ? void 0 : _h.data)
152
152
  ? config.options.data.pagesByTagId
153
- : ((_h = config.options) === null || _h === void 0 ? void 0 : _h.eagerly)
153
+ : ((_j = config.options) === null || _j === void 0 ? void 0 : _j.eagerly)
154
154
  ? buildPagesByTagEagerly(__assign(__assign({}, c), { sitemap: sitemap }), dataE.right.data.locales)
155
155
  : new Map();
156
156
  ctx_1 = __assign(__assign({}, c), { data: dataE.right.data, sitemap: sitemap, pagesByTagId: pagesByTagIdRecord, lastUpdatedAt: new Date().toISOString(), regenerationInProgress: false });
@@ -82,6 +82,7 @@ var getAllPageEntriesRecursively = function (_a, contentfulClientType, pageConte
82
82
  pageContentTypeOpt.slugFieldId,
83
83
  pageContentTypeOpt.childPagesFieldId,
84
84
  pageContentTypeOpt.titleFieldId,
85
+ pageContentTypeOpt.parentPageFieldId,
85
86
  ].flatMap(function (id) { return (id ? ["fields.".concat(id)] : []); });
86
87
  return [2 /*return*/, getAllEntriesRecursively({ getEntries: getEntries }, contentfulClientType, pageContentTypeOpt.id, skip, acc,
87
88
  /* For some reason select param causes errors in CMA. I'm getting the following response:
@@ -1,28 +1,25 @@
1
- import { localizeField } from '../helpers/localize-contentful-field';
2
- export var pageEntryAdapter = function (data, slugField, titleField, childPagesField, locale, parent, entry) {
1
+ import { localizeField } from "../helpers/localize-contentful-field";
2
+ export var pageEntryAdapter = function (data, slugField, titleField, childrenRefs, locale, parent, entry) {
3
3
  var _a;
4
4
  var fieldValue = localizeField(data, locale);
5
5
  var slug = (_a = fieldValue(slugField.localized, entry.fields[slugField.id])) === null || _a === void 0 ? void 0 : _a.toLowerCase();
6
6
  var title = titleField
7
7
  ? fieldValue(titleField.localized, entry.fields[titleField.id])
8
8
  : undefined;
9
- var childPages = childPagesField
10
- ? fieldValue(childPagesField.localized, entry.fields[childPagesField.id])
11
- : undefined;
12
9
  return slug
13
10
  ? {
14
- type: 'page',
11
+ type: "page",
15
12
  parent: parent ? { sys: parent.sys } : undefined,
16
- path: parent ? parent.path + '/' + slug : '/' + locale.code,
13
+ path: parent ? parent.path + "/" + slug : "/" + locale.code,
17
14
  slug: slug,
18
15
  title: title || undefined,
19
16
  sys: {
20
17
  id: entry.sys.id,
21
18
  cmaOnlyStatus: entry.sys.cmaOnlyStatus,
22
- contentTypeId: entry.sys.contentType.sys.id
19
+ contentTypeId: entry.sys.contentType.sys.id,
23
20
  },
24
21
  metadata: entry.metadata,
25
- childPages: (childPages === null || childPages === void 0 ? void 0 : childPages.flatMap(function (childPage) { var _a; return ((_a = childPage === null || childPage === void 0 ? void 0 : childPage.sys) === null || _a === void 0 ? void 0 : _a.id) ? [{ sys: { id: childPage === null || childPage === void 0 ? void 0 : childPage.sys.id } }] : []; })) || [],
22
+ childPages: (childrenRefs === null || childrenRefs === void 0 ? void 0 : childrenRefs.flatMap(function (childPage) { var _a; return ((_a = childPage === null || childPage === void 0 ? void 0 : childPage.sys) === null || _a === void 0 ? void 0 : _a.id) ? [{ sys: { id: childPage === null || childPage === void 0 ? void 0 : childPage.sys.id } }] : []; })) || [],
26
23
  }
27
24
  : undefined;
28
25
  };
@@ -34,10 +34,11 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
34
34
  }
35
35
  return to.concat(ar || Array.prototype.slice.call(from));
36
36
  };
37
- import { left, right } from '../../../utils/fp-utils';
38
- import { pageEntryAdapter } from '../adapters/page-entry-adapter';
39
- import { redirectEntryAdapter } from '../adapters/redirect-entry-adapter';
40
- import { isRoot } from './is-root';
37
+ import { left, right } from "../../../utils/fp-utils";
38
+ import { pageEntryAdapter } from "../adapters/page-entry-adapter";
39
+ import { redirectEntryAdapter } from "../adapters/redirect-entry-adapter";
40
+ import { isRoot } from "./is-root";
41
+ import { localizeField } from "./localize-contentful-field";
41
42
  var getAllRedirects = function (data, options, locale, parent) {
42
43
  var redirectContentType = options.id
43
44
  ? data.contentTypes.get(options.id)
@@ -58,15 +59,49 @@ var getAllRedirects = function (data, options, locale, parent) {
58
59
  return redirect ? [redirect] : [];
59
60
  });
60
61
  };
61
- var buildLocalizedSitemapArrRecursively = function (data, options, locale, parent, pageEntry, acc) {
62
- var pages = data.pages, contentTypes = data.contentTypes;
63
- var pageContentType = contentTypes.get(pageEntry.sys.contentType.sys.id);
64
- var pageContentTypeConfig = options.pageContentTypes[pageEntry.sys.contentType.sys.id];
65
- var findPageField = function (fieldId) {
62
+ var findPageFieldF = function (data, options, pageContentTypeId) {
63
+ return function (fieldId) {
64
+ var contentTypes = data.contentTypes;
65
+ var pageContentType = contentTypes.get(pageContentTypeId);
66
+ var pageContentTypeConfig = options.pageContentTypes[pageContentTypeId];
66
67
  return pageContentType && pageContentTypeConfig
67
68
  ? pageContentType.fields.get(fieldId)
68
69
  : undefined;
69
70
  };
71
+ };
72
+ var getChildrenRefsByParentId = function (data, locale, options) {
73
+ var pages = data.pages;
74
+ var acc = new Map();
75
+ pages.forEach(function (page) {
76
+ var _a;
77
+ var pageContentTypeConfig = options.pageContentTypes[page.sys.contentType.sys.id];
78
+ var findPageField = findPageFieldF(data, options, page.sys.contentType.sys.id);
79
+ var parentPageField = pageContentTypeConfig.parentPageFieldId
80
+ ? findPageField(pageContentTypeConfig.parentPageFieldId)
81
+ : undefined;
82
+ var fieldValue = localizeField(data, locale);
83
+ var parentPage = parentPageField
84
+ ? fieldValue(parentPageField.localized, page.fields[parentPageField.id])
85
+ : undefined;
86
+ var parentPageId = (_a = parentPage === null || parentPage === void 0 ? void 0 : parentPage.sys) === null || _a === void 0 ? void 0 : _a.id;
87
+ if (parentPageId) {
88
+ var pageRef = { sys: { id: page.sys.id } };
89
+ var maybePages = acc.get(parentPageId);
90
+ if (maybePages) {
91
+ maybePages.push(pageRef);
92
+ }
93
+ else {
94
+ acc.set(parentPageId, [pageRef]);
95
+ }
96
+ }
97
+ });
98
+ return acc;
99
+ };
100
+ var buildLocalizedSitemapArrRecursively = function (data, options, locale, parent, childrenRefsByPageId, pageEntry, acc) {
101
+ var _a;
102
+ var pages = data.pages;
103
+ var pageContentTypeConfig = options.pageContentTypes[pageEntry.sys.contentType.sys.id];
104
+ var findPageField = findPageFieldF(data, options, pageEntry.sys.contentType.sys.id);
70
105
  var slugField = findPageField(pageContentTypeConfig.slugFieldId);
71
106
  var titleField = pageContentTypeConfig.titleFieldId
72
107
  ? findPageField(pageContentTypeConfig.titleFieldId)
@@ -74,8 +109,14 @@ var buildLocalizedSitemapArrRecursively = function (data, options, locale, paren
74
109
  var childPagesField = pageContentTypeConfig.childPagesFieldId
75
110
  ? findPageField(pageContentTypeConfig.childPagesFieldId)
76
111
  : undefined;
112
+ var fieldValue = localizeField(data, locale);
113
+ var childrenRefs = childrenRefsByPageId
114
+ ? childrenRefsByPageId.get(pageEntry.sys.id) || []
115
+ : childPagesField
116
+ ? ((_a = fieldValue(childPagesField.localized, pageEntry.fields[childPagesField.id])) === null || _a === void 0 ? void 0 : _a.flatMap(function (childPage) { var _a; return ((_a = childPage === null || childPage === void 0 ? void 0 : childPage.sys) === null || _a === void 0 ? void 0 : _a.id) ? [{ sys: { id: childPage === null || childPage === void 0 ? void 0 : childPage.sys.id } }] : []; })) || []
117
+ : [];
77
118
  var page = slugField
78
- ? pageEntryAdapter(data, slugField, titleField, childPagesField, locale, parent, pageEntry)
119
+ ? pageEntryAdapter(data, slugField, titleField, childrenRefs, locale, parent, pageEntry)
79
120
  : undefined;
80
121
  if (page) {
81
122
  var validChildPages_1 = page.childPages.filter(function (c) { return pages.get(c.sys.id) && !acc.get(c.sys.id); });
@@ -93,7 +134,7 @@ var buildLocalizedSitemapArrRecursively = function (data, options, locale, paren
93
134
  var id = _a.sys.id;
94
135
  var childPageEntry = pages.get(id);
95
136
  childPageEntry &&
96
- buildLocalizedSitemapArrRecursively(data, options, locale, { sys: pageEntry.sys, path: page.path }, childPageEntry, acc);
137
+ buildLocalizedSitemapArrRecursively(data, options, locale, { sys: pageEntry.sys, path: page.path }, childrenRefsByPageId, childPageEntry, acc);
97
138
  });
98
139
  return acc;
99
140
  }
@@ -107,7 +148,10 @@ export var buildLocalizedSitemap = function (data, options, pageHomeTagId, local
107
148
  var homePageRef = (_d = (_c = (_b = (_a = homePagesByTagId === null || homePagesByTagId === void 0 ? void 0 : homePagesByTagId.get(locale.code)) === null || _a === void 0 ? void 0 : _a.get(pageHomeTagId)) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.sys) === null || _d === void 0 ? void 0 : _d.id;
108
149
  var homePageEntry = homePageRef ? pages.get(homePageRef) : undefined;
109
150
  if (homePageEntry) {
110
- var sitemap = buildLocalizedSitemapArrRecursively(data, options, locale, undefined, homePageEntry, new Map());
151
+ var childrenRefsByPageId = options.sitemapRepresentation === "child-to-parent"
152
+ ? getChildrenRefsByParentId(data, locale, options)
153
+ : undefined;
154
+ var sitemap = buildLocalizedSitemapArrRecursively(data, options, locale, undefined, childrenRefsByPageId, homePageEntry, new Map());
111
155
  return right({
112
156
  root: { sys: homePageEntry.sys },
113
157
  sitemap: sitemap,
@@ -111,9 +111,9 @@ var arboretumConfigOptions = function (config) {
111
111
  };
112
112
  var createArboretumClient = function (config) { return __awaiter(void 0, void 0, void 0, function () {
113
113
  var clientApi, cmaPreviewClientApi, includeEntryStatus, options, sitemapDataCtx, dataE, _a, c, sitemap, pagesByTagIdRecord, ctx_1;
114
- var _b, _c, _d, _e, _f, _g, _h;
115
- return __generator(this, function (_j) {
116
- switch (_j.label) {
114
+ var _b, _c, _d, _e, _f, _g, _h, _j;
115
+ return __generator(this, function (_k) {
116
+ switch (_k.label) {
117
117
  case 0:
118
118
  clientApi = (0, contentful_client_impl_1.createContentfulClient)(config);
119
119
  cmaPreviewClientApi = config.type === "cma-client"
@@ -122,11 +122,11 @@ var createArboretumClient = function (config) { return __awaiter(void 0, void 0,
122
122
  includeEntryStatus = config.type === "cma-client" ? !!((_b = config.options) === null || _b === void 0 ? void 0 : _b.includeEntryStatus) : false;
123
123
  return [4 /*yield*/, arboretumConfigOptions(config)];
124
124
  case 1:
125
- options = _j.sent();
125
+ options = _k.sent();
126
126
  sitemapDataCtx = {
127
127
  preview: config.preview,
128
128
  clientApi: clientApi,
129
- options: __assign(__assign({}, options), { includeEntryStatus: includeEntryStatus }),
129
+ options: __assign(__assign({}, options), { includeEntryStatus: includeEntryStatus, sitemapRepresentation: (_c = config.options) === null || _c === void 0 ? void 0 : _c.sitemapRepresentation }),
130
130
  localeTagIdPrefix: localeTagIdPrefix,
131
131
  pageHomeTagId: options.homePageTagId || pageHomeTagId,
132
132
  contentfulClientType: config.type === "cda-client" && config.contentful.client.withAllLocales
@@ -135,25 +135,25 @@ var createArboretumClient = function (config) { return __awaiter(void 0, void 0,
135
135
  pageTagIdPrefix: pageTagIdPrefix,
136
136
  cmaPreviewClientApi: cmaPreviewClientApi,
137
137
  };
138
- if (!((_c = config.options) === null || _c === void 0 ? void 0 : _c.data)) return [3 /*break*/, 2];
139
- _a = (0, fp_utils_1.right)({ data: (_d = config.options) === null || _d === void 0 ? void 0 : _d.data.data, warnings: undefined });
138
+ if (!((_d = config.options) === null || _d === void 0 ? void 0 : _d.data)) return [3 /*break*/, 2];
139
+ _a = (0, fp_utils_1.right)({ data: (_e = config.options) === null || _e === void 0 ? void 0 : _e.data.data, warnings: undefined });
140
140
  return [3 /*break*/, 4];
141
141
  case 2: return [4 /*yield*/, (0, sitemap_data_1.sitemapData)(sitemapDataCtx)];
142
142
  case 3:
143
- _a = _j.sent();
144
- _j.label = 4;
143
+ _a = _k.sent();
144
+ _k.label = 4;
145
145
  case 4:
146
146
  dataE = _a;
147
147
  if (dataE._tag === "Right") {
148
148
  c = __assign(__assign({}, sitemapDataCtx), { data: dataE.right.data });
149
- sitemap = ((_e = config.options) === null || _e === void 0 ? void 0 : _e.data)
149
+ sitemap = ((_f = config.options) === null || _f === void 0 ? void 0 : _f.data)
150
150
  ? config.options.data.sitemap
151
- : ((_f = config.options) === null || _f === void 0 ? void 0 : _f.eagerly)
151
+ : ((_g = config.options) === null || _g === void 0 ? void 0 : _g.eagerly)
152
152
  ? (0, build_sitemap_eagerly_1.buildSitemapEagerly)(c, dataE.right.data.locales)
153
153
  : new Map();
154
- pagesByTagIdRecord = ((_g = config.options) === null || _g === void 0 ? void 0 : _g.data)
154
+ pagesByTagIdRecord = ((_h = config.options) === null || _h === void 0 ? void 0 : _h.data)
155
155
  ? config.options.data.pagesByTagId
156
- : ((_h = config.options) === null || _h === void 0 ? void 0 : _h.eagerly)
156
+ : ((_j = config.options) === null || _j === void 0 ? void 0 : _j.eagerly)
157
157
  ? (0, build_pages_by_tag_eagerly_1.buildPagesByTagEagerly)(__assign(__assign({}, c), { sitemap: sitemap }), dataE.right.data.locales)
158
158
  : new Map();
159
159
  ctx_1 = __assign(__assign({}, c), { data: dataE.right.data, sitemap: sitemap, pagesByTagId: pagesByTagIdRecord, lastUpdatedAt: new Date().toISOString(), regenerationInProgress: false });
@@ -85,6 +85,7 @@ var getAllPageEntriesRecursively = function (_a, contentfulClientType, pageConte
85
85
  pageContentTypeOpt.slugFieldId,
86
86
  pageContentTypeOpt.childPagesFieldId,
87
87
  pageContentTypeOpt.titleFieldId,
88
+ pageContentTypeOpt.parentPageFieldId,
88
89
  ].flatMap(function (id) { return (id ? ["fields.".concat(id)] : []); });
89
90
  return [2 /*return*/, (0, get_all_entries_recursively_1.getAllEntriesRecursively)({ getEntries: getEntries }, contentfulClientType, pageContentTypeOpt.id, skip, acc,
90
91
  /* For some reason select param causes errors in CMA. I'm getting the following response:
@@ -2,30 +2,27 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.pageEntryAdapter = void 0;
4
4
  var localize_contentful_field_1 = require("../helpers/localize-contentful-field");
5
- var pageEntryAdapter = function (data, slugField, titleField, childPagesField, locale, parent, entry) {
5
+ var pageEntryAdapter = function (data, slugField, titleField, childrenRefs, locale, parent, entry) {
6
6
  var _a;
7
7
  var fieldValue = (0, localize_contentful_field_1.localizeField)(data, locale);
8
8
  var slug = (_a = fieldValue(slugField.localized, entry.fields[slugField.id])) === null || _a === void 0 ? void 0 : _a.toLowerCase();
9
9
  var title = titleField
10
10
  ? fieldValue(titleField.localized, entry.fields[titleField.id])
11
11
  : undefined;
12
- var childPages = childPagesField
13
- ? fieldValue(childPagesField.localized, entry.fields[childPagesField.id])
14
- : undefined;
15
12
  return slug
16
13
  ? {
17
- type: 'page',
14
+ type: "page",
18
15
  parent: parent ? { sys: parent.sys } : undefined,
19
- path: parent ? parent.path + '/' + slug : '/' + locale.code,
16
+ path: parent ? parent.path + "/" + slug : "/" + locale.code,
20
17
  slug: slug,
21
18
  title: title || undefined,
22
19
  sys: {
23
20
  id: entry.sys.id,
24
21
  cmaOnlyStatus: entry.sys.cmaOnlyStatus,
25
- contentTypeId: entry.sys.contentType.sys.id
22
+ contentTypeId: entry.sys.contentType.sys.id,
26
23
  },
27
24
  metadata: entry.metadata,
28
- childPages: (childPages === null || childPages === void 0 ? void 0 : childPages.flatMap(function (childPage) { var _a; return ((_a = childPage === null || childPage === void 0 ? void 0 : childPage.sys) === null || _a === void 0 ? void 0 : _a.id) ? [{ sys: { id: childPage === null || childPage === void 0 ? void 0 : childPage.sys.id } }] : []; })) || [],
25
+ childPages: (childrenRefs === null || childrenRefs === void 0 ? void 0 : childrenRefs.flatMap(function (childPage) { var _a; return ((_a = childPage === null || childPage === void 0 ? void 0 : childPage.sys) === null || _a === void 0 ? void 0 : _a.id) ? [{ sys: { id: childPage === null || childPage === void 0 ? void 0 : childPage.sys.id } }] : []; })) || [],
29
26
  }
30
27
  : undefined;
31
28
  };
@@ -41,6 +41,7 @@ var fp_utils_1 = require("../../../utils/fp-utils");
41
41
  var page_entry_adapter_1 = require("../adapters/page-entry-adapter");
42
42
  var redirect_entry_adapter_1 = require("../adapters/redirect-entry-adapter");
43
43
  var is_root_1 = require("./is-root");
44
+ var localize_contentful_field_1 = require("./localize-contentful-field");
44
45
  var getAllRedirects = function (data, options, locale, parent) {
45
46
  var redirectContentType = options.id
46
47
  ? data.contentTypes.get(options.id)
@@ -61,15 +62,49 @@ var getAllRedirects = function (data, options, locale, parent) {
61
62
  return redirect ? [redirect] : [];
62
63
  });
63
64
  };
64
- var buildLocalizedSitemapArrRecursively = function (data, options, locale, parent, pageEntry, acc) {
65
- var pages = data.pages, contentTypes = data.contentTypes;
66
- var pageContentType = contentTypes.get(pageEntry.sys.contentType.sys.id);
67
- var pageContentTypeConfig = options.pageContentTypes[pageEntry.sys.contentType.sys.id];
68
- var findPageField = function (fieldId) {
65
+ var findPageFieldF = function (data, options, pageContentTypeId) {
66
+ return function (fieldId) {
67
+ var contentTypes = data.contentTypes;
68
+ var pageContentType = contentTypes.get(pageContentTypeId);
69
+ var pageContentTypeConfig = options.pageContentTypes[pageContentTypeId];
69
70
  return pageContentType && pageContentTypeConfig
70
71
  ? pageContentType.fields.get(fieldId)
71
72
  : undefined;
72
73
  };
74
+ };
75
+ var getChildrenRefsByParentId = function (data, locale, options) {
76
+ var pages = data.pages;
77
+ var acc = new Map();
78
+ pages.forEach(function (page) {
79
+ var _a;
80
+ var pageContentTypeConfig = options.pageContentTypes[page.sys.contentType.sys.id];
81
+ var findPageField = findPageFieldF(data, options, page.sys.contentType.sys.id);
82
+ var parentPageField = pageContentTypeConfig.parentPageFieldId
83
+ ? findPageField(pageContentTypeConfig.parentPageFieldId)
84
+ : undefined;
85
+ var fieldValue = (0, localize_contentful_field_1.localizeField)(data, locale);
86
+ var parentPage = parentPageField
87
+ ? fieldValue(parentPageField.localized, page.fields[parentPageField.id])
88
+ : undefined;
89
+ var parentPageId = (_a = parentPage === null || parentPage === void 0 ? void 0 : parentPage.sys) === null || _a === void 0 ? void 0 : _a.id;
90
+ if (parentPageId) {
91
+ var pageRef = { sys: { id: page.sys.id } };
92
+ var maybePages = acc.get(parentPageId);
93
+ if (maybePages) {
94
+ maybePages.push(pageRef);
95
+ }
96
+ else {
97
+ acc.set(parentPageId, [pageRef]);
98
+ }
99
+ }
100
+ });
101
+ return acc;
102
+ };
103
+ var buildLocalizedSitemapArrRecursively = function (data, options, locale, parent, childrenRefsByPageId, pageEntry, acc) {
104
+ var _a;
105
+ var pages = data.pages;
106
+ var pageContentTypeConfig = options.pageContentTypes[pageEntry.sys.contentType.sys.id];
107
+ var findPageField = findPageFieldF(data, options, pageEntry.sys.contentType.sys.id);
73
108
  var slugField = findPageField(pageContentTypeConfig.slugFieldId);
74
109
  var titleField = pageContentTypeConfig.titleFieldId
75
110
  ? findPageField(pageContentTypeConfig.titleFieldId)
@@ -77,8 +112,14 @@ var buildLocalizedSitemapArrRecursively = function (data, options, locale, paren
77
112
  var childPagesField = pageContentTypeConfig.childPagesFieldId
78
113
  ? findPageField(pageContentTypeConfig.childPagesFieldId)
79
114
  : undefined;
115
+ var fieldValue = (0, localize_contentful_field_1.localizeField)(data, locale);
116
+ var childrenRefs = childrenRefsByPageId
117
+ ? childrenRefsByPageId.get(pageEntry.sys.id) || []
118
+ : childPagesField
119
+ ? ((_a = fieldValue(childPagesField.localized, pageEntry.fields[childPagesField.id])) === null || _a === void 0 ? void 0 : _a.flatMap(function (childPage) { var _a; return ((_a = childPage === null || childPage === void 0 ? void 0 : childPage.sys) === null || _a === void 0 ? void 0 : _a.id) ? [{ sys: { id: childPage === null || childPage === void 0 ? void 0 : childPage.sys.id } }] : []; })) || []
120
+ : [];
80
121
  var page = slugField
81
- ? (0, page_entry_adapter_1.pageEntryAdapter)(data, slugField, titleField, childPagesField, locale, parent, pageEntry)
122
+ ? (0, page_entry_adapter_1.pageEntryAdapter)(data, slugField, titleField, childrenRefs, locale, parent, pageEntry)
82
123
  : undefined;
83
124
  if (page) {
84
125
  var validChildPages_1 = page.childPages.filter(function (c) { return pages.get(c.sys.id) && !acc.get(c.sys.id); });
@@ -96,7 +137,7 @@ var buildLocalizedSitemapArrRecursively = function (data, options, locale, paren
96
137
  var id = _a.sys.id;
97
138
  var childPageEntry = pages.get(id);
98
139
  childPageEntry &&
99
- buildLocalizedSitemapArrRecursively(data, options, locale, { sys: pageEntry.sys, path: page.path }, childPageEntry, acc);
140
+ buildLocalizedSitemapArrRecursively(data, options, locale, { sys: pageEntry.sys, path: page.path }, childrenRefsByPageId, childPageEntry, acc);
100
141
  });
101
142
  return acc;
102
143
  }
@@ -110,7 +151,10 @@ var buildLocalizedSitemap = function (data, options, pageHomeTagId, locale) {
110
151
  var homePageRef = (_d = (_c = (_b = (_a = homePagesByTagId === null || homePagesByTagId === void 0 ? void 0 : homePagesByTagId.get(locale.code)) === null || _a === void 0 ? void 0 : _a.get(pageHomeTagId)) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.sys) === null || _d === void 0 ? void 0 : _d.id;
111
152
  var homePageEntry = homePageRef ? pages.get(homePageRef) : undefined;
112
153
  if (homePageEntry) {
113
- var sitemap = buildLocalizedSitemapArrRecursively(data, options, locale, undefined, homePageEntry, new Map());
154
+ var childrenRefsByPageId = options.sitemapRepresentation === "child-to-parent"
155
+ ? getChildrenRefsByParentId(data, locale, options)
156
+ : undefined;
157
+ var sitemap = buildLocalizedSitemapArrRecursively(data, options, locale, undefined, childrenRefsByPageId, homePageEntry, new Map());
114
158
  return (0, fp_utils_1.right)({
115
159
  root: { sys: homePageEntry.sys },
116
160
  sitemap: sitemap,
@@ -4,6 +4,7 @@ import { ContentTypeT, EntryT, LocaleT, StatusT, TagT } from "./clients/contentf
4
4
  export type ArboretumClientOptions = {
5
5
  data?: CachedDataT;
6
6
  eagerly?: boolean;
7
+ sitemapRepresentation?: "parent-to-children" | "child-to-parent";
7
8
  includeEntryStatus?: boolean;
8
9
  };
9
10
  export type ArboretumClientContentfulConfigOptionsT = {
@@ -12,6 +13,7 @@ export type ArboretumClientContentfulConfigOptionsT = {
12
13
  slugFieldId: string;
13
14
  titleFieldId?: string;
14
15
  childPagesFieldId?: string;
16
+ parentPageFieldId?: string;
15
17
  };
16
18
  };
17
19
  redirectContentType?: {
@@ -59,7 +61,7 @@ export type ArboretumClientConfigFromCdaT = {
59
61
  client: ContentfulClientApi;
60
62
  options: ArboretumClientContentfulConfigOptionsT;
61
63
  };
62
- options?: Pick<ArboretumClientOptions, "data" | "eagerly">;
64
+ options?: Pick<ArboretumClientOptions, "data" | "eagerly" | "sitemapRepresentation">;
63
65
  };
64
66
  export type CreateClientParams = {
65
67
  space: string;
@@ -76,7 +78,7 @@ export type ArboretumClientConfigFromCdaParamsT = {
76
78
  contentful: Omit<CreateClientParams, "host"> & {
77
79
  options: ArboretumClientContentfulConfigOptionsT;
78
80
  };
79
- options?: Pick<ArboretumClientOptions, "data" | "eagerly">;
81
+ options?: Pick<ArboretumClientOptions, "data" | "eagerly" | "sitemapRepresentation">;
80
82
  };
81
83
  export type ArboretumClientConfigT = ArboretumClientConfigFromCmaT | ArboretumClientConfigFromCdaT | ArboretumClientConfigFromCdaParamsT;
82
84
  type ArboretumPageBaseT = {
@@ -107,7 +109,7 @@ export type OptionsT = {
107
109
  withChildren?: boolean;
108
110
  withAncestors?: boolean;
109
111
  };
110
- export type ArboretumClientOptionsT = Pick<ArboretumClientContentfulConfigOptionsT, "pageContentTypes" | "redirectContentType"> & Pick<ArboretumClientOptions, "includeEntryStatus">;
112
+ export type ArboretumClientOptionsT = Pick<ArboretumClientContentfulConfigOptionsT, "pageContentTypes" | "redirectContentType"> & Pick<ArboretumClientOptions, "includeEntryStatus" | "sitemapRepresentation">;
111
113
  export type ArboretumClientT = {
112
114
  homePage: (localeCode: string, options?: OptionsT) => Either<string, ArboretumPageT>;
113
115
  pageByPath: (path: string, options?: OptionsT) => Either<string, ArboretumPageNodeT>;
@@ -1,6 +1,6 @@
1
- import { PageT } from '../../arboretum-client.impl';
2
- import { ContentTypeT, EntryT, LocaleT } from '../../../clients/contentful-client/contentful-client';
3
- import { SitemapDataT } from '../../data/sitemap-data';
4
- export declare const pageEntryAdapter: (data: Pick<SitemapDataT, 'locales' | 'defaultLocaleCode'>, slugField: ContentTypeT['fields'][number], titleField: ContentTypeT['fields'][number] | undefined, childPagesField: ContentTypeT['fields'][number] | undefined, locale: LocaleT, parent: (NonNullable<PageT['parent']> & {
1
+ import { PageT } from "../../arboretum-client.impl";
2
+ import { ContentTypeT, EntryT, LocaleT } from "../../../clients/contentful-client/contentful-client";
3
+ import { SitemapDataT } from "../../data/sitemap-data";
4
+ export declare const pageEntryAdapter: (data: Pick<SitemapDataT, "locales" | "defaultLocaleCode" | "pages">, slugField: ContentTypeT["fields"][number], titleField: ContentTypeT["fields"][number] | undefined, childrenRefs: PageT["childPages"], locale: LocaleT, parent: (NonNullable<PageT["parent"]> & {
5
5
  path: string;
6
6
  }) | undefined, entry: EntryT) => PageT | undefined;
@@ -1,5 +1,5 @@
1
- import { Either } from '../../../utils/fp-utils';
2
- import { ArboretumClientCtx, LocalizedSitemapT } from '../../arboretum-client.impl';
3
- import { LocaleT } from '../../../clients/contentful-client/contentful-client';
4
- export declare const buildLocalizedSitemap: (data: Pick<ArboretumClientCtx['data'], 'homePagesByTagId' | 'pages' | 'contentTypes' | 'defaultLocaleCode' | 'locales' | 'redirects'>, options: ArboretumClientCtx['options'], pageHomeTagId: ArboretumClientCtx['pageHomeTagId'], locale: LocaleT) => Either<string, LocalizedSitemapT>;
5
- export declare const localizedSitemapFromCacheOrBuildEff: (ctx: Pick<ArboretumClientCtx, 'options' | 'data' | 'pageHomeTagId' | 'sitemap'>, locale: LocaleT) => Either<string, LocalizedSitemapT>;
1
+ import { Either } from "../../../utils/fp-utils";
2
+ import { ArboretumClientCtx, LocalizedSitemapT } from "../../arboretum-client.impl";
3
+ import { LocaleT } from "../../../clients/contentful-client/contentful-client";
4
+ export declare const buildLocalizedSitemap: (data: Pick<ArboretumClientCtx["data"], "homePagesByTagId" | "pages" | "contentTypes" | "defaultLocaleCode" | "locales" | "redirects">, options: ArboretumClientCtx["options"], pageHomeTagId: ArboretumClientCtx["pageHomeTagId"], locale: LocaleT) => Either<string, LocalizedSitemapT>;
5
+ export declare const localizedSitemapFromCacheOrBuildEff: (ctx: Pick<ArboretumClientCtx, "options" | "data" | "pageHomeTagId" | "sitemap">, locale: LocaleT) => Either<string, LocalizedSitemapT>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bright.global/arboretum-sdk",
3
- "version": "0.0.9",
3
+ "version": "0.1.0-rc.0",
4
4
  "license": "MIT",
5
5
  "description": "The sitemap for contentful",
6
6
  "dependencies": {},