import {z} from 'zod' export type PartnerType = 'INTERNAL' | 'EXTERNAL'; export type GetApiBusinessesBusinessIdPartnersArgs = {business_id: string, assets_summary?: boolean | undefined, partner_type?: PartnerType | undefined, partner_ids?: string | undefined, start_index?: number | undefined, sort_ascending?: boolean | undefined, bookmark?: string | undefined, page_size?: number | undefined}; export type GetApiBusinessesBusinessIdPartnersOptions = {baseUrl?: string | undefined, pinterestOauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const assetIdWithPermissions = z.object({id: z.string().min(1).max(20).optional(), permissions: z.array(z.string()).optional()}); export const businessMemberAssetsSummary = z.object({ad_accounts: z.array(assetIdWithPermissions).optional(), profiles: z.array(assetIdWithPermissions).optional()}); export const businessRoleResponse = z.string(); export const businessAccessUserSummary = z.object({email: z.string().nullable().optional(), id: z.string().min(1).max(20).nullable().optional(), username: z.string().nullable().optional()}); export const userBusinessRoleBinding = z.object({assets_summary: businessMemberAssetsSummary.nullable().optional(), business_roles: z.array(businessRoleResponse).optional(), created_by_business: businessAccessUserSummary.nullable().optional(), created_by_user: businessAccessUserSummary.nullable().optional(), created_time: z.number().int().nullable().optional(), id: z.string().optional(), is_shared_partner: z.boolean().optional(), user: businessAccessUserSummary.nullable().optional()}); export const getApiBusinessesBusinessIdPartnersResponse = z.object({bookmark: z.string().nullable().optional(), items: z.array(userBusinessRoleBinding)}); 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 business partners * * GET /businesses/{business_id}/partners */ export const getApiBusinessesBusinessIdPartners = async (args: GetApiBusinessesBusinessIdPartnersArgs, options?: GetApiBusinessesBusinessIdPartnersOptions) => { 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.assets_summary != null) query.append("assets_summary", String(args.assets_summary)) if (args.partner_type != null) query.append("partner_type", String(args.partner_type)) if (args.partner_ids != null) query.append("partner_ids", String(args.partner_ids)) if (args.start_index != null) query.append("start_index", String(args.start_index)) if (args.sort_ascending != null) query.append("sort_ascending", String(args.sort_ascending)) if (args.bookmark != null) query.append("bookmark", String(args.bookmark)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (pinterestOauth2 !== undefined) headers.set('authorization', `Bearer ${pinterestOauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/businesses/${encodeURIComponent(String(args.business_id))}/partners${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(getApiBusinessesBusinessIdPartnersResponse, await res.json()) };