import {z} from 'zod' export type SummaryStatus = 'RUNNING' | 'PAUSED' | 'NOT_STARTED' | 'COMPLETED' | 'ADVERTISER_DISABLED' | 'ARCHIVED' | 'DRAFT' | 'DELETED_DRAFT'; export type ConversionObjectiveType = 'AWARENESS' | 'CONSIDERATION' | 'WEB_CONVERSION' | 'CATALOG_SALES' | 'VIDEO_COMPLETION' | 'APP_INSTALL' | 'SALES' | 'LEADS' | 'CTV_CONSIDERATION'; export type BulkDownloadCampaignFilter = {campaign_status?: Array | undefined, end_time?: string | undefined, name?: string | undefined, objective_type?: Array | undefined, start_time?: string | undefined}; export type BulkDownloadEntityIdsItems = string; export type BulkEntityType = 'CAMPAIGN' | 'AD_GROUP' | 'PRODUCT_GROUP' | 'AD' | 'KEYWORD' | 'LABEL' | 'SCHEDULE' | 'ENTITY_HISTORY'; export type BulkOutputFormat = 'CSV' | 'JSON'; export type BulkDownloadCreate = {campaign_filter?: BulkDownloadCampaignFilter | undefined, entity_ids?: Array | undefined, entity_types?: Array | undefined, output_format?: BulkOutputFormat | undefined, updated_since?: string | undefined}; export type CreateApiAdAccountsAdAccountIdBulkDownloadArgs = {ad_account_id: string, body: BulkDownloadCreate}; export type CreateApiAdAccountsAdAccountIdBulkDownloadOptions = {baseUrl?: string | undefined, pinterestOauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const bulkDownload = z.object({request_id: 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 }; /** * Get advertiser entities in bulk * * POST /ad_accounts/{ad_account_id}/bulk/download */ export const createApiAdAccountsAdAccountIdBulkDownload = async (args: CreateApiAdAccountsAdAccountIdBulkDownloadArgs, options?: CreateApiAdAccountsAdAccountIdBulkDownloadOptions) => { 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))}/bulk/download${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(bulkDownload, await res.json()) };