import {z} from 'zod' export type AssetGroupType = 'BRAND' | 'LOCATION_OR_LANGUAGE' | 'PRODUCT_LINE' | 'OTHER'; export type AssetGroupTypesReadOrUpdateItem = Array; export type AssetGroupUpdateItemReadOrUpdateItem = {asset_group_id: string, asset_group_types?: AssetGroupTypesReadOrUpdateItem | undefined, assets_to_add?: Array | undefined, assets_to_remove?: Array | undefined, description?: string | undefined, name?: string | undefined}; export type AssetGroupUpdateError = {asset_group_id?: string | undefined, code?: number | undefined, message?: string | undefined}; export type AssetGroupBindingAdAccountsIdsItems = string; export type AssetGroupBindingCatalogsIdsItems = string; export type BusinessAccessUserSummary = {email?: (string | null) | undefined, id?: (string | null) | undefined, username?: (string | null) | undefined}; export type AssetGroupBindingProfilesIdsItems = string; export type AssetGroupBinding = {ad_accounts_ids: Array, asset_group_description: string | null, asset_group_name: string | null, asset_group_types: Array, catalogs_ids: Array, created_by: BusinessAccessUserSummary, created_time: number | null, id: string, owner: BusinessAccessUserSummary, profiles_ids: Array, updated_time: number | null}; export type AssetGroupModificationReadOrUpdate = {asset_groups_to_update?: Array | undefined, exceptions?: Array | undefined, updated_asset_groups?: Array | undefined}; export type PatchApiBusinessesBusinessIdAssetGroupsArgs = {business_id: string, body: AssetGroupModificationReadOrUpdate}; export type PatchApiBusinessesBusinessIdAssetGroupsOptions = {baseUrl?: string | undefined, pinterestOauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const assetGroupUpdateError = z.object({asset_group_id: z.string().optional(), code: z.number().int().optional(), message: z.string().optional()}); 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 assetGroupModification = z.object({exceptions: z.array(assetGroupUpdateError).optional(), updated_asset_groups: z.array(assetGroupBinding).optional()}); 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 asset groups. * * PATCH /businesses/{business_id}/asset_groups */ export const patchApiBusinessesBusinessIdAssetGroups = async (args: PatchApiBusinessesBusinessIdAssetGroupsArgs, options?: PatchApiBusinessesBusinessIdAssetGroupsOptions) => { 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(/\/+$/, '')}/businesses/${encodeURIComponent(String(args.business_id))}/asset_groups${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(assetGroupModification, await res.json()) };