mcp and content fix
This commit is contained in:
@@ -2,6 +2,7 @@ import type { BasePayload } from 'payload'
|
||||
|
||||
import type { I18nConfig } from '../i18n/index.js'
|
||||
|
||||
import { checkRateLimit, clientIp, tooManyRequests } from '../shared/rateLimit.js'
|
||||
import { searchContent } from './searchContent.js'
|
||||
|
||||
type CreateSearchRouteArgs = {
|
||||
@@ -15,6 +16,11 @@ type CreateSearchRouteArgs = {
|
||||
getCachedPayload: () => Promise<BasePayload>
|
||||
/** Home slug map/string so the home result collapses to root. */
|
||||
homeSlug?: Record<string, string> | string
|
||||
/**
|
||||
* Per-IP limit (in memory, per instance). Default { max: 120, windowMs: 60_000 }.
|
||||
* `false` = off (a limiter in front, e.g. Cloudflare WAF rate limiting).
|
||||
*/
|
||||
rateLimit?: { max?: number; windowMs?: number } | false
|
||||
/** Field holding a richer AI summary (buildSummaryField) to prefer as description. */
|
||||
summaryField?: string
|
||||
}
|
||||
@@ -37,20 +43,18 @@ type CreateSearchRouteArgs = {
|
||||
*
|
||||
* Uses the global Request/Response — no next/server import.
|
||||
*/
|
||||
export function createSearchRoute(
|
||||
args: CreateSearchRouteArgs,
|
||||
): (req: Request) => Promise<Response> {
|
||||
const {
|
||||
baseUrl,
|
||||
collection = 'pages',
|
||||
config,
|
||||
defaultLimit = 10,
|
||||
getCachedPayload,
|
||||
homeSlug,
|
||||
summaryField,
|
||||
} = args
|
||||
export function createSearchRoute(args: CreateSearchRouteArgs): (req: Request) => Promise<Response> {
|
||||
const { baseUrl, collection = 'pages', config, defaultLimit = 10, getCachedPayload, homeSlug, summaryField } = args
|
||||
// 120/min: a debounced search-as-you-type box stays well under it; a scraper doesn't.
|
||||
const rl = args.rateLimit === false ? null : { max: 120, windowMs: 60_000, ...args.rateLimit }
|
||||
|
||||
return async (req: Request) => {
|
||||
if (rl) {
|
||||
const ip = clientIp(req)
|
||||
if (!checkRateLimit({ key: ip, scope: 'search', ...rl })) {
|
||||
return tooManyRequests(ip, 'search', { count: 0, error: 'Too many requests', query: '', results: [] })
|
||||
}
|
||||
}
|
||||
const url = new URL(req.url)
|
||||
const query = url.searchParams.get('q')?.trim() ?? ''
|
||||
const locale = url.searchParams.get('locale') ?? undefined
|
||||
@@ -79,11 +83,7 @@ export function createSearchRoute(
|
||||
})
|
||||
return Response.json(
|
||||
{ count: results.length, locale: locale ?? config.defaultLocale, query, results },
|
||||
{
|
||||
headers: {
|
||||
'cache-control': 'public, max-age=0, s-maxage=60, stale-while-revalidate=600',
|
||||
},
|
||||
},
|
||||
{ headers: { 'cache-control': 'public, max-age=0, s-maxage=60, stale-while-revalidate=600' } },
|
||||
)
|
||||
} catch (err) {
|
||||
// DB unreachable (build) or unexpected — empty result rather than 500.
|
||||
|
||||
Reference in New Issue
Block a user