@code.store/arcxp-sdk-ts 5.0.0-beta → 5.0.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.
package/dist/index.js CHANGED
@@ -5,6 +5,7 @@ import * as ws from 'ws';
5
5
  import FormData from 'form-data';
6
6
  import encode from 'base32-encode';
7
7
  import { v5 } from 'uuid';
8
+ import assert from 'node:assert';
8
9
  import { TextNode, HTMLElement, CommentNode, parse } from 'node-html-parser';
9
10
  import { decode } from 'html-entities';
10
11
 
@@ -717,7 +718,7 @@ const ArcAPI = (options) => {
717
718
  RetailEvents: new ArcRetailEvents(options),
718
719
  Custom: new Custom(options),
719
720
  };
720
- API.MigrationCenter.setMaxRPS(8);
721
+ API.MigrationCenter.setMaxRPS(12);
721
722
  API.Draft.setMaxRPS(4);
722
723
  API.Content.setMaxRPS(3);
723
724
  return API;
@@ -1148,10 +1149,186 @@ var Id = /*#__PURE__*/Object.freeze({
1148
1149
  generateArcId: generateArcId
1149
1150
  });
1150
1151
 
1152
+ const buildTree = (items) => {
1153
+ const tree = [
1154
+ {
1155
+ id: '/',
1156
+ children: [],
1157
+ meta: new Proxy({}, {
1158
+ get: () => {
1159
+ throw new Error('Root node meta is not accessible');
1160
+ },
1161
+ }),
1162
+ parent: null,
1163
+ },
1164
+ ];
1165
+ // Track nodes at each level to maintain parent-child relationships
1166
+ // stores last node at each level
1167
+ const currLevelNodes = {
1168
+ 0: tree[0],
1169
+ };
1170
+ for (const item of items) {
1171
+ const node = {
1172
+ id: item.id,
1173
+ parent: null,
1174
+ children: [],
1175
+ meta: item,
1176
+ };
1177
+ // Determine the level of this node
1178
+ const levelKey = Object.keys(item).find((key) => key.startsWith('N') && item[key]);
1179
+ const level = Number(levelKey?.replace('N', '')) || 0;
1180
+ if (!level) {
1181
+ throw new Error(`Invalid level for section ${item.id}`);
1182
+ }
1183
+ // This is a child node - attach to its parent
1184
+ const parentLevel = level - 1;
1185
+ const parentNode = currLevelNodes[parentLevel];
1186
+ if (parentNode) {
1187
+ node.parent = parentNode;
1188
+ parentNode.children.push(node);
1189
+ }
1190
+ else {
1191
+ throw new Error(`Parent node not found for section ${item.id}`);
1192
+ }
1193
+ // Set this as the current node for its level
1194
+ currLevelNodes[level] = node;
1195
+ }
1196
+ // return root nodes children
1197
+ return tree[0].children;
1198
+ };
1199
+ const flattenTree = (tree) => {
1200
+ const flatten = [];
1201
+ const traverse = (node) => {
1202
+ flatten.push(node);
1203
+ for (const child of node.children) {
1204
+ traverse(child);
1205
+ }
1206
+ };
1207
+ // traverse all root nodes and their children
1208
+ for (const node of tree) {
1209
+ traverse(node);
1210
+ }
1211
+ return flatten;
1212
+ };
1213
+ const buildAndFlattenTree = (items) => flattenTree(buildTree(items));
1214
+ const groupByWebsites = (sections) => {
1215
+ return sections.reduce((acc, section) => {
1216
+ const website = section._website;
1217
+ if (!acc[website])
1218
+ acc[website] = [];
1219
+ acc[website].push(section);
1220
+ return acc;
1221
+ }, {});
1222
+ };
1223
+ const references = (sections) => {
1224
+ return sections.map((s) => reference({
1225
+ id: s._id,
1226
+ website: s._website,
1227
+ type: 'section',
1228
+ }));
1229
+ };
1230
+ const isReference = (section) => {
1231
+ return section?.type === 'reference' && section?.referent?.type === 'section';
1232
+ };
1233
+ const removeDuplicates = (sections) => {
1234
+ const map = new Map();
1235
+ sections.forEach((s) => {
1236
+ if (isReference(s)) {
1237
+ map.set(`${s.referent.id}${s.referent.website}`, s);
1238
+ }
1239
+ else {
1240
+ map.set(`${s._id}${s._website}`, s);
1241
+ }
1242
+ });
1243
+ return [...map.values()];
1244
+ };
1245
+ class SectionsRepository {
1246
+ constructor(arc) {
1247
+ this.arc = arc;
1248
+ this.sectionsByWebsite = {};
1249
+ this.websitesAreLoaded = false;
1250
+ }
1251
+ async put(ans) {
1252
+ await this.arc.Site.putSection(ans);
1253
+ const created = await this.arc.Site.getSection(ans._id, ans.website);
1254
+ this.save(created);
1255
+ }
1256
+ async loadWebsite(website) {
1257
+ const sections = [];
1258
+ let next = true;
1259
+ let offset = 0;
1260
+ while (next) {
1261
+ const migrated = await this.arc.Site.getSections({ website, offset }).catch((_) => {
1262
+ return { q_results: [] };
1263
+ });
1264
+ if (migrated.q_results.length) {
1265
+ sections.push(...migrated.q_results);
1266
+ offset += migrated.q_results.length;
1267
+ }
1268
+ else {
1269
+ next = false;
1270
+ }
1271
+ }
1272
+ return sections;
1273
+ }
1274
+ async loadWebsites(websites) {
1275
+ for (const website of websites) {
1276
+ this.sectionsByWebsite[website] = await this.loadWebsite(website);
1277
+ }
1278
+ this.websitesAreLoaded = true;
1279
+ }
1280
+ save(section) {
1281
+ const website = section._website;
1282
+ assert.ok(website, 'Section must have a website');
1283
+ this.sectionsByWebsite[website] = this.sectionsByWebsite[website] || [];
1284
+ if (!this.sectionsByWebsite[website].find((s) => s._id === section._id)) {
1285
+ this.sectionsByWebsite[website].push(section);
1286
+ }
1287
+ }
1288
+ getById(id, website) {
1289
+ this.ensureWebsitesLoaded();
1290
+ const section = this.sectionsByWebsite[website]?.find((s) => s._id === id);
1291
+ return section;
1292
+ }
1293
+ getByWebsite(website) {
1294
+ this.ensureWebsitesLoaded();
1295
+ return this.sectionsByWebsite[website];
1296
+ }
1297
+ getParentSections(section) {
1298
+ this.ensureWebsitesLoaded();
1299
+ const parents = [];
1300
+ let current = section;
1301
+ while (current.parent?.default && current.parent.default !== '/') {
1302
+ const parent = this.getById(current.parent.default, section._website);
1303
+ if (!parent)
1304
+ break;
1305
+ parents.push(parent);
1306
+ current = parent;
1307
+ }
1308
+ return parents;
1309
+ }
1310
+ ensureWebsitesLoaded() {
1311
+ assert.ok(this.websitesAreLoaded, 'call .loadWebsites() first');
1312
+ }
1313
+ }
1314
+
1315
+ var Section = /*#__PURE__*/Object.freeze({
1316
+ __proto__: null,
1317
+ SectionsRepository: SectionsRepository,
1318
+ buildAndFlattenTree: buildAndFlattenTree,
1319
+ buildTree: buildTree,
1320
+ flattenTree: flattenTree,
1321
+ groupByWebsites: groupByWebsites,
1322
+ isReference: isReference,
1323
+ references: references,
1324
+ removeDuplicates: removeDuplicates
1325
+ });
1326
+
1151
1327
  const ArcUtils = {
1152
1328
  Id,
1153
1329
  ANS,
1154
1330
  ContentElements,
1331
+ Section,
1155
1332
  };
1156
1333
 
1157
1334
  /**