import {z} from 'zod' export type GetApiV1GoogleMapsPostsArgs = {data_id?: string | undefined, place_id?: (string | null) | undefined, hl?: string | undefined, gl?: string | undefined}; export type GetApiV1GoogleMapsPostsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const mapsLocationDetails = z.object({name: z.string().nullable().optional(), logo: z.string().nullable().optional()}); export const mapsPost = z.object({description: z.string().nullable().optional(), date: z.string().nullable().optional(), image: z.string().nullable().optional(), link: 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 mapsPostsResponse = z.object({location_details: mapsLocationDetails.nullable().optional(), post_data: z.array(mapsPost).optional(), has_updates: z.boolean().optional(), merchant_description: z.string().nullable().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 business posts * * GET /v1/google/maps/posts */ export const getApiV1GoogleMapsPosts = async (args: GetApiV1GoogleMapsPostsArgs, options?: GetApiV1GoogleMapsPostsOptions) => { 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.place_id != null) query.append("place_id", String(args.place_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/posts${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(mapsPostsResponse, await res.json()) };