sfmc-dataloader 2.5.0 → 2.6.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.
package/README.md CHANGED
@@ -175,6 +175,14 @@ Interactive: type `YES` when prompted. In CI, add `--i-accept-clear-data-risk` a
175
175
 
176
176
  Log lines include **row counts** and show file paths as **absolute paths in double-quotes** (e.g. `"C:\data\MyCred\DEV\Contact.mcdata.csv"`) so they are clickable in VS Code's integrated terminal.
177
177
 
178
+ ## Programmatic API (Node.js)
179
+
180
+ Import from `sfmc-dataloader` for use in other tools (for example the **SFMC Data Loader** VS Code extension):
181
+
182
+ | Export | Purpose |
183
+ |--------|---------|
184
+ | `fetchDeList(projectRoot, credential, bu)` | Returns all Data Extension **names** and **customer keys** for one BU via SOAP `retrieveBulk` (pagination handled by **sfmc-sdk**). Not a CLI command — intended for in-process callers that already have mcdev/mcdata config on disk. |
185
+
178
186
  ## License
179
187
 
180
188
  MIT — Author: Jörn Berkefeld
@@ -46,7 +46,18 @@ export function processBusinessUnitResults(results, enterpriseId) {
46
46
  }
47
47
  }
48
48
 
49
- return { eid, businessUnits };
49
+ /** @type {Record<string, number>} */
50
+ const sorted = {};
51
+ if (Object.hasOwn(businessUnits, '_ParentBU_')) {
52
+ sorted['_ParentBU_'] = businessUnits['_ParentBU_'];
53
+ }
54
+ for (const key of Object.keys(businessUnits)
55
+ .filter((k) => k !== '_ParentBU_')
56
+ .toSorted((a, b) => a.localeCompare(b, undefined, { sensitivity: 'base' }))) {
57
+ sorted[key] = businessUnits[key];
58
+ }
59
+
60
+ return { eid, businessUnits: sorted };
50
61
  }
51
62
 
52
63
  /**
package/lib/config.mjs CHANGED
@@ -170,9 +170,32 @@ export function buildSdkAuthObject(authCred, mid) {
170
170
  */
171
171
  export function buildSdkOptions(logger = null) {
172
172
  /** @type {import('sfmc-sdk').SdkOptions} */
173
- const options = { requestAttempts: 3 };
173
+ const options = {
174
+ requestAttempts: 3,
175
+ retryOnConnectionError: true,
176
+ eventHandlers: {
177
+ onLoop: (_type, accumulator) => {
178
+ process.stdout.write(
179
+ ` - Requesting next batch (currently ${accumulator?.length ?? 0} records)\n`,
180
+ );
181
+ },
182
+ onConnectionError: (ex, remainingAttempts) => {
183
+ const endpointStr = ex.endpoint ? String(ex.endpoint) : '';
184
+ const endpointSuffix = endpointStr
185
+ ? ` - ${endpointStr.split('rest.marketingcloudapis.com')[1] ?? endpointStr}`
186
+ : '';
187
+ process.stdout.write(
188
+ ` - Connection problem (Code: ${ex.code}). Retrying ${remainingAttempts} time${
189
+ remainingAttempts > 1 ? 's' : ''
190
+ }${endpointSuffix}\n`,
191
+ );
192
+ console.error(ex);
193
+ },
194
+ },
195
+ };
174
196
  if (logger) {
175
197
  options.eventHandlers = {
198
+ ...options.eventHandlers,
176
199
  logRequest: (req) => {
177
200
  const msg = structuredClone(req);
178
201
  if (msg.headers?.Authorization) {
@@ -0,0 +1,59 @@
1
+ import SDK from 'sfmc-sdk';
2
+ import {
3
+ loadProjectConfig,
4
+ resolveCredentialAndMid,
5
+ buildSdkAuthObject,
6
+ buildSdkOptions,
7
+ } from './config.mjs';
8
+
9
+ /**
10
+ * Maps a SOAP retrieveBulk result for DataExtension into sorted `{ name, key }` rows.
11
+ * Exported for unit tests; not part of the stable public API contract beyond testing.
12
+ *
13
+ * @param {object|null|undefined} bulkResult - `sdk.soap.retrieveBulk` response
14
+ * @returns {{ name: string, key: string }[]}
15
+ */
16
+ export function normalizeDeListFromBulkResult(bulkResult) {
17
+ const rows = bulkResult?.Results;
18
+ if (!Array.isArray(rows) || rows.length === 0) {
19
+ return [];
20
+ }
21
+
22
+ const items = rows
23
+ .map((row) => ({
24
+ name: String(row.Name ?? ''),
25
+ key: String(row.CustomerKey ?? ''),
26
+ }))
27
+ .filter((item) => item.key.length > 0);
28
+
29
+ items.sort((a, b) => a.name.localeCompare(b.name, undefined, { sensitivity: 'base' }));
30
+ return items;
31
+ }
32
+
33
+ /**
34
+ * Retrieves all Data Extension `Name` and `CustomerKey` values for a credential/BU via SOAP
35
+ * (`retrieveBulk` handles pagination). For programmatic use (e.g. VS Code extension cache), not the CLI.
36
+ *
37
+ * @param {string} projectRoot - Absolute path to project root (mcdev or mcdata config pair)
38
+ * @param {string} credential - Credential name from config
39
+ * @param {string} bu - Business unit key from config
40
+ * @returns {Promise.<{ name: string, key: string }[]>}
41
+ */
42
+ export async function fetchDeList(projectRoot, credential, bu) {
43
+ const { mcdevrc, mcdevAuth } = loadProjectConfig(projectRoot);
44
+ const { mid, authCred } = resolveCredentialAndMid(mcdevrc, mcdevAuth, credential, bu);
45
+ const sdk = new SDK(buildSdkAuthObject(authCred, mid), buildSdkOptions());
46
+
47
+ let bulkResult;
48
+ try {
49
+ bulkResult = await sdk.soap.retrieveBulk('DataExtension', ['Name', 'CustomerKey'], {});
50
+ } catch (ex) {
51
+ const message = ex instanceof Error ? ex.message : String(ex);
52
+ throw new Error(
53
+ `Could not retrieve Data Extensions — check credentials and BU. Original error: ${message}`,
54
+ { cause: ex },
55
+ );
56
+ }
57
+
58
+ return normalizeDeListFromBulkResult(bulkResult);
59
+ }
package/lib/index.mjs CHANGED
@@ -29,3 +29,4 @@ export { multiBuExport } from './multi-bu-export.mjs';
29
29
  export { crossBuImport } from './cross-bu-import.mjs';
30
30
  export { getDeRowCount } from './row-count.mjs';
31
31
  export { projectRelativePosix } from './paths.mjs';
32
+ export { fetchDeList } from './de-list.mjs';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sfmc-dataloader",
3
- "version": "2.5.0",
3
+ "version": "2.6.1",
4
4
  "description": "SFMC Data Loader CLI (mcdata) — standalone export/import of Marketing Cloud Data Extension rows; optional mcdev integration",
5
5
  "author": "Jörn Berkefeld <joern.berkefeld@gmail.com>",
6
6
  "license": "MIT",