@crombolo/contracts 0.37.0 → 0.38.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crombolo/contracts",
3
- "version": "0.37.0",
3
+ "version": "0.38.1",
4
4
  "description": "The Umbraco Management and Delivery API OpenAPI documents and the TypeScript types generated from them.",
5
5
  "license": "MIT",
6
6
  "author": "Crombolo",
package/src/overrides.ts CHANGED
@@ -38,3 +38,69 @@ export function applyOverrides<T extends { components: { schemas: Record<string,
38
38
  }
39
39
  return copy
40
40
  }
41
+
42
+ const RICH_TEXT_ELEMENTS = {
43
+ oneOf: [
44
+ { $ref: '#/components/schemas/RichTextGenericElement' },
45
+ { $ref: '#/components/schemas/RichTextTextElement' },
46
+ ],
47
+ discriminator: { propertyName: '$type' },
48
+ }
49
+
50
+ /**
51
+ * Schemas crombolo's Delivery API adds to Umbraco's generic contract, where a
52
+ * property's value is an untyped object. `DeliveryApi.json` stays exactly
53
+ * Umbraco's; these are added when it is loaded.
54
+ */
55
+ export const DELIVERY_CONTRACT_ADDITIONS: readonly {
56
+ schema: string
57
+ story: string
58
+ reason: string
59
+ definition: Record<string, unknown>
60
+ }[] = [
61
+ {
62
+ schema: 'RichTextRootElement',
63
+ story: '216',
64
+ reason:
65
+ "Every rich-text value is this tree, Umbraco's RichTextOutputAsJson form, always on; the HTML form is not served.",
66
+ definition: {
67
+ type: 'object',
68
+ required: ['tag', 'attributes', 'elements', 'blocks'],
69
+ properties: {
70
+ tag: { const: '#root' },
71
+ attributes: { type: 'object' },
72
+ elements: { type: 'array', items: RICH_TEXT_ELEMENTS },
73
+ blocks: { type: 'array', items: { type: 'object' } },
74
+ },
75
+ },
76
+ },
77
+ {
78
+ schema: 'RichTextGenericElement',
79
+ story: '216',
80
+ reason: 'An element of a rich-text tree.',
81
+ definition: {
82
+ type: 'object',
83
+ required: ['$type', 'tag', 'attributes', 'elements'],
84
+ properties: {
85
+ $type: { const: 'RichTextGenericElement' },
86
+ tag: { type: 'string' },
87
+ attributes: { type: 'object' },
88
+ elements: { type: 'array', items: RICH_TEXT_ELEMENTS },
89
+ },
90
+ },
91
+ },
92
+ {
93
+ schema: 'RichTextTextElement',
94
+ story: '216',
95
+ reason: 'A text node of a rich-text tree.',
96
+ definition: {
97
+ type: 'object',
98
+ required: ['$type', 'tag', 'text'],
99
+ properties: {
100
+ $type: { const: 'RichTextTextElement' },
101
+ tag: { const: '#text' },
102
+ text: { type: 'string' },
103
+ },
104
+ },
105
+ },
106
+ ]
package/src/spec.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /** Loads and indexes the vendored Umbraco Management API OpenAPI contract. */
2
2
  import { readFileSync } from 'node:fs'
3
3
  import { join } from 'node:path'
4
- import { applyOverrides } from './overrides.ts'
4
+ import { applyOverrides, DELIVERY_CONTRACT_ADDITIONS } from './overrides.ts'
5
5
 
6
6
  export const SPEC_PATH = join(import.meta.dir, '../OpenApi.json')
7
7
 
@@ -75,9 +75,22 @@ const servedAt = new Map<string, string>()
75
75
 
76
76
  let deliveryCached: OpenApiSpec | undefined
77
77
 
78
+ /** Umbraco's Delivery API contract exactly as vendored. */
79
+ export function loadUmbracoDeliverySpec(): OpenApiSpec {
80
+ return JSON.parse(readFileSync(DELIVERY_SPEC_PATH, 'utf8')) as OpenApiSpec
81
+ }
82
+
83
+ /** The Delivery API contract crombolo answers to: Umbraco's, with `DELIVERY_CONTRACT_ADDITIONS`. */
78
84
  export function loadDeliverySpec(): OpenApiSpec {
79
- if (!deliveryCached)
80
- deliveryCached = JSON.parse(readFileSync(DELIVERY_SPEC_PATH, 'utf8')) as OpenApiSpec
85
+ if (!deliveryCached) {
86
+ const spec = loadUmbracoDeliverySpec()
87
+ for (const addition of DELIVERY_CONTRACT_ADDITIONS) {
88
+ if (spec.components.schemas[addition.schema])
89
+ throw new Error(`The Delivery API contract already has a schema ${addition.schema}`)
90
+ spec.components.schemas[addition.schema] = structuredClone(addition.definition)
91
+ }
92
+ deliveryCached = spec
93
+ }
81
94
  return deliveryCached
82
95
  }
83
96