Download src/lib/grabber/search.functions.ts from launch-calcium/Grabber-In-Tanstack-CPU: direct link, hf CLI and curl.
- Browser
- Download file 4.33 kB
-
https://huggingface.co/spaces/launch-calcium/Grabber-In-Tanstack-CPU/resolve/main/src/lib/grabber/search.functions.ts
- Command line
-
hf download hf://spaces/launch-calcium/Grabber-In-Tanstack-CPU/src/lib/grabber/search.functions.ts
-
curl -L -o search.functions.ts https://huggingface.co/spaces/launch-calcium/Grabber-In-Tanstack-CPU/resolve/main/src/lib/grabber/search.functions.ts
4.33 kB
| import { createServerFn } from "@tanstack/react-start"; | |
| import { z } from "zod"; | |
| import { normalizePost, getByPath, type NormalizedPost, type SiteConfig } from "./sites"; | |
| const SiteSchema: z.ZodType<SiteConfig> = z.object({ | |
| id: z.string(), | |
| name: z.string(), | |
| baseUrl: z.string().url(), | |
| searchPath: z.string(), | |
| params: z.object({ tags: z.string(), page: z.string(), limit: z.string() }), | |
| postsPath: z.string(), | |
| fields: z.object({ | |
| id: z.string(), | |
| previewUrl: z.string(), | |
| fileUrl: z.string(), | |
| ext: z.string(), | |
| tags: z.string(), | |
| source: z.string().optional(), | |
| rating: z.string().optional(), | |
| score: z.string().optional(), | |
| sampleUrl: z.string().optional(), | |
| fileSize: z.string().optional(), | |
| width: z.string().optional(), | |
| height: z.string().optional(), | |
| md5: z.string().optional(), | |
| uploader: z.string().optional(), | |
| createdAt: z.string().optional(), | |
| }), | |
| headers: z.record(z.string(), z.string()).optional(), | |
| pageStartsAt: z.number().optional(), | |
| }); | |
| const InputSchema = z.object({ | |
| site: SiteSchema, | |
| tags: z.string().default(""), | |
| page: z.number().int().min(1).default(1), | |
| limit: z.number().int().min(1).max(69426734621).default(40), | |
| login: z.string().optional(), | |
| apiKey: z.string().optional(), | |
| }); | |
| export type SearchResult = { | |
| posts: NormalizedPost[]; | |
| error?: string; | |
| }; | |
| export const searchPosts = createServerFn({ method: "POST" }) | |
| .validator((input: unknown) => InputSchema.parse(input)) | |
| .handler(async ({ data }): Promise<SearchResult> => { | |
| const { site, tags, page, limit, login, apiKey } = data; | |
| const pageParam = (site.pageStartsAt ?? 1) === 0 ? page - 1 : page; | |
| const url = new URL(site.searchPath, site.baseUrl); | |
| url.searchParams.set(site.params.tags, tags); | |
| url.searchParams.set(site.params.page, String(pageParam)); | |
| url.searchParams.set(site.params.limit, String(limit)); | |
| if (login) { | |
| url.searchParams.set("login", login); | |
| } | |
| if (apiKey) { | |
| url.searchParams.set("api_key", apiKey); | |
| } | |
| const tag = `[searchPosts:${site.id}]`; | |
| const safeUrl = url.toString().replace(/api_key=[^&]+/, "api_key=***"); | |
| console.log(`${tag} GET ${safeUrl}`); | |
| try { | |
| const res = await fetch(url.toString(), { | |
| headers: { | |
| Accept: "application/json", | |
| "User-Agent": "LovableGrabber/1.0", | |
| ...(site.headers ?? {}), | |
| }, | |
| }); | |
| console.log(`${tag} status ${res.status} ${res.headers.get("content-type") ?? ""}`); | |
| if (!res.ok) { | |
| const body = await res.text().catch(() => ""); | |
| console.error(`${tag} error body: ${body.slice(0, 500)}`); | |
| return { posts: [], error: `${site.name} returned ${res.status}` }; | |
| } | |
| let json: unknown; | |
| try { | |
| json = await res.json(); | |
| } catch (e) { | |
| console.error(`${tag} invalid JSON`, e); | |
| return { posts: [], error: `${site.name} did not return JSON` }; | |
| } | |
| let arr = getByPath(json, site.postsPath); | |
| // Sites (like e621 in Nov 2026) may change between {posts:[...]} and a bare array. | |
| if (!Array.isArray(arr) && Array.isArray(json)) { | |
| console.warn(`${tag} "${site.postsPath}" not an array; using root array`); | |
| arr = json; | |
| } | |
| if (!Array.isArray(arr) && json && typeof json === "object" && Array.isArray((json as { posts?: unknown }).posts)) { | |
| console.warn(`${tag} using "posts" fallback`); | |
| arr = (json as { posts: unknown[] }).posts; | |
| } | |
| if (!Array.isArray(arr)) { | |
| console.error(`${tag} no posts array; top-level keys: ${json && typeof json === "object" ? Object.keys(json).join(",") : typeof json}`); | |
| return { posts: [], error: `Response at "${site.postsPath}" is not an array` }; | |
| } | |
| const posts = arr | |
| .map((raw) => normalizePost(raw, site.fields)) | |
| .filter((p): p is NormalizedPost => p !== null); | |
| console.log(`${tag} ${arr.length} raw -> ${posts.length} usable posts`); | |
| if (arr.length && !posts.length) console.warn(`${tag} field mapping matched nothing; sample keys: ${Object.keys(arr[0] ?? {}).join(",")}`); | |
| return { posts }; | |
| } catch (err) { | |
| console.error(`${tag} request failed`, err); | |
| return { posts: [], error: err instanceof Error ? err.message : "Request failed" }; | |
| } | |
| }); | |