Skip to content

Commit bd4b5b6

Browse files
authored
fix: Export correct MapInfoResponse type (#34)
1 parent ae7bc34 commit bd4b5b6

4 files changed

Lines changed: 39 additions & 19 deletions

File tree

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export { CUSTOM_MAP_ID, DEFAULT_MAP_ID } from './lib/constants.js'
2020

2121
export type {
2222
MapInfo,
23+
MapInfoResponse,
2324
MapShareState,
2425
MapShareStateUpdate,
2526
DownloadStateUpdate,

src/routes/maps.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from '../lib/constants.js'
1111
import { errors } from '../lib/errors.js'
1212
import { addTrailingSlash, noop } from '../lib/utils.js'
13+
import type { MapInfoResponse } from '../types.js'
1314

1415
type MapRequest = IRequestStrict & {
1516
params: {
@@ -27,14 +28,17 @@ export function MapsRouter({ base = '/' }, ctx: Context) {
2728

2829
const router = IttyRouter<IRequestStrict>({ base })
2930

30-
router.get<MapRequest>(`/:mapId/info`, async (request) => {
31-
const info = await ctx.getMapInfo(request.params.mapId)
32-
return {
33-
created: info.mapCreatedAt,
34-
size: info.estimatedSizeBytes,
35-
name: info.mapName,
36-
}
37-
})
31+
router.get<MapRequest>(
32+
`/:mapId/info`,
33+
async (request): Promise<MapInfoResponse> => {
34+
const info = await ctx.getMapInfo(request.params.mapId)
35+
return {
36+
created: info.mapCreatedAt,
37+
size: info.estimatedSizeBytes,
38+
name: info.mapName,
39+
}
40+
},
41+
)
3842

3943
const uploadHandler: RequestHandler<MapRequest> = async (request) => {
4044
const writable = ctx.createMapWritableStream(request.params.mapId)

src/types.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,16 @@ const MapInfo = T.Object({
105105
}),
106106
})
107107

108+
export const MapInfoResponse = T.Object({
109+
created: T.Number({
110+
description: 'Timestamp (ms since epoch) when the map was created',
111+
}),
112+
size: T.Number({ description: 'Estimated size of the map data in bytes' }),
113+
name: T.String({ description: 'The name of the map' }),
114+
})
115+
116+
export type MapInfoResponse = Static<typeof MapInfoResponse>
117+
108118
const MapShareBase = T.Intersect([
109119
T.Object({
110120
receiverDeviceId: T.String({
@@ -126,6 +136,9 @@ export type MapShareState = DistributiveIntersection<
126136
Static<typeof MapShareStateUpdate>
127137
>
128138

139+
/**
140+
* @deprecated We don't yet return this type from any API, and it may be removed in a future release. Use MapInfoResponse instead.
141+
*/
129142
export type MapInfo = Static<typeof MapInfo>
130143

131144
export type FetchContext = {

test/maps.test.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import { setTimeout as delay } from 'node:timers/promises'
66
import { fileURLToPath } from 'node:url'
77

88
import { Reader } from 'styled-map-package'
9+
import Value from 'typebox/value'
910
import { describe, it, expect, vi } from 'vitest'
1011

12+
import { MapInfoResponse } from '../src/types.js'
1113
import {
1214
DEMOTILES_Z2,
1315
goOffline,
@@ -60,6 +62,7 @@ describe('Maps API', () => {
6062
const response = await fetch(`${localBaseUrl}/maps/custom/info`)
6163
expect(response.status).toBe(200)
6264
const info = await response.json()
65+
expect(Value.Check(MapInfoResponse, info)).toBe(true)
6366
expect(info).toHaveProperty('created')
6467
expect(info).toHaveProperty('size')
6568
expect(info).toHaveProperty('name')
@@ -73,6 +76,7 @@ describe('Maps API', () => {
7376
const response = await fetch(`${localBaseUrl}/maps/fallback/info`)
7477
expect(response.status).toBe(200)
7578
const info = await response.json()
79+
expect(Value.Check(MapInfoResponse, info)).toBe(true)
7680
expect(info).toHaveProperty('created')
7781
expect(info).toHaveProperty('size')
7882
expect(info).toHaveProperty('name')
@@ -580,8 +584,9 @@ describe('Map Upload', () => {
580584
// Mock fs.createWriteStream to return a writable that errors on write,
581585
// simulating a disk-full scenario
582586
const originalCreateWriteStream = fs.createWriteStream
583-
const spy = vi.spyOn(fs, 'createWriteStream').mockImplementation(
584-
(...args: any[]) => {
587+
const spy = vi
588+
.spyOn(fs, 'createWriteStream')
589+
.mockImplementation((...args: any[]) => {
585590
const stream = originalCreateWriteStream.apply(fs, args as any)
586591
stream._write = (
587592
_chunk: any,
@@ -591,8 +596,7 @@ describe('Map Upload', () => {
591596
callback(new Error('ENOSPC: no space left on device'))
592597
}
593598
return stream
594-
},
595-
)
599+
})
596600
t.onTestFinished(() => spy.mockRestore())
597601

598602
const fileBuffer = fs.readFileSync(OSM_BRIGHT_Z6)
@@ -627,17 +631,15 @@ describe('Map Upload', () => {
627631
// Mock fs.createWriteStream to return a writable that writes successfully
628632
// but errors on close (_final), simulating a flush/sync failure
629633
const originalCreateWriteStream = fs.createWriteStream
630-
const spy = vi.spyOn(fs, 'createWriteStream').mockImplementation(
631-
(...args: any[]) => {
634+
const spy = vi
635+
.spyOn(fs, 'createWriteStream')
636+
.mockImplementation((...args: any[]) => {
632637
const stream = originalCreateWriteStream.apply(fs, args as any)
633-
stream._final = (
634-
callback: (error?: Error | null) => void,
635-
) => {
638+
stream._final = (callback: (error?: Error | null) => void) => {
636639
callback(new Error('EIO: i/o error'))
637640
}
638641
return stream
639-
},
640-
)
642+
})
641643
t.onTestFinished(() => spy.mockRestore())
642644

643645
const fileBuffer = fs.readFileSync(OSM_BRIGHT_Z6)

0 commit comments

Comments
 (0)