import {z} from 'zod' export type UserListType = 'EMAIL' | 'IDFA' | 'MAID' | 'LR_ID' | 'DLX_ID' | 'HASHED_PINNER_ID'; export type CustomerListRecordRow = {email?: string | undefined, external_id?: string | undefined, hashed_phone_number?: string | undefined, hashed_pinner_id?: string | undefined, ip_address?: string | undefined, liveramp_envelope?: string | undefined, maid?: string | undefined, user_agent?: string | undefined}; export type CustomerListCreate = {is_nca?: boolean | undefined, list_type?: UserListType | undefined, name: string, records?: string | undefined, records_v2?: Array | undefined}; export type CreateApiAdAccountsAdAccountIdCustomerListsArgs = {ad_account_id: string, body: CustomerListCreate}; export type CreateApiAdAccountsAdAccountIdCustomerListsOptions = {baseUrl?: string | undefined, pinterestOauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const customerListStatus = z.enum(["PROCESSING", "READY", "TOO_SMALL", "UPLOADING"]); export const customerList = z.object({ad_account_id: z.string().optional(), created_time: z.number().optional(), exceptions: z.object({}).optional(), id: z.string().max(18), is_nca: z.boolean().optional(), name: z.string(), num_batches: z.number().optional(), num_removed_user_records: z.number().optional(), num_uploaded_user_records: z.number().optional(), status: customerListStatus.optional(), type: z.string().optional(), updated_time: z.number().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 customer lists * * POST /ad_accounts/{ad_account_id}/customer_lists */ export const createApiAdAccountsAdAccountIdCustomerLists = async (args: CreateApiAdAccountsAdAccountIdCustomerListsArgs, options?: CreateApiAdAccountsAdAccountIdCustomerListsOptions) => { 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))}/customer_lists${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(customerList, await res.json()) };