import {z} from 'zod' export type GetApiBusinessesEmployersArgs = {assets_summary?: boolean | undefined, bookmark?: string | undefined, page_size?: number | undefined}; export type GetApiBusinessesEmployersOptions = {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 getApiBusinessesEmployersResponse = 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 }; /** * List business employers for user * * GET /businesses/employers */ export const getApiBusinessesEmployers = async (args: GetApiBusinessesEmployersArgs, options?: GetApiBusinessesEmployersOptions) => { 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.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/employers${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(getApiBusinessesEmployersResponse, await res.json()) };