@crombolo/render 0.55.0 → 0.57.0

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/render",
3
- "version": "0.55.0",
3
+ "version": "0.57.0",
4
4
  "description": "Internal to crombolo, with no compatibility promise: use @crombolo/crombolo. TSX server rendering and the published-content model for crombolo.",
5
5
  "license": "MIT",
6
6
  "author": "Crombolo",
@@ -24,7 +24,8 @@
24
24
  "./jsx-runtime": "./src/jsx-runtime.ts",
25
25
  "./jsx-dev-runtime": "./src/jsx-runtime.ts",
26
26
  "./loose/jsx-runtime": "./src/loose/jsx-runtime.ts",
27
- "./loose/jsx-dev-runtime": "./src/loose/jsx-runtime.ts"
27
+ "./loose/jsx-dev-runtime": "./src/loose/jsx-runtime.ts",
28
+ "./html": "./src/html.ts"
28
29
  },
29
30
  "files": [
30
31
  "src",
@@ -34,8 +35,8 @@
34
35
  "access": "public"
35
36
  },
36
37
  "dependencies": {
37
- "@crombolo/common": "0.55.0",
38
- "@crombolo/core": "0.55.0",
38
+ "@crombolo/common": "0.57.0",
39
+ "@crombolo/core": "0.57.0",
39
40
  "@types/react": "^19.3.0"
40
41
  }
41
42
  }
package/src/context.ts CHANGED
@@ -28,6 +28,8 @@ interface RenderScope {
28
28
  ids: { next: number }
29
29
  /** A block's component by element type alias, from the page's own views generation. */
30
30
  blockComponent?: BlockComponentLoader
31
+ /** An extension's component, `<id>/<name>`, for `<Extension use>` (story 312). */
32
+ extensionComponent?: ExtensionComponentLoader
31
33
  /** A draft is being previewed in the back office. */
32
34
  preview?: boolean
33
35
  /** The signed-in member's anti-forgery token, for `<AntiforgeryMeta />`. */
@@ -65,9 +67,15 @@ export type BlockComponentLoader = (
65
67
  ) => Promise<((props: never) => Child | Promise<Child>) | undefined>
66
68
 
67
69
  /** What a page render carries besides its props: the search, blocks, and whether it is a preview. */
70
+ /** An extension's component as a render function, or undefined when no extension offers it. */
71
+ export type ExtensionComponentLoader = (
72
+ alias: string,
73
+ ) => ((props: object) => Child | Promise<Child>) | undefined
74
+
68
75
  export interface RenderScopeOptions {
69
76
  search?: SiteSearchPort
70
77
  blockComponent?: BlockComponentLoader
78
+ extensionComponent?: ExtensionComponentLoader
71
79
  preview?: boolean
72
80
  antiforgery?: string
73
81
  request?: RequestValues
@@ -208,6 +216,11 @@ export function actionState<T = unknown>(action: unknown): ActionState<T> | unde
208
216
  }
209
217
 
210
218
  /** The signed-in member's anti-forgery token for this render, or undefined for a visitor. */
219
+ /** The loader for an extension's components, while a page renders; undefined outside one. */
220
+ export function currentExtensionComponent(): ExtensionComponentLoader | undefined {
221
+ return store.getStore()?.extensionComponent
222
+ }
223
+
211
224
  export function currentAntiforgery(): string | undefined {
212
225
  return memberAntiforgery(within('<AntiforgeryMeta />'))
213
226
  }
@@ -0,0 +1,29 @@
1
+ /**
2
+ * `<Extension use="brochures/card" … />`: an extension's component in a site's
3
+ * page (story 312). It renders in the extension's own process, given the page's
4
+ * props and these, and its HTML goes here. Children render here first and go as
5
+ * HTML. Nothing renders where no extension offers it, or where it is not granted
6
+ * `templates`, and the site's log says why. docs/writing-components.md.
7
+ */
8
+ import { currentExtensionComponent, usePage } from './context.ts'
9
+ import { RawHtml } from './html.ts'
10
+ import { type Child, renderToString } from './jsx-runtime.ts'
11
+
12
+ export interface ExtensionProps {
13
+ /** `<extension id>/<component>`. */
14
+ use: string
15
+ children?: Child
16
+ [prop: string]: unknown
17
+ }
18
+
19
+ export async function Extension({ use, children, ...props }: ExtensionProps): Promise<RawHtml> {
20
+ const render = currentExtensionComponent()?.(use)
21
+ if (!render) return new RawHtml('')
22
+ const inner = children === undefined ? undefined : new RawHtml(await renderToString(children))
23
+ const rendered = await render({
24
+ ...usePage(),
25
+ ...props,
26
+ ...(inner ? { children: inner } : {}),
27
+ })
28
+ return rendered instanceof RawHtml ? rendered : new RawHtml(await renderToString(rendered))
29
+ }
package/src/index.ts CHANGED
@@ -7,6 +7,7 @@ export * from './context.ts'
7
7
  export * from './deferred.ts'
8
8
  export * from './email.ts'
9
9
  export * from './excerpt.ts'
10
+ export * from './extension.ts'
10
11
  export * from './form-script.ts'
11
12
  export * from './forms.ts'
12
13
  export * from './gap.ts'
package/src/renderer.ts CHANGED
@@ -72,6 +72,12 @@ export interface RenderOptions {
72
72
  * with (story 209); by default the generation's own build.
73
73
  */
74
74
  islandsManifest?: (generation: string | undefined) => string | undefined
75
+ /**
76
+ * Components an extension owns (story 312): `<id>/<name>`, or a block's
77
+ * `blocks/<alias>`, where the site has none of its own. Each renders in the
78
+ * extension's process.
79
+ */
80
+ extensionComponents?: { module(alias: string): ComponentModule | undefined }
75
81
  }
76
82
 
77
83
  export const DEFAULT_RENDER_TIMEOUT_MS = 10_000
@@ -228,6 +234,8 @@ export class Renderer {
228
234
  #renderTimeoutMs: number
229
235
  #islandsManifest: (generation: string | undefined) => string | undefined
230
236
 
237
+ #extensionComponents: RenderOptions['extensionComponents']
238
+
231
239
  constructor(options: RenderOptions) {
232
240
  this.#cache = options.cache
233
241
  // Resolved to an absolute path: a dynamic import of a relative specifier
@@ -235,6 +243,7 @@ export class Renderer {
235
243
  this.#componentsDir = resolve(options.componentsDir)
236
244
  this.#snapshots = options.snapshots
237
245
  this.#renderTimeoutMs = options.renderTimeoutMs ?? DEFAULT_RENDER_TIMEOUT_MS
246
+ this.#extensionComponents = options.extensionComponents
238
247
  this.#islandsManifest =
239
248
  options.islandsManifest ??
240
249
  ((generation) => (generation ? `${ISLANDS_PATH}/${generation}/manifest.json` : undefined))
@@ -577,6 +586,10 @@ export class Renderer {
577
586
  preview: request.preview,
578
587
  antiforgery: request.antiforgery,
579
588
  blockComponent: (blockAlias) => this.blockComponent(blockAlias, used),
589
+ extensionComponent: (use) =>
590
+ this.#extensionComponents?.module(use)?.default as
591
+ | ((props: object) => Child | Promise<Child>)
592
+ | undefined,
580
593
  request: { headers: shared ? new Headers() : (request.headers ?? new Headers()) },
581
594
  site: request.site,
582
595
  personal: shared ? { value: false } : personal,
@@ -662,7 +675,8 @@ export class Renderer {
662
675
 
663
676
  async #load(alias: string, used?: Set<string>): Promise<ComponentModule | undefined> {
664
677
  const file = this.componentPath(alias)
665
- if (!file) return undefined
678
+ // The site's own component wins; an extension's renders in its own process.
679
+ if (!file) return this.#extensionComponents?.module(alias)
666
680
  used?.add(file)
667
681
  // The path carries the generation, so one entry per view per generation and
668
682
  // a swap never answers from the generation before it.
package/src/site.ts CHANGED
@@ -83,11 +83,81 @@ export interface SiteJobs {
83
83
  status(id: string): Promise<SiteJobStatus | undefined>
84
84
  }
85
85
 
86
+ /**
87
+ * A page as the site's code reads and writes it (story 298): its values by
88
+ * alias, and by culture for a property that varies, `{ title: { 'en-GB': 'Hi' } }`.
89
+ */
90
+ export interface SiteDocument {
91
+ key: string
92
+ /** Its content type's alias. */
93
+ contentType: string
94
+ parent: string | null
95
+ /** Its name, or its name in each culture where the type varies. */
96
+ name: string | Readonly<Record<string, string>>
97
+ values: Readonly<Record<string, unknown>>
98
+ /** Whether it, or any of its cultures, is published. */
99
+ published: boolean
100
+ }
101
+
102
+ /** What a new page is: where, of what type, and its name and values. */
103
+ export interface SiteDocumentInput {
104
+ parent: string | null
105
+ /** The content type's alias. */
106
+ contentType: string
107
+ name: string
108
+ /** The culture `name`, and any plain value of a varying property, are in. */
109
+ culture?: string
110
+ values?: Readonly<Record<string, unknown>>
111
+ }
112
+
113
+ /** A change to a page: a new name, and the values given; the rest stay. */
114
+ export interface SiteDocumentChange {
115
+ name?: string
116
+ culture?: string
117
+ values?: Readonly<Record<string, unknown>>
118
+ }
119
+
120
+ type ContentWrite = Promise<Result<{ key: string }, 400 | 403 | 404 | 422>>
121
+
122
+ /**
123
+ * Pages, as the API user the config's `content.actAs` names: its permissions
124
+ * bound what this may do, and every change is audited as it. Writes happen in
125
+ * an action, a route, a job or an event handler; a page render only reads.
126
+ */
127
+ export interface SiteContent {
128
+ /** The page as last saved. */
129
+ get(key: string): Promise<Result<SiteDocument, 403 | 404>>
130
+ /** A page's children as last saved, in order; `null` for the top level. */
131
+ children(key: string | null): Promise<Result<SiteDocument[], 403 | 404>>
132
+ create(input: SiteDocumentInput): ContentWrite
133
+ update(key: string, change: SiteDocumentChange): ContentWrite
134
+ /** Publishes it, or some of its cultures; a workflow may hold it for approval. */
135
+ publish(key: string, options?: { cultures?: string[] }): ContentWrite
136
+ unpublish(key: string, options?: { cultures?: string[] }): ContentWrite
137
+ /** To the recycle bin. */
138
+ delete(key: string): ContentWrite
139
+ }
140
+
141
+ /** A content event as the config's `events` handlers receive it (story 298). */
142
+ export interface SiteContentEvent {
143
+ /** As webhooks name it: `Umbraco.ContentPublish`. */
144
+ alias: string
145
+ /** The page it is about. */
146
+ key: string
147
+ /** Its content type's alias, when it is still there to say. */
148
+ contentType: string | null
149
+ /** The cultures it was about, when it was about some. */
150
+ cultures: string[] | null
151
+ /** Who made the change. */
152
+ actor: { kind: string; name: string }
153
+ }
154
+
86
155
  export interface SiteServices {
87
156
  media: SiteMedia
88
157
  queries: SiteQueries
89
158
  members: SiteMembers
90
159
  jobs: SiteJobs
160
+ content: SiteContent
91
161
  }
92
162
 
93
163
  /** The site's services as the visitor; only while a page renders. */