import {z} from 'zod' export type PermissionsWithOwner = 'ADMIN' | 'ANALYST' | 'FINANCE_MANAGER' | 'FINANCE_EDIT' | 'FINANCE_VIEW' | 'AUDIENCE_MANAGER' | 'CAMPAIGN_MANAGER' | 'CATALOGS_MANAGER' | 'CATALOGS_VIEWER' | 'PROFILE_PUBLISHER' | 'OWNER'; export type GetApiBusinessesBusinessIdAssetsArgs = {business_id: string, permissions?: Array | undefined, child_asset_id?: string | undefined, asset_group_id?: string | undefined, asset_type?: ('AD_ACCOUNT' | 'PROFILE' | 'ASSET_GROUP' | 'CATALOG' | 'CONSUMER') | undefined, start_index?: number | undefined, bookmark?: string | undefined, page_size?: number | undefined}; export type GetApiBusinessesBusinessIdAssetsOptions = {baseUrl?: string | undefined, pinterestOauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const assetGroupBindingAdAccountsIdsItems = z.string(); export const assetGroupBindingCatalogsIdsItems = 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 assetGroupBindingProfilesIdsItems = z.string(); export const assetGroupBinding = z.object({ad_accounts_ids: z.array(assetGroupBindingAdAccountsIdsItems), asset_group_description: z.string().nullable(), asset_group_name: z.string().nullable(), asset_group_types: z.array(z.string()), catalogs_ids: z.array(assetGroupBindingCatalogsIdsItems), created_by: businessAccessUserSummary, created_time: z.number().int().nullable(), id: z.string(), owner: businessAccessUserSummary, profiles_ids: z.array(assetGroupBindingProfilesIdsItems), updated_time: z.number().int().nullable()}); export const assetTypeResponse = z.enum(["AD_ACCOUNT", "MERCHANT", "PROFILE", "ASSET_GROUP", "PINNER_LIST", "CONVERSION_TAG", "CATALOG", "CONVERSION_SEGMENT"]); export const businessAssets = z.object({asset_group_info: assetGroupBinding.optional(), asset_id: z.string().min(1).max(20).optional(), asset_type: assetTypeResponse.optional(), permissions: z.array(z.string()).optional()}); export const getApiBusinessesBusinessIdAssetsResponse = z.object({bookmark: z.string().nullable().optional(), items: z.array(businessAssets)}); 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 assets * * GET /businesses/{business_id}/assets */ export const getApiBusinessesBusinessIdAssets = async (args: GetApiBusinessesBusinessIdAssetsArgs, options?: GetApiBusinessesBusinessIdAssetsOptions) => { 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") for (const item of args.permissions ?? []) query.append("permissions", String(item)) if (args.child_asset_id != null) query.append("child_asset_id", String(args.child_asset_id)) if (args.asset_group_id != null) query.append("asset_group_id", String(args.asset_group_id)) if (args.asset_type != null) query.append("asset_type", String(args.asset_type)) if (args.start_index != null) query.append("start_index", String(args.start_index)) 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))}/assets${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(getApiBusinessesBusinessIdAssetsResponse, await res.json()) };