import {z} from 'zod' export type GetApiV1AmazonProductsAsinOffersArgs = {asin: string, domain?: string | undefined, zip?: string | undefined}; export type GetApiV1AmazonProductsAsinOffersOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const amazonPrice = z.object({value: z.number().nullable().optional(), currency: z.string().nullable().optional(), symbol: z.string().nullable().optional(), raw: z.string().nullable().optional()}); export const offer = z.object({position: z.number().int().optional(), seller: z.object({}).optional(), price: amazonPrice.optional(), condition: z.object({}).optional(), delivery: z.object({}).optional(), buybox_winner: z.boolean().optional(), is_prime: z.boolean().optional(), minimum_order_quantity: z.number().int().nullable().optional(), maximum_order_quantity: z.number().int().nullable().optional()}); export const getApiV1AmazonProductsAsinOffersResponse = z.object({asin: z.string().optional(), domain: z.string().optional(), offers: z.array(offer).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 Offers * * GET /v1/amazon/products/{asin}/offers */ export const getApiV1AmazonProductsAsinOffers = async (args: GetApiV1AmazonProductsAsinOffersArgs, options?: GetApiV1AmazonProductsAsinOffersOptions) => { 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.zip != null) query.append("zip", String(args.zip)) if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/amazon/products/${encodeURIComponent(String(args.asin))}/offers${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(getApiV1AmazonProductsAsinOffersResponse, await res.json()) };