import {z} from 'zod' export type DuplicateListingStatusType = 'POTENTIAL' | 'PROCESSING' | 'FIXED' | 'FAILED'; export type GetApiApiV4LocationsListingsDuplicatesArgs = {status?: DuplicateListingStatusType | undefined, siteId?: number | undefined, tag?: string | undefined, page?: number | undefined, perPage?: number | undefined}; export type GetApiApiV4LocationsListingsDuplicatesOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const duplicateListingType = z.object({id: z.string().optional(), listingItemId: z.string().optional(), name: z.string().optional(), liveLink: z.string().optional(), status: z.string().optional()}); export const offsetBasedPageInfoType = z.object({page: z.number().int().optional(), perPage: z.number().int().optional(), totalPages: z.number().int().optional()}); export const duplicateListingRollupType = z.object({total: z.number().int().optional(), listings: z.array(duplicateListingType).optional(), pageInfo: offsetBasedPageInfoType.optional()}); export const apiError = z.object({message: z.string(), code: z.string().optional(), contextInfo: z.array(z.object({key: z.string().optional(), value: z.string().optional()})).optional()}); export const getApiApiV4LocationsListingsDuplicatesResponse = z.object({data: z.object({duplicateListingsRollup: duplicateListingRollupType.optional()}), errors: z.array(apiError).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 }; /** * Duplicate listing rollup for tag. * * GET /api/v4/locations/listings/duplicates * * Also accepts auth: rapidApiKey + rapidApiHost */ export const getApiApiV4LocationsListingsDuplicates = async (args: GetApiApiV4LocationsListingsDuplicatesArgs, options?: GetApiApiV4LocationsListingsDuplicatesOptions) => { const {baseUrl = "https://listingsapi.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.status != null) query.append("status", String(args.status)) if (args.siteId != null) query.append("siteId", String(args.siteId)) if (args.tag != null) query.append("tag", String(args.tag)) if (args.page != null) query.append("page", String(args.page)) if (args.perPage != null) query.append("perPage", String(args.perPage)) if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v4/locations/listings/duplicates${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(getApiApiV4LocationsListingsDuplicatesResponse, await res.json()) };