import {z} from 'zod' export type PinterestLibIntegerFormatType = string; export type OperationType = 'SHARE' | 'REVOKE'; export type AdAccountToBusinessSharedAudienceUpdateWithRequiredBody = {audience_id: PinterestLibIntegerFormatType, operation_type: OperationType, recipient_business_ids: Array}; export type PatchApiAdAccountsAdAccountIdAudiencesBusinessesSharedArgs = {ad_account_id: string, body: AdAccountToBusinessSharedAudienceUpdateWithRequiredBody}; export type PatchApiAdAccountsAdAccountIdAudiencesBusinessesSharedOptions = {baseUrl?: string | undefined, pinterestOauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const pinterestLibIntegerFormatType = z.string(); export const role = z.enum(["UNKNOWN", "OWNER", "ADMIN", "ANALYST", "SOS_READER", "FINANCE_MANAGER", "FINANCE_EDIT", "FINANCE_VIEW", "AUDIENCE_MANAGER", "CAMPAIGN_MANAGER", "CATALOGS_MANAGER", "CATALOGS_VIEWER", "RESTRICTED_OWNER", "PROFILE_MANAGER", "PROFILE_PUBLISHER", "RESOURCE_PINNER_LIST_OWNER", "RESOURCE_PINNER_LIST_READER", "BIZ_PINNER_LIST_SHARER", "RESOURCE_CONVERSION_TAGS_READER"]); export const adAccountToBusinessSharedAudience = z.object({audience_id: pinterestLibIntegerFormatType, permissions: z.array(role).optional(), recipient_business_ids: z.array(pinterestLibIntegerFormatType)}); 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 audience sharing from an ad account to businesses * * PATCH /ad_accounts/{ad_account_id}/audiences/businesses/shared */ export const patchApiAdAccountsAdAccountIdAudiencesBusinessesShared = async (args: PatchApiAdAccountsAdAccountIdAudiencesBusinessesSharedArgs, options?: PatchApiAdAccountsAdAccountIdAudiencesBusinessesSharedOptions) => { 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))}/audiences/businesses/shared${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(adAccountToBusinessSharedAudience, await res.json()) };