|
1 | 1 | import { NextResponse } from "next/server"; |
2 | 2 |
|
3 | | -import { searchPosts } from "@/apis/search"; |
4 | | -import { SearchResponse } from "@/interfaces/search"; |
| 3 | +import { SearchResponse, SearchResult } from "@/interfaces/search"; |
| 4 | +import { blogClient } from "@/lib/api-client"; |
5 | 5 |
|
6 | 6 | const MIN_QUERY_LENGTH = 1; |
7 | 7 | const DEFAULT_LIMIT = 20; |
8 | 8 | const MAX_LIMIT = 50; |
| 9 | +const DEFAULT_SNIPPET_LENGTH = 180; |
| 10 | + |
| 11 | +function toStringValue(value: unknown) { |
| 12 | + return typeof value === "string" ? value : ""; |
| 13 | +} |
| 14 | + |
| 15 | +function toNumberValue(value: unknown, fallback = 0) { |
| 16 | + return typeof value === "number" && Number.isFinite(value) ? value : fallback; |
| 17 | +} |
| 18 | + |
| 19 | +function toStringArray(value: unknown) { |
| 20 | + if (!Array.isArray(value)) { |
| 21 | + return []; |
| 22 | + } |
| 23 | + |
| 24 | + return value.filter((item): item is string => typeof item === "string"); |
| 25 | +} |
| 26 | + |
| 27 | +function getMinRead(content: string, fallback = 1) { |
| 28 | + if (!content.trim()) { |
| 29 | + return fallback; |
| 30 | + } |
| 31 | + |
| 32 | + const wordsPerMinute = 200; |
| 33 | + const wordCount = content.trim().split(/\s+/).filter(Boolean).length; |
| 34 | + return Math.max(1, Math.round(wordCount / wordsPerMinute)); |
| 35 | +} |
| 36 | + |
| 37 | +function getSnippetFromContent(content: string, fallback: string) { |
| 38 | + const baseText = content.trim() || fallback; |
| 39 | + if (!baseText) { |
| 40 | + return ""; |
| 41 | + } |
| 42 | + |
| 43 | + return baseText.slice(0, DEFAULT_SNIPPET_LENGTH); |
| 44 | +} |
| 45 | + |
| 46 | +function normalizeSearchResults(rawResults: unknown[]): SearchResult[] { |
| 47 | + return rawResults.map((raw, index) => { |
| 48 | + const item = (raw ?? {}) as Record<string, unknown>; |
| 49 | + const title = toStringValue(item.title); |
| 50 | + const description = toStringValue(item.description); |
| 51 | + const content = toStringValue(item.content); |
| 52 | + const date = |
| 53 | + toStringValue(item.date) || |
| 54 | + toStringValue(item.createdAt) || |
| 55 | + new Date().toISOString(); |
| 56 | + |
| 57 | + return { |
| 58 | + id: toNumberValue(item.id, index + 1), |
| 59 | + title, |
| 60 | + description, |
| 61 | + category: toStringValue(item.category), |
| 62 | + slug: toStringValue(item.slug), |
| 63 | + date, |
| 64 | + minRead: toNumberValue(item.minRead, getMinRead(content)), |
| 65 | + tags: toStringArray(item.tags), |
| 66 | + snippet: |
| 67 | + toStringValue(item.snippet) || |
| 68 | + getSnippetFromContent(content, description), |
| 69 | + matchedFields: toStringArray(item.matchedFields).filter( |
| 70 | + (field): field is SearchResult["matchedFields"][number] => |
| 71 | + field === "title" || field === "tags" || field === "body", |
| 72 | + ), |
| 73 | + }; |
| 74 | + }); |
| 75 | +} |
9 | 76 |
|
10 | 77 | function getLimit(limitQuery: string | null) { |
11 | 78 | if (!limitQuery) { |
@@ -36,10 +103,12 @@ export async function GET(request: Request) { |
36 | 103 | return NextResponse.json(emptyResponse); |
37 | 104 | } |
38 | 105 |
|
39 | | - const results = searchPosts(query, limit); |
| 106 | + const upstream = await blogClient.search({ q: query, limit }); |
| 107 | + const rawResults = Array.isArray(upstream.results) ? upstream.results : []; |
| 108 | + const results = normalizeSearchResults(rawResults); |
40 | 109 | const response: SearchResponse = { |
41 | | - query, |
42 | | - total: results.length, |
| 110 | + query: toStringValue(upstream.query) || query, |
| 111 | + total: toNumberValue(upstream.total, results.length), |
43 | 112 | results, |
44 | 113 | }; |
45 | 114 |
|
|
0 commit comments