import {z} from 'zod' export type GetApiV1EbayItemsItemIdReviewsArgs = {item_id: string, domain?: string | undefined, page?: number | undefined}; export type GetApiV1EbayItemsItemIdReviewsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1EbayItemsItemIdReviewsResponse = z.object({domain: z.string().optional(), item_id: z.string().nullable().optional(), product_id: z.string().nullable().optional(), rating: z.number().nullable().optional(), ratings_total: z.number().int().nullable().optional(), histogram: z.object({five_star: z.number().int().nullable().optional(), four_star: z.number().int().nullable().optional(), three_star: z.number().int().nullable().optional(), two_star: z.number().int().nullable().optional(), one_star: z.number().int().nullable().optional()}).optional(), reviews: z.array(z.object({title: z.string().nullable().optional(), body: z.string().nullable().optional(), rating: z.number().nullable().optional(), author: z.string().nullable().optional(), date_raw: z.string().nullable().optional(), date_utc: z.number().nullable().optional(), date_at: z.string().nullable().optional(), helpful_votes: z.number().int().nullable().optional(), verified_purchase: z.boolean().optional()})).optional(), pagination: z.object({current_page: z.number().int().optional(), per_page: z.number().int().nullable().optional(), total_pages: z.number().int().nullable().optional(), total_results: z.number().int().nullable().optional()}).optional(), scraped_utc: z.number().nullable().optional(), scraped_at: z.string().nullable().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 }; /** * Get Item Reviews * * GET /v1/ebay/items/{item_id}/reviews */ export const getApiV1EbayItemsItemIdReviews = async (args: GetApiV1EbayItemsItemIdReviewsArgs, options?: GetApiV1EbayItemsItemIdReviewsOptions) => { 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.domain != null) query.append("domain", String(args.domain)) if (args.page != null) query.append("page", String(args.page)) if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/ebay/items/${encodeURIComponent(String(args.item_id))}/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(getApiV1EbayItemsItemIdReviewsResponse, await res.json()) };