import {z} from 'zod' export type CatalogsRetailReportStatsParametersReport = {feed_id: string, processing_result_id?: string | undefined, report_type: 'FEED_INGESTION_ISSUES'} | {catalog_id?: string | undefined, report_type: 'DISTRIBUTION_ISSUES'}; export type CatalogsHotelReportStatsParametersReport = {feed_id: string, processing_result_id?: string | undefined, report_type: 'FEED_INGESTION_ISSUES'} | {catalog_id?: string | undefined, report_type: 'DISTRIBUTION_ISSUES'}; export type CatalogsReportStatsParameters = {catalog_type: 'RETAIL', report: CatalogsRetailReportStatsParametersReport} | {catalog_type: 'HOTEL', report: CatalogsHotelReportStatsParametersReport}; export type GetApiCatalogsReportsStatsArgs = {ad_account_id?: string | undefined, parameters: CatalogsReportStatsParameters, bookmark?: string | undefined, page_size?: number | undefined}; export type GetApiCatalogsReportsStatsOptions = {baseUrl?: string | undefined, pinterestOauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const catalogsReportStats = z.discriminatedUnion("report_type", [z.object({catalog_id: z.string().optional(), code: z.number().int().optional(), code_label: z.string().optional(), message: z.string().optional(), occurrences: z.number().int().optional(), report_type: z.literal("FEED_INGESTION_ISSUES").optional(), severity: z.enum(["WARN", "ERROR"]).optional()}), z.object({catalog_id: z.string().optional(), code: z.number().int().optional(), code_label: z.string().optional(), ineligible_for_ads: z.boolean().optional(), ineligible_for_organic: z.boolean().optional(), message: z.string().optional(), occurrences: z.number().int().optional(), report_type: z.literal("DISTRIBUTION_ISSUES").optional()})]); export const getApiCatalogsReportsStatsResponse = z.object({bookmark: z.string().nullable().optional(), items: z.array(catalogsReportStats)}); 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 }; /** * List report stats * * GET /catalogs/reports/stats */ export const getApiCatalogsReportsStats = async (args: GetApiCatalogsReportsStatsArgs, options?: GetApiCatalogsReportsStatsOptions) => { const {baseUrl = "https://api.pinterest.com/v5", fetch: fetchFn = fetch, pinterestOauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.ad_account_id != null) query.append("ad_account_id", String(args.ad_account_id)) if (args.parameters != null) query.append("parameters", String(args.parameters)) if (args.bookmark != null) query.append("bookmark", String(args.bookmark)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (pinterestOauth2 !== undefined) headers.set('authorization', `Bearer ${pinterestOauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/catalogs/reports/stats${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(getApiCatalogsReportsStatsResponse, await res.json()) };