storyblok 4.19.1 → 4.19.3

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
@@ -6105,11 +6105,18 @@ const generateStoryblokTypes = async (options = {}) => {
6105
6105
  const pushDatasource = async (spaceId, datasource) => {
6106
6106
  try {
6107
6107
  const client = getMapiClient();
6108
+ const { dimensions, ...datasourceFields } = datasource;
6109
+ const dimensionsAttributes = (dimensions ?? []).filter((dimension) => dimension.entry_value).map((dimension) => ({ name: dimension.name ?? dimension.entry_value ?? "", entry_value: dimension.entry_value ?? "" }));
6108
6110
  const { data } = await client.datasources.create({
6109
6111
  path: {
6110
6112
  space_id: Number(spaceId)
6111
6113
  },
6112
- body: { datasource },
6114
+ body: {
6115
+ datasource: {
6116
+ ...datasourceFields,
6117
+ ...dimensionsAttributes.length > 0 && { dimensions_attributes: dimensionsAttributes }
6118
+ }
6119
+ },
6113
6120
  throwOnError: true
6114
6121
  });
6115
6122
  return data.datasource;
@@ -6144,13 +6151,14 @@ const upsertDatasource = async (space, datasource, existingId) => {
6144
6151
  const pushDatasourceEntry = async (spaceId, datasourceId, entry, position) => {
6145
6152
  try {
6146
6153
  const client = getMapiClient();
6154
+ const { dimension_values, ...entryFields } = entry;
6147
6155
  const { data } = await client.datasourceEntries.create({
6148
6156
  path: {
6149
6157
  space_id: Number(spaceId)
6150
6158
  },
6151
6159
  body: {
6152
6160
  datasource_entry: {
6153
- ...entry,
6161
+ ...entryFields,
6154
6162
  value: entry.value ?? "",
6155
6163
  datasource_id: datasourceId,
6156
6164
  ...position != null && { position }
@@ -6166,13 +6174,14 @@ const pushDatasourceEntry = async (spaceId, datasourceId, entry, position) => {
6166
6174
  const updateDatasourceEntry = async (spaceId, entryId, entry, position) => {
6167
6175
  try {
6168
6176
  const client = getMapiClient();
6177
+ const { dimension_values, ...entryFields } = entry;
6169
6178
  await client.datasourceEntries.update(entryId, {
6170
6179
  path: {
6171
6180
  space_id: Number(spaceId)
6172
6181
  },
6173
6182
  body: {
6174
6183
  datasource_entry: {
6175
- ...entry,
6184
+ ...entryFields,
6176
6185
  ...position != null && { position }
6177
6186
  }
6178
6187
  },
@@ -6182,6 +6191,27 @@ const updateDatasourceEntry = async (spaceId, entryId, entry, position) => {
6182
6191
  handleAPIError("update_datasource", error, `Failed to update datasource entry ${entry.name}`);
6183
6192
  }
6184
6193
  };
6194
+ const updateDatasourceEntryDimension = async (spaceId, entryId, entry, dimensionId, dimensionValue) => {
6195
+ try {
6196
+ const client = getMapiClient();
6197
+ await client.datasourceEntries.update(entryId, {
6198
+ path: {
6199
+ space_id: Number(spaceId)
6200
+ },
6201
+ query: { dimension_id: dimensionId },
6202
+ body: {
6203
+ datasource_entry: {
6204
+ name: entry.name,
6205
+ value: entry.value ?? "",
6206
+ dimension_value: dimensionValue
6207
+ }
6208
+ },
6209
+ throwOnError: true
6210
+ });
6211
+ } catch (error) {
6212
+ handleAPIError("update_datasource", error, `Failed to update datasource entry ${entry.name} for dimension ${dimensionId}`);
6213
+ }
6214
+ };
6185
6215
  const upsertDatasourceEntry = async (space, datasourceId, entry, existingId, position) => {
6186
6216
  if (existingId) {
6187
6217
  await updateDatasourceEntry(space, existingId, entry, position);
@@ -6341,7 +6371,7 @@ const datasourcesCommand = program$6.command(commands.DATASOURCES).alias("ds").d
6341
6371
 
6342
6372
  const DEFAULT_DATASOURCES_FILENAME = "datasources";
6343
6373
 
6344
- const fetchDatasourceEntries = async (spaceId, datasourceId) => {
6374
+ const fetchDatasourceEntries = async (spaceId, datasourceId, dimensionId) => {
6345
6375
  try {
6346
6376
  const client = getMapiClient();
6347
6377
  return await fetchAllPages(
@@ -6351,7 +6381,9 @@ const fetchDatasourceEntries = async (spaceId, datasourceId) => {
6351
6381
  },
6352
6382
  query: {
6353
6383
  datasource_id: datasourceId,
6354
- page
6384
+ page,
6385
+ // MAPI matches `dimension` against the numeric dimension id.
6386
+ ...dimensionId != null && { dimension: String(dimensionId) }
6355
6387
  },
6356
6388
  throwOnError: true
6357
6389
  }),
@@ -6361,6 +6393,34 @@ const fetchDatasourceEntries = async (spaceId, datasourceId) => {
6361
6393
  handleAPIError("pull_datasources", error);
6362
6394
  }
6363
6395
  };
6396
+ const fetchDatasourceEntriesWithDimensions = async (spaceId, datasource) => {
6397
+ const defaultEntries = await fetchDatasourceEntries(spaceId, datasource.id) ?? [];
6398
+ const entries = defaultEntries.map(({ dimension_value, ...rest }) => rest);
6399
+ const dimensions = datasource.dimensions ?? [];
6400
+ if (!dimensions.length) {
6401
+ return entries;
6402
+ }
6403
+ const entriesById = new Map(entries.map((entry) => [entry.id, entry]));
6404
+ for (const dimension of dimensions) {
6405
+ if (dimension.id == null || !dimension.entry_value) {
6406
+ continue;
6407
+ }
6408
+ const dimensionEntries = await fetchDatasourceEntries(spaceId, datasource.id, dimension.id) ?? [];
6409
+ for (const dimensionEntry of dimensionEntries) {
6410
+ const value = dimensionEntry.dimension_value;
6411
+ if (!value) {
6412
+ continue;
6413
+ }
6414
+ const entry = entriesById.get(dimensionEntry.id);
6415
+ if (!entry) {
6416
+ continue;
6417
+ }
6418
+ entry.dimension_values ??= {};
6419
+ entry.dimension_values[dimension.entry_value] = value;
6420
+ }
6421
+ }
6422
+ return entries;
6423
+ };
6364
6424
  const fetchDatasources = async (spaceId) => {
6365
6425
  try {
6366
6426
  const client = getMapiClient();
@@ -6381,7 +6441,7 @@ const fetchDatasources = async (spaceId) => {
6381
6441
  if (!ds.id) {
6382
6442
  return { ...ds, entries: [] };
6383
6443
  }
6384
- const entries = await fetchDatasourceEntries(spaceId, ds.id);
6444
+ const entries = await fetchDatasourceEntriesWithDimensions(spaceId, ds);
6385
6445
  return { ...ds, entries };
6386
6446
  })
6387
6447
  );
@@ -6410,8 +6470,8 @@ const fetchDatasource = async (spaceId, datasourceName) => {
6410
6470
  if (!found) {
6411
6471
  return void 0;
6412
6472
  }
6413
- const entries = await fetchDatasourceEntries(spaceId, found.id);
6414
- return { ...found, entries: entries || [] };
6473
+ const entries = await fetchDatasourceEntriesWithDimensions(spaceId, found);
6474
+ return { ...found, entries };
6415
6475
  } catch (error) {
6416
6476
  handleAPIError("pull_datasources", error, `Failed to fetch datasource ${datasourceName}`);
6417
6477
  }
@@ -6509,6 +6569,14 @@ pullCmd$2.action(async (datasourceName, options, command) => {
6509
6569
  }
6510
6570
  });
6511
6571
 
6572
+ function dimensionValuesEqual(a, b) {
6573
+ const aKeys = Object.keys(a ?? {});
6574
+ const bKeys = Object.keys(b ?? {});
6575
+ if (aKeys.length !== bKeys.length) {
6576
+ return false;
6577
+ }
6578
+ return aKeys.every((key) => a?.[key] === b?.[key]);
6579
+ }
6512
6580
  const pushCmd$2 = datasourcesCommand.command("push [datasourceName]").description(`Push your space's datasources schema as json`).option("-f, --from <from>", "source space id").option("--fi, --filter <filter>", "glob filter to apply to the datasources before pushing").option("--sf, --separate-files", "Read from separate files instead of consolidated files").option("--su, --suffix <suffix>", "Load only files matching *.<suffix>.json (e.g. datasources.dev.json)").option("-s, --space <space>", "space ID");
6513
6581
  pushCmd$2.action(async (datasourceName, options, command) => {
6514
6582
  konsola.title(`${commands.DATASOURCES}`, colorPalette.DATASOURCES, datasourceName ? `Pushing datasource ${datasourceName}...` : "Pushing datasources...");
@@ -6580,19 +6648,39 @@ pushCmd$2.action(async (datasourceName, options, command) => {
6580
6648
  results.successful.push(datasource.name);
6581
6649
  const localEntries = entries ?? [];
6582
6650
  const existingEntries = existingDatasource?.entries ?? [];
6651
+ const dimensionWarnings = [];
6583
6652
  const existingEntryMap = new Map(existingEntries.map((e, idx) => [e.name, { entry: e, position: idx + 1 }]));
6584
6653
  for (let i = 0; i < localEntries.length; i++) {
6585
6654
  const entry = localEntries[i];
6586
6655
  const existing = existingEntryMap.get(entry.name);
6587
6656
  const existingEntryId = existing?.entry.id;
6588
6657
  const targetPosition = i + 1;
6589
- if (existing && existing.entry.value === entry.value && existing.entry.dimension_value === entry.dimension_value && existing.position === targetPosition) {
6658
+ if (existing && existing.entry.value === entry.value && dimensionValuesEqual(existing.entry.dimension_values, entry.dimension_values) && existing.position === targetPosition) {
6590
6659
  logger.info("Skipped datasource entry (unchanged)", { datasource: datasource.name, entry: entry.name, position: targetPosition });
6591
6660
  continue;
6592
6661
  }
6593
6662
  try {
6594
- await upsertDatasourceEntry(space, result.id, entry, existingEntryId, i + 1);
6663
+ const upserted = await upsertDatasourceEntry(space, result.id, entry, existingEntryId, i + 1);
6664
+ const entryId = existingEntryId ?? upserted?.id;
6595
6665
  logger.info(existingEntryId ? "Updated datasource entry" : "Created datasource entry", { datasource: datasource.name, entry: entry.name, position: i + 1 });
6666
+ if (entryId != null && entry.dimension_values !== void 0) {
6667
+ const localDimensionValues = entry.dimension_values;
6668
+ const targetDimensionValues = existing?.entry.dimension_values ?? {};
6669
+ const dimensionCodes = /* @__PURE__ */ new Set([...Object.keys(localDimensionValues), ...Object.keys(targetDimensionValues)]);
6670
+ for (const code of dimensionCodes) {
6671
+ const localValue = localDimensionValues[code] ?? "";
6672
+ const targetValue = targetDimensionValues[code] ?? "";
6673
+ if (localValue === targetValue) {
6674
+ continue;
6675
+ }
6676
+ const dimension = result.dimensions?.find((d) => d.entry_value === code);
6677
+ if (!dimension?.id) {
6678
+ dimensionWarnings.push(`Skipping dimension "${code}" for entry "${entry.name}": no matching dimension in target space ${space}`);
6679
+ continue;
6680
+ }
6681
+ await updateDatasourceEntryDimension(space, entryId, entry, dimension.id, localValue);
6682
+ }
6683
+ }
6596
6684
  } catch (entryError) {
6597
6685
  results.failed.push({ name: datasource.name, error: entryError });
6598
6686
  spinner.failed(`${chalk.hex(colorPalette.DATASOURCES)(datasource.name)} - Failed in ${spinner.elapsedTime.toFixed(2)}ms`);
@@ -6610,6 +6698,7 @@ pushCmd$2.action(async (datasourceName, options, command) => {
6610
6698
  }
6611
6699
  }
6612
6700
  spinner.succeed(`${chalk.hex(colorPalette.DATASOURCES)(datasource.name)} - Completed in ${spinner.elapsedTime.toFixed(2)}ms`);
6701
+ dimensionWarnings.forEach((warning) => konsola.warn(warning));
6613
6702
  } else {
6614
6703
  results.failed.push({ name: datasource.name, error: result });
6615
6704
  spinner.failed(`${chalk.hex(colorPalette.DATASOURCES)(datasource.name)} - Failed in ${spinner.elapsedTime.toFixed(2)}ms`);