import {z} from 'zod' export type MMMReportingColumn = 'SPEND_IN_DOLLAR' | 'SPEND_IN_MICRO_DOLLAR' | 'ECPC_IN_DOLLAR' | 'ECTR' | 'CAMPAIGN_NAME' | 'TOTAL_ENGAGEMENT' | 'EENGAGEMENT_RATE' | 'ECPM_IN_DOLLAR' | 'CAMPAIGN_ID' | 'ADVERTISER_ID' | 'AD_GROUP_ID' | 'AD_GROUP_NAME' | 'CLICKTHROUGH_1' | 'IMPRESSION_1' | 'CLICKTHROUGH_2' | 'IMPRESSION_2' | 'TOTAL_CLICKTHROUGH' | 'TOTAL_IMPRESSION' | 'ADVERTISER_NAME' | 'SPEND_ORDER_LINE_PAID_TYPE' | 'CAMPAIGN_OBJECTIVE_TYPE' | 'PINNER_LIST_NAME' | 'ADS_CREDIT_SPEND_IN_DOLLAR' | 'ADVERTISER_PAID_SPEND_IN_DOLLAR' | 'ADVERTISER_PAID_ECPC_IN_DOLLAR' | 'ADVERTISER_PAID_ECPM_IN_DOLLAR'; export type TargetingAdvertiserCountry = 'US' | 'GB' | 'CA' | 'IE' | 'AU' | 'NZ' | 'FR' | 'SE' | 'IL' | 'DE' | 'AT' | 'IT' | 'ES' | 'NL' | 'BE' | 'PT' | 'CH' | 'HK' | 'JP' | 'KR' | 'SG' | 'NO' | 'DK' | 'FI' | 'CY' | 'LU' | 'MT' | 'PL' | 'RO' | 'HU' | 'CZ' | 'GR' | 'SK' | 'BR' | 'MX' | 'AR' | 'CL' | 'CO' | null; export type MMMReportGranularity = 'DAY' | 'WEEK'; export type MMMReportLevel = 'CAMPAIGN_TARGETING' | 'AD_GROUP_TARGETING'; export type MMMReportingTargetingType = 'APPTYPE' | 'COUNTRY' | 'CREATIVE_TYPE' | 'GENDER' | 'LOCATION' | 'PLACEMENT' | 'AUDIENCE_INCLUDE'; export type MMMReportCreate = {advertiser_ids?: Array | undefined, columns: Array, countries?: Array | undefined, custom_column_ids?: Array | undefined, end_date: string, granularity: MMMReportGranularity, level: MMMReportLevel, report_name: string, start_date: string, targeting_types: Array}; export type CreateApiAdAccountsAdAccountIdMmmReportsArgs = {ad_account_id: string, body: MMMReportCreate}; export type CreateApiAdAccountsAdAccountIdMmmReportsOptions = {baseUrl?: string | undefined, pinterestOauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const bulkReportingJobStatus = z.enum(["DOES_NOT_EXIST", "FINISHED", "IN_PROGRESS", "EXPIRED", "FAILED", "CANCELLED"]); export const mMMReport = z.object({message: z.string().optional(), report_status: bulkReportingJobStatus.optional(), size: z.number().optional(), status: z.string().optional(), token: z.string().optional(), url: z.string().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 }; /** * Create a request for a Marketing Mix Modeling (MMM) report * * POST /ad_accounts/{ad_account_id}/mmm_reports */ export const createApiAdAccountsAdAccountIdMmmReports = async (args: CreateApiAdAccountsAdAccountIdMmmReportsArgs, options?: CreateApiAdAccountsAdAccountIdMmmReportsOptions) => { const {baseUrl = "https://api.pinterest.com/v5", fetch: fetchFn = fetch, pinterestOauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (pinterestOauth2 !== undefined) headers.set('authorization', `Bearer ${pinterestOauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/ad_accounts/${encodeURIComponent(String(args.ad_account_id))}/mmm_reports${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(mMMReport, await res.json()) };