storyblok 4.2.2 → 4.3.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
@@ -11,6 +11,7 @@ import fs, { mkdir, writeFile, readFile as readFile$1, access, readdir } from 'n
11
11
  import path, { join, parse, resolve } from 'node:path';
12
12
  import { exec, spawn } from 'node:child_process';
13
13
  import { promisify } from 'node:util';
14
+ import { getRegion } from '@storyblok/region-helper';
14
15
  import { minimatch } from 'minimatch';
15
16
  import { hash } from 'ohash';
16
17
  import { compile } from 'json-schema-to-typescript';
@@ -1019,8 +1020,30 @@ program$f.command(commands.USER).description("Get the current user").action(asyn
1019
1020
  konsola.br();
1020
1021
  });
1021
1022
 
1023
+ function getRegionFromSpaceId(spaceId) {
1024
+ try {
1025
+ const region = getRegion(spaceId);
1026
+ return region;
1027
+ } catch (error) {
1028
+ console.warn(`Failed to determine region from space ID: ${error}`);
1029
+ return void 0;
1030
+ }
1031
+ }
1032
+ const resolveRegion = async (thisCommand) => {
1033
+ const options = thisCommand.opts();
1034
+ const spaceId = options.space;
1035
+ if (spaceId) {
1036
+ const { state, initializeSession } = session();
1037
+ await initializeSession();
1038
+ const detectedRegion = getRegionFromSpaceId(spaceId);
1039
+ if (detectedRegion) {
1040
+ state.region = detectedRegion;
1041
+ }
1042
+ }
1043
+ };
1044
+
1022
1045
  const program$e = getProgram();
1023
- const componentsCommand = program$e.command(commands.COMPONENTS).alias("comp").description(`Manage your space's block schema`).option("-s, --space <space>", "space ID").option("-p, --path <path>", "path to save the file. Default is .storyblok/components");
1046
+ const componentsCommand = program$e.command(commands.COMPONENTS).alias("comp").description(`Manage your space's block schema`).option("-s, --space <space>", "space ID").option("-p, --path <path>", "path to save the file. Default is .storyblok/components").hook("preAction", resolveRegion);
1024
1047
 
1025
1048
  let instance = null;
1026
1049
  const createMapiClient = (options) => {
@@ -2916,7 +2939,7 @@ const saveLanguagesToFile = async (space, internationalizationOptions, options)
2916
2939
  };
2917
2940
 
2918
2941
  const program$b = getProgram();
2919
- const languagesCommand = program$b.command(commands.LANGUAGES).alias("lang").description(`Manage your space's languages`).option("-s, --space <space>", "space ID").option("-p, --path <path>", "path to save the file. Default is .storyblok/languages");
2942
+ const languagesCommand = program$b.command(commands.LANGUAGES).alias("lang").description(`Manage your space's languages`).option("-s, --space <space>", "space ID").option("-p, --path <path>", "path to save the file. Default is .storyblok/languages").hook("preAction", resolveRegion);
2920
2943
  languagesCommand.command("pull").description(`Download your space's languages schema as json`).option("-f, --filename <filename>", "filename to save the file as <filename>.<suffix>.json").option("--su, --suffix <suffix>", "suffix to add to the file name (e.g. languages.<suffix>.json). By default, the space ID is used.").action(async (options) => {
2921
2944
  konsola.title(`${commands.LANGUAGES}`, colorPalette.LANGUAGES);
2922
2945
  const verbose = program$b.opts().verbose;
@@ -2961,7 +2984,7 @@ languagesCommand.command("pull").description(`Download your space's languages sc
2961
2984
  });
2962
2985
 
2963
2986
  const program$a = getProgram();
2964
- const migrationsCommand = program$a.command(commands.MIGRATIONS).alias("mig").description(`Manage your space's migrations`).option("-s, --space <space>", "space ID").option("-p, --path <path>", "path to save the file. Default is .storyblok/migrations");
2987
+ const migrationsCommand = program$a.command(commands.MIGRATIONS).alias("mig").description(`Manage your space's migrations`).option("-s, --space <space>", "space ID").option("-p, --path <path>", "path to save the file. Default is .storyblok/migrations").hook("preAction", resolveRegion);
2965
2988
 
2966
2989
  const getMigrationTemplate = () => {
2967
2990
  return `export default function (block) {
@@ -3034,9 +3057,9 @@ migrationsCommand.command("generate [componentName]").description("Generate a mi
3034
3057
  }
3035
3058
  });
3036
3059
 
3037
- const fetchStories = async (space, token, region, params) => {
3060
+ const fetchStories = async (space, params) => {
3038
3061
  try {
3039
- const url = getStoryblokUrl(region);
3062
+ const client = mapiClient();
3040
3063
  const allStories = [];
3041
3064
  let currentPage = 1;
3042
3065
  let hasMorePages = true;
@@ -3047,14 +3070,10 @@ const fetchStories = async (space, token, region, params) => {
3047
3070
  ...currentPage > 1 && { page: currentPage.toString() }
3048
3071
  }).toString();
3049
3072
  const queryString = filter_query ? `${regularParams ? `${regularParams}&` : ""}${filter_query}` : regularParams;
3050
- const endpoint = `${url}/spaces/${space}/stories${queryString ? `?${queryString}` : ""}`;
3051
- const response = await customFetch(endpoint, {
3052
- headers: {
3053
- Authorization: token
3054
- }
3055
- });
3056
- allStories.push(...response.stories);
3057
- const totalPages = Math.ceil(response.total / response.perPage);
3073
+ const endpoint = `spaces/${space}/stories${queryString ? `?${queryString}` : ""}`;
3074
+ const { data } = await client.get(endpoint, {});
3075
+ allStories.push(...data.stories);
3076
+ const totalPages = Math.ceil(data.total / data.per_page);
3058
3077
  hasMorePages = currentPage < totalPages;
3059
3078
  currentPage++;
3060
3079
  }
@@ -3064,7 +3083,7 @@ const fetchStories = async (space, token, region, params) => {
3064
3083
  }
3065
3084
  };
3066
3085
  async function fetchStoriesByComponent(spaceOptions, filterOptions) {
3067
- const { spaceId, token, region } = spaceOptions;
3086
+ const { spaceId } = spaceOptions;
3068
3087
  const { componentName = "", query, starts_with } = filterOptions || {};
3069
3088
  const params = {
3070
3089
  ...starts_with && { starts_with }
@@ -3076,39 +3095,30 @@ async function fetchStoriesByComponent(spaceOptions, filterOptions) {
3076
3095
  params.filter_query = query.startsWith("filter_query") ? query : `filter_query${query}`;
3077
3096
  }
3078
3097
  try {
3079
- const stories = await fetchStories(spaceId, token, region, params);
3098
+ const stories = await fetchStories(spaceId, params);
3080
3099
  return stories ?? [];
3081
3100
  } catch (error) {
3082
3101
  handleAPIError("pull_stories", error);
3083
3102
  }
3084
3103
  }
3085
- const fetchStory = async (space, token, region, storyId) => {
3104
+ const fetchStory = async (space, storyId) => {
3086
3105
  try {
3087
- const url = getStoryblokUrl(region);
3088
- const endpoint = `${url}/spaces/${space}/stories/${storyId}`;
3089
- const response = await customFetch(endpoint, {
3090
- headers: {
3091
- Authorization: token
3092
- }
3093
- });
3094
- return response.story;
3106
+ const client = mapiClient();
3107
+ const endpoint = `spaces/${space}/stories/${storyId}`;
3108
+ const { data } = await client.get(endpoint, {});
3109
+ return data.story;
3095
3110
  } catch (error) {
3096
3111
  handleAPIError("pull_story", error);
3097
3112
  }
3098
3113
  };
3099
- const updateStory = async (space, token, region, storyId, payload) => {
3114
+ const updateStory = async (space, storyId, payload) => {
3100
3115
  try {
3101
- const url = getStoryblokUrl(region);
3102
- const endpoint = `${url}/spaces/${space}/stories/${storyId}`;
3103
- const response = await customFetch(endpoint, {
3104
- method: "PUT",
3105
- headers: {
3106
- "Authorization": token,
3107
- "Content-Type": "application/json"
3108
- },
3116
+ const client = mapiClient();
3117
+ const endpoint = `spaces/${space}/stories/${storyId}`;
3118
+ const { data } = await client.put(endpoint, {
3109
3119
  body: JSON.stringify(payload)
3110
3120
  });
3111
- return response.story;
3121
+ return data.story;
3112
3122
  } catch (error) {
3113
3123
  handleAPIError("update_story", error);
3114
3124
  }
@@ -3424,6 +3434,10 @@ migrationsCommand.command("run [componentName]").description("Run migrations").o
3424
3434
  return;
3425
3435
  }
3426
3436
  const { password, region } = state;
3437
+ mapiClient({
3438
+ token: password,
3439
+ region
3440
+ });
3427
3441
  try {
3428
3442
  const spinner = new Spinner({
3429
3443
  verbose: !isVitest
@@ -3448,9 +3462,7 @@ migrationsCommand.command("run [componentName]").description("Run migrations").o
3448
3462
  const storiesSpinner = new Spinner({ verbose: !isVitest }).start(`Fetching stories...`);
3449
3463
  const stories = await fetchStoriesByComponent(
3450
3464
  {
3451
- spaceId: space,
3452
- token: password,
3453
- region
3465
+ spaceId: space
3454
3466
  },
3455
3467
  // Filter options
3456
3468
  {
@@ -3464,7 +3476,7 @@ migrationsCommand.command("run [componentName]").description("Run migrations").o
3464
3476
  return;
3465
3477
  }
3466
3478
  const storiesWithContent = await Promise.all(stories.map(async (story) => {
3467
- const fullStory = await fetchStory(space, password, region, story.id.toString());
3479
+ const fullStory = await fetchStory(space, story.id.toString());
3468
3480
  return {
3469
3481
  ...story,
3470
3482
  content: fullStory?.content
@@ -3536,7 +3548,7 @@ migrationsCommand.command("run [componentName]").description("Run migrations").o
3536
3548
  payload.publish = 1;
3537
3549
  }
3538
3550
  try {
3539
- const updatedStory = await updateStory(space, password, region, story.id, payload);
3551
+ const updatedStory = await updateStory(space, story.id, payload);
3540
3552
  if (updatedStory) {
3541
3553
  successCount++;
3542
3554
  storySpinner.succeed(`Updated story ${chalk.hex(colorPalette.PRIMARY)(story.name || story.id.toString())} - Completed in ${storySpinner.elapsedTime.toFixed(2)}ms`);
@@ -3608,7 +3620,7 @@ migrationsCommand.command("rollback [migrationFile]").description("Rollback a mi
3608
3620
  });
3609
3621
 
3610
3622
  const program$6 = getProgram();
3611
- const typesCommand = program$6.command(commands.TYPES).alias("ts").description(`Generate types d.ts for your component schemas`).option("-s, --space <space>", "space ID").option("-p, --path <path>", "path to save the file. Default is .storyblok/types");
3623
+ const typesCommand = program$6.command(commands.TYPES).alias("ts").description(`Generate types d.ts for your component schemas`).option("-s, --space <space>", "space ID").option("-p, --path <path>", "path to save the file. Default is .storyblok/types").hook("preAction", resolveRegion);
3612
3624
 
3613
3625
  const getAssetJSONSchema = (title) => ({
3614
3626
  $id: "#/asset",
@@ -4034,7 +4046,7 @@ const DEFAULT_TYPEDEFS_HEADER = [
4034
4046
  "// This file was generated by the storyblok CLI.",
4035
4047
  "// DO NOT MODIFY THIS FILE BY HAND."
4036
4048
  ];
4037
- const getPropertyTypeAnnotation = (property, prefix) => {
4049
+ const getPropertyTypeAnnotation = (property, prefix, suffix) => {
4038
4050
  if (Array.from(storyblokSchemas.keys()).includes(property.type)) {
4039
4051
  return { type: property.type };
4040
4052
  }
@@ -4047,11 +4059,11 @@ const getPropertyTypeAnnotation = (property, prefix) => {
4047
4059
  if (property.filter_content_type) {
4048
4060
  if (typeof property.filter_content_type === "string") {
4049
4061
  return {
4050
- tsType: `(${getStoryType(property.filter_content_type, prefix)} | string )${property.type === "options" ? "[]" : ""}`
4062
+ tsType: `(${getStoryType(property.filter_content_type, prefix, suffix)} | string )${property.type === "options" ? "[]" : ""}`
4051
4063
  };
4052
4064
  }
4053
4065
  return {
4054
- tsType: `(${property.filter_content_type.map((type2) => getStoryType(type2, prefix)).join(" | ")} | string )${property.type === "options" ? "[]" : ""}`
4066
+ tsType: `(${property.filter_content_type.map((type2) => getStoryType(type2, prefix, suffix)).join(" | ")} | string )${property.type === "options" ? "[]" : ""}`
4055
4067
  };
4056
4068
  }
4057
4069
  }
@@ -4106,13 +4118,14 @@ const getPropertyTypeAnnotation = (property, prefix) => {
4106
4118
  return { type: "any" };
4107
4119
  }
4108
4120
  };
4109
- function getStoryType(property, prefix) {
4110
- return `${STORY_TYPE}<${prefix ?? ""}${capitalize(toCamelCase(property))}>`;
4121
+ function getStoryType(property, prefix, suffix) {
4122
+ return `${STORY_TYPE}<${prefix ?? ""}${capitalize(toCamelCase(property))}${suffix ?? ""}>`;
4111
4123
  }
4112
4124
  const getComponentType = (componentName, options) => {
4113
4125
  const prefix = options.typePrefix ?? "";
4126
+ const suffix = options.typeSuffix ?? "";
4114
4127
  const sanitizedName = componentName.replace(/[^a-z0-9]/gi, "_").replace(/_+/g, "_").replace(/^_+|_+$/g, "");
4115
- const componentType = toPascalCase(toCamelCase(`${prefix}_${sanitizedName}`));
4128
+ const componentType = toPascalCase(toCamelCase(`${prefix}_${sanitizedName}_${suffix}`));
4116
4129
  const isFirstCharacterNumber = !Number.isNaN(Number.parseInt(componentType.charAt(0)));
4117
4130
  return isFirstCharacterNumber ? `_${componentType}` : componentType;
4118
4131
  };
@@ -4124,7 +4137,7 @@ const getComponentPropertiesTypeAnnotations = async (component, options, spaceDa
4124
4137
  }
4125
4138
  const propertyType = value.type;
4126
4139
  const propertyTypeAnnotation = {
4127
- [key]: getPropertyTypeAnnotation(value, options.typePrefix)
4140
+ [key]: getPropertyTypeAnnotation(value, options.typePrefix, options.typeSuffix)
4128
4141
  };
4129
4142
  if (propertyType === "custom" && customFieldsParser) {
4130
4143
  const customField = typeof customFieldsParser === "function" ? customFieldsParser(key, value) : {};
@@ -4308,7 +4321,7 @@ const generateStoryblokTypes = async (options = {}) => {
4308
4321
  };
4309
4322
 
4310
4323
  const program$5 = getProgram();
4311
- typesCommand.command("generate").description("Generate types d.ts for your component schemas").option("--sf, --separate-files", "").option("--strict", "strict mode, no loose typing").option("--type-prefix <prefix>", "prefix to be prepended to all generated component type names").option("--suffix <suffix>", "Components suffix").option("--custom-fields-parser <path>", "Path to the parser file for Custom Field Types").option("--compiler-options <options>", "path to the compiler options from json-schema-to-typescript").action(async (options) => {
4324
+ typesCommand.command("generate").description("Generate types d.ts for your component schemas").option("--sf, --separate-files", "").option("--strict", "strict mode, no loose typing").option("--type-prefix <prefix>", "prefix to be prepended to all generated component type names").option("--type-suffix <suffix>", "suffix to be appended to all generated component type names").option("--suffix <suffix>", "Components suffix").option("--custom-fields-parser <path>", "Path to the parser file for Custom Field Types").option("--compiler-options <options>", "path to the compiler options from json-schema-to-typescript").action(async (options) => {
4312
4325
  konsola.title(`${commands.TYPES}`, colorPalette.TYPES, "Generating types...");
4313
4326
  const verbose = program$5.opts().verbose;
4314
4327
  const { space, path } = typesCommand.opts();
@@ -4351,7 +4364,7 @@ typesCommand.command("generate").description("Generate types d.ts for your compo
4351
4364
  });
4352
4365
 
4353
4366
  const program$4 = getProgram();
4354
- const datasourcesCommand = program$4.command(commands.DATASOURCES).alias("ds").description(`Manage your space's datasources`).option("-s, --space <space>", "space ID").option("-p, --path <path>", "path to save the file. Default is .storyblok/datasources");
4367
+ const datasourcesCommand = program$4.command(commands.DATASOURCES).alias("ds").description(`Manage your space's datasources`).option("-s, --space <space>", "space ID").option("-p, --path <path>", "path to save the file. Default is .storyblok/datasources").hook("preAction", resolveRegion);
4355
4368
 
4356
4369
  const program$3 = getProgram();
4357
4370
  datasourcesCommand.command("pull [datasourceName]").option("-f, --filename <filename>", "custom name to be used in file(s) name instead of space id").option("--sf, --separate-files", "Argument to create a single file for each datasource").option("--su, --suffix <suffix>", "suffix to add to the file name (e.g. datasources.<suffix>.json)").description("Pull datasources from your space").action(async (datasourceName, options) => {
@@ -4872,7 +4885,7 @@ program$1.command(`${commands.CREATE} [project-path]`).alias("c").description(`S
4872
4885
  konsola.br();
4873
4886
  });
4874
4887
 
4875
- const version = "4.2.2";
4888
+ const version = "4.3.1";
4876
4889
  const pkg = {
4877
4890
  version: version};
4878
4891
 
@@ -4887,7 +4900,8 @@ program.helpOption("-h, --help", "Display help for command");
4887
4900
  program.on("command:*", () => {
4888
4901
  console.error(`Invalid command: ${program.args.join(" ")}`);
4889
4902
  konsola.br();
4890
- program.help();
4903
+ program.outputHelp();
4904
+ process.exit(1);
4891
4905
  });
4892
4906
  try {
4893
4907
  program.parse(process.argv);