@crombolo/api-management 0.26.0 → 0.28.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/api-management",
3
- "version": "0.26.0",
3
+ "version": "0.28.0",
4
4
  "description": "Umbraco-compatible Management API handlers for crombolo.",
5
5
  "license": "MIT",
6
6
  "author": "Crombolo",
@@ -28,8 +28,8 @@
28
28
  "access": "public"
29
29
  },
30
30
  "dependencies": {
31
- "@crombolo/content": "0.26.0",
32
- "@crombolo/contracts": "0.26.0",
33
- "@crombolo/core": "0.26.0"
31
+ "@crombolo/content": "0.28.0",
32
+ "@crombolo/contracts": "0.28.0",
33
+ "@crombolo/core": "0.28.0"
34
34
  }
35
35
  }
@@ -29,3 +29,22 @@ export const DOCUMENT_WRITE_OPERATIONS: ReadonlySet<string> = new Set([
29
29
  'PutDocumentByIdPublicAccess',
30
30
  'DeleteDocumentByIdPublicAccess',
31
31
  ])
32
+
33
+ /**
34
+ * The media writes that go through the media operations (story 165). Each
35
+ * writes its own audit entry and webhook event in the change's transaction; the
36
+ * router still raises their live events and change notices.
37
+ */
38
+ export const MEDIA_WRITE_OPERATIONS: ReadonlySet<string> = new Set([
39
+ 'PostMedia',
40
+ 'PutMediaById',
41
+ 'DeleteMediaById',
42
+ 'PutMediaByIdMoveToRecycleBin',
43
+ 'PutMediaByIdMove',
44
+ 'PutMediaSort',
45
+ 'PutMediaByIdSortChildren',
46
+ 'PutMediaRootSortChildren',
47
+ 'PutRecycleBinMediaByIdRestore',
48
+ 'DeleteRecycleBinMediaById',
49
+ 'DeleteRecycleBinMedia',
50
+ ])
@@ -708,8 +708,10 @@ export function registerDocumentHandlers(
708
708
  },
709
709
  )
710
710
  if (!result.ok) return failed('Could not copy the document', result)
711
+ // Not a dry run, so the copy has its key.
712
+ const key = result.key as string
711
713
  ctx.notifications.push({ message: 'Document copied', category: 'Content', type: 'Success' })
712
- return created(`${ctx.url.origin}/umbraco/management/api/v1/document/${result.key}`, result.key)
714
+ return created(`${ctx.url.origin}/umbraco/management/api/v1/document/${key}`, key)
713
715
  })
714
716
 
715
717
  router.handle('PutDocumentSort', async (ctx) => {
@@ -2,6 +2,9 @@
2
2
  * The Media section: the tree, the editor, the collection view, the recycle bin
3
3
  * and the file URLs its thumbnails and pickers need.
4
4
  */
5
+
6
+ import type { MediaOperations, Refusal } from '@crombolo/content'
7
+ import { userActor } from '@crombolo/content/rules'
5
8
  import type { DocumentAggregate, DocumentTreeItem } from '@crombolo/core'
6
9
  import {
7
10
  invalidSkipTake,
@@ -109,10 +112,20 @@ function toSaveMedia(key: string, body: Record<string, unknown>) {
109
112
  }
110
113
  }
111
114
 
112
- export function registerMediaHandlers(router: ManagementApiRouter, port: MediaPort): void {
115
+ export function registerMediaHandlers(
116
+ router: ManagementApiRouter,
117
+ port: MediaPort,
118
+ operations: MediaOperations,
119
+ ): void {
113
120
  const mediaStart = (ctx: RequestContext) => ctx.principal?.startNodes.media ?? ROOT_ACCESS
114
121
 
115
- const principal = (ctx: RequestContext) => ctx.principal as NonNullable<typeof ctx.principal>
122
+ /** The signed-in user, as the operations know them: their rules apply (story 165). */
123
+ const actorOf = (ctx: RequestContext) =>
124
+ userActor(ctx.principal as NonNullable<typeof ctx.principal>, ctx.client ?? {})
125
+ const missing = (result: Refusal, title: string) =>
126
+ result.kind === 'notFound'
127
+ ? problemResponse(notFound('The media item could not be found'))
128
+ : failed(title, result)
116
129
  const done = (ctx: RequestContext, message?: string) => {
117
130
  if (message) ctx.notifications.push({ message, category: 'Media', type: 'Success' })
118
131
  return new Response(null, { status: 200 })
@@ -183,7 +196,7 @@ export function registerMediaHandlers(router: ManagementApiRouter, port: MediaPo
183
196
  const input = toSaveMedia(key, body)
184
197
  const errors = await port.validate(input)
185
198
  if (errors.length > 0) return problemResponse(validationProblem(errors, input.values))
186
- const result = await port.create(input, principal(ctx))
199
+ const result = await operations.create(actorOf(ctx), input)
187
200
  if (!result.ok) return failed('Could not create the media item', result)
188
201
  return created(`${ctx.url.origin}${API}/media/${result.key}`, result.key)
189
202
  })
@@ -199,25 +212,27 @@ export function registerMediaHandlers(router: ManagementApiRouter, port: MediaPo
199
212
  }
200
213
  const errors = await port.validate(input)
201
214
  if (errors.length > 0) return problemResponse(validationProblem(errors, input.values))
202
- const result = await port.update(key, input, principal(ctx))
215
+ const result = await operations.save(actorOf(ctx), key, input)
203
216
  if (!result.ok) return failed('Could not save the media item', result)
204
217
  return done(ctx)
205
218
  })
206
219
 
207
220
  router.handle('DeleteMediaById', async (ctx) => {
208
- if (!(await port.remove(ctx.params.id as string)))
209
- return problemResponse(notFound('The media item could not be found'))
210
- return done(ctx)
221
+ const result = await operations.delete(actorOf(ctx), ctx.params.id as string)
222
+ return result.ok ? done(ctx) : missing(result, 'Could not delete the media item')
211
223
  })
212
224
 
213
225
  router.handle('PutMediaByIdMoveToRecycleBin', async (ctx) => {
214
- if (!(await port.moveToRecycleBin(ctx.params.id as string)))
215
- return problemResponse(notFound('The media item could not be found'))
216
- return done(ctx)
226
+ const result = await operations.trash(actorOf(ctx), ctx.params.id as string)
227
+ return result.ok ? done(ctx) : missing(result, 'Could not move the media item')
217
228
  })
218
229
 
219
230
  router.handle('PutMediaByIdMove', async (ctx) => {
220
- const result = await port.move(ctx.params.id as string, refId((await readBody(ctx)).target))
231
+ const result = await operations.move(
232
+ actorOf(ctx),
233
+ ctx.params.id as string,
234
+ refId((await readBody(ctx)).target),
235
+ )
221
236
  return result.ok ? done(ctx) : failed('Could not move the media item', result)
222
237
  })
223
238
 
@@ -229,7 +244,7 @@ export function registerMediaHandlers(router: ManagementApiRouter, port: MediaPo
229
244
  key: String(item.id ?? ''),
230
245
  sortOrder: Number(item.sortOrder ?? 0),
231
246
  }))
232
- const result = await port.sort(refId(body.parent), sorting)
247
+ const result = await operations.sort(actorOf(ctx), refId(body.parent), sorting)
233
248
  return result.ok ? done(ctx) : failed('Could not sort', result)
234
249
  })
235
250
 
@@ -241,7 +256,8 @@ export function registerMediaHandlers(router: ManagementApiRouter, port: MediaPo
241
256
  return problemResponse(problemDetails({ title: 'Unknown sort field', status: 400 }))
242
257
  if (!['Ascending', 'Descending'].includes(direction))
243
258
  return problemResponse(problemDetails({ title: 'Unknown sort direction', status: 400 }))
244
- const result = await port.sortChildrenBy(
259
+ const result = await operations.sortChildrenBy(
260
+ actorOf(ctx),
245
261
  parentKey,
246
262
  field as 'Name' | 'CreateDate' | 'UpdateDate',
247
263
  direction as 'Ascending' | 'Descending',
@@ -425,7 +441,11 @@ export function registerMediaHandlers(router: ManagementApiRouter, port: MediaPo
425
441
  router.handle('PutRecycleBinMediaByIdRestore', async (ctx) => {
426
442
  const body = await readBody(ctx)
427
443
  const target = 'target' in body ? refId(body.target) : undefined
428
- const result = await port.restore(ctx.params.id as string, target ?? undefined)
444
+ const result = await operations.restore(
445
+ actorOf(ctx),
446
+ ctx.params.id as string,
447
+ target ?? undefined,
448
+ )
429
449
  return result.ok ? done(ctx) : failed('Could not restore', result)
430
450
  })
431
451
  router.handle('DeleteRecycleBinMediaById', async (ctx) => {
@@ -436,12 +456,12 @@ export function registerMediaHandlers(router: ManagementApiRouter, port: MediaPo
436
456
  return problemResponse(
437
457
  problemDetails({ title: 'The media item is not in the recycle bin', status: 400 }),
438
458
  )
439
- await port.remove(key)
440
- return done(ctx)
459
+ const result = await operations.delete(actorOf(ctx), key, { fromBin: true })
460
+ return result.ok ? done(ctx) : missing(result, 'Could not delete the media item')
441
461
  })
442
462
  router.handle('DeleteRecycleBinMedia', async (ctx) => {
443
- await port.emptyRecycleBin()
444
- return done(ctx)
463
+ const result = await operations.emptyBin(actorOf(ctx))
464
+ return result.ok ? done(ctx) : failed('Could not empty the recycle bin', result)
445
465
  })
446
466
 
447
467
  // References arrive with relations (story 046); nothing is known to refer yet.
@@ -459,30 +459,6 @@ export interface BlueprintPort {
459
459
  export interface MediaPort {
460
460
  byKey(key: string): Promise<DocumentAggregate | undefined>
461
461
  validate(input: SaveDocument): Promise<DocumentValidationError[]>
462
- create(
463
- input: SaveDocument,
464
- principal: Principal,
465
- ): Promise<{ ok: true; key: string } | PortFailure>
466
- update(
467
- key: string,
468
- input: SaveDocument,
469
- principal: Principal,
470
- ): Promise<{ ok: true } | PortFailure>
471
- /** Deletes a branch for good, and its files. */
472
- remove(key: string): Promise<boolean>
473
- moveToRecycleBin(key: string): Promise<boolean>
474
- move(key: string, targetKey: string | null): Promise<{ ok: true } | PortFailure>
475
- sort(
476
- parentKey: string | null,
477
- sorting: ReadonlyArray<{ key: string; sortOrder: number }>,
478
- ): Promise<{ ok: true } | PortFailure>
479
- sortChildrenBy(
480
- parentKey: string | null,
481
- field: 'Name' | 'CreateDate' | 'UpdateDate',
482
- direction: 'Ascending' | 'Descending',
483
- ): Promise<{ ok: true } | PortFailure>
484
- restore(key: string, targetKey: string | null | undefined): Promise<{ ok: true } | PortFailure>
485
- emptyRecycleBin(): Promise<void>
486
462
  originalParent(key: string): Promise<string | null | undefined>
487
463
  versions(key: string): Promise<DocumentVersionSummary[]>
488
464
  systemUserKey(): Promise<string | null>
package/src/ports.ts CHANGED
@@ -7,7 +7,7 @@
7
7
  * drifts from the wire shape fails to compile.
8
8
  */
9
9
 
10
- import type { DocumentOperations } from '@crombolo/content'
10
+ import type { DocumentOperations, MediaOperations } from '@crombolo/content'
11
11
  import type { ResponseOf } from '@crombolo/contracts'
12
12
  import type { UploadSettings } from '@crombolo/core'
13
13
  import type {
@@ -270,6 +270,8 @@ export interface ManagementApiDeps {
270
270
  dynamicRoots?: DynamicRootPort
271
271
  temporaryFiles?: TemporaryFilePort
272
272
  media?: MediaPort
273
+ /** Every media write, with its rules and its events (story 165); required alongside `media`. */
274
+ mediaOperations?: MediaOperations
273
275
  tags?: TagPort
274
276
  oembed?: OEmbedPort
275
277
  blueprints?: BlueprintPort
package/src/router.ts CHANGED
@@ -25,7 +25,7 @@ import {
25
25
  auditReadsBody,
26
26
  } from './audit.ts'
27
27
  import { authorize, LOCAL_LOGIN_OPERATIONS, type NodeLookup } from './authorization.ts'
28
- import { DOCUMENT_WRITE_OPERATIONS } from './document-writes.ts'
28
+ import { DOCUMENT_WRITE_OPERATIONS, MEDIA_WRITE_OPERATIONS } from './document-writes.ts'
29
29
  import {
30
30
  changeReadsBody,
31
31
  contentChangeFor,
@@ -251,8 +251,8 @@ export class ManagementApiRouter {
251
251
  if (!handler) this.#onNotImplemented?.(operation, request)
252
252
  // A document write announces itself from its operation (story 170).
253
253
  const announces = !DOCUMENT_WRITE_OPERATIONS.has(operation.operationId)
254
- const plan =
255
- handler && this.#audit && announces ? auditPlanFor(operation.operationId) : undefined
254
+ const audits = announces && !MEDIA_WRITE_OPERATIONS.has(operation.operationId)
255
+ const plan = handler && this.#audit && audits ? auditPlanFor(operation.operationId) : undefined
256
256
  const audit = plan ? await this.#beforeAudit(plan, params, request) : undefined
257
257
  const changeBody =
258
258
  handler && this.#onContentChange && changeReadsBody(operation.operationId)
package/src/routes.ts CHANGED
@@ -100,7 +100,8 @@ export function createManagementApiRouter(options: ManagementApiOptions = {}): M
100
100
  registerDocumentHandlers(router, deps.documents, deps.documentOperations, deps.preview)
101
101
  if (deps.blueprints) registerDocumentBlueprintHandlers(router, deps.blueprints)
102
102
  if (deps.temporaryFiles) registerTemporaryFileHandlers(router, deps.temporaryFiles)
103
- if (deps.media) registerMediaHandlers(router, deps.media)
103
+ if (deps.media && deps.mediaOperations)
104
+ registerMediaHandlers(router, deps.media, deps.mediaOperations)
104
105
  if (deps.dictionary) registerDictionaryHandlers(router, deps.dictionary)
105
106
  // The components tree is served through the PartialView file API: it is the
106
107
  // path-addressed one, so it is the only one that can express a folder.