@crombolo/api-delivery 0.39.0 → 0.40.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 +4 -4
- package/src/handler.ts +68 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crombolo/api-delivery",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.40.0",
|
|
4
4
|
"description": "Internal to crombolo, with no compatibility promise: use @crombolo/crombolo. Umbraco's Content Delivery API for crombolo: published content and media as JSON, served from the published cache.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Crombolo",
|
|
@@ -28,8 +28,8 @@
|
|
|
28
28
|
"access": "public"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@crombolo/core": "0.
|
|
32
|
-
"@crombolo/contracts": "0.
|
|
33
|
-
"@crombolo/render": "0.
|
|
31
|
+
"@crombolo/core": "0.40.0",
|
|
32
|
+
"@crombolo/contracts": "0.40.0",
|
|
33
|
+
"@crombolo/render": "0.40.0"
|
|
34
34
|
}
|
|
35
35
|
}
|
package/src/handler.ts
CHANGED
|
@@ -163,7 +163,10 @@ export interface DeliveryApi {
|
|
|
163
163
|
* browser reaches by navigating (sign-in and sign-out) or that is not the API's. Pure.
|
|
164
164
|
*/
|
|
165
165
|
export function corsMethodsFor(rest: string): string | undefined {
|
|
166
|
-
if (
|
|
166
|
+
if (
|
|
167
|
+
/^v2\/(content|media|search|route|sitemap|forms|menus|dictionary|settings)(\/.*)?$/.test(rest)
|
|
168
|
+
)
|
|
169
|
+
return 'GET, HEAD'
|
|
167
170
|
const security = /^v1\/security\/member\/([^/]+)\/?$/.exec(rest)?.[1]
|
|
168
171
|
if (security === 'token' || security === 'revoke') return 'POST'
|
|
169
172
|
if (security === 'userinfo') return 'GET, POST'
|
|
@@ -173,6 +176,8 @@ export function corsMethodsFor(rest: string): string | undefined {
|
|
|
173
176
|
const GUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
|
|
174
177
|
const ROOT_KEY = '00000000-0000-0000-0000-000000000000'
|
|
175
178
|
const PREVIEW_PATH = /^preview-([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})$/i
|
|
179
|
+
/** The purge tag every dictionary answer carries: any change to it changes them all. */
|
|
180
|
+
export const DICTIONARY_TAG = 'crombolo-dictionary'
|
|
176
181
|
const CONTENT_VARY = 'Accept-Language, Accept-Segment, Preview, Start-Item'
|
|
177
182
|
|
|
178
183
|
/** 401, 403 and 404 carry no body in Umbraco's Delivery API. */
|
|
@@ -375,7 +380,9 @@ export function createDeliveryApi(ports: DeliveryApiPorts): DeliveryApi {
|
|
|
375
380
|
const ancestors = view.ancestors(here)
|
|
376
381
|
const root = ancestors[ancestors.length - 1] ?? here
|
|
377
382
|
let path: string
|
|
378
|
-
|
|
383
|
+
// A site's settings page has no address (story 058): `#`, as its URL is.
|
|
384
|
+
if (view.isSettings?.(here)) path = '#'
|
|
385
|
+
else if (ancestors.length === 0)
|
|
379
386
|
path = hide && first && here.id !== first.id ? `/${here.urlSegment}` : '/'
|
|
380
387
|
else {
|
|
381
388
|
const below = [...ancestors.slice(0, -1).reverse(), here].map((c) => c.urlSegment)
|
|
@@ -786,6 +793,28 @@ export function createDeliveryApi(ports: DeliveryApiPorts): DeliveryApi {
|
|
|
786
793
|
)
|
|
787
794
|
}
|
|
788
795
|
|
|
796
|
+
// Story 058: the dictionary as a map, and the site's settings page.
|
|
797
|
+
const siteWide = /^v2\/(dictionary|settings)\/?$/.exec(rest)?.[1]
|
|
798
|
+
if (siteWide) {
|
|
799
|
+
if (request.method !== 'GET' && request.method !== 'HEAD') return bare(405)
|
|
800
|
+
const preview = isPreview(request) ? await previewAccess(request) : undefined
|
|
801
|
+
if (preview === null) return bare(401)
|
|
802
|
+
if (!preview && !contentAccess(settings, request)) return bare(401)
|
|
803
|
+
const response =
|
|
804
|
+
siteWide === 'dictionary'
|
|
805
|
+
? await dictionaryAnswer(request, url, preview)
|
|
806
|
+
: await settingsAnswer(request, url, preview)
|
|
807
|
+
if (preview) {
|
|
808
|
+
response.headers.set('cache-control', 'no-store')
|
|
809
|
+
return response
|
|
810
|
+
}
|
|
811
|
+
return finish(
|
|
812
|
+
request,
|
|
813
|
+
response,
|
|
814
|
+
personal(request, settings.publicAccess, memberAuthorizationEnabled(settings)),
|
|
815
|
+
)
|
|
816
|
+
}
|
|
817
|
+
|
|
789
818
|
const formMatch = /^v2\/forms\/([^/]+)\/?$/.exec(rest)
|
|
790
819
|
if (formMatch) {
|
|
791
820
|
if (request.method !== 'GET' && request.method !== 'HEAD') return bare(405)
|
|
@@ -917,6 +946,43 @@ export function createDeliveryApi(ports: DeliveryApiPorts): DeliveryApi {
|
|
|
917
946
|
)
|
|
918
947
|
}
|
|
919
948
|
|
|
949
|
+
/**
|
|
950
|
+
* `v2/dictionary` (story 058): every item in the request's culture, falling
|
|
951
|
+
* back as `dictionary()` does, `''` where none does; `?keys=` narrows it.
|
|
952
|
+
*/
|
|
953
|
+
async function dictionaryAnswer(
|
|
954
|
+
request: Request,
|
|
955
|
+
url: URL,
|
|
956
|
+
preview: PreviewAccess | undefined,
|
|
957
|
+
): Promise<Response> {
|
|
958
|
+
const r = await contentRequest(request, preview)
|
|
959
|
+
if (!r.known) return bare(404)
|
|
960
|
+
const keys = url.searchParams
|
|
961
|
+
.get('keys')
|
|
962
|
+
?.split(',')
|
|
963
|
+
.map((key) => key.trim())
|
|
964
|
+
.filter(Boolean)
|
|
965
|
+
const items = await r.source.dictionaryEntries(r.culture, keys)
|
|
966
|
+
return holding(json(items), [], [DICTIONARY_TAG])
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
/**
|
|
970
|
+
* `v2/settings` (story 058): the settings page under the request's root, its
|
|
971
|
+
* `Start-Item`, else the root its host serves, else the first root.
|
|
972
|
+
*/
|
|
973
|
+
async function settingsAnswer(
|
|
974
|
+
request: Request,
|
|
975
|
+
url: URL,
|
|
976
|
+
preview: PreviewAccess | undefined,
|
|
977
|
+
): Promise<Response> {
|
|
978
|
+
const r = await contentRequest(request, preview)
|
|
979
|
+
if (r.badStartItem) return Response.json('The Start-Item could not be found', { status: 404 })
|
|
980
|
+
const rootKey =
|
|
981
|
+
r.startItem?.key ?? (await r.source.rootForHost(url.host)) ?? r.roots[0]?.key ?? null
|
|
982
|
+
const found = r.known ? await r.source.settings(rootKey, r.culture) : undefined
|
|
983
|
+
return contentItem(r, found, url)
|
|
984
|
+
}
|
|
985
|
+
|
|
920
986
|
/** A redirect as data. Never kept, as the site's are not: renaming back must not strand anyone. */
|
|
921
987
|
/**
|
|
922
988
|
* `v2/menus/{alias}` (story 141): a menu's items in the request's culture. A
|