Skip to content

Commit

Permalink
refactor(server): correct eslint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tawera-manaena committed Sep 30, 2024
1 parent d4546fb commit 40056fb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 26 deletions.
28 changes: 16 additions & 12 deletions packages/server/src/route.link.ts
Original file line number Diff line number Diff line change
@@ -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<string | null> {
const tileSet = await cfg.TileSet.get(id)
if (!tileSet) return null
export async function getTileSetPath(
cfg: BasemapsConfigProvider,
id: string,
projection: Epsg,
): Promise<string | null> {
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}`;
}
31 changes: 17 additions & 14 deletions packages/server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 40056fb

Please sign in to comment.