import {z} from 'zod' export type LeadFormPolicyLink = {label?: string | undefined, link?: string | undefined}; export type LeadFormQuestionFieldType = 'TEXT_FIELD' | 'TEXT_AREA' | 'RADIO_LIST' | 'CHECKBOX' | 'null' | null; export type LeadFormQuestionType = 'CUSTOM' | 'FULL_NAME' | 'FIRST_NAME' | 'LAST_NAME' | 'EMAIL' | 'PHONE_NUMBER' | 'ZIP_CODE' | 'GENDER' | 'CITY' | 'COUNTRY' | 'STATE_PROVINCE' | 'ADDRESS' | 'DATE_OF_BIRTH' | 'AGE'; export type LeadFormQuestion = {custom_question_field_type?: LeadFormQuestionFieldType | undefined, custom_question_label?: (string | null) | undefined, custom_question_options?: (Array | null) | undefined, question_type?: LeadFormQuestionType | undefined}; export type LeadFormStatus = 'DRAFT' | 'ACTIVE'; export type LeadFormBatchUpdate = {completion_message?: (string | null) | undefined, disclosure_language?: (string | null) | undefined, has_accepted_terms?: boolean | undefined, id: string, name?: (string | null) | undefined, policy_links?: Array | undefined, privacy_policy_link?: (string | null) | undefined, questions?: Array | undefined, status?: LeadFormStatus | undefined}; export type LeadFormsUpdateBody = Array; export type PatchApiAdAccountsAdAccountIdLeadFormsArgs = {ad_account_id: string, body: LeadFormsUpdateBody}; export type PatchApiAdAccountsAdAccountIdLeadFormsOptions = {baseUrl?: string | undefined, pinterestOauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const leadFormPolicyLink = z.object({label: z.string().optional(), link: z.string().optional()}); export const leadFormQuestionFieldType = z.enum(["TEXT_FIELD", "TEXT_AREA", "RADIO_LIST", "CHECKBOX", "null"]).nullable(); export const leadFormQuestionType = z.enum(["CUSTOM", "FULL_NAME", "FIRST_NAME", "LAST_NAME", "EMAIL", "PHONE_NUMBER", "ZIP_CODE", "GENDER", "CITY", "COUNTRY", "STATE_PROVINCE", "ADDRESS", "DATE_OF_BIRTH", "AGE"]); export const leadFormQuestion = z.object({custom_question_field_type: leadFormQuestionFieldType.optional(), custom_question_label: z.string().nullable().optional(), custom_question_options: z.array(z.string()).nullable().optional(), question_type: leadFormQuestionType.optional()}); export const leadFormStatus = z.enum(["DRAFT", "ACTIVE"]); export const leadForm = z.object({ad_account_id: z.string().optional(), completion_message: z.string().nullable(), created_time: z.number().int().optional(), disclosure_language: z.string().nullable().optional(), has_accepted_terms: z.boolean(), id: z.string(), name: z.string().nullable(), policy_links: z.array(leadFormPolicyLink).optional(), privacy_policy_link: z.string().nullable(), questions: z.array(leadFormQuestion), status: leadFormStatus.optional(), updated_time: z.number().int().optional()}); export const pinterestLibBatchItemException = z.object({code: z.number().int().optional(), message: z.string()}); export const patchApiAdAccountsAdAccountIdLeadFormsResponse = z.object({items: z.array(z.object({data: leadForm.optional(), exceptions: z.array(pinterestLibBatchItemException).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 }; /** * Update lead forms * * PATCH /ad_accounts/{ad_account_id}/lead_forms */ export const patchApiAdAccountsAdAccountIdLeadForms = async (args: PatchApiAdAccountsAdAccountIdLeadFormsArgs, options?: PatchApiAdAccountsAdAccountIdLeadFormsOptions) => { 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))}/lead_forms${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(patchApiAdAccountsAdAccountIdLeadFormsResponse, await res.json()) };