Skip to content

Commit

Permalink
refactor!: move import progress from HTTP API to JS API
Browse files Browse the repository at this point in the history
This is the next step in [a larger refactor][0] where we replace the
HTTP API with a JS one.

[0]: #111

BREAKING CHANGE: `GET /imports/progress/:importId` replaced with
  `getImportProgress()`
  • Loading branch information
EvanHahn committed Feb 27, 2024
1 parent 0d4ebeb commit 4e062df
Show file tree
Hide file tree
Showing 17 changed files with 367 additions and 576 deletions.
47 changes: 0 additions & 47 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,50 +162,3 @@ As of now, only [MBTiles](https://github.com/mapbox/mbtiles-spec) files are supp
- `y: number`: The y coordinate of the tile.

Retrieve a tile for a given tileset. Note that this is usually used by a map client (based on a style definition) and not directly by the end user ([more info](https://docs.mapbox.com/mapbox-gl-js/style-spec/sources/)).

---

## Imports

### `GET /imports/progress/:importId`

- Params:
- `importId: string`: The ID for the desired import.

Subscribe to progress information for an import. This is a [Server-Sent Events (SSE)](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events) endpoint, so it's expected to be used with an [EventSource](https://developer.mozilla.org/en-US/docs/Web/API/EventSource) by the client.

Messages that are received will have a `data` field with the following structure when deserialized:

- `type: string`: Type indicating the type of progress message. Can be one of the following values:
- `"progress"`: Import is still in progress
- `"complete"`: Import is complete
- `"error"`: Error occurred during import
- `importId: string`: ID for import
- `soFar: number`: Number of assets successfully imported so far
- `total: number`: Total number of assets to be imported

If a requested import is already completed or has errored, responds with `204 No Content`, which should prevent the event source from attempting to reconnect. Generally, the client should explicitly close the event source when:

1. Receiving a message and the deserialized `type` value in the event data is either `"complete"` or `"error"`.
2. Receiving an error

```js
const evtSource = new EventSource(
'http://localhost:3000/imports/progress/some-import-id'
)

evtSource.onmessage = (event) => {
const message = JSON.parse(event.data)

if (message.type === 'complete' || message.type === 'error') {
evtSource.close()
return
}

// Do something with message...
}

evtSource.onerror = (event) => {
evtSource.close()
}
```
158 changes: 83 additions & 75 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"fastify-plugin": "^3.0.1",
"got": "^11.8.5",
"is-url": "^1.2.4",
"iterpal": "^0.3.0",
"make-promises-safe": "^5.1.0",
"mem": "^8.1.1",
"piscina": "^3.2.0",
Expand Down Expand Up @@ -91,7 +92,7 @@
"prisma-erd-generator": "^1.2.3",
"standard-version": "^9.5.0",
"tap-arc": "^0.3.5",
"tape": "^5.7.5",
"tape": "^5.7.4",
"tmp": "^0.2.1",
"ts-node": "^9.1.1",
"ts-node-dev": "^1.1.8",
Expand Down
Loading

0 comments on commit 4e062df

Please sign in to comment.