diff --git a/packages/server/src/route.link.ts b/packages/server/src/route.link.ts index 652711970..b57053af3 100644 --- a/packages/server/src/route.link.ts +++ b/packages/server/src/route.link.ts @@ -1,27 +1,31 @@ -import { BasemapsConfigProvider, getAllImagery, } from '@basemaps/config'; +import { BasemapsConfigProvider, getAllImagery } from '@basemaps/config'; import { Epsg } from '@basemaps/geo'; import { getPreviewUrl } from '@basemaps/shared'; /** * If a tileset layer of the given id and projection exists, this function returns a Basemaps URL * path that is already zoomed to the extent of the tileset. Otherwise, this function returns null. - * + * * @param id - The id of the tileset. * @example "ashburton-2023-0.1m" - * + * * @param projection - The projection from the tileset's layers to consider. * @example Epsg.Google = 3857 */ -export async function getTileSetPath(cfg: BasemapsConfigProvider, id: string, projection: Epsg): Promise { - const tileSet = await cfg.TileSet.get(id) - if (!tileSet) return null +export async function getTileSetPath( + cfg: BasemapsConfigProvider, + id: string, + projection: Epsg, +): Promise { + const tileSet = await cfg.TileSet.get(id); + if (!tileSet) return null; - const imageryMap = await getAllImagery(cfg, tileSet.layers, [projection]); + const imageryMap = await getAllImagery(cfg, tileSet.layers, [projection]); - const [_, imagery] = Array.from(imageryMap)[0] - if (!imagery) return null + const imagery = [...imageryMap.values()][0]; + if (!imagery) return null; - const url = getPreviewUrl({ imagery }) - - return (`${url.slug}?i=${url.name}`) + const url = getPreviewUrl({ imagery }); + + return `${url.slug}?i=${url.name}`; } diff --git a/packages/server/src/server.ts b/packages/server/src/server.ts index 29a032da9..52549caae 100644 --- a/packages/server/src/server.ts +++ b/packages/server/src/server.ts @@ -99,24 +99,27 @@ export async function createServer(opts: ServerOptions, logger: LogType): Promis /** * @route GET /link/:tileSetId - * + * * @example /link/ashburton-2023-0.1m - * + * * @description If a tileset layer of the given id and 3857 projection exists, the server redirects the client to a Basemaps * URL that is already zoomed to the extent of the layer. Otherwise, the server returns an 'HTTP 404 Not found' status code. - * + * */ - BasemapsServer.get('/link/:tileSetId', async (req: FastifyRequest<{ Params: { tileSetId: string } }>, res: FastifyReply) => { - const { tileSetId } = req.params - - const path = await getTileSetPath(cfg, tileSetId, Epsg.Google); - - if (path) { - res.redirect(`/${path}`) - } else { - res.status(404) - } - }); + BasemapsServer.get( + '/link/:tileSetId', + async (req: FastifyRequest<{ Params: { tileSetId: string } }>, res: FastifyReply) => { + const { tileSetId } = req.params; + + const path = await getTileSetPath(cfg, tileSetId, Epsg.Google); + + if (path) { + res.redirect(`/${path}`); + } else { + res.status(404); + } + }, + ); return BasemapsServer; }