Quality / test (push) Canceled after 0s
Add strict public search numeric validation and URL-backed category and price filters to the catalog UI.
39 lines
1.4 KiB
JavaScript
39 lines
1.4 KiB
JavaScript
import { createApplicationError } from '../../../shared/errors/application-error.js';
|
|
|
|
function coerceNumber(value, fieldName, fallback) {
|
|
if (value === undefined || value === null || value === '') {
|
|
return fallback;
|
|
}
|
|
|
|
const parsed = Number.parseInt(String(value), 10);
|
|
if (Number.isNaN(parsed) || String(parsed) !== String(value)) {
|
|
throw createApplicationError('INVALID_SEARCH_FILTER', `${fieldName} must be an integer.`, 400);
|
|
}
|
|
return parsed;
|
|
}
|
|
|
|
export async function registerPublicSearchRoutes(app, options) {
|
|
const { searchService } = options;
|
|
|
|
app.get('/public/search', async (request) => {
|
|
if (!searchService || typeof searchService.searchPublic !== 'function') {
|
|
throw createApplicationError(
|
|
'SEARCH_SERVICE_UNAVAILABLE',
|
|
'Public search is currently unavailable.',
|
|
503,
|
|
);
|
|
}
|
|
|
|
const filters = {
|
|
query: request.query.query,
|
|
category: request.query.category,
|
|
minPriceMinor: coerceNumber(request.query.minPriceMinor, 'minPriceMinor', undefined),
|
|
maxPriceMinor: coerceNumber(request.query.maxPriceMinor, 'maxPriceMinor', undefined),
|
|
page: coerceNumber(request.query.page, 'page', 1),
|
|
limit: coerceNumber(request.query.limit, 'limit', 20),
|
|
includeSold: request.query.includeSold === 'true' || request.query.includeSold === true,
|
|
};
|
|
|
|
return { data: await searchService.searchPublic(filters) };
|
|
});
|
|
} |