@crvouga/mockingbird-service-google-maps
Local emulators. Real API contracts. Part of Mockingbird.
Emulator of the Google Maps Platform surface our member app uses for addresses: Places
Autocomplete, Place Details and Find Place From Text (the JSON web services), the Geocoding API,
a Maps JavaScript API shim (/maps/api/js?libraries=places) exposing
google.maps.places.* and google.maps.Geocoder over the same data, and the Address
Validation API (POST /v1:validateAddress, with USPS CASS/DPV data) for server-side ship-to
checks. Answers come from a corpus
matching our QA fixtures, so the address step that waits 5 s for Google predictions (and then
falls back to manual entry) resolves instantly and deterministically.
- Operation coverage: SUPPORT.md
- Google publishes no OpenAPI document for these endpoints:
openapi.yaml is hand-authored
from Google's documented shapes and the fields our consumer reads. Address Validation follows
the REST reference
and ValidationResult.
Install
npm install -D @crvouga/mockingbird-service-google-maps
ESM only. Node >= 22 or Bun >= 1.2. No native dependencies. Serve it with
npx mockingbird-google-maps serve, createServer from ./server (Node), or createRuntime
with any Fetch server.
Usage
The app hardcodes https://maps.googleapis.com (seam G-Y1: a base-URL env for
M/lib/ui/address-autocomplete/address-autocomplete-native-rest.tsx,
M/features/bloodwork/shared/lab-finder/use-geocoded-address.ts and
M/lib/ui/google-maps/load-google-maps-script.ts). Until it lands, rewrite that host to the emulator
in the stack's web dist (stack-web-dist.ts), as for PostHog. PLACES_KEY can be any non-empty
string unless you restrict keys.
npx mockingbird-google-maps serve --port 8814 --api-key "$PLACES_KEY"
import { Client, PlaceAutocompleteType } from "@googlemaps/google-maps-services-js"
import { createRuntime } from "@crvouga/mockingbird-service-google-maps"
// The Maps client your application already uses. Only its HTTP layer changes: axios's
// fetch adapter reaches the emulator in this process instead of maps.googleapis.com.
const emulator = createRuntime()
const maps = new Client({
config: {
adapter: "fetch",
env: { fetch: (url: string | URL | Request, init?: RequestInit) => emulator.fetch(new Request(url, init)) },
},
})
const { data } = await maps.placeAutocomplete({
params: { input: "1625 N Central", types: PlaceAutocompleteType.address, components: ["country:us"], key: "k" },
})
const details = await maps.placeDetails({
params: { place_id: data.predictions[0]?.place_id ?? "", fields: ["address_component"], key: "k" },
})
// details.data.result.address_components → 1625 / N Central Ave / Phoenix / AZ / 85004
// Test control: make the next two autocomplete calls fail, as QA's manual-entry fallback expects.
emulator.applyPreset("autocomplete_over_query_limit", "default", { count: 2 })
Address Validation
Google serves it from a different host, https://addressvalidation.googleapis.com; the paths do
not collide, so one emulator serves both. Point the server-side client's Address Validation base URL
at the emulator (or at <emulator>/__admin/ns/<namespace>), with the key as ?key= (or X-Goog-Api-Key).
import { AddressValidationClient } from "@googlemaps/addressvalidation"
import { createServer } from "@crvouga/mockingbird-service-google-maps/server"
const server = await createServer()
const { hostname, port } = new URL(server.url)
// The application's client, in REST mode. Only the endpoint changes.
const client = new AddressValidationClient({
fallback: true,
protocol: "http",
apiEndpoint: hostname,
port: Number(port),
apiKey: "k",
})
const [{ result }] = await client.validateAddress({
address: {
regionCode: "US",
addressLines: ["1625 N Central Ave"],
locality: "Phoenix",
administrativeArea: "AZ",
postalCode: "85004",
},
enableUspsCass: true,
})
// result.verdict → PREMISE / addressComplete; result.uspsData.dpvConfirmation → "Y"
// Test control: force the USPS "not deliverable" answer on the next call.
server.runtime.applyPreset("address_validation_dpv_n", "default", { count: 1 })
await client.close()
await server.close()
On web, load <emulator>/maps/api/js?key=…&libraries=places exactly as the app loads Google's
script; the shim calls back into the emulator's REST endpoints on the same origin and namespace.
Routes
Every web-service answer is HTTP 200 with Google's status (OK, ZERO_RESULTS,
INVALID_REQUEST, NOT_FOUND, REQUEST_DENIED, OVER_QUERY_LIMIT, UNKNOWN_ERROR) and, for
errors, Google's error_message. Responses carry access-control-allow-origin: * so the shim
works from a browser.
| Route |
Behaviour |
GET /maps/api/place/autocomplete/json |
input (required, else INVALID_REQUEST), types, components=country:us, sessiontoken. predictions[] with description, place_id, reference, structured_formatting{main_text, main_text_matched_substrings, secondary_text}, terms, types, matched_substrings; at most 5. Matches corpus rows by token prefix (street, city, state, ZIP); "<number> <street> <corpus city>[ <ST>][ <ZIP>]" is synthesized in that row's city/state/ZIP (QA's fuzzed search). Nothing → ZERO_RESULTS; a non-US components → ZERO_RESULTS. |
GET /maps/api/place/details/json |
place_id (required), fields (honoured: address_component returns only address_components; unknown field → INVALID_REQUEST), sessiontoken. result with address_components (street_number, route, locality, administrative_area_level_2, administrative_area_level_1 long/short, country, postal_code), formatted_address, geometry{location, viewport}, place_id, types, name, url, vicinity. Unknown id → NOT_FOUND. |
GET /maps/api/geocode/json |
address, place_id or components=postal_code:… (none → INVALID_REQUEST). results[0] with address_components, formatted_address, geometry{location, location_type, viewport}, place_id, types. A full address ("line1, city, ST zip") resolves to its row or a synthesized address; a bare ZIP or "City, ST" to the ZIP centroid; a street without a locatable city is ZERO_RESULTS. |
GET /maps/api/place/findplacefromtext/json |
input + inputtype=textquery (else INVALID_REQUEST), fields (default: place_id only, as Google). Geocoding first, then the looser autocomplete match, so it finds what our geocode fallback needs. |
POST /v1:validateAddress |
Address Validation (see below). A Google Cloud API: errors are HTTP 4xx/5xx with {"error": {code, message, status}}, not a 200 with status. |
GET /maps/api/js |
The shim (text/javascript): google.maps.places.AutocompleteService#getPlacePredictions, PlacesService#getDetails / #findPlaceFromQuery, AutocompleteSessionToken, PlacesServiceStatus, google.maps.Geocoder#geocode, GeocoderStatus, LatLng (lat()/lng()), LatLngBounds, importLibrary. Callbacks and promises (rejecting with MapsRequestError on errors when no callback is given). Calls window[callback] for &callback=, and window.gm_authFailure() when the key is refused. |
Address Validation verdicts
Answers are proto3 JSON, as Google sends them: false booleans and empty lists are omitted
(no unconfirmedComponentTypes: []). result carries verdict (inputGranularity,
validationGranularity, geocodeGranularity, addressComplete, has*Components,
possibleNextAction), address (formattedAddress, postalAddress, addressComponents with
confirmationLevel / inferred / replaced, missingComponentTypes,
unconfirmedComponentTypes), geocode (location, placeId, placeTypes) and, for US
addresses, uspsData (standardizedAddress, dpvConfirmation, dpvFootnote,
postOfficeCity/State, errorMessage, cassProcessed when enableUspsCass). Every answer
has a responseId. Send a componentized address (line 1 + optional unit line 2, locality,
administrativeArea, postalCode) or everything in addressLines.
| Input |
Verdict |
A corpus row (1625 N Central Ave, Phoenix AZ 85004); spelled-out words (North, Avenue) match |
PREMISE, complete, ACCEPT, DPV Y; postalAddress echoes the corpus spelling with a deterministic ZIP+4 (85004-NNNN) |
<number> <street> in a corpus city |
Synthesized, as for Autocomplete: PREMISE, ACCEPT, DPV Y, the street USPS-abbreviated (Maple Street → Maple St) |
A unit on a single-delivery-point row (… Building A, Apt 4) |
PREMISE, complete, CONFIRM, subpremise unconfirmed, DPV Y |
No house number (N Central Ave) in a corpus city |
ROUTE, FIX, missingComponentTypes: ["street_number"], no DPV code |
| A city/ZIP the corpus does not hold |
OTHER, FIX, the unplaceable components unconfirmed, DPV N |
| A ZIP that is wrong for the city (or a city that is wrong for the ZIP) |
Replaced: hasReplacedComponents, CONFIRM, the corrected value in postalAddress |
A row with validation.multiUnit |
No unit: CONFIRM_ADD_SUBPREMISES, missingComponentTypes: ["subpremise"], DPV D. A unit in validation.units (or any, when units is absent): SUB_PREMISE, ACCEPT, Y. Another unit: PREMISE, CONFIRM, subpremise unconfirmed, DPV S |
A row with validation.uspsErrorMessage |
uspsData: {errorMessage} and no DPV code |
No address, empty addressLines, an unknown field, non-JSON, over 280 characters, or enableUspsCass outside US/PR |
400 INVALID_ARGUMENT |
| No key |
403 PERMISSION_DENIED "The request is missing a valid API key." |
A key outside settings.keys |
400 INVALID_ARGUMENT "API key not valid. Please pass a valid API key." (reason: API_KEY_INVALID), as Google answers an unknown key; address_validation_denied gives the 403 of a key without the API enabled |
Corpus
59 addresses: every row of QA's ROUTING_ZIP_CORPUS (packages/qa/src/world/gen/addresses.ts,
≥1 real ZIP per state + DC, ids kept), which includes the Phoenix AZ demo member
(ADDRESS_AT_HOME_PHLEBOTOMY: 1625 N Central Ave, Phoenix, AZ 85004) and the other
packages/app/src/test-addresses members, plus ADDRESS_AT_HOME_PHLEBOTOMY_2 (501 N 5th St).
Each has its real county and the city's coordinates. Place ids are stable (ChIJ… for rows;
synthesized addresses get an Ei… id carrying the address, as Google's own address ids do, so
Details needs no stored state). GET /__admin/health reports the corpus.