Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/migrate-downloaded-styles.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'styled-map-package-api': patch
---

Fix: automatically migrate v7 styles to v8 when downloading or constructing a StyleDownloader, instead of rejecting them as invalid.
20 changes: 11 additions & 9 deletions packages/api/lib/style-downloader.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { migrate } from '@maplibre/maplibre-gl-style-spec'
import { check as checkGeoJson } from '@placemarkio/check-geojson'
import { includeKeys } from 'filter-obj'
import ky from 'ky'
Expand Down Expand Up @@ -60,10 +61,13 @@ export class StyleDownloader {
this.#mapboxAccessToken =
searchParams.get('access_token') || mapboxAccessToken
this.#styleURL = normalizeStyleURL(style, this.#mapboxAccessToken)
} else if (validateStyle(style)) {
this.#inputStyle = clone(style)
} else {
throw new AggregateError(validateStyle.errors, 'Invalid style')
// Migrate actually mutates the input, so we act on a clone.
const styleV8 = migrate(clone(style))
if (!validateStyle(styleV8)) {
throw new AggregateError(validateStyle.errors, 'Invalid style')
}
this.#inputStyle = styleV8
}
this.#fetchQueue = new FetchQueue(concurrency)
}
Expand All @@ -83,13 +87,11 @@ export class StyleDownloader {
async getStyle() {
if (!this.#inputStyle && this.#styleURL) {
const downloadedStyle = await ky(this.#styleURL).json()
if (!validateStyle(downloadedStyle)) {
throw new AggregateError(
validateStyle.errors,
'Invalid style: ' + this.#styleURL,
)
const styleV8 = migrate(downloadedStyle)
if (!validateStyle(styleV8)) {
throw new AggregateError(validateStyle.errors, 'Invalid style')
}
this.#inputStyle = downloadedStyle
this.#inputStyle = styleV8
} else if (!this.#inputStyle) {
throw new Error('Unexpected state: no style or style URL provided')
}
Expand Down
Loading