diff --git a/plugins/search-backend/src/service/router.ts b/plugins/search-backend/src/service/router.ts index 046d9d27b3..02bc553bb9 100644 --- a/plugins/search-backend/src/service/router.ts +++ b/plugins/search-backend/src/service/router.ts @@ -14,9 +14,11 @@ * limitations under the License. */ +import { errorHandler } from '@backstage/backend-common'; import express from 'express'; import Router from 'express-promise-router'; import { Logger } from 'winston'; +import { SearchQuery, SearchResultSet } from '../types'; type RouterOptions = { logger: Logger; @@ -27,21 +29,33 @@ export async function createRouter({ }: RouterOptions): Promise { const router = Router(); - router.get('/query', async (req, res) => { - // Transform req.params into search engine specific query. - const { term } = req.query; + router.get( + '/query', + async ( + req: express.Request, + res: express.Response, + ) => { + // @todo Actually transform req.params into search engine specific query. + const { term, filters = {}, pageIndex = 0, pageSize = 30 } = req.query; + logger.info( + `Search requested received: ${term}, ${JSON.stringify( + filters, + )}, ${pageIndex}, ${pageSize}`, + ); - try { - // Query search engine. - // Transform results into frontend-readable result - res.send({ - term, - }); - } catch (err) { - logger.error(`There was a problem performing the search query.`); - res.status(500).send(`There was a problem performing the search query.`); - } - }); + try { + // @todo Actually query search engine. + // @todo And actually transform results into frontend-readable result + res.send({ + results: [], + }); + } catch (err) { + logger.error(`There was a problem performing the search query.`); + throw new Error(`There was a problem performing the search query.`); + } + }, + ); + router.use(errorHandler()); return router; } diff --git a/plugins/search-backend/src/types.ts b/plugins/search-backend/src/types.ts new file mode 100644 index 0000000000..79b72ad80a --- /dev/null +++ b/plugins/search-backend/src/types.ts @@ -0,0 +1,26 @@ +/* + * Copyright 2021 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export interface SearchQuery { + term: string; + filters?: Record; + pageIndex?: number; + pageSize?: number; +} + +export interface SearchResultSet { + results: any[]; // @todo Use IndexableDocument from search-indexer-backend? +}