import {z} from 'zod' export type PinterestLibPaginationOrder = 'ASCENDING' | 'DESCENDING'; export type GetApiAdAccountsAdAccountIdLeadFormsArgs = {bookmark?: string | undefined, page_size?: number | undefined, order?: PinterestLibPaginationOrder | undefined, ad_account_id: string}; export type GetApiAdAccountsAdAccountIdLeadFormsOptions = {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 getApiAdAccountsAdAccountIdLeadFormsResponse = z.object({bookmark: z.string().nullable().optional(), items: z.array(leadForm)}); 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 lead forms * * GET /ad_accounts/{ad_account_id}/lead_forms */ export const getApiAdAccountsAdAccountIdLeadForms = async (args: GetApiAdAccountsAdAccountIdLeadFormsArgs, options?: GetApiAdAccountsAdAccountIdLeadFormsOptions) => { 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.bookmark != null) query.append("bookmark", String(args.bookmark)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.order != null) query.append("order", String(args.order)) 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiAdAccountsAdAccountIdLeadFormsResponse, await res.json()) };