import {z} from 'zod' export type GoogleAssetGroupAssetLink = {fieldType: string, asset?: string | undefined, text?: string | undefined, imageUrl?: string | undefined, youtubeVideoId?: string | undefined}; export type GoogleListingGroupDimension = {productBrand?: ({value?: string | undefined}) | undefined, productCategory?: ({categoryId?: string | undefined, level: 'LEVEL1' | 'LEVEL2' | 'LEVEL3' | 'LEVEL4' | 'LEVEL5'}) | undefined, productType?: ({value?: string | undefined, level: 'LEVEL1' | 'LEVEL2' | 'LEVEL3' | 'LEVEL4' | 'LEVEL5'}) | undefined, productItemId?: ({value?: string | undefined}) | undefined, productCondition?: ({condition?: ('NEW' | 'REFURBISHED' | 'USED') | undefined}) | undefined, productChannel?: ({channel?: ('ONLINE' | 'LOCAL') | undefined}) | undefined, productCustomAttribute?: ({value?: string | undefined, index: 'INDEX0' | 'INDEX1' | 'INDEX2' | 'INDEX3' | 'INDEX4'}) | undefined}; export type GoogleListingGroupNode = {dimension: GoogleListingGroupDimension, excluded?: boolean | undefined, children?: Array | undefined}; export type GoogleListingGroupTree = {children?: Array | undefined}; export type CreateApiV1AdsCampaignsCampaignIdAssetGroupsArgs = {campaignId: string, body: {name: string, finalUrls: Array, finalMobileUrls?: Array | undefined, path1?: string | undefined, path2?: string | undefined, status?: ('ENABLED' | 'PAUSED') | undefined, assets?: Array | undefined, listingGroupFilter?: GoogleListingGroupTree | undefined, validateOnly?: boolean | undefined}}; export type CreateApiV1AdsCampaignsCampaignIdAssetGroupsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1AdsCampaignsCampaignIdAssetGroupsResponse = z.object({validateOnly: z.boolean().optional(), message: z.string().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 }; /** * Create a Performance Max asset group * * POST /v1/ads/campaigns/{campaignId}/asset-groups */ export const createApiV1AdsCampaignsCampaignIdAssetGroups = async (args: CreateApiV1AdsCampaignsCampaignIdAssetGroupsArgs, options?: CreateApiV1AdsCampaignsCampaignIdAssetGroupsOptions) => { const {baseUrl = "https://zernio.com/api", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/ads/campaigns/${encodeURIComponent(String(args.campaignId))}/asset-groups${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(createApiV1AdsCampaignsCampaignIdAssetGroupsResponse, await res.json()) };