import {z} from 'zod' export type GetApiV1GoogleMapsReviewsArgs = {data_id: string, sort_by?: ('qualityScore' | 'newestFirst' | 'ratingHigh' | 'ratingLow') | undefined, hl?: string | undefined, gl?: string | undefined, next_page_token?: (string | null) | undefined, offset?: number | undefined, results?: number | undefined}; export type GetApiV1GoogleMapsReviewsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const mapsLocationInfo = z.object({title: z.string(), address: z.string().nullable().optional(), rating: z.number().nullable().optional(), reviews_count: z.number().int().nullable().optional()}); export const mapsReviewTopic = z.object({keyword: z.string(), mentions: z.number().int().optional(), id: z.string().nullable().optional()}); export const mapsReviewUser = z.object({name: z.string(), contributor_id: z.string().nullable().optional(), thumbnail: z.string().nullable().optional(), local_guide: z.boolean().optional(), reviews_count: z.number().int().nullable().optional(), photos_count: z.number().int().nullable().optional()}); export const mapsOwnerResponse = z.object({text: z.string(), date: z.string().nullable().optional()}); export const mapsReview = z.object({rating: z.number().int(), text: z.string().nullable().optional(), date: z.string().nullable().optional(), iso_date: z.string().nullable().optional(), user: mapsReviewUser.nullable().optional(), likes: z.number().int().optional(), images: z.array(z.string()).optional(), response_from_owner: mapsOwnerResponse.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 mapsReviewsResponse = z.object({location: mapsLocationInfo.nullable().optional(), topics: z.array(mapsReviewTopic).optional(), reviews: z.array(mapsReview).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 reviews * * GET /v1/google/maps/reviews */ export const getApiV1GoogleMapsReviews = async (args: GetApiV1GoogleMapsReviewsArgs, options?: GetApiV1GoogleMapsReviewsOptions) => { 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.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.hl != null) query.append("hl", String(args.hl)) if (args.gl != null) query.append("gl", String(args.gl)) if (args.next_page_token != null) query.append("next_page_token", String(args.next_page_token)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.results != null) query.append("results", String(args.results)) if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/google/maps/reviews${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(mapsReviewsResponse, await res.json()) };