From 66464d6a2b1e8fa61678eb4e1fb59452df5681aa Mon Sep 17 00:00:00 2001 From: Kai Volland Date: Fri, 21 Jun 2024 10:32:07 +0200 Subject: [PATCH] fix: update versions endpoint --- src/index.ts | 8 ++--- src/routes/api.ts | 76 +++++++++++++++++++++++++++++---------------- src/routes/info.tsx | 60 +++++++++++++++++++++++++++++------ tsconfig.json | 2 +- 4 files changed, 103 insertions(+), 43 deletions(-) diff --git a/src/index.ts b/src/index.ts index 3c10186..9ae539e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,13 +3,13 @@ import { swagger } from '@elysiajs/swagger' import { html } from '@elysiajs/html' import { transFormApi, transform } from "./routes/api"; import loggisch from 'loggisch'; -import { versions } from "./routes/info"; +import { versions, versionsApi } from "./routes/info"; loggisch.setLogLevel('trace'); const port = process.env.NODE_API_PORT || 8888; -const app = new Elysia() +export const app = new Elysia() .use(swagger({ path: "/api-docs", exclude: ["/api-docs", "/api-docs/json"], @@ -23,7 +23,7 @@ const app = new Elysia() })) .group("/info", (app) => app .use(html()) - .get("/versions", versions) + .get("/versions", versions, versionsApi) ) .group("/api", (app) => app .post("/transform", transform, transFormApi) @@ -33,5 +33,3 @@ const app = new Elysia() console.log( `🦊 Elysia is running at http://${app.server?.hostname}:${app.server?.port}` ); - -export default app; diff --git a/src/routes/api.ts b/src/routes/api.ts index 84883b3..f5ba8cb 100644 --- a/src/routes/api.ts +++ b/src/routes/api.ts @@ -1,4 +1,7 @@ import { Handler, ParseError, t } from "elysia"; +import GeoStylerLyrxParser, { LyrxParser } from "geostyler-lyrx-parser"; +import MapboxStyleParser from "geostyler-mapbox-parser"; +import QGISStyleParser from "geostyler-qgis-parser"; // import LyrxParser from "geostyler-lyrx-parser"; import SldParser from "geostyler-sld-parser"; @@ -20,22 +23,38 @@ export const transFormApi = { body: t.Any({ description: 'The style to transform in the specified format', required: true, - example: { - "name": "My Style", - "rules": [ - { - "name": "My Rule", - "symbolizers": [ - { - "kind": "Mark", - "wellKnownName": "Circle", - "color": "#FF0000", - "radius": 6 - } - ] - } - ] - } + examples: [ + ` + + + My Style + + My Style + My Style + + + My Rule + + + + circle + + #FF0000 + + + 12 + + + + + + + ` + ] }), response: t.Any({ description: 'The transformed style in the specified format' @@ -47,9 +66,7 @@ export const transform: Handler = async ({ query: { sourceFormat, targetFormat } }) => { - const sourceStyle = body as string; - - if (!sourceStyle) { + if (!body) { log.error('Error: No source style style given in POST body.'); throw new ParseError('Error', 'No source style style given in POST body.'); } @@ -64,8 +81,12 @@ export const transform: Handler = async ({ let readResponse; + // TODO: type should be fixed here + let sourceStyle = body as any; + + // if no sourceParser is given we expect the body to be a geostyler-style if (sourceParser === undefined) { - readResponse = { output: JSON.parse(sourceStyle) }; + readResponse = { output: sourceStyle }; } else { readResponse = await sourceParser.readStyle(sourceStyle); if (Array.isArray(readResponse.errors) && readResponse.errors.length) { @@ -74,6 +95,7 @@ export const transform: Handler = async ({ } } + // if no targetParser is given we return a geostyler-style if (targetParser === undefined) { return readResponse.output; } @@ -104,7 +126,7 @@ export const transform: Handler = async ({ * @param paramVal Query param value for the format, e.g. 'qml' * @returns Content-Type */ -const getContentTypeFromUrlParam = (paramVal: string) => { +const getContentTypeFromParserName = (paramVal: string) => { if (!paramVal) { return undefined; } @@ -135,16 +157,16 @@ const getParserFromUrlParam = (paramVal: string) => { } switch (paramVal.toLowerCase()) { - // case 'lyrx': - // return new LyrxParser(); - // case 'mapbox': - // return new MapboxParser(); + case 'lyrx': + return new LyrxParser(); + case 'mapbox': + return new MapboxStyleParser(); // case 'mapserver': // return new MapfileParser(); case 'sld': return new SldParser(); - // case 'qml': - // return new QgisParser(); + case 'qml': + return new QGISStyleParser(); case 'geostyler': default: return undefined; diff --git a/src/routes/info.tsx b/src/routes/info.tsx index 99ae391..99fd1e9 100644 --- a/src/routes/info.tsx +++ b/src/routes/info.tsx @@ -1,23 +1,63 @@ -import { html } from '@elysiajs/html' -import { Handler } from 'elysia'; +// import { html } from '@elysiajs/html' +import { Handler, t } from 'elysia'; +import { + dependencies as deps, + version +} from '../../package.json'; + +export const versionsApi = { + response: t.Any({ + description: 'Information about the installed versions of the GeoStyler parsers.' + + ' If the `Accept` header is set to `application/json`, the response will be a JSON object. ' + + 'Otherwise, the response will be an HTML page.' + }) +}; export const versions: Handler = ({ request }) => { - if (request.headers.get('accept') === 'text/html') { + if (request.headers.get('accept') === 'application/json') { + return { + 'geostyler-rest': version, + 'geostyler-mapbox-parser': getVersionString('geostyler-mapbox-parser'), + 'geostyler-mapfile-parser': getVersionString('geostyler-mapfile-parser'), + 'geostyler-qgis-parser': getVersionString('geostyler-qgis-parser'), + 'geostyler-sld-parser': getVersionString('geostyler-sld-parser'), + 'geostyler-arcgis-parser': getVersionString('geostyler-lyrx-parser') + }; + } else { return ( - Hello World + GeoStyler REST API Versions -

Hello World

- + GeoStyler REST version {version} + + ); - } else { - return { - peter: 'pan' - }; } }; + +const getVersionString = (parserName: string) => { + return deps[parserName as keyof typeof deps] + ? `${deps[parserName as keyof typeof deps]}` + : 'not installed'; +} diff --git a/tsconfig.json b/tsconfig.json index 22de8e9..bf30490 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -38,7 +38,7 @@ "types": ["bun-types"], /* Specify type package names to be included without being referenced in a source file. */ // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */ - // "resolveJsonModule": true, /* Enable importing .json files. */ + "resolveJsonModule": true, /* Enable importing .json files. */ // "noResolve": true, /* Disallow 'import's, 'require's or ''s from expanding the number of files TypeScript should add to a project. */ /* JavaScript Support */