storyblok 4.20.0 → 4.21.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/dist/index.mjs CHANGED
@@ -714,6 +714,7 @@ const API_ACTIONS = {
714
714
  update_story: "Failed to update story",
715
715
  pull_asset: "Failed to pull asset",
716
716
  pull_assets: "Failed to pull assets",
717
+ transfer_enumerate_assets: "Failed to enumerate assets for transfer",
717
718
  pull_asset_folder: "Failed to pull asset folder",
718
719
  pull_asset_folders: "Failed to pull asset folders",
719
720
  push_asset_folder: "Failed to push asset folder",
@@ -6059,6 +6060,9 @@ const generateTypes = async (spaceData, options = {
6059
6060
  },
6060
6061
  _uid: {
6061
6062
  type: "string"
6063
+ },
6064
+ _editable: {
6065
+ tsType: "string | undefined"
6062
6066
  }
6063
6067
  }
6064
6068
  };
@@ -7476,6 +7480,27 @@ const fetchAssets = async ({ spaceId, params }) => {
7476
7480
  handleAPIError("pull_assets", toError(maybeError));
7477
7481
  }
7478
7482
  };
7483
+ const fetchAllSpaceAssetIds = async (spaceId, params) => {
7484
+ try {
7485
+ const client = getMapiClient();
7486
+ const assets = await fetchAllPages(
7487
+ (page) => client.assets.list({
7488
+ path: { space_id: Number(spaceId) },
7489
+ query: { ...params, page, per_page: 100 },
7490
+ throwOnError: true
7491
+ }),
7492
+ (data) => data?.assets ?? []
7493
+ );
7494
+ return assets.reduce((ids, asset) => {
7495
+ if (typeof asset?.id === "number") {
7496
+ ids.push(asset.id);
7497
+ }
7498
+ return ids;
7499
+ }, []);
7500
+ } catch (maybeError) {
7501
+ handleAPIError("transfer_enumerate_assets", toError(maybeError));
7502
+ }
7503
+ };
7479
7504
  const fetchAssetInternalTagsByName = async (spaceId) => {
7480
7505
  try {
7481
7506
  const client = getMapiClient();
@@ -7651,22 +7676,32 @@ const transferAsset = async (spaceId, assetId, folderId) => {
7651
7676
  );
7652
7677
  }
7653
7678
  };
7679
+ const TRANSFER_CHUNK_SIZE = 500;
7654
7680
  const transferAssets = async (spaceId, assetIds, folderId, callbacks = {}) => {
7655
7681
  const lock = createPipelineBackpressureLock();
7656
- return Promise.all(assetIds.map(async (assetId) => {
7657
- await lock.acquire();
7658
- try {
7659
- const asset = await transferAsset(spaceId, assetId, folderId);
7660
- callbacks.onSuccess?.({ assetId, filename: asset.filename });
7661
- return { assetId, status: "transferred", filename: asset.filename };
7662
- } catch (maybeError) {
7663
- const error = toError(maybeError);
7664
- callbacks.onError?.(error, assetId);
7665
- return { assetId, status: "failed", reason: error.message };
7666
- } finally {
7667
- lock.release();
7668
- }
7669
- }));
7682
+ const results = [];
7683
+ let completed = 0;
7684
+ for (let start = 0; start < assetIds.length; start += TRANSFER_CHUNK_SIZE) {
7685
+ const chunk = assetIds.slice(start, start + TRANSFER_CHUNK_SIZE);
7686
+ const chunkResults = await Promise.all(chunk.map(async (assetId) => {
7687
+ await lock.acquire();
7688
+ try {
7689
+ const asset = await transferAsset(spaceId, assetId, folderId);
7690
+ callbacks.onSuccess?.({ assetId, filename: asset.filename });
7691
+ return { assetId, status: "transferred", filename: asset.filename };
7692
+ } catch (maybeError) {
7693
+ const error = toError(maybeError);
7694
+ callbacks.onError?.(error, assetId);
7695
+ return { assetId, status: "failed", reason: error.message };
7696
+ } finally {
7697
+ lock.release();
7698
+ completed += 1;
7699
+ callbacks.onProgress?.(completed, assetIds.length);
7700
+ }
7701
+ }));
7702
+ results.push(...chunkResults);
7703
+ }
7704
+ return results;
7670
7705
  };
7671
7706
  const fetchSharedAssets = async ({ spaceId, libraryId, params }) => {
7672
7707
  try {
@@ -10174,7 +10209,7 @@ pushCmd$1.action(async (assetInput, options, command) => {
10174
10209
  }
10175
10210
  });
10176
10211
 
10177
- const transferCmd = assetsCommand.command("transfer <asset-id...>").option("-s, --space <space>", "space ID").option("--folder-id <folderId>", "destination asset folder ID in the shared library").option("-d, --dry-run", "Preview changes without applying them to Storyblok").description(`Transfer space assets into the organization's shared asset library.`);
10212
+ const transferCmd = assetsCommand.command("transfer [asset-id...]").option("-s, --space <space>", "space ID").option("--folder-id <folderId>", "destination asset folder ID in the shared library").option("--all", "Transfer every asset in the space to the shared library").option("-q, --query <query>", 'Transfer every asset in the space matching a Storyblok filter query. Example: --query="search=my-file.jpg&with_tags=tag1,tag2"').option("-d, --dry-run", "Preview changes without applying them to Storyblok").description(`Transfer space assets into the organization's shared asset library.`);
10178
10213
  transferCmd.action(async (assetIds, options, command) => {
10179
10214
  const ui = getUI();
10180
10215
  const logger = getLogger();
@@ -10203,12 +10238,43 @@ transferCmd.action(async (assetIds, options, command) => {
10203
10238
  process.exitCode = 2;
10204
10239
  return;
10205
10240
  }
10206
- const ids = assetIds.map((id) => Number(id)).filter((id) => !Number.isNaN(id));
10207
- if (ids.length === 0) {
10208
- handleError(new CommandError(`Please provide at least one valid asset ID.`), verbose);
10241
+ if (options.all && options.query) {
10242
+ handleError(new CommandError(`Cannot combine --all with --query. Use --all to transfer every asset, or --query to transfer a filtered subset.`), verbose);
10243
+ process.exitCode = 2;
10244
+ return;
10245
+ }
10246
+ const bulk = Boolean(options.all || options.query);
10247
+ if (bulk && assetIds.length > 0) {
10248
+ handleError(new CommandError(`Cannot combine explicit asset IDs with --all or --query.`), verbose);
10209
10249
  process.exitCode = 2;
10210
10250
  return;
10211
10251
  }
10252
+ let ids;
10253
+ if (bulk) {
10254
+ const params = options.query ? Object.fromEntries(new URLSearchParams(options.query)) : void 0;
10255
+ logger.info("Enumerating space assets", { space, query: options.query });
10256
+ try {
10257
+ ids = await fetchAllSpaceAssetIds(space, params);
10258
+ } catch (maybeError) {
10259
+ handleError(toError(maybeError), verbose);
10260
+ process.exitCode = 2;
10261
+ return;
10262
+ }
10263
+ logger.info("Enumerated space assets", { count: ids.length });
10264
+ if (ids.length === 0) {
10265
+ ui.info(options.query ? `No assets in space ${space} match the query. Nothing to transfer.` : `No assets found in space ${space}. Nothing to transfer.`);
10266
+ logger.info("Transferring assets finished (no assets found)");
10267
+ process.exitCode = 0;
10268
+ return;
10269
+ }
10270
+ } else {
10271
+ ids = assetIds.map((id) => Number(id)).filter((id) => !Number.isNaN(id));
10272
+ if (ids.length === 0) {
10273
+ handleError(new CommandError(`Please provide at least one valid asset ID, or use --all.`), verbose);
10274
+ process.exitCode = 2;
10275
+ return;
10276
+ }
10277
+ }
10212
10278
  if (options.dryRun) {
10213
10279
  const summary2 = { total: ids.length, succeeded: 0, failed: 0 };
10214
10280
  ui.info(`Transfer plan: ${ids.length} asset(s) to folder ${folderId}`);
@@ -10219,14 +10285,33 @@ transferCmd.action(async (assetIds, options, command) => {
10219
10285
  process.exitCode = 0;
10220
10286
  return;
10221
10287
  }
10288
+ const progress = ui.createProgressBar({ title: "Transferring assets..." });
10289
+ progress.setTotal(ids.length);
10222
10290
  const results = await transferAssets(space, ids, folderId, {
10223
10291
  onSuccess: ({ assetId, filename }) => logger.info("Transferred asset", { assetId, filename }),
10224
- onError: (error, assetId) => logOnlyError(error, { assetId })
10292
+ onError: (error, assetId) => logOnlyError(error, { assetId }),
10293
+ onProgress: () => progress.increment()
10225
10294
  });
10295
+ ui.stopAllProgressBars();
10226
10296
  const succeeded = results.filter((result) => result.status === "transferred").length;
10227
10297
  const summary = { total: results.length, succeeded, failed: results.length - succeeded };
10228
- ui.info(`Transfer results: ${summary.total} processed, ${summary.failed} failed`);
10229
- ui.list(results.map((result) => result.status === "transferred" ? `${result.assetId} \u2713 transferred -> ${result.filename}` : `${result.assetId} \u2717 failed: ${result.reason}`));
10298
+ ui.info(`Transfer results: ${summary.succeeded} transferred, ${summary.failed} failed (of ${summary.total}).`);
10299
+ if (summary.failed > 0) {
10300
+ const countsByReason = /* @__PURE__ */ new Map();
10301
+ for (const result of results) {
10302
+ if (result.status === "failed") {
10303
+ const reason = result.reason ?? "Unknown error";
10304
+ countsByReason.set(reason, (countsByReason.get(reason) ?? 0) + 1);
10305
+ }
10306
+ }
10307
+ const byReason = [...countsByReason.entries()].sort((a, b) => b[1] - a[1]);
10308
+ const MAX_REASONS = 10;
10309
+ const lines = byReason.slice(0, MAX_REASONS).map(([reason, count]) => `\u2717 ${reason} (${count})`);
10310
+ if (byReason.length > MAX_REASONS) {
10311
+ lines.push(`\u2026 and ${byReason.length - MAX_REASONS} more failure reason(s)`);
10312
+ }
10313
+ ui.list(lines);
10314
+ }
10230
10315
  reporter.addSummary("transferResults", summary);
10231
10316
  reporter.finalize();
10232
10317
  logger.info("Transferring assets finished", { summary });