All files / api/routes search.ts

88.88% Statements 16/18
80% Branches 8/10
100% Functions 2/2
93.75% Lines 15/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32            357x   357x 6x 6x   4x   4x 1x 1x       4x 4x 4x   4x 4x 4x     357x    
import type Database from "better-sqlite3-multiple-ciphers";
import { Hono } from "hono";
import { MessageSearch } from "../../search/search.js";
import { parsePagination } from "../validation.js";
 
export function searchRoutes(getDb: () => Database.Database): Hono {
	const api = new Hono();
 
	api.get("/", (c) => {
		const query = c.req.query("q");
		if (!query) return c.json({ error: "Query parameter 'q' is required" }, 400);
 
		const rawConnectorId = c.req.query("inbound_connector_id");
		let inboundConnectorId: number | undefined;
		if (rawConnectorId !== undefined) {
			inboundConnectorId = Number(rawConnectorId);
			Iif (!Number.isFinite(inboundConnectorId) || inboundConnectorId < 1) {
				return c.json({ error: "Invalid inbound_connector_id: must be a positive integer" }, 400);
			}
		}
		const pagination = parsePagination(c);
		Iif (pagination instanceof Response) return pagination;
		const { limit, offset } = pagination;
 
		const search = new MessageSearch(getDb());
		const results = search.search(query, { inboundConnectorId, limit, offset });
		return c.json(results);
	});
 
	return api;
}