Appearance
Migrating to 4.0.1 (the current release) from v2
This page is the direct-to-current migration reference for Mapsted Maps JavaScript API customers currently on v2 (2.x, UMD-only, untyped). v3 was an intermediate release — v2 customers should skip it and migrate straight to the current release (4.0.1), which carries forward every v3 improvement and layers safety on top (typed events, structured errors, origin-validated postMessage).
Upgrading is optional. mapi.mapsted.com keeps serving v2.13.0, so your existing <script> integration continues to work with no action required — there is no forced upgrade. Migrate when you are ready to adopt the newer features below.
If you are currently on v3, see Migrating from v3 to 4.0.1 instead — a shorter doc that only covers the v3 → 4.0.1 deltas.
Every v2 method name still works in
4.0.1via the backward-compatibility shim (src/migration/shim.ts). Each deprecated call logs oneconsole.warnand then delegates to the current equivalent. The shim preserves your old method names, so you can migrate incrementally. It will be removed in a future major release — migrate before then.
1. Method renames
v2's public method names were renamed at the 4.0.1 boundary for clarity, casing consistency, and to align the event API with idiomatic JavaScript (on/off/once vs DOM-style listeners). One method (highlightStyle) was removed as dead code.
| v2 method | 4.0.1 method | Notes |
|---|---|---|
initialize(options) | init(options) | Now returns Promise<MapInstance> — await it. |
set(options) | init(options) | v2 set() was an alias for initialize(). |
changeFloorById(id) | navigateToFloorById(id) | |
changeFloorByName(name) | navigateToFloorByName(name) | |
setMapView(view) | setViewport(view) | |
centerOnMapoverlay(id) | centerOnMapOverlay(id) | Casing fix: Mapoverlay → MapOverlay. |
setDefaultCustomRoutingConfig(cfg) | setDefaultRoutingConfig(cfg) | |
changeLanguage(code) | setLanguage(code) | |
addEventListener(event, fn) | on(event, fn) | on() returns an Unsubscribe function. Payloads are typed via discriminated overloads. |
removeEventListener(event, fn) | off(event, fn) | Also adds once() for one-shot listeners. |
highlightStyle() | (removed, no replacement) | v2 dead code — feature was never implemented. Always throws MAPSTED-1093 in 4.0.1. |
Plus one public-constant removal:
PRIVATE_CHILD_ACTION— was exposed in v2 and v3 by accident.4.0.1removes it from the public surface (CHL-27 security review). Remove the import; protocol handling is fully internal.
2. Event name normalization (WIRE_TO_PUBLIC_NAME)
v2 exposed two event names in the upstream wire-protocol casing (MAP_MOUNTED, IDLE_CHANGE). 4.0.1 normalizes these to camelCase public names. Normalization happens transparently inside the emitter, but handlers subscribed to the old wire names must rename:
| v2 / wire-protocol name | 4.0.1 public name |
|---|---|
MAP_MOUNTED | mapMounted |
IDLE_CHANGE | idleChange |
| (all other event names) | unchanged — already matched the current public names |
The authoritative source is src/events.ts — the WIRE_TO_PUBLIC_NAME export. The full list of the 32 known events is in the Events reference.
3. SecurityConfig — postMessage origin validation (new in 4.0.1)
v2 accepted postMessage events from any origin. 4.0.1 introduces a public SecurityConfig contract so you can configure a multi-origin whitelist declaratively at init() time.
ts
import {
init,
type SecurityConfig,
DEFAULT_SECURITY_CONFIG,
resolveConfig,
validateOriginWithConfig,
} from '@mapsted/maps-js-api';
// Option 1 — supply security config at init() (recommended for most apps)
await init({
mapsDomain: 'https://maps.mapsted.com',
propertyId: 603,
security: {
allowedOrigins: ['https://maps.mapsted.com'],
additionalOrigins: ['https://proxy.customer.com'],
strictMode: true,
},
});
// Option 2 — build your own validator (advanced consumers / custom integrations)
const cfg: SecurityConfig = resolveConfig({
allowedOrigins: ['https://maps.mapsted.com'],
strictMode: true,
});
window.addEventListener('message', (event) => {
if (!validateOriginWithConfig(event, cfg)) return;
// ... handle message ...
});Key behaviour changes you need to be aware of:
SecurityConfig.strictModedefaults totrue. Origin mismatches throwMapstedErrorwith codeMAPSTED-1303(ERR_SECURITY_CONFIG_ORIGIN_MISMATCH), not silent-discard. v2 consumers that relied on the forgiving default must either passsecurity.strictMode: falseexplicitly or whitelist the origins they actually need.allowedOriginsdefaults to[](deny-all). Callers must supply at least one origin. Forgetting to do so is caught at config-resolve time.additionalOriginsis a separate field for proxy setups, so you can extend the whitelist without reconstructingallowedOrigins.
Note on two strict modes.
SecurityConfig.strictModecontrols origin validation and istrueby default. A separate migration shim strict mode (set viasetStrictMode(true)orinit({ strict: true })) controls deprecated-method behaviour and isfalseby default. The two are independent.
See the full contract in the TypeDoc reference.
4. Other breaking changes
- TypeScript + npm.
4.0.1ships TypeScript types,.d.tsdeclarations, and anmapsted.mapsnpm package. v2 was CDN-only UMD. - Error codes. v2 threw
Errorwith arbitrary string messages.4.0.1introduces 34 structured codes in theMAPSTED-1xxxrange, all surfaced viaMapstedError(extendsErrorwith acodeproperty). See Error Codes. - Async contract.
4.0.1init()returnsPromise<MapInstance>. v2initialize()was sync-looking but race-prone — the change makes readiness explicit. elementstring semantics (CSS selector, not raw id). v2 and4.0.1both acceptelement: HTMLElement | stringas the init target. The string form changed meaning: in v2 examples, the string was interpreted as a raw element id (e.g.element: 'map'matched<div id="map">);4.0.1interprets the string as a CSS selector (element: '#map'). Update v2 code accordingly:element: 'map'→element: '#map'.- State API.
4.0.1addsgetState()andsubscribe(fn)for read-only state observation (getState()returns a Promise —awaitit). v2 had no equivalent. - New methods.
4.0.1addssetAccessibilityMode(mode)andsetStrictMode(enabled). For building and floor navigation, usenavigateToFloorById(id)/navigateToFloorByName(name).
5. Six-step upgrade checklist
- Update method names using the rename table in §1 above. The runtime shim warns on first call, so you can do this incrementally. Ten renames + one removal.
- Update
elementstring form to a CSS selector. v2initialize({ element: 'map' })(raw id) becomes4.0.1init({ element: '#map' })(CSS selector).HTMLElementreferences work unchanged. awaiteveryinit()call.init()returnsPromise<MapInstance>— capture the returned handle if you needdestroy()/isReady()/ state queries.- Wrap
init()+ command calls intry/catchand checkerr instanceof MapstedError. Map the codes you care about (MAPSTED-1001already-initialised,MAPSTED-1010missing propertyId,MAPSTED-1200invalid API key,MAPSTED-1303origin mismatch). - Migrate event handlers from
addEventListener/removeEventListenertoon/off/once. RenameMAP_MOUNTED→mapMountedandIDLE_CHANGE→idleChange. Remove everyhighlightStyle()call — use thehighlightfield onMapEntityobjects instead. - Configure
SecurityConfig. Addsecurity.allowedOriginsto yourinit()options. If you previously relied on the v2 no-op default, now is the time to audit which origins you actually accept postMessage from.
Optional: opt into migration strict mode during the upgrade (setStrictMode(true) or strict: true in init() options) to turn deprecation warnings into hard errors. This ensures you have no remaining v2 call sites before the shim is removed in a future major release.
6. Where to go next
- Embed your first map — 5-minute
4.0.1starter. - Complete API reference
- Error codes
- Changelog
- Legacy v2 API reference — historical v2 surface for lookup.
- v3 archive — if you happen to be on v3 instead of v2, the v3 → 4.0.1 migration is a shorter walk.