import {z} from 'zod' export type GetApiV1GoogleImagesSearchArgs = {q: string, gl?: string | undefined, hl?: string | undefined, tbs?: (string | null) | undefined, imgsz?: (string | null) | undefined, imgcolor?: (string | null) | undefined, imgtype?: (string | null) | undefined, safe?: string | undefined, page?: number | undefined}; export type GetApiV1GoogleImagesSearchOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const imageResult = z.object({position: z.number().int().nullable().optional(), title: z.string().nullable().optional(), link: z.string().nullable().optional(), source: z.string().nullable().optional(), thumbnail: z.string().nullable().optional(), original: z.string().nullable().optional(), original_width: z.number().int().nullable().optional(), original_height: z.number().int().nullable().optional()}); export const imagesSearchResponse = z.object({results: z.array(imageResult).optional(), query: z.string(), language: z.string().optional(), country: z.string().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Search Google Images * * GET /v1/google/images/search */ export const getApiV1GoogleImagesSearch = async (args: GetApiV1GoogleImagesSearchArgs, options?: GetApiV1GoogleImagesSearchOptions) => { const {baseUrl = "https://scrapebadger.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.q != null) query.append("q", String(args.q)) if (args.gl != null) query.append("gl", String(args.gl)) if (args.hl != null) query.append("hl", String(args.hl)) if (args.tbs != null) query.append("tbs", String(args.tbs)) if (args.imgsz != null) query.append("imgsz", String(args.imgsz)) if (args.imgcolor != null) query.append("imgcolor", String(args.imgcolor)) if (args.imgtype != null) query.append("imgtype", String(args.imgtype)) if (args.safe != null) query.append("safe", String(args.safe)) if (args.page != null) query.append("page", String(args.page)) if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/google/images/search${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(imagesSearchResponse, await res.json()) };