@crombolo/core 0.24.1 → 0.26.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/api-paths.ts +59 -11
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crombolo/core",
3
- "version": "0.24.1",
3
+ "version": "0.26.0",
4
4
  "description": "Domain model, entity keys and shared contracts for crombolo. No I/O.",
5
5
  "license": "MIT",
6
6
  "author": "Crombolo",
package/src/api-paths.ts CHANGED
@@ -1,28 +1,76 @@
1
1
  /**
2
- * The paths the vendored client hard-codes.
2
+ * Where the APIs are served.
3
3
  *
4
- * Umbraco's `UmbracoPath` moves the editor, not its API: the Management API is
5
- * declared by the contract and the client's generated SDK calls it at
6
- * `/umbraco/management/api/v1/...` however the backoffice is mounted. The two
7
- * SignalR hubs are built the same way in the client's own source. So these stay
8
- * put while `backOfficePath` moves the shell, the login page, the static assets
9
- * and the SPA's own routes.
4
+ * The Management API and the back office's two live-update hubs live under the
5
+ * back office's own path (`backOfficePath`, `/crombolo` unless a site moves
6
+ * it), so the editor and everything it talks to move together. The Delivery
7
+ * API is public and serves apps, so it stays at one fixed path however the
8
+ * back office is mounted: a site that locks its back office down must not cut
9
+ * its apps off with it.
10
+ *
11
+ * Umbraco's own paths — the ones the vendored contract and client declare —
12
+ * keep answering as aliases, so an Umbraco client, an older command line or an
13
+ * older installed client still reach the same API. A request on the new paths
14
+ * is mapped onto these on the way in (`contractPathFor`).
10
15
  */
11
16
  export const MANAGEMENT_API_PREFIX = '/umbraco/management/api/'
12
17
 
13
18
  export const MANAGEMENT_API_PATH = `${MANAGEMENT_API_PREFIX}v1`
14
19
 
15
- /**
16
- * Umbraco's Delivery API, at the path its contract and every headless client
17
- * use, wherever the backoffice is mounted.
18
- */
20
+ /** The Management API under the back office: `<backOfficePath>/management/api/`. */
21
+ export const managementApiPrefix = (backOfficePath: string): string =>
22
+ `${backOfficePath}/management/api/`
23
+
24
+ /** `<backOfficePath>/management/api/v1`. */
25
+ export const managementApiPath = (backOfficePath: string): string =>
26
+ `${managementApiPrefix(backOfficePath)}v1`
27
+
28
+ /** Umbraco's Delivery API at the path its contract and Umbraco's headless clients use. */
19
29
  export const DELIVERY_API_PREFIX = '/umbraco/delivery/api'
20
30
 
31
+ /** The Delivery API at crombolo's own fixed path, wherever the back office is. */
32
+ export const FIXED_DELIVERY_API_PREFIX = '/crombolo/delivery/api'
33
+
21
34
  /** `server-event.context.js`: `${serverURL}/umbraco/serverEventHub`. */
22
35
  export const SERVER_EVENT_HUB_PATH = '/umbraco/serverEventHub'
23
36
 
24
37
  /** `preview.context.js`: `${serverUrl}/umbraco/PreviewHub`. */
25
38
  export const PREVIEW_HUB_PATH = '/umbraco/PreviewHub'
26
39
 
40
+ export const serverEventHubPath = (backOfficePath: string): string =>
41
+ `${backOfficePath}/serverEventHub`
42
+
43
+ export const previewHubPath = (backOfficePath: string): string => `${backOfficePath}/PreviewHub`
44
+
27
45
  /** Where the editor is mounted unless a site overrides `backOfficePath`. */
28
46
  export const DEFAULT_BACKOFFICE_PATH = '/crombolo'
47
+
48
+ /**
49
+ * The contract's path for a request on one of the new paths, or undefined when
50
+ * it is not one. The rest of the path and the query are kept.
51
+ */
52
+ export function contractPathFor(backOfficePath: string, pathname: string): string | undefined {
53
+ const management = managementApiPrefix(backOfficePath)
54
+ if (pathname.startsWith(management))
55
+ return `${MANAGEMENT_API_PREFIX}${pathname.slice(management.length)}`
56
+ if (
57
+ pathname === FIXED_DELIVERY_API_PREFIX ||
58
+ pathname.startsWith(`${FIXED_DELIVERY_API_PREFIX}/`)
59
+ )
60
+ return `${DELIVERY_API_PREFIX}${pathname.slice(FIXED_DELIVERY_API_PREFIX.length)}`
61
+ if (pathname === serverEventHubPath(backOfficePath)) return SERVER_EVENT_HUB_PATH
62
+ if (pathname === previewHubPath(backOfficePath)) return PREVIEW_HUB_PATH
63
+ return undefined
64
+ }
65
+
66
+ /**
67
+ * The vendored client's own requests, moved to the paths above (a pure text
68
+ * rewrite of its modules as they are served). The contract's prefixes appear in
69
+ * the client only as request addresses.
70
+ */
71
+ export function rehomeClientModule(source: string, backOfficePath: string): string {
72
+ return source
73
+ .replaceAll(MANAGEMENT_API_PREFIX, managementApiPrefix(backOfficePath))
74
+ .replaceAll(SERVER_EVENT_HUB_PATH, serverEventHubPath(backOfficePath))
75
+ .replaceAll(PREVIEW_HUB_PATH, previewHubPath(backOfficePath))
76
+ }