storyblok 4.22.1 → 4.22.2

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.d.mts CHANGED
@@ -516,7 +516,7 @@ type StoryblokPropertyType = "asset" | "multiasset" | "multilink" | "table" | "r
516
516
  interface StoryblokAsset {
517
517
  alt: string | null;
518
518
  fieldtype: "asset";
519
- id: number;
519
+ id: number | null;
520
520
  filename: string | null;
521
521
  name: string;
522
522
  title: string | null;
package/dist/index.mjs CHANGED
@@ -4958,317 +4958,15 @@ migrationsCommand.command("rollback [migrationFile]").description("Rollback a mi
4958
4958
  });
4959
4959
  const typesCommand = getProgram().command(commands.TYPES).alias("ts").description(`Generate types d.ts for your component schemas`);
4960
4960
  //#endregion
4961
- //#region src/utils/storyblok-schemas.ts
4962
- const getAssetJSONSchema = (title) => ({
4963
- $id: "#/asset",
4964
- title,
4965
- type: "object",
4966
- required: [
4967
- "id",
4968
- "fieldtype",
4969
- "filename",
4970
- "name",
4971
- "title",
4972
- "focus",
4973
- "alt"
4974
- ],
4975
- properties: {
4976
- alt: { type: ["string", "null"] },
4977
- copyright: { type: ["string", "null"] },
4978
- fieldtype: {
4979
- type: "string",
4980
- enum: ["asset"]
4981
- },
4982
- id: { type: "number" },
4983
- filename: { type: ["string", "null"] },
4984
- name: { type: "string" },
4985
- title: { type: ["string", "null"] },
4986
- focus: { type: ["string", "null"] },
4987
- meta_data: { type: "object" },
4988
- source: { type: ["string", "null"] },
4989
- is_external_url: { type: "boolean" },
4990
- is_private: { type: "boolean" },
4991
- src: { type: "string" },
4992
- updated_at: { type: "string" },
4993
- width: { type: ["number", "null"] },
4994
- height: { type: ["number", "null"] },
4995
- aspect_ratio: { type: ["number", "null"] },
4996
- public_id: { type: ["string", "null"] },
4997
- content_type: { type: "string" }
4998
- }
4999
- });
5000
- const getMultiassetJSONSchema = (title) => ({
5001
- $id: "#/multiasset",
5002
- title,
5003
- type: "array",
5004
- items: {
5005
- type: "object",
5006
- required: [
5007
- "id",
5008
- "fieldtype",
5009
- "filename",
5010
- "name",
5011
- "title",
5012
- "focus",
5013
- "alt"
5014
- ],
5015
- properties: {
5016
- alt: { type: ["string", "null"] },
5017
- copyright: { type: ["string", "null"] },
5018
- fieldtype: {
5019
- type: "string",
5020
- enum: ["asset"]
5021
- },
5022
- id: { type: "number" },
5023
- filename: { type: ["string", "null"] },
5024
- name: { type: "string" },
5025
- title: { type: ["string", "null"] },
5026
- focus: { type: ["string", "null"] },
5027
- meta_data: { type: "object" },
5028
- source: { type: ["string", "null"] },
5029
- is_external_url: { type: "boolean" },
5030
- is_private: { type: "boolean" },
5031
- src: { type: "string" },
5032
- updated_at: { type: "string" },
5033
- width: { type: ["number", "null"] },
5034
- height: { type: ["number", "null"] },
5035
- aspect_ratio: { type: ["number", "null"] },
5036
- public_id: { type: ["string", "null"] },
5037
- content_type: { type: "string" }
5038
- }
5039
- }
5040
- });
5041
- const multilinkSharedRequiredProps = [
5042
- "fieldtype",
5043
- "id",
5044
- "url",
5045
- "cached_url",
5046
- "linktype"
5047
- ];
5048
- const getMultilinkJSONSchema = (title) => ({
5049
- $id: "#/multilink",
5050
- title,
5051
- oneOf: [
5052
- {
5053
- type: "object",
5054
- required: multilinkSharedRequiredProps,
5055
- properties: {
5056
- fieldtype: {
5057
- type: "string",
5058
- enum: ["multilink"]
5059
- },
5060
- id: { type: "string" },
5061
- url: { type: "string" },
5062
- cached_url: { type: "string" },
5063
- target: {
5064
- type: "string",
5065
- enum: ["_blank", "_self"]
5066
- },
5067
- anchor: { type: "string" },
5068
- rel: { type: "string" },
5069
- title: { type: "string" },
5070
- prep: { type: "string" },
5071
- linktype: {
5072
- type: "string",
5073
- enum: ["story"]
5074
- },
5075
- story: {
5076
- type: "object",
5077
- required: [
5078
- "name",
5079
- "id",
5080
- "uuid",
5081
- "slug",
5082
- "full_slug"
5083
- ],
5084
- properties: {
5085
- name: { type: "string" },
5086
- created_at: {
5087
- type: "string",
5088
- format: "date-time"
5089
- },
5090
- published_at: {
5091
- type: "string",
5092
- format: "date-time"
5093
- },
5094
- id: { type: "integer" },
5095
- uuid: {
5096
- type: "string",
5097
- format: "uuid"
5098
- },
5099
- content: { type: "object" },
5100
- slug: { type: "string" },
5101
- full_slug: { type: "string" },
5102
- sort_by_date: {
5103
- type: ["null", "string"],
5104
- format: "date-time"
5105
- },
5106
- position: { type: "integer" },
5107
- tag_list: {
5108
- type: "array",
5109
- items: { type: "string" }
5110
- },
5111
- is_startpage: { type: "boolean" },
5112
- parent_id: { type: ["null", "integer"] },
5113
- meta_data: { type: ["null", "object"] },
5114
- group_id: {
5115
- type: "string",
5116
- format: "uuid"
5117
- },
5118
- first_published_at: {
5119
- type: "string",
5120
- format: "date-time"
5121
- },
5122
- release_id: { type: ["null", "integer"] },
5123
- lang: { type: "string" },
5124
- path: { type: ["null", "string"] },
5125
- alternates: { type: "array" },
5126
- default_full_slug: { type: ["null", "string"] },
5127
- translated_slugs: { type: ["null", "array"] }
5128
- }
5129
- }
5130
- }
5131
- },
5132
- {
5133
- type: "object",
5134
- required: multilinkSharedRequiredProps,
5135
- properties: {
5136
- fieldtype: {
5137
- type: "string",
5138
- enum: ["multilink"]
5139
- },
5140
- id: { type: "string" },
5141
- url: { type: "string" },
5142
- cached_url: { type: "string" },
5143
- target: {
5144
- type: "string",
5145
- enum: ["_blank", "_self"]
5146
- },
5147
- linktype: {
5148
- type: "string",
5149
- enum: ["url"]
5150
- },
5151
- rel: { type: "string" },
5152
- title: { type: "string" }
5153
- }
5154
- },
5155
- {
5156
- type: "object",
5157
- required: multilinkSharedRequiredProps,
5158
- properties: {
5159
- fieldtype: {
5160
- type: "string",
5161
- enum: ["multilink"]
5162
- },
5163
- id: { type: "string" },
5164
- url: { type: "string" },
5165
- cached_url: { type: "string" },
5166
- target: {
5167
- type: "string",
5168
- enum: ["_blank", "_self"]
5169
- },
5170
- email: { type: "string" },
5171
- linktype: {
5172
- type: "string",
5173
- enum: ["email"]
5174
- }
5175
- }
5176
- },
5177
- {
5178
- type: "object",
5179
- required: multilinkSharedRequiredProps,
5180
- properties: {
5181
- fieldtype: {
5182
- type: "string",
5183
- enum: ["multilink"]
5184
- },
5185
- id: { type: "string" },
5186
- url: { type: "string" },
5187
- cached_url: { type: "string" },
5188
- target: {
5189
- type: "string",
5190
- enum: ["_blank", "_self"]
5191
- },
5192
- linktype: {
5193
- type: "string",
5194
- enum: ["asset"]
5195
- }
5196
- }
5197
- }
5198
- ]
5199
- });
5200
- const getRichtextJSONSchema = (title) => ({
5201
- $id: "#/richtext",
5202
- title,
5203
- type: "object",
5204
- required: ["type"],
5205
- properties: {
5206
- type: { type: "string" },
5207
- content: {
5208
- type: "array",
5209
- items: { $ref: "#" }
5210
- },
5211
- marks: {
5212
- type: "array",
5213
- items: { $ref: "#" }
5214
- },
5215
- attrs: {},
5216
- text: { type: "string" }
5217
- }
5218
- });
5219
- const getTableJSONSchema = (title) => ({
5220
- $id: "#/table",
5221
- title,
5222
- type: "object",
5223
- required: ["tbody", "thead"],
5224
- properties: {
5225
- thead: {
5226
- type: "array",
5227
- items: {
5228
- type: "object",
5229
- required: ["_uid", "component"],
5230
- properties: {
5231
- _uid: { type: "string" },
5232
- value: { type: "string" },
5233
- component: { type: "number" }
5234
- }
5235
- }
5236
- },
5237
- tbody: {
5238
- type: "array",
5239
- items: {
5240
- type: "object",
5241
- required: [
5242
- "_uid",
5243
- "component",
5244
- "body"
5245
- ],
5246
- properties: {
5247
- _uid: { type: "string" },
5248
- body: {
5249
- type: "array",
5250
- items: {
5251
- type: "object",
5252
- properties: {
5253
- _uid: { type: "string" },
5254
- value: { type: "string" },
5255
- component: { type: "number" }
5256
- }
5257
- }
5258
- },
5259
- component: { type: "number" }
5260
- }
5261
- }
5262
- }
5263
- }
5264
- });
5265
- const storyblokSchemas = /* @__PURE__ */ new Map([
5266
- ["asset", getAssetJSONSchema],
5267
- ["multiasset", getMultiassetJSONSchema],
5268
- ["multilink", getMultilinkJSONSchema],
5269
- ["table", getTableJSONSchema],
5270
- ["richtext", getRichtextJSONSchema]
5271
- ]);
4961
+ //#region src/utils/storyblok-property-types.ts
4962
+ const STORYBLOK_PROPERTY_TYPES = {
4963
+ asset: true,
4964
+ multiasset: true,
4965
+ multilink: true,
4966
+ richtext: true,
4967
+ table: true
4968
+ };
4969
+ const isStoryblokPropertyType = (type) => typeof type === "string" && Object.hasOwn(STORYBLOK_PROPERTY_TYPES, type);
5272
4970
  //#endregion
5273
4971
  //#region src/commands/types/generate/utils.ts
5274
4972
  /**
@@ -5427,10 +5125,39 @@ function createComponentFile(componentResult, typeDefs, componentImports) {
5427
5125
  //#region src/commands/types/generate/actions.ts
5428
5126
  const STORY_TYPE = "ISbStoryData";
5429
5127
  const DEFAULT_COMPONENT_FILENAME = "storyblok-components";
5430
- const DEFAULT_TYPEDEFS_HEADER = ["// This file was generated by the storyblok CLI.", "// DO NOT MODIFY THIS FILE BY HAND."];
5128
+ const DEFAULT_TYPEDEFS_HEADER = ["// This file was generated by the Storyblok CLI.", "// DO NOT MODIFY THIS FILE BY HAND."];
5129
+ const STORYBLOK_TYPES_FILENAME = "storyblok-types.d.mts";
5130
+ const DECLARATION_REGION_START = "//#region";
5131
+ const DECLARATION_REGION_MARKER = /^\/\/#(?:end)?region\b.*\n?/gm;
5132
+ const SOURCE_MAP_START = "//# sourceMappingURL=";
5133
+ const IMPORT_STATEMENT = /^import\s+(?:(.+?)\s+from\s+)?["'][^"']+["'];?[ \t]*$/gm;
5134
+ const LAYOUT_FIELD_TYPES = /* @__PURE__ */ new Set(["section", "tab"]);
5135
+ const isLayoutFieldType = (type) => LAYOUT_FIELD_TYPES.has(type);
5431
5136
  const getDatasourceTypeTitle = (slug) => `${toPascalCase(slug)}DataSource`;
5137
+ const getImportedBindings = (content) => [...content.matchAll(IMPORT_STATEMENT)].flatMap(([, clause]) => clause?.split(/[{},]/) ?? []).map((binding) => binding.trim().split(/\s+as\s+/).pop() ?? "").filter(Boolean);
5138
+ /**
5139
+ * Extracts the Storyblok type definitions from a bundled declaration file.
5140
+ *
5141
+ * The result is written verbatim into the user's `types/storyblok.d.ts`, so it has to
5142
+ * stand on its own: anything the declarations resolve through an import would dangle
5143
+ * once the file leaves the CLI package. Imports the declarations do not reference are
5144
+ * bundler leftovers and safe to drop; an import they *do* reference means the build
5145
+ * leaked something, which is a bug to surface rather than to silently strip.
5146
+ *
5147
+ * Falls through unchanged for declaration files that were not region-bundled.
5148
+ */
5149
+ const extractBundledTypeDefinitions = (content) => {
5150
+ const declarationStart = content.indexOf(DECLARATION_REGION_START);
5151
+ if (declarationStart === -1) return content;
5152
+ const sourceMapStart = content.indexOf(SOURCE_MAP_START, declarationStart);
5153
+ const declarationEnd = sourceMapStart === -1 ? content.length : sourceMapStart;
5154
+ const declarations = content.slice(declarationStart, declarationEnd).replaceAll(DECLARATION_REGION_MARKER, "").trimEnd();
5155
+ const leaked = getImportedBindings(content.slice(0, declarationStart)).filter((binding) => new RegExp(`\\b${binding}\\b`).test(declarations));
5156
+ if (leaked.length > 0) throw new CommandError(`The packaged CLI declarations resolve ${leaked.join(", ")} through an import, so self-contained Storyblok types cannot be generated. This is a CLI packaging bug, please report it at https://github.com/storyblok/monoblok/issues.`);
5157
+ return declarations;
5158
+ };
5432
5159
  const getPropertyTypeAnnotation = (property, prefix, suffix) => {
5433
- if (Array.from(storyblokSchemas.keys()).includes(property.type)) return { type: property.type };
5160
+ if (isStoryblokPropertyType(property.type)) return { type: property.type };
5434
5161
  let type = "unknown";
5435
5162
  const optionValues = property.options && property.options.length > 0 ? property.options.map((item) => item.value).filter((value) => value !== "") : [];
5436
5163
  const options = optionValues.length > 0 && !property.required ? ["", ...optionValues] : optionValues;
@@ -5491,10 +5218,10 @@ const getComponentPropertiesTypeAnnotations = async (component, options, spaceDa
5491
5218
  if (!component.schema || typeof component.schema !== "object") return {};
5492
5219
  return Object.entries(component.schema).reduce(async (accPromise, [key, value]) => {
5493
5220
  const acc = await accPromise;
5494
- if (key.startsWith("tab-")) return acc;
5495
5221
  if (!value || typeof value !== "object" || !("type" in value)) return acc;
5496
5222
  const schema = value;
5497
5223
  const propertyType = schema.type;
5224
+ if (isLayoutFieldType(propertyType)) return acc;
5498
5225
  const propertyTypeAnnotation = { [key]: getPropertyTypeAnnotation(schema, options.typePrefix, options.typeSuffix) };
5499
5226
  if (propertyType === "custom" && customFieldsParser) {
5500
5227
  const customField = typeof customFieldsParser === "function" ? customFieldsParser(key, schema) : {};
@@ -5503,10 +5230,7 @@ const getComponentPropertiesTypeAnnotations = async (component, options, spaceDa
5503
5230
  ...customField
5504
5231
  };
5505
5232
  }
5506
- if (Array.from(storyblokSchemas.keys()).includes(propertyType)) {
5507
- const componentType = toPascalCase(propertyType);
5508
- propertyTypeAnnotation[key].tsType = `Storyblok${componentType}`;
5509
- }
5233
+ if (isStoryblokPropertyType(propertyType)) propertyTypeAnnotation[key].tsType = storyblokTypeName(propertyType);
5510
5234
  if (spaceData.datasources.length > 0 && schema.source === "internal" && schema?.datasource_slug) {
5511
5235
  if (spaceData.datasources.some((ds) => ds.slug === schema.datasource_slug)) {
5512
5236
  const type = getDatasourceTypeTitle(schema.datasource_slug);
@@ -5571,7 +5295,7 @@ const generateTypes = async (spaceData, options = { strict: false }) => {
5571
5295
  return acc;
5572
5296
  }, ["component", "_uid"]);
5573
5297
  if (componentPropertiesTypeAnnotations) Object.entries(componentPropertiesTypeAnnotations).forEach(([_, property]) => {
5574
- if (property.type && Array.from(storyblokSchemas.keys()).includes(property.type)) storyblokPropertyTypes.add(property.type);
5298
+ if (property.type && isStoryblokPropertyType(property.type)) storyblokPropertyTypes.add(property.type);
5575
5299
  if (property.tsType && property.tsType.includes(STORY_TYPE)) storyblokPropertyTypes.add(STORY_TYPE);
5576
5300
  });
5577
5301
  return {
@@ -5680,18 +5404,21 @@ const saveTypesToComponentsFile = async (space, typedefData, options) => {
5680
5404
  const generateStoryblokTypes = async (options = {}) => {
5681
5405
  const { path } = options;
5682
5406
  try {
5683
- const storyblokTypesPath = resolve(__dirname, "./index.d.ts");
5684
- const storyblokTypesContent = readFileSync(storyblokTypesPath, "utf-8");
5685
- const typeDefs = [
5686
- "// This file was generated by the Storyblok CLI.",
5687
- "// DO NOT MODIFY THIS FILE BY HAND.",
5407
+ const storyblokTypesPath = resolve(__dirname, STORYBLOK_TYPES_FILENAME);
5408
+ const storyblokTypesContent = extractBundledTypeDefinitions(readFileSync(storyblokTypesPath, "utf-8"));
5409
+ const typeDefs = `${[
5410
+ ...DEFAULT_TYPEDEFS_HEADER,
5688
5411
  `import type { ${STORY_TYPE} } from '@storyblok/js';`,
5689
5412
  storyblokTypesContent
5690
- ].join("\n");
5413
+ ].join("\n")}\n`;
5691
5414
  const resolvedPath = path ? resolve(process.cwd(), path, "types") : resolvePath(path, "types");
5692
5415
  await saveToFile(join(resolvedPath, `storyblok.d.ts`), typeDefs);
5693
5416
  return true;
5694
5417
  } catch (error) {
5418
+ if (error instanceof CommandError) {
5419
+ handleError(error);
5420
+ return false;
5421
+ }
5695
5422
  handleFileSystemError("read", error);
5696
5423
  return false;
5697
5424
  }
@@ -5913,7 +5640,10 @@ typesCommand.command("generate").description("Generate types d.ts for your compo
5913
5640
  if (error instanceof FileSystemError && error.errorId === "file_not_found") dataSourceData = { datasources: [] };
5914
5641
  else throw error;
5915
5642
  }
5916
- await generateStoryblokTypes({ path });
5643
+ if (!await generateStoryblokTypes({ path })) {
5644
+ spinner.failed("Type generation failed");
5645
+ return;
5646
+ }
5917
5647
  const spaceDataWithComponentsAndDatasources = {
5918
5648
  ...componentsData,
5919
5649
  ...dataSourceData