import {z} from 'zod' export type GetApiV1GoogleMapsPhotosArgs = {data_id: string, hl?: string | undefined, gl?: string | undefined}; export type GetApiV1GoogleMapsPhotosOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const mapsPhotoCategory = z.object({name: z.string(), id: z.string().nullable().optional()}); export const mapsPhoto = z.object({thumbnail: z.string().nullable().optional(), image: z.string().nullable().optional()}); export const pagination = z.object({current: z.union([z.number().int(), z.string()]).optional(), next: z.string().nullable().optional(), total_results: z.number().int().nullable().optional(), total_pages: z.number().int().nullable().optional(), page_no: z.record(z.string(), z.string()).optional()}); export const mapsPhotosResponse = z.object({categories: z.array(mapsPhotoCategory).optional(), photos: z.array(mapsPhoto).optional(), pagination: pagination}); 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 }; /** * Get place photos * * GET /v1/google/maps/photos */ export const getApiV1GoogleMapsPhotos = async (args: GetApiV1GoogleMapsPhotosArgs, options?: GetApiV1GoogleMapsPhotosOptions) => { 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.data_id != null) query.append("data_id", String(args.data_id)) if (args.hl != null) query.append("hl", String(args.hl)) if (args.gl != null) query.append("gl", String(args.gl)) if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/google/maps/photos${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(mapsPhotosResponse, await res.json()) };