@mailwoman/photon 10.0.0 → 10.2.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 (45) hide show
  1. package/README.md +2 -2
  2. package/{lib/cli.ts → cli/main.ts} +42 -56
  3. package/lib/app.ts +12 -34
  4. package/lib/engine.ts +21 -13
  5. package/lib/index.ts +6 -11
  6. package/lib/locality-postcode.ts +13 -17
  7. package/lib/projection.ts +61 -56
  8. package/lib/routes.ts +14 -26
  9. package/lib/schema.ts +31 -24
  10. package/out/app.d.ts +6 -28
  11. package/out/app.d.ts.map +1 -1
  12. package/out/app.js +5 -12
  13. package/out/app.js.map +1 -1
  14. package/out/cli/main.d.ts +12 -0
  15. package/out/cli/main.d.ts.map +1 -0
  16. package/out/{cli.js → cli/main.js} +42 -56
  17. package/out/cli/main.js.map +1 -0
  18. package/out/engine.d.ts +21 -13
  19. package/out/engine.d.ts.map +1 -1
  20. package/out/engine.js +2 -2
  21. package/out/engine.js.map +1 -1
  22. package/out/index.d.ts +6 -11
  23. package/out/index.d.ts.map +1 -1
  24. package/out/index.js +6 -11
  25. package/out/index.js.map +1 -1
  26. package/out/locality-postcode.d.ts +5 -8
  27. package/out/locality-postcode.d.ts.map +1 -1
  28. package/out/locality-postcode.js +11 -15
  29. package/out/locality-postcode.js.map +1 -1
  30. package/out/projection.d.ts +48 -38
  31. package/out/projection.d.ts.map +1 -1
  32. package/out/projection.js +38 -36
  33. package/out/projection.js.map +1 -1
  34. package/out/routes.d.ts +2 -2
  35. package/out/routes.d.ts.map +1 -1
  36. package/out/routes.js +13 -25
  37. package/out/routes.js.map +1 -1
  38. package/out/schema.d.ts +31 -24
  39. package/out/schema.d.ts.map +1 -1
  40. package/out/schema.js +31 -24
  41. package/out/schema.js.map +1 -1
  42. package/package.json +49 -33
  43. package/out/cli.d.ts +0 -17
  44. package/out/cli.d.ts.map +0 -1
  45. package/out/cli.js.map +0 -1
package/lib/projection.ts CHANGED
@@ -3,10 +3,9 @@
3
3
  * @license AGPL-3.0
4
4
  * @author Teffen Ellis, et al.
5
5
  *
6
- * The RESOLVED-place → Photon-schema projection: decorating a resolved gazetteer place (proper-
7
- * cased names + ancestry + country, house/street grade overrides) into {@link PhotonProperties},
8
- * plus the `format=jsonld` schema.org re-serialization. Wire types + feature/collection
9
- * construction live in `engine.ts`.
6
+ * The resolved-place → Photon-schema projection: decorating a resolved gazetteer place (proper-
7
+ * cased names, ancestry, country, house and street grade overrides) into {@link PhotonProperties},
8
+ * plus the `format=jsonld` schema.org re-serialization.
10
9
  */
11
10
 
12
11
  import { composeStreetAddress, type SchemaOrgPlace, toSchemaOrg } from "@mailwoman/annotations"
@@ -20,9 +19,11 @@ import {
20
19
  } from "#engine"
21
20
 
22
21
  /**
23
- * The resolved-place info a forward `/api` result carries: the admin ladder (MOST-SPECIFIC first) with GAZETTEER names,
24
- * the coordinate, the resolved country, and the postcode. {@link photonForwardProperties} projects it onto Photon's
25
- * schema. #1014 — decorate from the resolved place, not the parsed input span.
22
+ * The resolved-place info in a forward `/api` result: the admin ladder (most-specific first)
23
+ * with gazetteer names, the coordinate, the resolved country and the postcode.
24
+ * {@link photonForwardProperties} projects it onto Photon's schema.
25
+ *
26
+ * Decorate from the resolved place rather than the parsed input span.
26
27
  */
27
28
  export interface PhotonForwardInput {
28
29
  lat: number
@@ -33,34 +34,36 @@ export interface PhotonForwardInput {
33
34
  */
34
35
  country?: { name?: string; code?: string } | null
35
36
  /**
36
- * Resolved admin ancestry, most-specific first, each carrying the gazetteer's canonical name (not the parsed span).
37
+ * Resolved admin ancestry, most-specific first, each carrying the gazetteer's
38
+ * canonical name rather than the parsed span.
37
39
  */
38
40
  places: ReadonlyArray<{ tag: string; name: string }>
39
41
  /**
40
- * A HOUSE-GRADE result (#1041): set only when the resolver produced a specific building coordinate — the
41
- * `address_point` (rooftop) or `interpolated` tier fired, NOT an admin centroid. {@link photonForwardProperties} then
42
- * re-tags the schema `osm_key: place` / `osm_value: house` / `type: house` and surfaces the parsed `housenumber` +
43
- * `street`, matching upstream komoot/photon's own bare-address-point shape (verified against `photon.komoot.io`: a
44
- * residential rooftop returns `{osm_key:"place", osm_value:"house", type:"house", housenumber, street}` with NO
45
- * `name`). Absent → the result keeps its admin-ancestry schema. Without it a rooftop reads as `type: city` and a
46
- * client zooms to city scale (or paints a city marker) on a doorstep match — the #1041 regression.
42
+ * A house-grade result: set only when the resolver produced a specific building coordinate,
43
+ * the `address_point` (rooftop) or `interpolated` tier rather than an admin centroid.
44
+ * {@link photonForwardProperties} then re-tags the schema `osm_key: place` /
45
+ * `osm_value: house` / `type: house` and surfaces the parsed `housenumber` + `street`,
46
+ * matching upstream komoot/photon's bare-address-point shape.
47
+ *
48
+ * Absent means the result keeps its admin-ancestry schema.
47
49
  */
48
50
  house?: { number?: string | null; street?: string | null } | null
49
51
  /**
50
- * A STREET-GRADE result (#1050): set when the street-centroid tier (#1042/#1046) fired — a street-level coordinate,
51
- * below rooftop/interp, above admin. {@link photonForwardProperties} re-tags it `osm_key: highway` / `osm_value:
52
- * residential` / `type: street` with the FULL assembled street name in `name` — matching upstream komoot's street
53
- * results (verified live 2026-07-10: a street primary returns `{osm_key:"highway", osm_value:<class>, type:"street",
54
- * name:"Rue de la République", city, …}`; the street name rides `name`, not `street`). Without it a street centroid
55
- * reads `type: city` with the city's name — the #1050 regression. `house` wins when both are set (a numbered query
56
- * never street-tiers).
52
+ * A street-grade result: set when the street-centroid tier fired, a street-level
53
+ * coordinate below rooftop/interp and above admin. {@link photonForwardProperties}
54
+ * re-tags it `osm_key: highway` / `osm_value: residential` / `type: street` with the
55
+ * full assembled street name in `name`, matching upstream komoot's street results.
56
+ *
57
+ * `house` wins when both are set, since a numbered query never street-tiers.
57
58
  */
58
59
  street?: { name?: string | null } | null
59
60
  }
60
61
 
61
62
  /**
62
- * How a resolved tag projects onto Photon's schema: the property key that holds its name, plus the OSM `key`/`value`/
63
- * `type` a Photon client reads. Kept close to komoot Photon's own values so client label-builders behave.
63
+ * How a resolved tag projects onto Photon's schema: the property key that holds its name,
64
+ * plus the OSM `key`/`value`/ `type` a Photon client reads.
65
+ *
66
+ * Kept close to komoot Photon's own values so client label-builders behave.
64
67
  */
65
68
  const FORWARD_TAG_PROJECTION: Record<
66
69
  string,
@@ -70,18 +73,17 @@ const FORWARD_TAG_PROJECTION: Record<
70
73
  house: { key: "name", osmKey: "building", osmValue: "yes", type: "house" },
71
74
  street: { key: "street", osmKey: "highway", osmValue: "residential", type: "street" },
72
75
  neighbourhood: { key: "district", osmKey: "place", osmValue: "suburb", type: "district" },
73
- // The finest tier the /reverse descent ladder (resolver-wof-sqlite `DESCENT_TIERS`) can return — without a row
74
- // here a microhood-deepest reverse result fell through to the `place/yes/other` default. #1041 close-out.
76
+ // The finest tier the /reverse descent ladder (resolver-wof-sqlite `DESCENT_TIERS`) can return.
75
77
  microhood: { key: "district", osmKey: "place", osmValue: "neighbourhood", type: "district" },
76
78
  dependent_locality: { key: "district", osmKey: "place", osmValue: "suburb", type: "district" },
77
79
  borough: { key: "district", osmKey: "place", osmValue: "borough", type: "district" },
78
80
  locality: { key: "city", osmKey: "place", osmValue: "city", type: "city" },
79
- // The JP tiers the character-path model emits (#2164): a municipality is Photon's city, a prefecture its state, a
80
- // district (大字 / 町名) its district — the same keys Nominatim assigns those ranks.
81
+ // The JP tiers the character-path model emits: a municipality is Photon's city, a prefecture
82
+ // its state, a district (大字 / 町名) its district, the same keys Nominatim assigns those ranks.
81
83
  municipality: { key: "city", osmKey: "place", osmValue: "city", type: "city" },
82
84
  prefecture: { key: "state", osmKey: "place", osmValue: "state", type: "state" },
83
85
  district: { key: "district", osmKey: "place", osmValue: "suburb", type: "district" },
84
- // WOF placetypes the /reverse hierarchy uses (so both endpoints derive osm tags from ONE table).
86
+ // WOF placetypes the /reverse hierarchy uses (so both endpoints derive osm tags from one table).
85
87
  localadmin: { key: "city", osmKey: "place", osmValue: "city", type: "city" },
86
88
  subregion: { key: "county", osmKey: "place", osmValue: "county", type: "county" },
87
89
  county: { key: "county", osmKey: "place", osmValue: "county", type: "county" },
@@ -91,14 +93,17 @@ const FORWARD_TAG_PROJECTION: Record<
91
93
  }
92
94
 
93
95
  /**
94
- * Fallback OSM tags — a Photon client reads `osm_key`/`osm_value`/`type` unconditionally, so they must never be absent.
96
+ * Fallback OSM tags.
97
+ *
98
+ * A Photon client reads `osm_key`/`osm_value`/`type` unconditionally, so they must never be absent.
95
99
  */
96
100
  const DEFAULT_OSM_TAGS = { osm_key: "place", osm_value: "yes", type: "other" } as const
97
101
 
98
102
  /**
99
- * The Photon `osm_key`/`osm_value`/`type` for a resolved tag or WOF placetype (`locality`, `region`, `country`, …),
100
- * falling back to a safe default so the fields are always present. Shared by the forward projection and `/reverse` so
101
- * the two endpoints report a place the SAME way. #1014.
103
+ * The Photon `osm_key`/`osm_value`/`type` for a resolved tag or WOF placetype
104
+ * (`locality`, `region`, `country`, …), falling back to a safe default so the fields are always present.
105
+ *
106
+ * Shared by the forward projection and `/reverse` so the two endpoints report a place the same way.
102
107
  */
103
108
  export function photonOSMTags(tagOrPlacetype: string): { osm_key: string; osm_value: string; type: string } {
104
109
  const proj = FORWARD_TAG_PROJECTION[tagOrPlacetype]
@@ -107,12 +112,13 @@ export function photonOSMTags(tagOrPlacetype: string): { osm_key: string; osm_va
107
112
  }
108
113
 
109
114
  /**
110
- * Project a resolved forward result into Photon properties — decorating from the RESOLVED gazetteer place (proper-cased
111
- * names + ancestry + country), NOT the parsed input span, and ALWAYS emitting `osm_key`/`osm_value`/`type` so Photon
112
- * client libraries (leaflet-control-geocoder, @openrunner/photon-geocoder) never dereference undefined. #1014.
115
+ * Project a resolved forward result into Photon properties, decorating from the resolved
116
+ * gazetteer place (proper-cased names, ancestry, country) rather than the parsed input span.
117
+ *
118
+ * Always emit `osm_key`/`osm_value`/`type` so Photon client libraries
119
+ * (leaflet-control-geocoder, @openrunner/photon-geocoder) never dereference undefined.
113
120
  */
114
121
  export function photonForwardProperties(input: PhotonForwardInput): PhotonProperties {
115
- // Safe defaults: a Photon client reads osm_key/osm_value/type unconditionally, so they must never be undefined.
116
122
  const props: PhotonProperties = { ...DEFAULT_OSM_TAGS }
117
123
  const [primary] = input.places
118
124
 
@@ -133,7 +139,7 @@ export function photonForwardProperties(input: PhotonForwardInput): PhotonProper
133
139
  props.postcode = input.postcode
134
140
  }
135
141
 
136
- if (input.country?.name && props.country == null) {
142
+ if (input.country?.name && !props.country) {
137
143
  props.country = input.country.name
138
144
  }
139
145
 
@@ -141,19 +147,12 @@ export function photonForwardProperties(input: PhotonForwardInput): PhotonProper
141
147
  props.countrycode = input.country.code.toLowerCase()
142
148
  }
143
149
 
144
- // #1041: house-grade override. A rooftop / interpolated coordinate is a BUILDING, not the admin locality the
145
- // ancestry above would label it — re-tag the schema so a Photon client renders (and zooms to) a house, and surface
146
- // the parsed housenumber + street. Matches upstream komoot/photon's bare address point (osm_key:place, osm_value:
147
- // house, type:house, with housenumber + street and NO name). Drop the admin-derived `name` — else the QGIS FLF label
148
- // (name + housenumber + street + city + postcode) doubles the city ("Paris 8 Boulevard du Palais Paris 75001"). The
149
- // city/state/postcode/country the ancestry filled stay put (upstream carries them on a house result too).
150
- // #1050: street-grade — the street-centroid tier fired. Re-tag per upstream's street shape (full
151
- // name in `name`, highway/street osm tags); the admin ancestry (city/state/…) stays as context.
152
150
  if (input.street?.name && !input.house) {
153
151
  props.name = input.street.name
154
152
  Object.assign(props, { osm_key: "highway", osm_value: "residential", type: "street" })
155
153
  }
156
154
 
155
+ // Drop the admin-derived `name`, or a QGIS FLF label doubles the city.
157
156
  if (input.house) {
158
157
  props.osm_key = "place"
159
158
  props.osm_value = "house"
@@ -175,14 +174,14 @@ export function photonForwardProperties(input: PhotonForwardInput): PhotonProper
175
174
  }
176
175
 
177
176
  /**
178
- * {@link photonForwardProperties}, wrapped as a Photon Point `Feature` at the resolved coordinate. #1014.
177
+ * {@link photonForwardProperties}, wrapped as a Photon Point `Feature` at the resolved coordinate.
179
178
  */
180
179
  export function photonForwardFeature(input: PhotonForwardInput): PhotonFeature {
181
180
  return photonFeature(input.lon, input.lat, photonForwardProperties(input))
182
181
  }
183
182
 
184
183
  /**
185
- * The winning place plus its ranked alternatives — the input to {@link photonForwardCollection}. #1016.
184
+ * The winning place plus its ranked alternatives, the input to {@link photonForwardCollection}.
186
185
  */
187
186
  export interface PhotonForwardResult {
188
187
  /**
@@ -190,14 +189,17 @@ export interface PhotonForwardResult {
190
189
  */
191
190
  primary: PhotonForwardInput
192
191
  /**
193
- * Ranked alternative places (Springfield MA / IL / …), each a single-place input; excludes the primary.
192
+ * Ranked alternative places (Springfield MA / IL / …), each a single-place input.
193
+ * Excludes the primary.
194
194
  */
195
195
  alternatives: PhotonForwardInput[]
196
196
  }
197
197
 
198
198
  /**
199
- * Assemble a Photon `FeatureCollection` honoring `limit` (#1016): the primary feature first, then ranked alternatives,
200
- * capped at `limit`. `limit` floors to 1 (Photon always returns at least the best match).
199
+ * Assemble a Photon `FeatureCollection` honoring `limit`: the primary feature first,
200
+ * then ranked alternatives, capped at `limit`.
201
+ *
202
+ * `limit` floors to 1, since Photon always returns at least the best match.
201
203
  */
202
204
  export function photonForwardCollection(result: PhotonForwardResult, limit: number): PhotonFeatureCollection {
203
205
  const cap = Math.max(1, Math.floor(limit) || 1)
@@ -212,10 +214,13 @@ export function photonForwardCollection(result: PhotonForwardResult, limit: numb
212
214
  }
213
215
 
214
216
  /**
215
- * Project a Photon `Feature` into a schema.org `Place` JSON-LD object (`format=jsonld`, #1052) — the OUTPUT-format
216
- * projection. Reads the feature's already-decorated {@link PhotonProperties} (housenumber/street/city/state/postcode/
217
- * countrycode + the coordinate), so it stays a pure re-serialization of the SAME resolved place the FeatureCollection
218
- * carries. `streetAddress` is the plain house-number-first join (the light router has no locale formatter).
217
+ * Project a Photon `Feature` into a schema.org `Place` JSON-LD object (`format=jsonld`),
218
+ * the output-format projection.
219
+ *
220
+ * Reads the feature's already-decorated {@link PhotonProperties}
221
+ * (housenumber/street/city/state/postcode/countrycode and the coordinate), so it stays a
222
+ * pure re-serialization of the same resolved place included in the FeatureCollection.
223
+ * `streetAddress` is the plain house-number-first join, since the light router has no locale formatter.
219
224
  */
220
225
  export function photonFeatureToSchemaOrg(feature: PhotonFeature): SchemaOrgPlace {
221
226
  const p = feature.properties
@@ -235,7 +240,7 @@ export function photonFeatureToSchemaOrg(feature: PhotonFeature): SchemaOrgPlace
235
240
  }
236
241
 
237
242
  /**
238
- * Project a whole Photon `FeatureCollection` into an array of schema.org `Place` objects (`format=jsonld`). #1052.
243
+ * Project a whole Photon `FeatureCollection` into an array of schema.org `Place` objects (`format=jsonld`).
239
244
  */
240
245
  export function photonToSchemaOrg(collection: PhotonFeatureCollection): SchemaOrgPlace[] {
241
246
  return collection.features.map(photonFeatureToSchemaOrg)
package/lib/routes.ts CHANGED
@@ -4,8 +4,8 @@
4
4
  * @author Teffen Ellis, et al.
5
5
  *
6
6
  * Route definitions + handlers for the Photon-compatible surface. The OpenAPI document is
7
- * emitted from these definitions — no handwritten spec. Handlers parse params from the
8
- * `legacyQuery` express-shaped view; the zod query schemas drive only the emitted document.
7
+ * emitted from these definitions, so no handwritten spec exists. Handlers parse params from
8
+ * the `legacyQuery` express-shaped view. The zod query schemas drive only the emitted document.
9
9
  */
10
10
 
11
11
  import { createRoute, type OpenAPIHono, z } from "@hono/zod-openapi"
@@ -16,42 +16,30 @@ import type { PhotonEngine, PhotonFeatureCollection, PhotonReverseParams, Photon
16
16
  import { photonToSchemaOrg } from "#projection"
17
17
  import { PhotonMessageCollectionSchema, PhotonResponseSchema, reverseQueryParams, searchQueryParams } from "#schema"
18
18
 
19
- /**
20
- * Southern limit of latitude in WGS-84 degrees.
21
- */
22
19
  const MIN_LATITUDE = -90
23
20
 
24
- /**
25
- * Northern limit of latitude in WGS-84 degrees.
26
- */
27
21
  const MAX_LATITUDE = 90
28
22
 
29
- /**
30
- * Western limit of longitude in WGS-84 degrees.
31
- */
32
23
  const MIN_LONGITUDE = -180
33
24
 
34
- /**
35
- * Eastern limit of longitude in WGS-84 degrees.
36
- */
37
25
  const MAX_LONGITUDE = 180
38
26
 
39
27
  const DEFAULT_LIMIT = 15
40
28
 
41
29
  const EMPTY: PhotonFeatureCollection = { type: "FeatureCollection", features: [] }
42
30
 
43
- function asStringArray(raw: unknown): string[] | undefined {
31
+ function asStringArray(raw: unknown): string[] | null {
44
32
  if (Array.isArray(raw)) return raw.filter((v): v is string => typeof v === "string")
45
33
  const s = asString(raw)
46
34
 
47
- return s ? [s] : undefined
35
+ return s ? [s] : null
48
36
  }
49
37
 
50
38
  /**
51
- * A friendly HTML landing page for `GET /` (#1022). Upstream komoot/photon serves no root page, so there's no wire
52
- * contract to match — this is pure courtesy: a browser visitor (or an evaluator kicking the tires) who pastes the bare
53
- * host in gets a one-glance orientation with clickable example queries instead of Express's `Cannot GET /` 404, which
54
- * reads as "the service is broken". Relative example URLs so they resolve against whatever host/port serves this.
39
+ * A friendly html landing page for `GET /`.
40
+ *
41
+ * Upstream komoot/photon serves no root page, so there is no wire interface to match.
42
+ * Relative example URLs resolve against whatever host and port serve this.
55
43
  */
56
44
  const ROOT_HTML = `<!doctype html>
57
45
  <html lang="en">
@@ -73,7 +61,7 @@ footer { margin-top: 2rem; font-size: .9rem; opacity: .8 }
73
61
  </head>
74
62
  <body>
75
63
  <h1>@mailwoman/photon</h1>
76
- <p>A Photon-compatible autocomplete geocoding API — the same <code>/api</code> and <code>/reverse</code> contract, served from a SQLite gazetteer instead of an Elasticsearch cluster.</p>
64
+ <p>A Photon-compatible autocomplete geocoding API — the same <code>/api</code> and <code>/reverse</code> interface, served from a SQLite gazetteer instead of an Elasticsearch cluster.</p>
77
65
  <p>Try a query:</p>
78
66
  <ul>
79
67
  <li><a class="q" href="/api?q=berlin&amp;limit=3">/api?q=berlin&amp;limit=3</a></li>
@@ -146,15 +134,15 @@ export function registerPhotonRoutes(app: OpenAPIHono, engine: PhotonEngine, sta
146
134
  q: query,
147
135
  limit: Number(q["limit"] ?? DEFAULT_LIMIT) || DEFAULT_LIMIT,
148
136
  lang: asString(q["lang"]),
149
- lat: q["lat"] != null ? Number(q["lat"]) : undefined,
150
- lon: q["lon"] != null ? Number(q["lon"]) : undefined,
137
+ lat: q["lat"] ? Number(q["lat"]) : undefined,
138
+ lon: q["lon"] ? Number(q["lon"]) : undefined,
151
139
  osmTag: asStringArray(q["osm_tag"]),
152
140
  layer: asStringArray(q["layer"]),
153
141
  }
154
142
 
155
143
  const collection = await engine.search(params)
156
144
 
157
- // #1052: `format=jsonld` re-serializes the SAME FeatureCollection as schema.org `Place[]` JSON-LD.
145
+ // `format=jsonld` re-serializes the FeatureCollection as schema.org `Place[]` JSON-LD.
158
146
  if (asString(q["format"]) === "jsonld") {
159
147
  return c.json(photonToSchemaOrg(collection), 200)
160
148
  }
@@ -181,12 +169,12 @@ export function registerPhotonRoutes(app: OpenAPIHono, engine: PhotonEngine, sta
181
169
  lon,
182
170
  limit: Number(q["limit"] ?? DEFAULT_LIMIT) || DEFAULT_LIMIT,
183
171
  lang: asString(q["lang"]),
184
- radius: q["radius"] != null ? Number(q["radius"]) : undefined,
172
+ radius: q["radius"] ? Number(q["radius"]) : undefined,
185
173
  }
186
174
 
187
175
  const collection = await engine.reverse(params)
188
176
 
189
- // #1052: `format=jsonld` re-serializes the reverse FeatureCollection as schema.org `Place[]` JSON-LD.
177
+ // `format=jsonld` re-serializes the reverse FeatureCollection as schema.org `Place[]` JSON-LD.
190
178
  if (asString(q["format"]) === "jsonld") {
191
179
  return c.json(photonToSchemaOrg(collection), 200)
192
180
  }
package/lib/schema.ts CHANGED
@@ -3,17 +3,17 @@
3
3
  * @license AGPL-3.0
4
4
  * @author Teffen Ellis, et al.
5
5
  *
6
- * Zod wire schemas for the Photon-compatible surface. Key names and envelopes are the vendor
7
- * contract — immutable. Query schemas are validator-proof by construction (unions accepting
8
- * string or repeated values) with doc-exact `.openapi()` overrides: validation can never fail,
9
- * and every wire decision stays in the handlers (see routes.ts's legacyQuery adapter).
6
+ * Zod schemas for the Photon-compatible API. Key names and envelopes match Photon and must not
7
+ * change. The query schemas accept any string or string array, so validation never fails. The
8
+ * handlers in `routes.ts` do the actual parsing.
10
9
  */
11
10
 
12
11
  import { z } from "@hono/zod-openapi"
13
12
  import { featureCollectionSchema, featureSchema, stampedResponseSchema } from "@mailwoman/api-kit"
14
13
 
15
14
  /**
16
- * Photon feature properties — OSM-derived keys; tolerant of extras (`[key: string]: unknown` on the wire type).
15
+ * Photon feature properties with OSM-derived keys.
16
+ * Extra keys are allowed.
17
17
  */
18
18
  export const PhotonPropertiesSchema = z
19
19
  .object({
@@ -38,18 +38,19 @@ export const PhotonPropertiesSchema = z
38
38
  .openapi("PhotonProperties")
39
39
 
40
40
  /**
41
- * One Photon result feature — GeoJSON geometry plus Photon's property block.
41
+ * One Photon result feature with GeoJSON geometry and Photon's properties.
42
42
  */
43
43
  export const PhotonFeatureSchema = featureSchema(PhotonPropertiesSchema).openapi("PhotonFeature")
44
44
 
45
45
  /**
46
- * Response body of `/api`, a GeoJSON FeatureCollection as Photon returns it.
46
+ * A GeoJSON FeatureCollection of Photon features.
47
47
  */
48
48
  export const PhotonFeatureCollectionSchema =
49
49
  featureCollectionSchema(PhotonFeatureSchema).openapi("PhotonFeatureCollection")
50
50
 
51
51
  /**
52
- * The error/degenerate envelope: an EMPTY FeatureCollection carrying a message. Never `{error}` on this surface.
52
+ * The error envelope is an empty FeatureCollection with a message.
53
+ * Photon does not use an `{ error }` body.
53
54
  */
54
55
  export const PhotonMessageCollectionSchema = z
55
56
  .object({
@@ -60,10 +61,10 @@ export const PhotonMessageCollectionSchema = z
60
61
  .openapi("PhotonMessageCollection")
61
62
 
62
63
  /**
63
- * The schema.org [`GeoCoordinates`](https://schema.org/GeoCoordinates) node — mirrors `@mailwoman/annotations`'s
64
- * `SchemaOrgGeoCoordinates` interface. Hand-modeled locally (no import from `@mailwoman/annotations`) matching this
65
- * package's existing wire-schema convention: each surface owns its own doc-accuracy schemas rather than sharing a
66
- * schema across the package boundary.
64
+ * The schema.org [`GeoCoordinates`](https://schema.org/GeoCoordinates) node.
65
+ *
66
+ * It mirrors the `SchemaOrgGeoCoordinates` interface in `@mailwoman/annotations`.
67
+ * Each API package defines its own schemas, so this one is written out here.
67
68
  */
68
69
  export const SchemaOrgGeoCoordinatesSchema = z
69
70
  .object({
@@ -74,7 +75,8 @@ export const SchemaOrgGeoCoordinatesSchema = z
74
75
  .openapi("SchemaOrgGeoCoordinates")
75
76
 
76
77
  /**
77
- * The schema.org [`PostalAddress`](https://schema.org/PostalAddress) node — mirrors `SchemaOrgPostalAddress`.
78
+ * The schema.org [`PostalAddress`](https://schema.org/PostalAddress) node.
79
+ * It mirrors `SchemaOrgPostalAddress`.
78
80
  */
79
81
  export const SchemaOrgPostalAddressSchema = z
80
82
  .object({
@@ -89,8 +91,9 @@ export const SchemaOrgPostalAddressSchema = z
89
91
  .openapi("SchemaOrgPostalAddress")
90
92
 
91
93
  /**
92
- * The `format=jsonld` re-serialization (#1052) — mirrors `@mailwoman/annotations`'s `SchemaOrgPlace` interface, the
93
- * shape `photonToSchemaOrg` actually produces.
94
+ * The `format=jsonld` output.
95
+ *
96
+ * It mirrors the `SchemaOrgPlace` interface that `photonToSchemaOrg` produces.
94
97
  */
95
98
  export const SchemaOrgPlaceSchema = z
96
99
  .object({
@@ -103,8 +106,10 @@ export const SchemaOrgPlaceSchema = z
103
106
  .openapi("SchemaOrgPlace")
104
107
 
105
108
  /**
106
- * The FeatureCollection as a route returns it — the collection plus the optional `engine` stamp. Named because it is a
107
- * union arm: an unnamed arm is inlined, and a generated client then names the variant after its position.
109
+ * The FeatureCollection with the optional `engine` stamp.
110
+ *
111
+ * The schema has an OpenAPI name because it is a union member.
112
+ * A generated client would otherwise label an inlined member by its position.
108
113
  */
109
114
  export const StampedPhotonFeatureCollectionSchema = stampedResponseSchema(
110
115
  PhotonFeatureCollectionSchema,
@@ -112,22 +117,24 @@ export const StampedPhotonFeatureCollectionSchema = stampedResponseSchema(
112
117
  )
113
118
 
114
119
  /**
115
- * The real `/api` + `/reverse` 200 response union (#1052 doc accuracy): a GeoJSON FeatureCollection by default, or an
116
- * array of schema.org `Place` JSON-LD objects when `format=jsonld` — see `routes.ts`'s handlers (`photonToSchemaOrg`).
117
- * Doc-only; the wire behavior is unchanged.
120
+ * The `/api` and `/reverse` 200 response for the OpenAPI document.
121
+ *
122
+ * The response is a GeoJSON FeatureCollection by default or an array of schema.org
123
+ * `Place` objects for `format=jsonld`.
118
124
  */
119
125
  export const PhotonResponseSchema = z
120
126
  .union([StampedPhotonFeatureCollectionSchema, z.array(SchemaOrgPlaceSchema)])
121
127
  .openapi("PhotonResponse")
122
128
 
123
129
  /**
124
- * A query param that may legally repeat (or that a client may repeat without the validator being allowed to answer for
125
- * us). Validator-proof: accepts one value or many; the doc override keeps the emitted parameter schema exact.
130
+ * A query parameter that accepts one value or a repeated value.
131
+ * Each use sets its documented type with `.openapi()`.
126
132
  */
127
133
  const tolerantParam = z.union([z.string(), z.array(z.string())]).optional()
128
134
 
129
135
  /**
130
- * `GET /api` query — documented shape; presence/parsing enforced in-handler.
136
+ * The documented query parameters of `GET /api`.
137
+ * The handler checks and parses them.
131
138
  */
132
139
  export const searchQueryParams = z.object({
133
140
  q: tolerantParam.openapi({ type: "string", description: "The query string to search for." }),
@@ -145,7 +152,7 @@ export const searchQueryParams = z.object({
145
152
  })
146
153
 
147
154
  /**
148
- * `GET /reverse` query.
155
+ * The documented query parameters of `GET /reverse`.
149
156
  */
150
157
  export const reverseQueryParams = z.object({
151
158
  lat: tolerantParam.openapi({ type: "number", description: "Latitude." }),
package/out/app.d.ts CHANGED
@@ -2,40 +2,18 @@
2
2
  * @copyright Sister Software
3
3
  * @license AGPL-3.0
4
4
  * @author Teffen Ellis, et al.
5
- *
6
- * The Photon-compatible Hono app: CORS + error safety net + routes + the emitted OpenAPI
7
- * document. Engine-agnostic — the CLI wires the real engine; tests inject fixtures.
8
5
  */
9
6
  import { OpenAPIHono } from "@hono/zod-openapi";
10
- import { type OpenAPIDocInfo } from "@mailwoman/api-kit";
11
- import type { EngineStamp } from "@mailwoman/core/license";
7
+ import { type CompatibilityAppOptions, type OpenAPIDocInfo } from "@mailwoman/api-kit";
12
8
  import type { PhotonEngine } from "#engine";
13
9
  /**
14
- * Options for {@link createPhotonApp}.
15
- */
16
- export interface PhotonAppOptions {
17
- /**
18
- * Emit permissive CORS headers (`Access-Control-Allow-Origin: *`) on every response and answer preflight `OPTIONS`
19
- * with `204`. Default `true` — upstream komoot/photon serves permissive CORS, and the map-widget use case
20
- * (leaflet-control-geocoder, @openrunner/photon-geocoder, …) needs it: a browser's cross-origin XHR is blocked
21
- * without it (#1017). Set `false` when a reverse proxy already owns the CORS headers.
22
- */
23
- cors?: boolean;
24
- /**
25
- * The engine stamp to carry on every response: `engine` as a foreign member of each FeatureCollection, and the
26
- * `Server` + `Link: rel="license"` headers everywhere. Absent when an embedding application builds the app without
27
- * the `mailwoman` package; the `photon` bin always passes one.
28
- */
29
- engine?: EngineStamp;
30
- }
31
- /**
32
- * The document info stamped into the emitted OpenAPI document. Exported (not inlined) so the CLI's `openapi` subcommand
33
- * can call `emitOpenAPIDocuments` with the SAME info the mounted `/openapi.json` route (below, via
34
- * {@link attachOpenAPIDocs}) uses — one source of truth, no risk of the two drifting.
10
+ * The OpenAPI document info for the Photon API.
11
+ *
12
+ * The served `/openapi.json` route and the CLI's `openapi` subcommand both use it.
35
13
  */
36
14
  export declare const PHOTON_DOC_INFO: OpenAPIDocInfo;
37
15
  /**
38
- * Build the Photon-compatible app around an injected {@link PhotonEngine}.
16
+ * Builds the Photon-compatible app around the given {@link PhotonEngine}.
39
17
  */
40
- export declare function createPhotonApp(engine: PhotonEngine, options?: PhotonAppOptions): OpenAPIHono;
18
+ export declare function createPhotonApp(engine: PhotonEngine, options?: CompatibilityAppOptions): OpenAPIHono;
41
19
  //# sourceMappingURL=app.d.ts.map
package/out/app.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"app.d.ts","sourceRoot":"","sources":["../lib/app.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAC/C,OAAO,EAAoC,KAAK,cAAc,EAA0B,MAAM,oBAAoB,CAAA;AAClH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAA;AAG1D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAG3C;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAChC;;;;;OAKG;IACH,IAAI,CAAC,EAAE,OAAO,CAAA;IAEd;;;;OAIG;IACH,MAAM,CAAC,EAAE,WAAW,CAAA;CACpB;AAED;;;;GAIG;AACH,eAAO,MAAM,eAAe,EAAE,cAmB7B,CAAA;AAED;;GAEG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,YAAY,EAAE,OAAO,GAAE,gBAAqB,GAAG,WAAW,CAqBjG"}
1
+ {"version":3,"file":"app.d.ts","sourceRoot":"","sources":["../lib/app.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAC/C,OAAO,EAEN,KAAK,uBAAuB,EAE5B,KAAK,cAAc,EAEnB,MAAM,oBAAoB,CAAA;AAG3B,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAG3C;;;;GAIG;AACH,eAAO,MAAM,eAAe,EAAE,cAmB7B,CAAA;AAED;;GAEG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,YAAY,EAAE,OAAO,GAAE,uBAA4B,GAAG,WAAW,CAiBxG"}
package/out/app.js CHANGED
@@ -2,18 +2,15 @@
2
2
  * @copyright Sister Software
3
3
  * @license AGPL-3.0
4
4
  * @author Teffen Ellis, et al.
5
- *
6
- * The Photon-compatible Hono app: CORS + error safety net + routes + the emitted OpenAPI
7
- * document. Engine-agnostic — the CLI wires the real engine; tests inject fixtures.
8
5
  */
9
6
  import { OpenAPIHono } from "@hono/zod-openapi";
10
- import { attachOpenAPIDocs, engineHeaders, readServedDocumentInfo } from "@mailwoman/api-kit";
7
+ import { attachOpenAPIDocs, engineHeaders, readServedDocumentInfo, } from "@mailwoman/api-kit";
11
8
  import { cors } from "hono/cors";
12
9
  import { registerPhotonRoutes } from "#routes";
13
10
  /**
14
- * The document info stamped into the emitted OpenAPI document. Exported (not inlined) so the CLI's `openapi` subcommand
15
- * can call `emitOpenAPIDocuments` with the SAME info the mounted `/openapi.json` route (below, via
16
- * {@link attachOpenAPIDocs}) uses — one source of truth, no risk of the two drifting.
11
+ * The OpenAPI document info for the Photon API.
12
+ *
13
+ * The served `/openapi.json` route and the CLI's `openapi` subcommand both use it.
17
14
  */
18
15
  export const PHOTON_DOC_INFO = {
19
16
  ...(await readServedDocumentInfo(import.meta.url, "@mailwoman/photon")),
@@ -36,20 +33,16 @@ export const PHOTON_DOC_INFO = {
36
33
  ],
37
34
  };
38
35
  /**
39
- * Build the Photon-compatible app around an injected {@link PhotonEngine}.
36
+ * Builds the Photon-compatible app around the given {@link PhotonEngine}.
40
37
  */
41
38
  export function createPhotonApp(engine, options = {}) {
42
39
  const app = new OpenAPIHono();
43
- // Browser-embedded widgets need CORS or their cross-origin XHR is blocked before the request completes (#1017).
44
- // GET-only — photon has no mutating routes, so unlike libpostal's CORS there is no POST in the methods list.
45
40
  if (options.cors !== false) {
46
41
  app.use(cors({ origin: "*", allowMethods: ["GET", "OPTIONS"], allowHeaders: ["*"], maxAge: 86_400 }));
47
42
  }
48
43
  if (options.engine) {
49
44
  app.use(engineHeaders(options.engine));
50
45
  }
51
- // Safety net: malformed input or an engine fault returns an empty FeatureCollection, never a crash (photon's
52
- // envelope — NOT `{error}`, which is the libpostal/nominatim shape).
53
46
  app.onError((_error, c) => c.json({ type: "FeatureCollection", features: [], message: "internal error" }, 500));
54
47
  registerPhotonRoutes(app, engine, options.engine);
55
48
  attachOpenAPI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AC,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,mBAAmB,EAAE,QAAQ,EAAE,EAAE,EAAE,OAAO,EAAE,gBAAgB,EAAE,EAAE,GAAG,CAAC,CAAC,CAAA;IAE/G,oBAAoB,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,CAAA;IACjD,iBAAiB,CAAC,GAAG,EAAE,eAAe,CAAC,CAAA;IAEvC,OAAO,GAAG,CAAA;AACX,CAAC"}
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * @copyright Sister Software
4
+ * @license AGPL-3.0
5
+ * @author Teffen Ellis, et al.
6
+ *
7
+ * `mailwoman-photon` — boot a Photon-compatible autocomplete endpoint via the `serve` command, wiring
8
+ * `/api` over `geocodeAddress` (parse → resolve) and `/reverse` over `WOFReverseGeocoder` into Photon's
9
+ * GeoJSON FeatureCollection.
10
+ */
11
+ export {};
12
+ //# sourceMappingURL=main.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../../cli/main.ts"],"names":[],"mappings":";AACA;;;;;;;;GAQG"}