@mailwoman/ogc 0.0.0-stage → 11.3.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/README.md +288 -2
- package/cli/geocoder.ts +50 -0
- package/cli/main.ts +407 -0
- package/lib/app.ts +107 -0
- package/lib/bbox.ts +69 -0
- package/lib/collections.ts +213 -0
- package/lib/engine.ts +143 -0
- package/lib/format.ts +46 -0
- package/lib/html.ts +298 -0
- package/lib/index.ts +24 -0
- package/lib/items.ts +169 -0
- package/lib/projection.ts +226 -0
- package/lib/provenance.ts +86 -0
- package/lib/refusal.ts +34 -0
- package/lib/routes.ts +852 -0
- package/lib/schema.ts +457 -0
- package/out/app.d.ts +41 -0
- package/out/app.d.ts.map +1 -0
- package/out/app.js +67 -0
- package/out/app.js.map +1 -0
- package/out/bbox.d.ts +23 -0
- package/out/bbox.d.ts.map +1 -0
- package/out/bbox.js +54 -0
- package/out/bbox.js.map +1 -0
- package/out/cli/geocoder.d.ts +24 -0
- package/out/cli/geocoder.d.ts.map +1 -0
- package/out/cli/geocoder.js +28 -0
- package/out/cli/geocoder.js.map +1 -0
- package/out/cli/main.d.ts +3 -0
- package/out/cli/main.d.ts.map +1 -0
- package/out/cli/main.js +297 -0
- package/out/cli/main.js.map +1 -0
- package/out/collections.d.ts +99 -0
- package/out/collections.d.ts.map +1 -0
- package/out/collections.js +132 -0
- package/out/collections.js.map +1 -0
- package/out/engine.d.ts +133 -0
- package/out/engine.d.ts.map +1 -0
- package/out/engine.js +4 -0
- package/out/engine.js.map +1 -0
- package/out/format.d.ts +18 -0
- package/out/format.d.ts.map +1 -0
- package/out/format.js +31 -0
- package/out/format.js.map +1 -0
- package/out/html.d.ts +52 -0
- package/out/html.d.ts.map +1 -0
- package/out/html.js +200 -0
- package/out/html.js.map +1 -0
- package/out/index.d.ts +21 -0
- package/out/index.d.ts.map +1 -0
- package/out/index.js +23 -0
- package/out/index.js.map +1 -0
- package/out/items.d.ts +38 -0
- package/out/items.d.ts.map +1 -0
- package/out/items.js +136 -0
- package/out/items.js.map +1 -0
- package/out/projection.d.ts +40 -0
- package/out/projection.d.ts.map +1 -0
- package/out/projection.js +181 -0
- package/out/projection.js.map +1 -0
- package/out/provenance.d.ts +41 -0
- package/out/provenance.d.ts.map +1 -0
- package/out/provenance.js +60 -0
- package/out/provenance.js.map +1 -0
- package/out/refusal.d.ts +18 -0
- package/out/refusal.d.ts.map +1 -0
- package/out/refusal.js +30 -0
- package/out/refusal.js.map +1 -0
- package/out/routes.d.ts +95 -0
- package/out/routes.d.ts.map +1 -0
- package/out/routes.js +624 -0
- package/out/routes.js.map +1 -0
- package/out/schema.d.ts +422 -0
- package/out/schema.d.ts.map +1 -0
- package/out/schema.js +378 -0
- package/out/schema.js.map +1 -0
- package/out/tools/ets.run.d.ts +2 -0
- package/out/tools/ets.run.d.ts.map +1 -0
- package/out/tools/ets.run.js +187 -0
- package/out/tools/ets.run.js.map +1 -0
- package/package.json +120 -3
- package/tools/ets.run.ts +258 -0
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The collections the OGC surface can describe, and the engine output each one holds.
|
|
6
|
+
*
|
|
7
|
+
* A collection groups the answers one kind of serving layer produces.
|
|
8
|
+
* The geocoder reports which tier placed a coordinate, so the tier decides the collection.
|
|
9
|
+
*
|
|
10
|
+
* The running engine lists only the collections whose layers it opened,
|
|
11
|
+
* and `/collections` serves that list.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import type { GeocodeResult } from "@mailwoman/core/geocode"
|
|
15
|
+
import type { RegionDatabaseProvider, RegionLayerDatabase } from "@mailwoman/core/resolver"
|
|
16
|
+
|
|
17
|
+
import type { OGCServedCollection } from "#engine"
|
|
18
|
+
|
|
19
|
+
type ResolutionTier = GeocodeResult["resolution_tier"]
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The table maps each collection's name to its id.
|
|
23
|
+
*/
|
|
24
|
+
export const OGCCollectionID = {
|
|
25
|
+
Addresses: "addresses",
|
|
26
|
+
Streets: "streets",
|
|
27
|
+
Postcodes: "postcodes",
|
|
28
|
+
Places: "places",
|
|
29
|
+
PlusCodes: "plus-codes",
|
|
30
|
+
} as const
|
|
31
|
+
|
|
32
|
+
export type OGCCollectionID = (typeof OGCCollectionID)[keyof typeof OGCCollectionID]
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The fixed title and description of one collection.
|
|
36
|
+
*/
|
|
37
|
+
export interface OGCCollectionEntry {
|
|
38
|
+
title: string
|
|
39
|
+
description: string
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The title and description of every collection.
|
|
44
|
+
*/
|
|
45
|
+
export const OGC_COLLECTION_CATALOG: Readonly<Record<OGCCollectionID, OGCCollectionEntry>> = {
|
|
46
|
+
[OGCCollectionID.Addresses]: {
|
|
47
|
+
title: "Addresses",
|
|
48
|
+
description:
|
|
49
|
+
"House-grade coordinates: a matched address point, or a point interpolated along a street's address range.",
|
|
50
|
+
},
|
|
51
|
+
[OGCCollectionID.Streets]: {
|
|
52
|
+
title: "Streets",
|
|
53
|
+
description: "Street centroids, answering a street named without a house number.",
|
|
54
|
+
},
|
|
55
|
+
[OGCCollectionID.Postcodes]: {
|
|
56
|
+
title: "Postcodes",
|
|
57
|
+
description: "Postcode places from the gazetteer, located at the postcode's own coordinate.",
|
|
58
|
+
},
|
|
59
|
+
[OGCCollectionID.Places]: {
|
|
60
|
+
title: "Places",
|
|
61
|
+
description: "Gazetteer places: localities, districts, counties, regions and countries.",
|
|
62
|
+
},
|
|
63
|
+
[OGCCollectionID.PlusCodes]: {
|
|
64
|
+
title: "Plus codes",
|
|
65
|
+
description: "Open Location Codes the engine decodes to the center of their area. No data layer backs it.",
|
|
66
|
+
},
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The collection that holds an answer the geocoder placed at `tier`.
|
|
71
|
+
*
|
|
72
|
+
* @returns Null for a tier no collection holds, such as `venue`, whose layer the OGC engine doesn't open.
|
|
73
|
+
*/
|
|
74
|
+
export function collectionOfTier(tier: ResolutionTier): OGCCollectionID | null {
|
|
75
|
+
switch (tier) {
|
|
76
|
+
case "address_point":
|
|
77
|
+
case "interpolated":
|
|
78
|
+
return OGCCollectionID.Addresses
|
|
79
|
+
case "street":
|
|
80
|
+
return OGCCollectionID.Streets
|
|
81
|
+
case "postcode":
|
|
82
|
+
return OGCCollectionID.Postcodes
|
|
83
|
+
case "admin":
|
|
84
|
+
return OGCCollectionID.Places
|
|
85
|
+
case "plus_code":
|
|
86
|
+
return OGCCollectionID.PlusCodes
|
|
87
|
+
case "venue":
|
|
88
|
+
return null
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The collection that holds a gazetteer place of `placetype`.
|
|
94
|
+
*/
|
|
95
|
+
export function collectionOfPlacetype(placetype: string): OGCCollectionID {
|
|
96
|
+
return placetype === "postalcode" ? OGCCollectionID.Postcodes : OGCCollectionID.Places
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* One region-keyed provider of street-tier layers, and the keys to ask it for.
|
|
101
|
+
*/
|
|
102
|
+
export interface RegionLayerProbe {
|
|
103
|
+
provider: Pick<RegionDatabaseProvider, "for" | "layerDatabases">
|
|
104
|
+
keys: Iterable<string>
|
|
105
|
+
/**
|
|
106
|
+
* The region code a key names in a collection description, such as `US-TX` for the key `tx`.
|
|
107
|
+
*/
|
|
108
|
+
regionOf: (key: string) => string
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* One database file a region's layer reads, and the collection it serves.
|
|
113
|
+
*/
|
|
114
|
+
export interface RegionLayerFile extends RegionLayerDatabase {
|
|
115
|
+
collection: OGCCollectionID
|
|
116
|
+
/**
|
|
117
|
+
* The region code, such as `US-TX`.
|
|
118
|
+
*/
|
|
119
|
+
region: string
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* The collection each street-tier database serves.
|
|
124
|
+
*/
|
|
125
|
+
const COLLECTION_OF_TIER: Readonly<Record<RegionLayerDatabase["tier"], OGCCollectionID>> = {
|
|
126
|
+
addressPoints: OGCCollectionID.Addresses,
|
|
127
|
+
interpolation: OGCCollectionID.Addresses,
|
|
128
|
+
streetCentroids: OGCCollectionID.Streets,
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* The database files every probed region reads, from providers that report them.
|
|
133
|
+
*
|
|
134
|
+
* A provider without `layerDatabases` reports none, and its regions count as unread.
|
|
135
|
+
*/
|
|
136
|
+
export function regionLayerFiles(probes: readonly RegionLayerProbe[]): RegionLayerFile[] {
|
|
137
|
+
const files: RegionLayerFile[] = []
|
|
138
|
+
|
|
139
|
+
for (const { provider, keys, regionOf } of probes) {
|
|
140
|
+
for (const key of keys) {
|
|
141
|
+
for (const database of provider.layerDatabases?.(key) ?? []) {
|
|
142
|
+
files.push({ ...database, collection: COLLECTION_OF_TIER[database.tier], region: regionOf(key) })
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return files
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* What the opened gazetteer holds, for the collections it backs.
|
|
152
|
+
*/
|
|
153
|
+
export interface GazetteerHoldings {
|
|
154
|
+
/**
|
|
155
|
+
* Whether the gazetteer holds at least one postcode place.
|
|
156
|
+
*/
|
|
157
|
+
postcodes: boolean
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* The collections an engine serves, from the layers its providers opened.
|
|
162
|
+
*
|
|
163
|
+
* The gazetteer is always open, because the engine refuses to start without one,
|
|
164
|
+
* so places and plus codes are always served.
|
|
165
|
+
* Postcodes are served when the gazetteer holds postcode places.
|
|
166
|
+
*
|
|
167
|
+
* Addresses and streets are served when some region has an address-point, interpolation
|
|
168
|
+
* or street-centroid layer, and their descriptions name those regions.
|
|
169
|
+
*
|
|
170
|
+
* A collection no region has a layer for is left out of the list.
|
|
171
|
+
*
|
|
172
|
+
* A provider opens a region's layers when asked for the region.
|
|
173
|
+
* A query for that region would open them as well.
|
|
174
|
+
*/
|
|
175
|
+
export function servedCollections(
|
|
176
|
+
probes: readonly RegionLayerProbe[],
|
|
177
|
+
gazetteer: GazetteerHoldings
|
|
178
|
+
): OGCServedCollection[] {
|
|
179
|
+
const addressRegions = new Set<string>()
|
|
180
|
+
const streetRegions = new Set<string>()
|
|
181
|
+
|
|
182
|
+
for (const { provider, keys, regionOf } of probes) {
|
|
183
|
+
for (const key of keys) {
|
|
184
|
+
const databases = provider.for(key)
|
|
185
|
+
|
|
186
|
+
if (databases.addressPoints || databases.interpolation) {
|
|
187
|
+
addressRegions.add(regionOf(key))
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (databases.streetCentroids) {
|
|
191
|
+
streetRegions.add(regionOf(key))
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const served: OGCServedCollection[] = []
|
|
197
|
+
|
|
198
|
+
if (addressRegions.size) {
|
|
199
|
+
served.push({ id: OGCCollectionID.Addresses, regions: [...addressRegions].toSorted() })
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (streetRegions.size) {
|
|
203
|
+
served.push({ id: OGCCollectionID.Streets, regions: [...streetRegions].toSorted() })
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (gazetteer.postcodes) {
|
|
207
|
+
served.push({ id: OGCCollectionID.Postcodes, regions: null })
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
served.push({ id: OGCCollectionID.Places, regions: null }, { id: OGCCollectionID.PlusCodes, regions: null })
|
|
211
|
+
|
|
212
|
+
return served
|
|
213
|
+
}
|
package/lib/engine.ts
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The engine the OGC routes delegate to.
|
|
6
|
+
*
|
|
7
|
+
* The routes own validation, the collection and `bbox` filters, `limit` and the response envelope.
|
|
8
|
+
* The engine answers one query with every match it found, and lists the
|
|
9
|
+
* collections its opened layers serve.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import type { LayerTerms } from "@mailwoman/core/layers/provenance"
|
|
13
|
+
|
|
14
|
+
import type { OGCBBox } from "#bbox"
|
|
15
|
+
import type { OGCCollectionID } from "#collections"
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* One gazetteer place a collection serves as an item.
|
|
19
|
+
*/
|
|
20
|
+
export interface OGCItem {
|
|
21
|
+
/**
|
|
22
|
+
* The place's WOF id, the number in its place ID `wof:<id>`.
|
|
23
|
+
*/
|
|
24
|
+
wofID: number
|
|
25
|
+
name: string
|
|
26
|
+
placetype: string
|
|
27
|
+
/**
|
|
28
|
+
* The ISO 3166-1 alpha-2 code the gazetteer records, or null when it records none.
|
|
29
|
+
*/
|
|
30
|
+
countryCode: string | null
|
|
31
|
+
lon: number
|
|
32
|
+
lat: number
|
|
33
|
+
/**
|
|
34
|
+
* The label `/search` gives the same place: its name, ancestors and country.
|
|
35
|
+
*/
|
|
36
|
+
displayName: string
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* One page of a collection's items.
|
|
41
|
+
*/
|
|
42
|
+
export interface OGCItemPageRequest {
|
|
43
|
+
/**
|
|
44
|
+
* Keep only places whose point lies inside the box, or every place when null.
|
|
45
|
+
*/
|
|
46
|
+
bbox: OGCBBox | null
|
|
47
|
+
/**
|
|
48
|
+
* Start after the place with this WOF id, or at the first place when null.
|
|
49
|
+
*/
|
|
50
|
+
after: number | null
|
|
51
|
+
limit: number
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The reader behind a collection's `/items` routes.
|
|
56
|
+
*/
|
|
57
|
+
export interface OGCItemSource {
|
|
58
|
+
/**
|
|
59
|
+
* The places the request selects, in ascending WOF id order, at most `limit` of them.
|
|
60
|
+
*/
|
|
61
|
+
page(request: OGCItemPageRequest): Promise<OGCItem[]>
|
|
62
|
+
/**
|
|
63
|
+
* The place with this WOF id, or null when the collection holds no such place.
|
|
64
|
+
*/
|
|
65
|
+
item(wofID: number): Promise<OGCItem | null>
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* A collection the engine serves, and the regions its layers cover.
|
|
70
|
+
*/
|
|
71
|
+
export interface OGCServedCollection {
|
|
72
|
+
id: OGCCollectionID
|
|
73
|
+
/**
|
|
74
|
+
* The regions that have a layer the engine opened for this collection, such as `FR` or `US-TX`.
|
|
75
|
+
*
|
|
76
|
+
* Null when the collection isn't divided by region, as for the worldwide gazetteer.
|
|
77
|
+
*/
|
|
78
|
+
regions: readonly string[] | null
|
|
79
|
+
/**
|
|
80
|
+
* The reader that serves the collection's items, absent for a collection without `/items`.
|
|
81
|
+
*/
|
|
82
|
+
items?: OGCItemSource
|
|
83
|
+
/**
|
|
84
|
+
* The manifests of the layers behind the collection.
|
|
85
|
+
* Its license and source links come from them.
|
|
86
|
+
*
|
|
87
|
+
* An empty array means no layer backs the collection.
|
|
88
|
+
* Absent means the engine read no manifest for it, and the description states that.
|
|
89
|
+
*/
|
|
90
|
+
layers?: readonly LayerTerms[]
|
|
91
|
+
/**
|
|
92
|
+
* The regions with a layer database whose `layer_manifest` the engine couldn't read, absent or unreadable.
|
|
93
|
+
*
|
|
94
|
+
* Such a layer contributes no link, and the description states the count.
|
|
95
|
+
*/
|
|
96
|
+
unreadRegions?: number
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* One match for a `/search` query.
|
|
101
|
+
*/
|
|
102
|
+
export interface OGCSearchHit {
|
|
103
|
+
collectionID: OGCCollectionID
|
|
104
|
+
lon: number
|
|
105
|
+
lat: number
|
|
106
|
+
displayName: string
|
|
107
|
+
/**
|
|
108
|
+
* The engine's ranking score for the match, or null when the engine measured none.
|
|
109
|
+
*/
|
|
110
|
+
score: number | null
|
|
111
|
+
/**
|
|
112
|
+
* Why `score` is null, absent when it holds a value.
|
|
113
|
+
*/
|
|
114
|
+
scoreUnmeasured?: string
|
|
115
|
+
/**
|
|
116
|
+
* The gazetteer place ID of a place match, such as `wof:85975673`.
|
|
117
|
+
*
|
|
118
|
+
* Absent for an address, a street or a plus code, and for a place the gazetteer gave no ID.
|
|
119
|
+
*/
|
|
120
|
+
placeID?: string
|
|
121
|
+
/**
|
|
122
|
+
* The gazetteer placetype of a place match, such as `locality` or `postalcode`.
|
|
123
|
+
*
|
|
124
|
+
* Absent for an address, a street or a plus code.
|
|
125
|
+
*/
|
|
126
|
+
placetype?: string
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* The engine behind the OGC routes.
|
|
131
|
+
*/
|
|
132
|
+
export interface OGCEngine {
|
|
133
|
+
/**
|
|
134
|
+
* The collections the engine serves, in the order `/collections` lists them.
|
|
135
|
+
*/
|
|
136
|
+
readonly collections: readonly OGCServedCollection[]
|
|
137
|
+
/**
|
|
138
|
+
* Every match for `q`, best first.
|
|
139
|
+
*
|
|
140
|
+
* An empty array means the engine found no match.
|
|
141
|
+
*/
|
|
142
|
+
search(q: string): Promise<OGCSearchHit[]>
|
|
143
|
+
}
|
package/lib/format.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Response encoding negotiation for the OGC API surface.
|
|
6
|
+
*
|
|
7
|
+
* OGC API Common negotiates the encoding through HTTP content negotiation
|
|
8
|
+
* and admits a query parameter that overrides it.
|
|
9
|
+
* The `f` parameter decides when present.
|
|
10
|
+
*
|
|
11
|
+
* Otherwise the `Accept` header decides, and JSON answers a request that names neither encoding.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import type { Context } from "hono"
|
|
15
|
+
import { accepts } from "hono/accepts"
|
|
16
|
+
|
|
17
|
+
import { OGCFormat, OGCMediaType } from "#schema"
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The JSON media types a request may name in `Accept`, beside `text/html`.
|
|
21
|
+
*
|
|
22
|
+
* `/api` serves the OpenAPI media type and answers a request for plain JSON with it too,
|
|
23
|
+
* and `/search` serves GeoJSON the same way.
|
|
24
|
+
*/
|
|
25
|
+
const JSON_MEDIA_TYPES: readonly string[] = [
|
|
26
|
+
OGCMediaType.JSON,
|
|
27
|
+
OGCMediaType.GeoJSON,
|
|
28
|
+
"application/vnd.oai.openapi+json",
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The encoding a request asks for.
|
|
33
|
+
*
|
|
34
|
+
* @param f The validated `f` query parameter, absent when the request didn't send one.
|
|
35
|
+
*/
|
|
36
|
+
export function negotiateFormat(c: Context, f: OGCFormat | undefined): OGCFormat {
|
|
37
|
+
if (f) return f
|
|
38
|
+
|
|
39
|
+
const matched = accepts(c, {
|
|
40
|
+
header: "Accept",
|
|
41
|
+
supports: [...JSON_MEDIA_TYPES, OGCMediaType.HTML],
|
|
42
|
+
default: OGCMediaType.JSON,
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
return matched === OGCMediaType.HTML ? OGCFormat.HTML : OGCFormat.JSON
|
|
46
|
+
}
|
package/lib/html.ts
ADDED
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* HTML encodings of the OGC API resources.
|
|
6
|
+
*
|
|
7
|
+
* OGC API Common's HTML class asks that an HTML response include everything the
|
|
8
|
+
* JSON response includes and render every link as an `<a>` element.
|
|
9
|
+
* Each page here renders from the same value the JSON route serves, so the two encodings can't disagree.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import type { OpenAPIHono } from "@hono/zod-openapi"
|
|
13
|
+
import { escapeHTML } from "@mailwoman/core/strings/escape"
|
|
14
|
+
|
|
15
|
+
import type { OGCServedCollection } from "#engine"
|
|
16
|
+
import {
|
|
17
|
+
collectionVersionParameter,
|
|
18
|
+
type OGCCollection,
|
|
19
|
+
type OGCCollections,
|
|
20
|
+
OGC_COLLECTION_VERSION,
|
|
21
|
+
type OGCConformance,
|
|
22
|
+
type OGCItemFeature,
|
|
23
|
+
type OGCItemsResponse,
|
|
24
|
+
type OGCLandingPage,
|
|
25
|
+
type OGCLink,
|
|
26
|
+
type OGCSearchFeature,
|
|
27
|
+
type OGCSearchResponse,
|
|
28
|
+
} from "#schema"
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The OpenAPI 3.0 document the generator emits.
|
|
32
|
+
*/
|
|
33
|
+
export type OpenAPI30Document = ReturnType<OpenAPIHono["getOpenAPIDocument"]>
|
|
34
|
+
|
|
35
|
+
const STYLE = `:root { color-scheme: light dark }
|
|
36
|
+
body { font: 16px/1.6 system-ui, -apple-system, sans-serif; max-width: 48rem; margin: 3rem auto; padding: 0 1.25rem }
|
|
37
|
+
h1 { font-size: 1.3rem; margin: 0 0 .5rem }
|
|
38
|
+
h2 { font-size: 1.1rem; margin: 1.5rem 0 .5rem }
|
|
39
|
+
code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace }
|
|
40
|
+
ul { padding-left: 1.2rem }
|
|
41
|
+
li { margin: .3rem 0 }
|
|
42
|
+
a { color: #2563eb }
|
|
43
|
+
.meta { opacity: .75; font-size: .9rem }`
|
|
44
|
+
|
|
45
|
+
function page(title: string, body: string): string {
|
|
46
|
+
return `<!doctype html>
|
|
47
|
+
<html lang="en">
|
|
48
|
+
<head>
|
|
49
|
+
<meta charset="utf-8">
|
|
50
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
51
|
+
<title>${escapeHTML(title)}</title>
|
|
52
|
+
<style>
|
|
53
|
+
${STYLE}
|
|
54
|
+
</style>
|
|
55
|
+
</head>
|
|
56
|
+
<body>
|
|
57
|
+
${body}
|
|
58
|
+
</body>
|
|
59
|
+
</html>
|
|
60
|
+
`
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function linkItem(link: OGCLink): string {
|
|
64
|
+
const label = escapeHTML(link.title ?? link.href)
|
|
65
|
+
const meta = [link.rel, link.type].filter((part): part is string => typeof part === "string").map(escapeHTML)
|
|
66
|
+
|
|
67
|
+
return `<li><a href="${escapeHTML(link.href)}" rel="${escapeHTML(link.rel)}">${label}</a> <span class="meta">${meta.join(" · ")}</span></li>`
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function linkList(links: readonly OGCLink[]): string {
|
|
71
|
+
return `<ul>\n${links.map(linkItem).join("\n")}\n</ul>`
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The landing page as HTML.
|
|
76
|
+
*/
|
|
77
|
+
export function landingPageHTML(landing: OGCLandingPage): string {
|
|
78
|
+
return page(
|
|
79
|
+
landing.title,
|
|
80
|
+
`<h1>${escapeHTML(landing.title)}</h1>
|
|
81
|
+
<p>${escapeHTML(landing.description)}</p>
|
|
82
|
+
<h2>Links</h2>
|
|
83
|
+
${linkList(landing.links)}`
|
|
84
|
+
)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The conformance declaration as HTML.
|
|
89
|
+
*/
|
|
90
|
+
export function conformanceHTML(conformance: OGCConformance, links: readonly OGCLink[]): string {
|
|
91
|
+
const classes = conformance.conformsTo.map(
|
|
92
|
+
(uri) => `<li><a href="${escapeHTML(uri)}"><code>${escapeHTML(uri)}</code></a></li>`
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
return page(
|
|
96
|
+
"Conformance",
|
|
97
|
+
`<h1>Conformance</h1>
|
|
98
|
+
<p>The API conforms to these conformance classes.</p>
|
|
99
|
+
<ul>
|
|
100
|
+
${classes.join("\n")}
|
|
101
|
+
</ul>
|
|
102
|
+
<h2>Links</h2>
|
|
103
|
+
${linkList(links)}`
|
|
104
|
+
)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function collectionSection(collection: OGCCollection, heading: "h1" | "h2"): string {
|
|
108
|
+
return `<${heading}>${escapeHTML(collection.title)} <code>${escapeHTML(collection.id)}</code></${heading}>
|
|
109
|
+
<p>${escapeHTML(collection.description)}</p>
|
|
110
|
+
<p class="meta">Point features, version ${collection.version}</p>
|
|
111
|
+
${linkList(collection.links)}`
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* The collections list as HTML.
|
|
116
|
+
*/
|
|
117
|
+
export function collectionsHTML(body: OGCCollections): string {
|
|
118
|
+
return page(
|
|
119
|
+
"Collections",
|
|
120
|
+
`<h1>Collections</h1>
|
|
121
|
+
${linkList(body.links)}
|
|
122
|
+
${body.collections.map((collection) => collectionSection(collection, "h2")).join("\n")}`
|
|
123
|
+
)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* One collection description as HTML.
|
|
128
|
+
*/
|
|
129
|
+
export function collectionHTML(collection: OGCCollection): string {
|
|
130
|
+
return page(collection.title, collectionSection(collection, "h1"))
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function searchFeatureItem(feature: OGCSearchFeature): string {
|
|
134
|
+
const [lon, lat] = feature.geometry.coordinates
|
|
135
|
+
const { display_name, score, score_unmeasured, collection_id, place_id, placetype, href } = feature.properties
|
|
136
|
+
const scoreText = score == null ? `score null (${score_unmeasured ?? "unmeasured"})` : `score ${score}`
|
|
137
|
+
const identity = [placetype, place_id].filter((part): part is string => typeof part === "string")
|
|
138
|
+
|
|
139
|
+
const meta = [collection_id, ...identity, `${lat}, ${lon}`, scoreText].map(escapeHTML).join(" · ")
|
|
140
|
+
const label = href ? `<a href="${escapeHTML(href)}">${escapeHTML(display_name)}</a>` : escapeHTML(display_name)
|
|
141
|
+
|
|
142
|
+
return `<li>${label} <span class="meta">${meta}</span></li>`
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* The properties of one item as a definition list, the same members its GeoJSON form holds.
|
|
147
|
+
*/
|
|
148
|
+
function itemDetails(feature: OGCItemFeature): string {
|
|
149
|
+
const [lon, lat] = feature.geometry.coordinates
|
|
150
|
+
const { name, placetype, country_code, display_name } = feature.properties
|
|
151
|
+
|
|
152
|
+
const rows: Array<[string, string]> = [
|
|
153
|
+
["id", feature.id],
|
|
154
|
+
["name", name],
|
|
155
|
+
["placetype", placetype],
|
|
156
|
+
["country_code", country_code ?? "null"],
|
|
157
|
+
["display_name", display_name],
|
|
158
|
+
["coordinates", `${lon}, ${lat}`],
|
|
159
|
+
]
|
|
160
|
+
|
|
161
|
+
return `<dl>\n${rows.map(([term, value]) => `<dt><code>${term}</code></dt><dd>${escapeHTML(value)}</dd>`).join("\n")}\n</dl>`
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* One page of a collection's items as HTML: each item linked to its own page, then the page's links.
|
|
166
|
+
*
|
|
167
|
+
* @param collectionID The collection the items belong to, for each item's link.
|
|
168
|
+
*/
|
|
169
|
+
export function itemsHTML(body: OGCItemsResponse, title: string, collectionID: string): string {
|
|
170
|
+
const items = body.features.map((feature) => {
|
|
171
|
+
const [lon, lat] = feature.geometry.coordinates
|
|
172
|
+
const href = `${encodeURIComponent(collectionID)}/items/${encodeURIComponent(feature.id)}?f=html`
|
|
173
|
+
const meta = [feature.id, feature.properties.placetype, `${lat}, ${lon}`].map(escapeHTML).join(" · ")
|
|
174
|
+
|
|
175
|
+
return `<li><a href="../${escapeHTML(href)}">${escapeHTML(feature.properties.display_name)}</a> <span class="meta">${meta}</span></li>`
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
return page(
|
|
179
|
+
`${title} items`,
|
|
180
|
+
`<h1>${escapeHTML(title)} items</h1>
|
|
181
|
+
<p class="meta">${body.numberReturned} returned at ${escapeHTML(body.timeStamp)}</p>
|
|
182
|
+
<ol>
|
|
183
|
+
${items.join("\n")}
|
|
184
|
+
</ol>
|
|
185
|
+
<h2>Links</h2>
|
|
186
|
+
${linkList(body.links)}`
|
|
187
|
+
)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* One item as HTML.
|
|
192
|
+
*/
|
|
193
|
+
export function itemHTML(feature: OGCItemFeature, collectionTitle: string): string {
|
|
194
|
+
return page(
|
|
195
|
+
feature.properties.name,
|
|
196
|
+
`<h1>${escapeHTML(feature.properties.name)}</h1>
|
|
197
|
+
<p class="meta">An item of ${escapeHTML(collectionTitle)}</p>
|
|
198
|
+
${itemDetails(feature)}
|
|
199
|
+
<h2>Links</h2>
|
|
200
|
+
${linkList(feature.links ?? [])}`
|
|
201
|
+
)
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* The search form, with the results of a search when `body` holds one.
|
|
206
|
+
*
|
|
207
|
+
* @param collections The collections a checkbox can restrict the search to.
|
|
208
|
+
*/
|
|
209
|
+
export function searchHTML(
|
|
210
|
+
body: OGCSearchResponse | null,
|
|
211
|
+
links: readonly OGCLink[],
|
|
212
|
+
collections: readonly OGCServedCollection[],
|
|
213
|
+
q = ""
|
|
214
|
+
): string {
|
|
215
|
+
const boxes = collections.map(
|
|
216
|
+
({ id }) =>
|
|
217
|
+
`<label><input type="checkbox" name="${escapeHTML(collectionVersionParameter(id))}" value="${OGC_COLLECTION_VERSION}"> ${escapeHTML(id)}</label>`
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
const results = body
|
|
221
|
+
? `<h2>Results</h2>
|
|
222
|
+
<p class="meta">${body.numberReturned} returned at ${escapeHTML(body.timeStamp)}</p>
|
|
223
|
+
<ol>
|
|
224
|
+
${body.features.map(searchFeatureItem).join("\n")}
|
|
225
|
+
</ol>`
|
|
226
|
+
: ""
|
|
227
|
+
|
|
228
|
+
return page(
|
|
229
|
+
"Search",
|
|
230
|
+
`<h1>Search</h1>
|
|
231
|
+
<p>Forward geocoding. Leave every collection unchecked to search them all.</p>
|
|
232
|
+
<form method="get" action="search">
|
|
233
|
+
<input type="hidden" name="f" value="html">
|
|
234
|
+
<p><input type="search" name="q" value="${escapeHTML(q)}" required size="48" aria-label="Address or place"> <button type="submit">Search</button></p>
|
|
235
|
+
<p>${boxes.join(" ")}</p>
|
|
236
|
+
</form>
|
|
237
|
+
${results}
|
|
238
|
+
<h2>Links</h2>
|
|
239
|
+
${linkList(links)}`
|
|
240
|
+
)
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
interface OperationLike {
|
|
244
|
+
summary?: string
|
|
245
|
+
description?: string
|
|
246
|
+
parameters?: Array<{ name?: string; in?: string; description?: string; required?: boolean }>
|
|
247
|
+
responses?: Record<string, { description?: string; content?: Record<string, unknown> }>
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function operationHTML(method: string, path: string, operation: OperationLike): string {
|
|
251
|
+
const parameters = (operation.parameters ?? []).map((parameter) => {
|
|
252
|
+
const required = parameter.required ? " (required)" : ""
|
|
253
|
+
const description = parameter.description ? `: ${escapeHTML(parameter.description)}` : ""
|
|
254
|
+
|
|
255
|
+
return `<li><code>${escapeHTML(parameter.name ?? "")}</code> in ${escapeHTML(parameter.in ?? "")}${required}${description}</li>`
|
|
256
|
+
})
|
|
257
|
+
|
|
258
|
+
const responses = Object.entries(operation.responses ?? {}).map(([status, response]) => {
|
|
259
|
+
const mediaTypes = Object.keys(response.content ?? {}).map((type) => `<code>${escapeHTML(type)}</code>`)
|
|
260
|
+
|
|
261
|
+
return `<li><code>${escapeHTML(status)}</code> ${escapeHTML(response.description ?? "")} ${mediaTypes.join(", ")}</li>`
|
|
262
|
+
})
|
|
263
|
+
|
|
264
|
+
return `<h2><code>${escapeHTML(method.toUpperCase())} ${escapeHTML(path)}</code></h2>
|
|
265
|
+
<p>${escapeHTML(operation.summary ?? "")}</p>
|
|
266
|
+
${operation.description ? `<p>${escapeHTML(operation.description)}</p>` : ""}
|
|
267
|
+
<p>Parameters:</p>
|
|
268
|
+
<ul>
|
|
269
|
+
${parameters.join("\n")}
|
|
270
|
+
</ul>
|
|
271
|
+
<p>Responses:</p>
|
|
272
|
+
<ul>
|
|
273
|
+
${responses.join("\n")}
|
|
274
|
+
</ul>`
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* The API definition as HTML: each operation with its parameters and responses,
|
|
279
|
+
* read from the OpenAPI 3.0 document the JSON encoding serves.
|
|
280
|
+
*/
|
|
281
|
+
export function apiDefinitionHTML(document: OpenAPI30Document, links: readonly OGCLink[]): string {
|
|
282
|
+
const operations: string[] = []
|
|
283
|
+
|
|
284
|
+
for (const [path, item] of Object.entries(document.paths ?? {})) {
|
|
285
|
+
for (const [method, operation] of Object.entries(item as Record<string, OperationLike>)) {
|
|
286
|
+
operations.push(operationHTML(method, path, operation))
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
return page(
|
|
291
|
+
`${document.info.title} API definition`,
|
|
292
|
+
`<h1>${escapeHTML(document.info.title)} API definition</h1>
|
|
293
|
+
<p>${escapeHTML(document.info.description ?? "")}</p>
|
|
294
|
+
<p class="meta">OpenAPI ${escapeHTML(document.openapi)}, version ${escapeHTML(document.info.version)}</p>
|
|
295
|
+
${linkList(links)}
|
|
296
|
+
${operations.join("\n")}`
|
|
297
|
+
)
|
|
298
|
+
}
|