@storyblok/schema 0.1.0-alpha.0 → 0.1.0-alpha.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.
Files changed (146) hide show
  1. package/dist/_virtual/_rolldown/runtime.cjs +29 -0
  2. package/dist/generated/capi/types.gen.d.cts +1 -126
  3. package/dist/generated/capi/types.gen.d.mts +1 -126
  4. package/dist/generated/mapi/_internal.gen.d.cts +1 -171
  5. package/dist/generated/mapi/_internal.gen.d.mts +1 -171
  6. package/dist/generated/overlay/zod.gen.cjs +409 -0
  7. package/dist/generated/overlay/zod.gen.cjs.map +1 -0
  8. package/dist/generated/overlay/zod.gen.mjs +403 -0
  9. package/dist/generated/overlay/zod.gen.mjs.map +1 -0
  10. package/dist/generated/types/block.d.cts +27 -14
  11. package/dist/generated/types/block.d.mts +27 -14
  12. package/dist/generated/types/field.d.cts +28 -13
  13. package/dist/generated/types/field.d.mts +28 -13
  14. package/dist/generated/types/mapi-story.d.cts +5 -9
  15. package/dist/generated/types/mapi-story.d.mts +5 -9
  16. package/dist/helpers/define-block.cjs +10 -28
  17. package/dist/helpers/define-block.cjs.map +1 -1
  18. package/dist/helpers/define-block.d.cts +14 -31
  19. package/dist/helpers/define-block.d.mts +14 -31
  20. package/dist/helpers/define-block.mjs +11 -27
  21. package/dist/helpers/define-block.mjs.map +1 -1
  22. package/dist/helpers/define-datasource.cjs +0 -16
  23. package/dist/helpers/define-datasource.cjs.map +1 -1
  24. package/dist/helpers/define-datasource.d.cts +14 -23
  25. package/dist/helpers/define-datasource.d.mts +14 -23
  26. package/dist/helpers/define-datasource.mjs +1 -15
  27. package/dist/helpers/define-datasource.mjs.map +1 -1
  28. package/dist/helpers/define-field.cjs +11 -11
  29. package/dist/helpers/define-field.cjs.map +1 -1
  30. package/dist/helpers/define-field.d.cts +46 -85
  31. package/dist/helpers/define-field.d.mts +46 -85
  32. package/dist/helpers/define-field.mjs +12 -11
  33. package/dist/helpers/define-field.mjs.map +1 -1
  34. package/dist/helpers/schema-type.d.cts +5 -9
  35. package/dist/helpers/schema-type.d.mts +5 -9
  36. package/dist/index.cjs +6 -47
  37. package/dist/index.d.cts +11 -20
  38. package/dist/index.d.mts +11 -20
  39. package/dist/index.mjs +6 -15
  40. package/dist/utils/is-record.cjs +10 -0
  41. package/dist/utils/is-record.cjs.map +1 -0
  42. package/dist/utils/is-record.mjs +9 -0
  43. package/dist/utils/is-record.mjs.map +1 -0
  44. package/dist/validators/create-story-validator.cjs +37 -0
  45. package/dist/validators/create-story-validator.cjs.map +1 -0
  46. package/dist/validators/create-story-validator.d.cts +18 -0
  47. package/dist/validators/create-story-validator.d.mts +18 -0
  48. package/dist/validators/create-story-validator.mjs +37 -0
  49. package/dist/validators/create-story-validator.mjs.map +1 -0
  50. package/dist/validators/internal-schemas.cjs +1 -0
  51. package/dist/validators/internal-schemas.mjs +3 -0
  52. package/dist/validators/shapes.cjs +12 -0
  53. package/dist/validators/shapes.cjs.map +1 -0
  54. package/dist/validators/shapes.d.cts +62 -0
  55. package/dist/validators/shapes.d.mts +62 -0
  56. package/dist/validators/shapes.mjs +12 -0
  57. package/dist/validators/shapes.mjs.map +1 -0
  58. package/dist/validators/types.d.cts +25 -0
  59. package/dist/validators/types.d.mts +25 -0
  60. package/dist/validators/validate-schema.cjs +125 -0
  61. package/dist/validators/validate-schema.cjs.map +1 -0
  62. package/dist/validators/validate-schema.d.cts +18 -0
  63. package/dist/validators/validate-schema.d.mts +18 -0
  64. package/dist/validators/validate-schema.mjs +125 -0
  65. package/dist/validators/validate-schema.mjs.map +1 -0
  66. package/dist/validators/validate-story.cjs +257 -0
  67. package/dist/validators/validate-story.cjs.map +1 -0
  68. package/dist/validators/validate-story.d.cts +17 -0
  69. package/dist/validators/validate-story.d.mts +17 -0
  70. package/dist/validators/validate-story.mjs +257 -0
  71. package/dist/validators/validate-story.mjs.map +1 -0
  72. package/package.json +5 -1
  73. package/dist/generated/mapi/types.gen.d.cts +0 -1272
  74. package/dist/generated/mapi/types.gen.d.mts +0 -1272
  75. package/dist/helpers/create-story-helpers.cjs +0 -42
  76. package/dist/helpers/create-story-helpers.cjs.map +0 -1
  77. package/dist/helpers/create-story-helpers.d.cts +0 -50
  78. package/dist/helpers/create-story-helpers.d.mts +0 -50
  79. package/dist/helpers/create-story-helpers.mjs +0 -42
  80. package/dist/helpers/create-story-helpers.mjs.map +0 -1
  81. package/dist/helpers/define-asset-folder.cjs +0 -46
  82. package/dist/helpers/define-asset-folder.cjs.map +0 -1
  83. package/dist/helpers/define-asset-folder.d.cts +0 -33
  84. package/dist/helpers/define-asset-folder.d.mts +0 -33
  85. package/dist/helpers/define-asset-folder.mjs +0 -43
  86. package/dist/helpers/define-asset-folder.mjs.map +0 -1
  87. package/dist/helpers/define-asset.cjs +0 -60
  88. package/dist/helpers/define-asset.cjs.map +0 -1
  89. package/dist/helpers/define-asset.d.cts +0 -32
  90. package/dist/helpers/define-asset.d.mts +0 -32
  91. package/dist/helpers/define-asset.mjs +0 -57
  92. package/dist/helpers/define-asset.mjs.map +0 -1
  93. package/dist/helpers/define-block-folder.cjs +0 -44
  94. package/dist/helpers/define-block-folder.cjs.map +0 -1
  95. package/dist/helpers/define-block-folder.d.cts +0 -34
  96. package/dist/helpers/define-block-folder.d.mts +0 -34
  97. package/dist/helpers/define-block-folder.mjs +0 -41
  98. package/dist/helpers/define-block-folder.mjs.map +0 -1
  99. package/dist/helpers/define-datasource-entry.cjs +0 -47
  100. package/dist/helpers/define-datasource-entry.cjs.map +0 -1
  101. package/dist/helpers/define-datasource-entry.d.cts +0 -49
  102. package/dist/helpers/define-datasource-entry.d.mts +0 -49
  103. package/dist/helpers/define-datasource-entry.mjs +0 -43
  104. package/dist/helpers/define-datasource-entry.mjs.map +0 -1
  105. package/dist/helpers/define-internal-tag.cjs +0 -41
  106. package/dist/helpers/define-internal-tag.cjs.map +0 -1
  107. package/dist/helpers/define-internal-tag.d.cts +0 -34
  108. package/dist/helpers/define-internal-tag.d.mts +0 -34
  109. package/dist/helpers/define-internal-tag.mjs +0 -38
  110. package/dist/helpers/define-internal-tag.mjs.map +0 -1
  111. package/dist/helpers/define-link.cjs +0 -23
  112. package/dist/helpers/define-link.cjs.map +0 -1
  113. package/dist/helpers/define-link.d.cts +0 -32
  114. package/dist/helpers/define-link.d.mts +0 -32
  115. package/dist/helpers/define-link.mjs +0 -22
  116. package/dist/helpers/define-link.mjs.map +0 -1
  117. package/dist/helpers/define-preset.cjs +0 -44
  118. package/dist/helpers/define-preset.cjs.map +0 -1
  119. package/dist/helpers/define-preset.d.cts +0 -35
  120. package/dist/helpers/define-preset.d.mts +0 -35
  121. package/dist/helpers/define-preset.mjs +0 -41
  122. package/dist/helpers/define-preset.mjs.map +0 -1
  123. package/dist/helpers/define-space.cjs +0 -47
  124. package/dist/helpers/define-space.cjs.map +0 -1
  125. package/dist/helpers/define-space.d.cts +0 -31
  126. package/dist/helpers/define-space.d.mts +0 -31
  127. package/dist/helpers/define-space.mjs +0 -44
  128. package/dist/helpers/define-space.mjs.map +0 -1
  129. package/dist/helpers/define-story.cjs +0 -122
  130. package/dist/helpers/define-story.cjs.map +0 -1
  131. package/dist/helpers/define-story.d.cts +0 -135
  132. package/dist/helpers/define-story.d.mts +0 -135
  133. package/dist/helpers/define-story.mjs +0 -118
  134. package/dist/helpers/define-story.mjs.map +0 -1
  135. package/dist/helpers/define-tag.cjs +0 -13
  136. package/dist/helpers/define-tag.cjs.map +0 -1
  137. package/dist/helpers/define-tag.d.cts +0 -23
  138. package/dist/helpers/define-tag.d.mts +0 -23
  139. package/dist/helpers/define-tag.mjs +0 -12
  140. package/dist/helpers/define-tag.mjs.map +0 -1
  141. package/dist/helpers/define-user.cjs +0 -50
  142. package/dist/helpers/define-user.cjs.map +0 -1
  143. package/dist/helpers/define-user.d.cts +0 -26
  144. package/dist/helpers/define-user.d.mts +0 -26
  145. package/dist/helpers/define-user.mjs +0 -48
  146. package/dist/helpers/define-user.mjs.map +0 -1
@@ -11,21 +11,7 @@ function defineDatasource(datasource) {
11
11
  ...datasource
12
12
  };
13
13
  }
14
- /**
15
- * Defines a datasource creation payload for the MAPI.
16
- *
17
- * @example
18
- * const payload = defineDatasourceCreate({ name: 'Colors', slug: 'colors' });
19
- */
20
- const defineDatasourceCreate = (datasource) => datasource;
21
- /**
22
- * Defines a datasource update payload for the MAPI.
23
- *
24
- * @example
25
- * const payload = defineDatasourceUpdate({ name: 'Updated Colors' });
26
- */
27
- const defineDatasourceUpdate = (datasource) => datasource;
28
14
 
29
15
  //#endregion
30
- export { defineDatasource, defineDatasourceCreate, defineDatasourceUpdate };
16
+ export { defineDatasource };
31
17
  //# sourceMappingURL=define-datasource.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"define-datasource.mjs","names":[],"sources":["../../src/helpers/define-datasource.ts"],"sourcesContent":["import type { Datasource } from '../generated/capi/types.gen';\nimport type { DatasourceCreate, DatasourceUpdate } from '../generated/mapi/types.gen';\nimport type { Prettify } from '../utils/prettify';\n\nconst DATASOURCE_DEFAULTS = {\n id: 1,\n created_at: '',\n updated_at: '',\n dimensions: [],\n};\n\nexport type { Datasource, DatasourceCreate, DatasourceUpdate };\n\n/** Fields that have safe defaults and may be omitted from datasource input. */\ntype DatasourceOptional = keyof typeof DATASOURCE_DEFAULTS;\n\ntype DatasourceInput = Prettify<Omit<Datasource, DatasourceOptional> & Partial<Pick<Datasource, DatasourceOptional>>>;\n\n/**\n * Returns a full {@link Datasource} with all fields populated. API-assigned\n * fields are optional and default to safe values.\n *\n * @example\n * const myDatasource = defineDatasource({\n * name: 'Colors',\n * slug: 'colors',\n * });\n */\n// Overload: provides the strict public signature for callers.\nexport function defineDatasource(datasource: DatasourceInput): Datasource;\n\n// Implementation signature: uses a loose parameter type because\n// TypeScript requires the implementation signature to be assignable\n// to all overloads. Not visible to callers.\nexport function defineDatasource(datasource: any) {\n return { ...DATASOURCE_DEFAULTS, ...datasource };\n}\n\n/**\n * Defines a datasource creation payload for the MAPI.\n *\n * @example\n * const payload = defineDatasourceCreate({ name: 'Colors', slug: 'colors' });\n */\nexport const defineDatasourceCreate = (datasource: DatasourceCreate): DatasourceCreate => datasource;\n\n/**\n * Defines a datasource update payload for the MAPI.\n *\n * @example\n * const payload = defineDatasourceUpdate({ name: 'Updated Colors' });\n */\nexport const defineDatasourceUpdate = (datasource: DatasourceUpdate): DatasourceUpdate => datasource;\n"],"mappings":";AAIA,MAAM,sBAAsB;CAC1B,IAAI;CACJ,YAAY;CACZ,YAAY;CACZ,YAAY,EAAE;CACf;AAyBD,SAAgB,iBAAiB,YAAiB;AAChD,QAAO;EAAE,GAAG;EAAqB,GAAG;EAAY;;;;;;;;AASlD,MAAa,0BAA0B,eAAmD;;;;;;;AAQ1F,MAAa,0BAA0B,eAAmD"}
1
+ {"version":3,"file":"define-datasource.mjs","names":[],"sources":["../../src/helpers/define-datasource.ts"],"sourcesContent":["import type { Datasource as DatasourceGenerated } from '../generated/capi/types.gen';\nimport type { Override } from '../generated/types/_utils';\nimport type { Prettify } from '../utils/prettify';\n\nconst DATASOURCE_DEFAULTS = {\n id: 1,\n created_at: '',\n updated_at: '',\n dimensions: [],\n};\n\n/**\n * A Storyblok datasource content shape. Carries the datasource definition only\n * (`name`, `slug`, dimensions). Entry values are content authored by editors,\n * not part of the schema, so they are neither modelled nor pushed here.\n */\nexport type Datasource = Override<DatasourceGenerated, { slug: string }>;\n\n/** Fields that have safe defaults and may be omitted from datasource input. */\ntype DatasourceOptional = keyof typeof DATASOURCE_DEFAULTS;\n\ntype DatasourceInput = Prettify<Omit<Datasource, DatasourceOptional> & Partial<Pick<Datasource, DatasourceOptional>>>;\n\n/**\n * Returns a {@link Datasource} content-shape definition. A thin, strongly-typed\n * helper it does not validate or throw.\n *\n * @example\n * const colors = defineDatasource({ name: 'Colors', slug: 'colors' });\n */\nexport function defineDatasource(datasource: DatasourceInput): Datasource;\nexport function defineDatasource(datasource: any) {\n return { ...DATASOURCE_DEFAULTS, ...datasource };\n}\n"],"mappings":";AAIA,MAAM,sBAAsB;CAC1B,IAAI;CACJ,YAAY;CACZ,YAAY;CACZ,YAAY,EAAE;CACf;AAsBD,SAAgB,iBAAiB,YAAiB;AAChD,QAAO;EAAE,GAAG;EAAqB,GAAG;EAAY"}
@@ -1,16 +1,16 @@
1
+ const require_is_record = require('../utils/is-record.cjs');
1
2
 
2
3
  //#region src/helpers/define-field.ts
3
- /**
4
- * Returns a {@link Field} stamped with the given `name`. Use inside a
5
- * {@link defineBlock} `schema` array — `pos` is inferred from array index.
6
- *
7
- * @example
8
- * defineField('headline', { type: 'text', max_length: 100, required: true });
9
- */
10
- const defineField = (name, field) => ({
11
- ...field,
12
- name
13
- });
4
+ function defineField(name, field) {
5
+ const { allow, datasource, ...rest } = field;
6
+ const normalized = {
7
+ ...rest,
8
+ name
9
+ };
10
+ if (allow !== void 0) normalized.allow = (Array.isArray(allow) ? allow : [allow]).map((ref) => typeof ref === "string" ? ref : require_is_record.isRecord(ref) ? ref.name : void 0);
11
+ if (datasource !== void 0) normalized.datasource = typeof datasource === "string" ? datasource : require_is_record.isRecord(datasource) ? datasource.slug : void 0;
12
+ return normalized;
13
+ }
14
14
 
15
15
  //#endregion
16
16
  exports.defineField = defineField;
@@ -1 +1 @@
1
- {"version":3,"file":"define-field.cjs","names":[],"sources":["../../src/helpers/define-field.ts"],"sourcesContent":["import type {\n AssetFieldValue,\n Field,\n MultilinkFieldValue,\n PluginFieldValue,\n RichtextFieldValue,\n TableFieldValue,\n} from '../generated/types/field';\nimport type { Block } from './define-block';\nimport type { Prettify } from '../utils/prettify';\n\nexport type { Field };\n\n/** Loose variant of the generated `BlokContent` with `_uid` optional used as the fallback for write operations when no block union is provided. */\ntype BlokContentLoose = {\n _uid?: string;\n component: string;\n _editable?: string | undefined;\n} & {\n [key: string]:\n | string\n | number\n | boolean\n | Array<string | AssetFieldValue | BlokContentLoose>\n | AssetFieldValue\n | MultilinkFieldValue\n | TableFieldValue\n | RichtextFieldValue\n | PluginFieldValue\n | undefined;\n};\n\n/** Keys in a schema record that have `required: true`. */\ntype RequiredFieldKeys<T> = {\n [K in keyof T]: T[K] extends { required: true } ? K : never\n}[keyof T];\n\n/** Keys in a schema record that do NOT have `required: true`. */\ntype OptionalFieldKeys<T> = Exclude<keyof T, RequiredFieldKeys<T>>;\n\n/**\n * Builds the content object for a single block instance as returned by the\n * Storyblok Content Delivery API. Includes `_uid` (always present in API\n * responses) and respects required/optional field semantics.\n *\n * For write operations (creating/updating stories), use {@link BlockContentInput}\n * instead, which omits `_uid` since Storyblok generates it automatically.\n */\nexport type BlockContent<TBlock extends Block = Block, TBlocks = false> = TBlock extends any\n ? { _uid: string; component: TBlock['name'] }\n & {\n [K in RequiredFieldKeys<TBlock['schema']>]: FieldValue<NonNullable<TBlock['schema'][K]>, TBlocks>\n }\n & {\n [K in OptionalFieldKeys<TBlock['schema']>]?: FieldValue<NonNullable<TBlock['schema'][K]>, TBlocks> | null\n }\n : never;\n\n/**\n * Input variant of {@link BlockContent} for write operations (creating/updating\n * stories via the MAPI). `_uid` is optional Storyblok generates it\n * automatically when omitted. Nested bloks fields also use this input variant\n * so that deeply nested blocks do not require `_uid` either.\n */\nexport type BlockContentInput<TBlock extends Block = Block, TBlocks = false> = TBlock extends any\n ? { _uid?: string; component: TBlock['name'] }\n & {\n [K in RequiredFieldKeys<TBlock['schema']>]: FieldValueInput<NonNullable<TBlock['schema'][K]>, TBlocks>\n }\n & {\n [K in OptionalFieldKeys<TBlock['schema']>]?: FieldValueInput<NonNullable<TBlock['schema'][K]>, TBlocks> | null\n }\n : never;\n\nexport type BlocksFieldValue<\n TBlock extends Block = Block,\n TBlocks = false,\n> = BlockContent<TBlock, TBlocks>[];\n\nexport type { AssetFieldValue, MultilinkFieldValue, PluginFieldValue, RichtextFieldValue, TableFieldValue };\n\n/** Union of all valid Storyblok field type discriminants (e.g., `text`, `bloks`). */\nexport type FieldType = Field['type'];\n\n/** Maps each field type discriminant to its runtime content value type. */\ninterface FieldTypeValueMap {\n text: string;\n textarea: string;\n richtext: RichtextFieldValue;\n markdown: string;\n number: number;\n datetime: string;\n boolean: boolean;\n option: string;\n options: string[];\n asset: AssetFieldValue;\n multiasset: AssetFieldValue[];\n multilink: MultilinkFieldValue;\n bloks: BlockContent[];\n table: TableFieldValue;\n section: never;\n tab: never;\n custom: PluginFieldValue;\n}\n\n/**\n * Checks whether a block is nestable, defaulting to `true` when\n * `is_nestable` is absent or undefined.\n */\ntype IsNestable<T> =\n T extends { is_nestable: false } ? false\n : T extends { is_nestable: true } ? true\n : true; // default: nestable when is_nestable is not specified\n\ntype ApplyWhitelist<TField, TBlocks> = TField extends { component_whitelist: ReadonlyArray<infer TWhitelisted extends string> }\n // With whitelist: filter by block name (distributive over TBlocks)\n ? TBlocks extends { name: TWhitelisted } ? TBlocks : never\n // No whitelist: filter by nestability (distributive over TBlocks)\n : TBlocks extends any\n ? IsNestable<TBlocks> extends true ? TBlocks : never\n : never;\n\n/** Resolves a field definition to its runtime content value type (read). */\nexport type FieldValue<\n TField extends Field = Field,\n TBlocks = false,\n> = Prettify<\n TField extends { type: 'bloks' }\n // Bloks fieldguard against `never` first (it satisfies `[never] extends [X]`\n // for all X, which would incorrectly enter the typed path with empty results).\n ? [TBlocks] extends [never]\n ? BlockContent[]\n : [TBlocks] extends [Block]\n ? BlockContent<ApplyWhitelist<TField, TBlocks>, TBlocks>[]\n : BlockContent[]\n // No bloks field\n : FieldTypeValueMap[TField['type']]\n>;\n\n/** Resolves a field definition to its input value type (write). Nested bloks use {@link BlockContentInput}. */\nexport type FieldValueInput<\n TField extends Field = Field,\n TBlocks = false,\n> = Prettify<\n TField extends { type: 'bloks' }\n ? [TBlocks] extends [never]\n ? BlokContentLoose[]\n : [TBlocks] extends [Block]\n ? BlockContentInput<ApplyWhitelist<TField, TBlocks>, TBlocks>[]\n : BlokContentLoose[]\n : FieldTypeValueMap[TField['type']]\n>;\n\n/**\n * Returns a {@link Field} stamped with the given `name`. Use inside a\n * {@link defineBlock} `schema` array `pos` is inferred from array index.\n *\n * @example\n * defineField('headline', { type: 'text', max_length: 100, required: true });\n */\nexport const defineField = <\n const TName extends string,\n const TField extends Field,\n>(\n name: TName,\n field: TField,\n): Omit<TField, 'name'> & { name: TName } => ({ ...field, name });\n"],"mappings":";;;;;;;;;AAgKA,MAAa,eAIX,MACA,WAC4C;CAAE,GAAG;CAAO;CAAM"}
1
+ {"version":3,"file":"define-field.cjs","names":["isRecord"],"sources":["../../src/helpers/define-field.ts"],"sourcesContent":["import type {\n AssetFieldValue,\n BlockContent,\n BlockContentInput,\n BlocksFieldValue,\n Field,\n FieldType,\n FieldValue,\n FieldValueInput,\n MultilinkFieldValue,\n PluginFieldValue,\n RichtextFieldValue,\n TableFieldValue,\n} from '../generated/types/field';\nimport type { Prettify } from '../utils/prettify';\nimport { isRecord } from '../utils/is-record';\n\nexport type {\n AssetFieldValue,\n BlockContent,\n BlockContentInput,\n BlocksFieldValue,\n Field,\n FieldType,\n FieldValue,\n FieldValueInput,\n MultilinkFieldValue,\n PluginFieldValue,\n RichtextFieldValue,\n TableFieldValue,\n};\n\n/** A block reference for `allow`: a defined block object or its name. */\ntype AllowRef = string | { name: string };\n/** A datasource reference for `datasource`: a defined datasource object or its slug. */\ntype DatasourceRef = string | { slug: string };\n\ntype NameOf<T> = T extends string ? T : T extends { name: infer N extends string } ? N : never;\ntype SlugOf<T> = T extends string ? T : T extends { slug: infer S extends string } ? S : never;\n\n/** Normalizes an `allow` input (ref, name, or array thereof) to a tuple of block-name strings. */\ntype NormalizeAllow<T> = T extends readonly any[]\n ? { [I in keyof T]: NameOf<T[I]> }\n : readonly [NameOf<T>];\n\n/**\n * Field config accepted by {@link defineField}: the content-shape field plus the\n * DSL reference keys. `allow` replaces the wire `component_whitelist`; `datasource`\n * holds the datasource ref/slug (the wire `source` selector still passes through).\n */\nexport type FieldInput = Field & {\n allow?: AllowRef | readonly AllowRef[];\n datasource?: DatasourceRef;\n required?: boolean;\n};\n\n/** Result of {@link defineField}: the field stamped with `name`, with refs normalized to strings. */\nexport type DefinedField<TName extends string, TField extends FieldInput> = Prettify<\n Omit<TField, 'allow' | 'datasource' | 'name'>\n & { name: TName }\n & (TField extends { allow: infer A } ? { allow: NormalizeAllow<A> } : unknown)\n & (TField extends { datasource: infer D } ? { datasource: SlugOf<D> } : unknown)\n>;\n\n/**\n * Returns a {@link Field} stamped with the given `name`, normalizing reference\n * keys to strings so everything downstream sees plain names/slugs. A thin,\n * strongly-typed identity helper it does not validate or throw.\n *\n * Use inside a {@link defineBlock} `fields` array — `pos` is injected from the\n * array index by `defineBlock`.\n *\n * @example\n * defineField('headline', { type: 'text', max_length: 100, required: true });\n * defineField('body', { type: 'bloks', allow: [heroBlock, 'teaser'] });\n * defineField('theme', { type: 'option', source: 'internal', datasource: colors });\n */\nexport function defineField<\n const TName extends string,\n const TField extends FieldInput,\n>(name: TName, field: TField): DefinedField<TName, TField>;\nexport function defineField(name: string, field: Record<string, unknown>): Record<string, unknown> {\n const { allow, datasource, ...rest } = field;\n const normalized: Record<string, unknown> = { ...rest, name };\n if (allow !== undefined) {\n const refs = Array.isArray(allow) ? allow : [allow];\n normalized.allow = refs.map(ref => (typeof ref === 'string' ? ref : isRecord(ref) ? ref.name : undefined));\n }\n if (datasource !== undefined) {\n normalized.datasource = typeof datasource === 'string' ? datasource : isRecord(datasource) ? datasource.slug : undefined;\n }\n return normalized;\n}\n"],"mappings":";;;AAiFA,SAAgB,YAAY,MAAc,OAAyD;CACjG,MAAM,EAAE,OAAO,YAAY,GAAG,SAAS;CACvC,MAAM,aAAsC;EAAE,GAAG;EAAM;EAAM;AAC7D,KAAI,UAAU,OAEZ,YAAW,SADE,MAAM,QAAQ,MAAM,GAAG,QAAQ,CAAC,MAAM,EAC3B,KAAI,QAAQ,OAAO,QAAQ,WAAW,MAAMA,2BAAS,IAAI,GAAG,IAAI,OAAO,OAAW;AAE5G,KAAI,eAAe,OACjB,YAAW,aAAa,OAAO,eAAe,WAAW,aAAaA,2BAAS,WAAW,GAAG,WAAW,OAAO;AAEjH,QAAO"}
@@ -1,99 +1,60 @@
1
1
  import { AssetFieldValue, Field, MultilinkFieldValue, PluginFieldValue, RichtextFieldValue, TableFieldValue } from "../generated/overlay/_internal.gen.cjs";
2
- import { Block } from "../generated/types/block.cjs";
2
+ import { BlockContent, BlockContentInput, BlocksFieldValue, FieldType, FieldValue, FieldValueInput } from "../generated/types/field.cjs";
3
3
  import { Prettify } from "../utils/prettify.cjs";
4
4
 
5
5
  //#region src/helpers/define-field.d.ts
6
- /** Loose variant of the generated `BlokContent` with `_uid` optional used as the fallback for write operations when no block union is provided. */
7
- type BlokContentLoose = {
8
- _uid?: string;
9
- component: string;
10
- _editable?: string | undefined;
11
- } & {
12
- [key: string]: string | number | boolean | Array<string | AssetFieldValue | BlokContentLoose> | AssetFieldValue | MultilinkFieldValue | TableFieldValue | RichtextFieldValue | PluginFieldValue | undefined;
6
+ /** A block reference for `allow`: a defined block object or its name. */
7
+ type AllowRef = string | {
8
+ name: string;
13
9
  };
14
- /** Keys in a schema record that have `required: true`. */
15
- type RequiredFieldKeys<T> = { [K in keyof T]: T[K] extends {
16
- required: true;
17
- } ? K : never }[keyof T];
18
- /** Keys in a schema record that do NOT have `required: true`. */
19
- type OptionalFieldKeys<T> = Exclude<keyof T, RequiredFieldKeys<T>>;
20
- /**
21
- * Builds the content object for a single block instance as returned by the
22
- * Storyblok Content Delivery API. Includes `_uid` (always present in API
23
- * responses) and respects required/optional field semantics.
24
- *
25
- * For write operations (creating/updating stories), use {@link BlockContentInput}
26
- * instead, which omits `_uid` since Storyblok generates it automatically.
27
- */
28
- type BlockContent<TBlock extends Block = Block, TBlocks = false> = TBlock extends any ? {
29
- _uid: string;
30
- component: TBlock['name'];
31
- } & { [K in RequiredFieldKeys<TBlock['schema']>]: FieldValue<NonNullable<TBlock['schema'][K]>, TBlocks> } & { [K in OptionalFieldKeys<TBlock['schema']>]?: FieldValue<NonNullable<TBlock['schema'][K]>, TBlocks> | null } : never;
32
- /**
33
- * Input variant of {@link BlockContent} for write operations (creating/updating
34
- * stories via the MAPI). `_uid` is optional — Storyblok generates it
35
- * automatically when omitted. Nested bloks fields also use this input variant
36
- * so that deeply nested blocks do not require `_uid` either.
37
- */
38
- type BlockContentInput<TBlock extends Block = Block, TBlocks = false> = TBlock extends any ? {
39
- _uid?: string;
40
- component: TBlock['name'];
41
- } & { [K in RequiredFieldKeys<TBlock['schema']>]: FieldValueInput<NonNullable<TBlock['schema'][K]>, TBlocks> } & { [K in OptionalFieldKeys<TBlock['schema']>]?: FieldValueInput<NonNullable<TBlock['schema'][K]>, TBlocks> | null } : never;
42
- type BlocksFieldValue<TBlock extends Block = Block, TBlocks = false> = BlockContent<TBlock, TBlocks>[];
43
- /** Union of all valid Storyblok field type discriminants (e.g., `text`, `bloks`). */
44
- type FieldType = Field['type'];
45
- /** Maps each field type discriminant to its runtime content value type. */
46
- interface FieldTypeValueMap {
47
- text: string;
48
- textarea: string;
49
- richtext: RichtextFieldValue;
50
- markdown: string;
51
- number: number;
52
- datetime: string;
53
- boolean: boolean;
54
- option: string;
55
- options: string[];
56
- asset: AssetFieldValue;
57
- multiasset: AssetFieldValue[];
58
- multilink: MultilinkFieldValue;
59
- bloks: BlockContent[];
60
- table: TableFieldValue;
61
- section: never;
62
- tab: never;
63
- custom: PluginFieldValue;
64
- }
10
+ /** A datasource reference for `datasource`: a defined datasource object or its slug. */
11
+ type DatasourceRef = string | {
12
+ slug: string;
13
+ };
14
+ type NameOf<T> = T extends string ? T : T extends {
15
+ name: infer N extends string;
16
+ } ? N : never;
17
+ type SlugOf<T> = T extends string ? T : T extends {
18
+ slug: infer S extends string;
19
+ } ? S : never;
20
+ /** Normalizes an `allow` input (ref, name, or array thereof) to a tuple of block-name strings. */
21
+ type NormalizeAllow<T> = T extends readonly any[] ? { [I in keyof T]: NameOf<T[I]> } : readonly [NameOf<T>];
65
22
  /**
66
- * Checks whether a block is nestable, defaulting to `true` when
67
- * `is_nestable` is absent or undefined.
23
+ * Field config accepted by {@link defineField}: the content-shape field plus the
24
+ * DSL reference keys. `allow` replaces the wire `component_whitelist`; `datasource`
25
+ * holds the datasource ref/slug (the wire `source` selector still passes through).
68
26
  */
69
- type IsNestable<T> = T extends {
70
- is_nestable: false;
71
- } ? false : T extends {
72
- is_nestable: true;
73
- } ? true : true;
74
- type ApplyWhitelist<TField, TBlocks> = TField extends {
75
- component_whitelist: ReadonlyArray<infer TWhitelisted extends string>;
76
- } ? TBlocks extends {
77
- name: TWhitelisted;
78
- } ? TBlocks : never : TBlocks extends any ? IsNestable<TBlocks> extends true ? TBlocks : never : never;
79
- /** Resolves a field definition to its runtime content value type (read). */
80
- type FieldValue<TField extends Field = Field, TBlocks = false> = Prettify<TField extends {
81
- type: 'bloks';
82
- } ? [TBlocks] extends [never] ? BlockContent[] : [TBlocks] extends [Block] ? BlockContent<ApplyWhitelist<TField, TBlocks>, TBlocks>[] : BlockContent[] : FieldTypeValueMap[TField['type']]>;
83
- /** Resolves a field definition to its input value type (write). Nested bloks use {@link BlockContentInput}. */
84
- type FieldValueInput<TField extends Field = Field, TBlocks = false> = Prettify<TField extends {
85
- type: 'bloks';
86
- } ? [TBlocks] extends [never] ? BlokContentLoose[] : [TBlocks] extends [Block] ? BlockContentInput<ApplyWhitelist<TField, TBlocks>, TBlocks>[] : BlokContentLoose[] : FieldTypeValueMap[TField['type']]>;
27
+ type FieldInput = Field & {
28
+ allow?: AllowRef | readonly AllowRef[];
29
+ datasource?: DatasourceRef;
30
+ required?: boolean;
31
+ };
32
+ /** Result of {@link defineField}: the field stamped with `name`, with refs normalized to strings. */
33
+ type DefinedField<TName extends string, TField extends FieldInput> = Prettify<Omit<TField, 'allow' | 'datasource' | 'name'> & {
34
+ name: TName;
35
+ } & (TField extends {
36
+ allow: infer A;
37
+ } ? {
38
+ allow: NormalizeAllow<A>;
39
+ } : unknown) & (TField extends {
40
+ datasource: infer D;
41
+ } ? {
42
+ datasource: SlugOf<D>;
43
+ } : unknown)>;
87
44
  /**
88
- * Returns a {@link Field} stamped with the given `name`. Use inside a
89
- * {@link defineBlock} `schema` array `pos` is inferred from array index.
45
+ * Returns a {@link Field} stamped with the given `name`, normalizing reference
46
+ * keys to strings so everything downstream sees plain names/slugs. A thin,
47
+ * strongly-typed identity helper — it does not validate or throw.
48
+ *
49
+ * Use inside a {@link defineBlock} `fields` array — `pos` is injected from the
50
+ * array index by `defineBlock`.
90
51
  *
91
52
  * @example
92
53
  * defineField('headline', { type: 'text', max_length: 100, required: true });
54
+ * defineField('body', { type: 'bloks', allow: [heroBlock, 'teaser'] });
55
+ * defineField('theme', { type: 'option', source: 'internal', datasource: colors });
93
56
  */
94
- declare const defineField: <const TName extends string, const TField extends Field>(name: TName, field: TField) => Omit<TField, "name"> & {
95
- name: TName;
96
- };
57
+ declare function defineField<const TName extends string, const TField extends FieldInput>(name: TName, field: TField): DefinedField<TName, TField>;
97
58
  //#endregion
98
- export { BlockContent, BlockContentInput, BlocksFieldValue, FieldType, FieldValue, FieldValueInput, defineField };
59
+ export { DefinedField, FieldInput, defineField };
99
60
  //# sourceMappingURL=define-field.d.cts.map
@@ -1,99 +1,60 @@
1
1
  import { AssetFieldValue, Field, MultilinkFieldValue, PluginFieldValue, RichtextFieldValue, TableFieldValue } from "../generated/overlay/_internal.gen.mjs";
2
- import { Block } from "../generated/types/block.mjs";
2
+ import { BlockContent, BlockContentInput, BlocksFieldValue, FieldType, FieldValue, FieldValueInput } from "../generated/types/field.mjs";
3
3
  import { Prettify } from "../utils/prettify.mjs";
4
4
 
5
5
  //#region src/helpers/define-field.d.ts
6
- /** Loose variant of the generated `BlokContent` with `_uid` optional used as the fallback for write operations when no block union is provided. */
7
- type BlokContentLoose = {
8
- _uid?: string;
9
- component: string;
10
- _editable?: string | undefined;
11
- } & {
12
- [key: string]: string | number | boolean | Array<string | AssetFieldValue | BlokContentLoose> | AssetFieldValue | MultilinkFieldValue | TableFieldValue | RichtextFieldValue | PluginFieldValue | undefined;
6
+ /** A block reference for `allow`: a defined block object or its name. */
7
+ type AllowRef = string | {
8
+ name: string;
13
9
  };
14
- /** Keys in a schema record that have `required: true`. */
15
- type RequiredFieldKeys<T> = { [K in keyof T]: T[K] extends {
16
- required: true;
17
- } ? K : never }[keyof T];
18
- /** Keys in a schema record that do NOT have `required: true`. */
19
- type OptionalFieldKeys<T> = Exclude<keyof T, RequiredFieldKeys<T>>;
20
- /**
21
- * Builds the content object for a single block instance as returned by the
22
- * Storyblok Content Delivery API. Includes `_uid` (always present in API
23
- * responses) and respects required/optional field semantics.
24
- *
25
- * For write operations (creating/updating stories), use {@link BlockContentInput}
26
- * instead, which omits `_uid` since Storyblok generates it automatically.
27
- */
28
- type BlockContent<TBlock extends Block = Block, TBlocks = false> = TBlock extends any ? {
29
- _uid: string;
30
- component: TBlock['name'];
31
- } & { [K in RequiredFieldKeys<TBlock['schema']>]: FieldValue<NonNullable<TBlock['schema'][K]>, TBlocks> } & { [K in OptionalFieldKeys<TBlock['schema']>]?: FieldValue<NonNullable<TBlock['schema'][K]>, TBlocks> | null } : never;
32
- /**
33
- * Input variant of {@link BlockContent} for write operations (creating/updating
34
- * stories via the MAPI). `_uid` is optional — Storyblok generates it
35
- * automatically when omitted. Nested bloks fields also use this input variant
36
- * so that deeply nested blocks do not require `_uid` either.
37
- */
38
- type BlockContentInput<TBlock extends Block = Block, TBlocks = false> = TBlock extends any ? {
39
- _uid?: string;
40
- component: TBlock['name'];
41
- } & { [K in RequiredFieldKeys<TBlock['schema']>]: FieldValueInput<NonNullable<TBlock['schema'][K]>, TBlocks> } & { [K in OptionalFieldKeys<TBlock['schema']>]?: FieldValueInput<NonNullable<TBlock['schema'][K]>, TBlocks> | null } : never;
42
- type BlocksFieldValue<TBlock extends Block = Block, TBlocks = false> = BlockContent<TBlock, TBlocks>[];
43
- /** Union of all valid Storyblok field type discriminants (e.g., `text`, `bloks`). */
44
- type FieldType = Field['type'];
45
- /** Maps each field type discriminant to its runtime content value type. */
46
- interface FieldTypeValueMap {
47
- text: string;
48
- textarea: string;
49
- richtext: RichtextFieldValue;
50
- markdown: string;
51
- number: number;
52
- datetime: string;
53
- boolean: boolean;
54
- option: string;
55
- options: string[];
56
- asset: AssetFieldValue;
57
- multiasset: AssetFieldValue[];
58
- multilink: MultilinkFieldValue;
59
- bloks: BlockContent[];
60
- table: TableFieldValue;
61
- section: never;
62
- tab: never;
63
- custom: PluginFieldValue;
64
- }
10
+ /** A datasource reference for `datasource`: a defined datasource object or its slug. */
11
+ type DatasourceRef = string | {
12
+ slug: string;
13
+ };
14
+ type NameOf<T> = T extends string ? T : T extends {
15
+ name: infer N extends string;
16
+ } ? N : never;
17
+ type SlugOf<T> = T extends string ? T : T extends {
18
+ slug: infer S extends string;
19
+ } ? S : never;
20
+ /** Normalizes an `allow` input (ref, name, or array thereof) to a tuple of block-name strings. */
21
+ type NormalizeAllow<T> = T extends readonly any[] ? { [I in keyof T]: NameOf<T[I]> } : readonly [NameOf<T>];
65
22
  /**
66
- * Checks whether a block is nestable, defaulting to `true` when
67
- * `is_nestable` is absent or undefined.
23
+ * Field config accepted by {@link defineField}: the content-shape field plus the
24
+ * DSL reference keys. `allow` replaces the wire `component_whitelist`; `datasource`
25
+ * holds the datasource ref/slug (the wire `source` selector still passes through).
68
26
  */
69
- type IsNestable<T> = T extends {
70
- is_nestable: false;
71
- } ? false : T extends {
72
- is_nestable: true;
73
- } ? true : true;
74
- type ApplyWhitelist<TField, TBlocks> = TField extends {
75
- component_whitelist: ReadonlyArray<infer TWhitelisted extends string>;
76
- } ? TBlocks extends {
77
- name: TWhitelisted;
78
- } ? TBlocks : never : TBlocks extends any ? IsNestable<TBlocks> extends true ? TBlocks : never : never;
79
- /** Resolves a field definition to its runtime content value type (read). */
80
- type FieldValue<TField extends Field = Field, TBlocks = false> = Prettify<TField extends {
81
- type: 'bloks';
82
- } ? [TBlocks] extends [never] ? BlockContent[] : [TBlocks] extends [Block] ? BlockContent<ApplyWhitelist<TField, TBlocks>, TBlocks>[] : BlockContent[] : FieldTypeValueMap[TField['type']]>;
83
- /** Resolves a field definition to its input value type (write). Nested bloks use {@link BlockContentInput}. */
84
- type FieldValueInput<TField extends Field = Field, TBlocks = false> = Prettify<TField extends {
85
- type: 'bloks';
86
- } ? [TBlocks] extends [never] ? BlokContentLoose[] : [TBlocks] extends [Block] ? BlockContentInput<ApplyWhitelist<TField, TBlocks>, TBlocks>[] : BlokContentLoose[] : FieldTypeValueMap[TField['type']]>;
27
+ type FieldInput = Field & {
28
+ allow?: AllowRef | readonly AllowRef[];
29
+ datasource?: DatasourceRef;
30
+ required?: boolean;
31
+ };
32
+ /** Result of {@link defineField}: the field stamped with `name`, with refs normalized to strings. */
33
+ type DefinedField<TName extends string, TField extends FieldInput> = Prettify<Omit<TField, 'allow' | 'datasource' | 'name'> & {
34
+ name: TName;
35
+ } & (TField extends {
36
+ allow: infer A;
37
+ } ? {
38
+ allow: NormalizeAllow<A>;
39
+ } : unknown) & (TField extends {
40
+ datasource: infer D;
41
+ } ? {
42
+ datasource: SlugOf<D>;
43
+ } : unknown)>;
87
44
  /**
88
- * Returns a {@link Field} stamped with the given `name`. Use inside a
89
- * {@link defineBlock} `schema` array `pos` is inferred from array index.
45
+ * Returns a {@link Field} stamped with the given `name`, normalizing reference
46
+ * keys to strings so everything downstream sees plain names/slugs. A thin,
47
+ * strongly-typed identity helper — it does not validate or throw.
48
+ *
49
+ * Use inside a {@link defineBlock} `fields` array — `pos` is injected from the
50
+ * array index by `defineBlock`.
90
51
  *
91
52
  * @example
92
53
  * defineField('headline', { type: 'text', max_length: 100, required: true });
54
+ * defineField('body', { type: 'bloks', allow: [heroBlock, 'teaser'] });
55
+ * defineField('theme', { type: 'option', source: 'internal', datasource: colors });
93
56
  */
94
- declare const defineField: <const TName extends string, const TField extends Field>(name: TName, field: TField) => Omit<TField, "name"> & {
95
- name: TName;
96
- };
57
+ declare function defineField<const TName extends string, const TField extends FieldInput>(name: TName, field: TField): DefinedField<TName, TField>;
97
58
  //#endregion
98
- export { BlockContent, BlockContentInput, BlocksFieldValue, FieldType, FieldValue, FieldValueInput, defineField };
59
+ export { DefinedField, FieldInput, defineField };
99
60
  //# sourceMappingURL=define-field.d.mts.map
@@ -1,15 +1,16 @@
1
+ import { isRecord } from "../utils/is-record.mjs";
2
+
1
3
  //#region src/helpers/define-field.ts
2
- /**
3
- * Returns a {@link Field} stamped with the given `name`. Use inside a
4
- * {@link defineBlock} `schema` array — `pos` is inferred from array index.
5
- *
6
- * @example
7
- * defineField('headline', { type: 'text', max_length: 100, required: true });
8
- */
9
- const defineField = (name, field) => ({
10
- ...field,
11
- name
12
- });
4
+ function defineField(name, field) {
5
+ const { allow, datasource, ...rest } = field;
6
+ const normalized = {
7
+ ...rest,
8
+ name
9
+ };
10
+ if (allow !== void 0) normalized.allow = (Array.isArray(allow) ? allow : [allow]).map((ref) => typeof ref === "string" ? ref : isRecord(ref) ? ref.name : void 0);
11
+ if (datasource !== void 0) normalized.datasource = typeof datasource === "string" ? datasource : isRecord(datasource) ? datasource.slug : void 0;
12
+ return normalized;
13
+ }
13
14
 
14
15
  //#endregion
15
16
  export { defineField };
@@ -1 +1 @@
1
- {"version":3,"file":"define-field.mjs","names":[],"sources":["../../src/helpers/define-field.ts"],"sourcesContent":["import type {\n AssetFieldValue,\n Field,\n MultilinkFieldValue,\n PluginFieldValue,\n RichtextFieldValue,\n TableFieldValue,\n} from '../generated/types/field';\nimport type { Block } from './define-block';\nimport type { Prettify } from '../utils/prettify';\n\nexport type { Field };\n\n/** Loose variant of the generated `BlokContent` with `_uid` optional used as the fallback for write operations when no block union is provided. */\ntype BlokContentLoose = {\n _uid?: string;\n component: string;\n _editable?: string | undefined;\n} & {\n [key: string]:\n | string\n | number\n | boolean\n | Array<string | AssetFieldValue | BlokContentLoose>\n | AssetFieldValue\n | MultilinkFieldValue\n | TableFieldValue\n | RichtextFieldValue\n | PluginFieldValue\n | undefined;\n};\n\n/** Keys in a schema record that have `required: true`. */\ntype RequiredFieldKeys<T> = {\n [K in keyof T]: T[K] extends { required: true } ? K : never\n}[keyof T];\n\n/** Keys in a schema record that do NOT have `required: true`. */\ntype OptionalFieldKeys<T> = Exclude<keyof T, RequiredFieldKeys<T>>;\n\n/**\n * Builds the content object for a single block instance as returned by the\n * Storyblok Content Delivery API. Includes `_uid` (always present in API\n * responses) and respects required/optional field semantics.\n *\n * For write operations (creating/updating stories), use {@link BlockContentInput}\n * instead, which omits `_uid` since Storyblok generates it automatically.\n */\nexport type BlockContent<TBlock extends Block = Block, TBlocks = false> = TBlock extends any\n ? { _uid: string; component: TBlock['name'] }\n & {\n [K in RequiredFieldKeys<TBlock['schema']>]: FieldValue<NonNullable<TBlock['schema'][K]>, TBlocks>\n }\n & {\n [K in OptionalFieldKeys<TBlock['schema']>]?: FieldValue<NonNullable<TBlock['schema'][K]>, TBlocks> | null\n }\n : never;\n\n/**\n * Input variant of {@link BlockContent} for write operations (creating/updating\n * stories via the MAPI). `_uid` is optional Storyblok generates it\n * automatically when omitted. Nested bloks fields also use this input variant\n * so that deeply nested blocks do not require `_uid` either.\n */\nexport type BlockContentInput<TBlock extends Block = Block, TBlocks = false> = TBlock extends any\n ? { _uid?: string; component: TBlock['name'] }\n & {\n [K in RequiredFieldKeys<TBlock['schema']>]: FieldValueInput<NonNullable<TBlock['schema'][K]>, TBlocks>\n }\n & {\n [K in OptionalFieldKeys<TBlock['schema']>]?: FieldValueInput<NonNullable<TBlock['schema'][K]>, TBlocks> | null\n }\n : never;\n\nexport type BlocksFieldValue<\n TBlock extends Block = Block,\n TBlocks = false,\n> = BlockContent<TBlock, TBlocks>[];\n\nexport type { AssetFieldValue, MultilinkFieldValue, PluginFieldValue, RichtextFieldValue, TableFieldValue };\n\n/** Union of all valid Storyblok field type discriminants (e.g., `text`, `bloks`). */\nexport type FieldType = Field['type'];\n\n/** Maps each field type discriminant to its runtime content value type. */\ninterface FieldTypeValueMap {\n text: string;\n textarea: string;\n richtext: RichtextFieldValue;\n markdown: string;\n number: number;\n datetime: string;\n boolean: boolean;\n option: string;\n options: string[];\n asset: AssetFieldValue;\n multiasset: AssetFieldValue[];\n multilink: MultilinkFieldValue;\n bloks: BlockContent[];\n table: TableFieldValue;\n section: never;\n tab: never;\n custom: PluginFieldValue;\n}\n\n/**\n * Checks whether a block is nestable, defaulting to `true` when\n * `is_nestable` is absent or undefined.\n */\ntype IsNestable<T> =\n T extends { is_nestable: false } ? false\n : T extends { is_nestable: true } ? true\n : true; // default: nestable when is_nestable is not specified\n\ntype ApplyWhitelist<TField, TBlocks> = TField extends { component_whitelist: ReadonlyArray<infer TWhitelisted extends string> }\n // With whitelist: filter by block name (distributive over TBlocks)\n ? TBlocks extends { name: TWhitelisted } ? TBlocks : never\n // No whitelist: filter by nestability (distributive over TBlocks)\n : TBlocks extends any\n ? IsNestable<TBlocks> extends true ? TBlocks : never\n : never;\n\n/** Resolves a field definition to its runtime content value type (read). */\nexport type FieldValue<\n TField extends Field = Field,\n TBlocks = false,\n> = Prettify<\n TField extends { type: 'bloks' }\n // Bloks fieldguard against `never` first (it satisfies `[never] extends [X]`\n // for all X, which would incorrectly enter the typed path with empty results).\n ? [TBlocks] extends [never]\n ? BlockContent[]\n : [TBlocks] extends [Block]\n ? BlockContent<ApplyWhitelist<TField, TBlocks>, TBlocks>[]\n : BlockContent[]\n // No bloks field\n : FieldTypeValueMap[TField['type']]\n>;\n\n/** Resolves a field definition to its input value type (write). Nested bloks use {@link BlockContentInput}. */\nexport type FieldValueInput<\n TField extends Field = Field,\n TBlocks = false,\n> = Prettify<\n TField extends { type: 'bloks' }\n ? [TBlocks] extends [never]\n ? BlokContentLoose[]\n : [TBlocks] extends [Block]\n ? BlockContentInput<ApplyWhitelist<TField, TBlocks>, TBlocks>[]\n : BlokContentLoose[]\n : FieldTypeValueMap[TField['type']]\n>;\n\n/**\n * Returns a {@link Field} stamped with the given `name`. Use inside a\n * {@link defineBlock} `schema` array `pos` is inferred from array index.\n *\n * @example\n * defineField('headline', { type: 'text', max_length: 100, required: true });\n */\nexport const defineField = <\n const TName extends string,\n const TField extends Field,\n>(\n name: TName,\n field: TField,\n): Omit<TField, 'name'> & { name: TName } => ({ ...field, name });\n"],"mappings":";;;;;;;;AAgKA,MAAa,eAIX,MACA,WAC4C;CAAE,GAAG;CAAO;CAAM"}
1
+ {"version":3,"file":"define-field.mjs","names":[],"sources":["../../src/helpers/define-field.ts"],"sourcesContent":["import type {\n AssetFieldValue,\n BlockContent,\n BlockContentInput,\n BlocksFieldValue,\n Field,\n FieldType,\n FieldValue,\n FieldValueInput,\n MultilinkFieldValue,\n PluginFieldValue,\n RichtextFieldValue,\n TableFieldValue,\n} from '../generated/types/field';\nimport type { Prettify } from '../utils/prettify';\nimport { isRecord } from '../utils/is-record';\n\nexport type {\n AssetFieldValue,\n BlockContent,\n BlockContentInput,\n BlocksFieldValue,\n Field,\n FieldType,\n FieldValue,\n FieldValueInput,\n MultilinkFieldValue,\n PluginFieldValue,\n RichtextFieldValue,\n TableFieldValue,\n};\n\n/** A block reference for `allow`: a defined block object or its name. */\ntype AllowRef = string | { name: string };\n/** A datasource reference for `datasource`: a defined datasource object or its slug. */\ntype DatasourceRef = string | { slug: string };\n\ntype NameOf<T> = T extends string ? T : T extends { name: infer N extends string } ? N : never;\ntype SlugOf<T> = T extends string ? T : T extends { slug: infer S extends string } ? S : never;\n\n/** Normalizes an `allow` input (ref, name, or array thereof) to a tuple of block-name strings. */\ntype NormalizeAllow<T> = T extends readonly any[]\n ? { [I in keyof T]: NameOf<T[I]> }\n : readonly [NameOf<T>];\n\n/**\n * Field config accepted by {@link defineField}: the content-shape field plus the\n * DSL reference keys. `allow` replaces the wire `component_whitelist`; `datasource`\n * holds the datasource ref/slug (the wire `source` selector still passes through).\n */\nexport type FieldInput = Field & {\n allow?: AllowRef | readonly AllowRef[];\n datasource?: DatasourceRef;\n required?: boolean;\n};\n\n/** Result of {@link defineField}: the field stamped with `name`, with refs normalized to strings. */\nexport type DefinedField<TName extends string, TField extends FieldInput> = Prettify<\n Omit<TField, 'allow' | 'datasource' | 'name'>\n & { name: TName }\n & (TField extends { allow: infer A } ? { allow: NormalizeAllow<A> } : unknown)\n & (TField extends { datasource: infer D } ? { datasource: SlugOf<D> } : unknown)\n>;\n\n/**\n * Returns a {@link Field} stamped with the given `name`, normalizing reference\n * keys to strings so everything downstream sees plain names/slugs. A thin,\n * strongly-typed identity helper it does not validate or throw.\n *\n * Use inside a {@link defineBlock} `fields` array — `pos` is injected from the\n * array index by `defineBlock`.\n *\n * @example\n * defineField('headline', { type: 'text', max_length: 100, required: true });\n * defineField('body', { type: 'bloks', allow: [heroBlock, 'teaser'] });\n * defineField('theme', { type: 'option', source: 'internal', datasource: colors });\n */\nexport function defineField<\n const TName extends string,\n const TField extends FieldInput,\n>(name: TName, field: TField): DefinedField<TName, TField>;\nexport function defineField(name: string, field: Record<string, unknown>): Record<string, unknown> {\n const { allow, datasource, ...rest } = field;\n const normalized: Record<string, unknown> = { ...rest, name };\n if (allow !== undefined) {\n const refs = Array.isArray(allow) ? allow : [allow];\n normalized.allow = refs.map(ref => (typeof ref === 'string' ? ref : isRecord(ref) ? ref.name : undefined));\n }\n if (datasource !== undefined) {\n normalized.datasource = typeof datasource === 'string' ? datasource : isRecord(datasource) ? datasource.slug : undefined;\n }\n return normalized;\n}\n"],"mappings":";;;AAiFA,SAAgB,YAAY,MAAc,OAAyD;CACjG,MAAM,EAAE,OAAO,YAAY,GAAG,SAAS;CACvC,MAAM,aAAsC;EAAE,GAAG;EAAM;EAAM;AAC7D,KAAI,UAAU,OAEZ,YAAW,SADE,MAAM,QAAQ,MAAM,GAAG,QAAQ,CAAC,MAAM,EAC3B,KAAI,QAAQ,OAAO,QAAQ,WAAW,MAAM,SAAS,IAAI,GAAG,IAAI,OAAO,OAAW;AAE5G,KAAI,eAAe,OACjB,YAAW,aAAa,OAAO,eAAe,WAAW,aAAa,SAAS,WAAW,GAAG,WAAW,OAAO;AAEjH,QAAO"}
@@ -1,14 +1,14 @@
1
1
  import { Block } from "../generated/types/block.cjs";
2
- import { Datasource } from "../generated/capi/types.gen.cjs";
3
- import { ComponentFolder } from "../generated/mapi/types.gen.cjs";
2
+ import { Datasource } from "./define-datasource.cjs";
4
3
 
5
4
  //#region src/helpers/schema-type.d.ts
6
5
  /**
7
6
  * Derives a schema types interface from a schema object.
8
7
  *
9
- * Accepts the `typeof` a schema object whose `blocks` property is a
10
- * record of `defineBlock()` results, and optionally `blockFolders`
11
- * and `datasources` records, and produces a type with unions of those types.
8
+ * Accepts the `typeof` a schema object whose `blocks` property is a record of
9
+ * `defineBlock()` results, and optionally a `datasources` record, and produces
10
+ * a type with unions of those types. A schema describes content shapes only;
11
+ * component groups are a UI concern and not part of it.
12
12
  *
13
13
  * @example
14
14
  * ```ts
@@ -16,23 +16,19 @@ import { ComponentFolder } from "../generated/mapi/types.gen.cjs";
16
16
  *
17
17
  * export const schema = {
18
18
  * blocks: { pageBlock, heroBlock },
19
- * blockFolders: { layoutFolder },
20
19
  * datasources: { colorsDatasource },
21
20
  * };
22
21
  *
23
22
  * export type Schema = InferSchema<typeof schema>;
24
23
  * export type Blocks = Schema['blocks'];
25
- * export type BlockFolders = Schema['blockFolders'];
26
24
  * export type Datasources = Schema['datasources'];
27
25
  * ```
28
26
  */
29
27
  interface Schema<T extends {
30
28
  blocks: Record<string, Block>;
31
- blockFolders?: Record<string, ComponentFolder>;
32
29
  datasources?: Record<string, Datasource>;
33
30
  }> {
34
31
  blocks: T['blocks'][keyof T['blocks']];
35
- blockFolders: T['blockFolders'] extends Record<string, ComponentFolder> ? T['blockFolders'][keyof T['blockFolders']] : never;
36
32
  datasources: T['datasources'] extends Record<string, Datasource> ? T['datasources'][keyof T['datasources']] : never;
37
33
  }
38
34
  //#endregion
@@ -1,14 +1,14 @@
1
1
  import { Block } from "../generated/types/block.mjs";
2
- import { Datasource } from "../generated/capi/types.gen.mjs";
3
- import { ComponentFolder } from "../generated/mapi/types.gen.mjs";
2
+ import { Datasource } from "./define-datasource.mjs";
4
3
 
5
4
  //#region src/helpers/schema-type.d.ts
6
5
  /**
7
6
  * Derives a schema types interface from a schema object.
8
7
  *
9
- * Accepts the `typeof` a schema object whose `blocks` property is a
10
- * record of `defineBlock()` results, and optionally `blockFolders`
11
- * and `datasources` records, and produces a type with unions of those types.
8
+ * Accepts the `typeof` a schema object whose `blocks` property is a record of
9
+ * `defineBlock()` results, and optionally a `datasources` record, and produces
10
+ * a type with unions of those types. A schema describes content shapes only;
11
+ * component groups are a UI concern and not part of it.
12
12
  *
13
13
  * @example
14
14
  * ```ts
@@ -16,23 +16,19 @@ import { ComponentFolder } from "../generated/mapi/types.gen.mjs";
16
16
  *
17
17
  * export const schema = {
18
18
  * blocks: { pageBlock, heroBlock },
19
- * blockFolders: { layoutFolder },
20
19
  * datasources: { colorsDatasource },
21
20
  * };
22
21
  *
23
22
  * export type Schema = InferSchema<typeof schema>;
24
23
  * export type Blocks = Schema['blocks'];
25
- * export type BlockFolders = Schema['blockFolders'];
26
24
  * export type Datasources = Schema['datasources'];
27
25
  * ```
28
26
  */
29
27
  interface Schema<T extends {
30
28
  blocks: Record<string, Block>;
31
- blockFolders?: Record<string, ComponentFolder>;
32
29
  datasources?: Record<string, Datasource>;
33
30
  }> {
34
31
  blocks: T['blocks'][keyof T['blocks']];
35
- blockFolders: T['blockFolders'] extends Record<string, ComponentFolder> ? T['blockFolders'][keyof T['blockFolders']] : never;
36
32
  datasources: T['datasources'] extends Record<string, Datasource> ? T['datasources'][keyof T['datasources']] : never;
37
33
  }
38
34
  //#endregion