import {z} from 'zod' export type GoogleLocationTargetingType = 'presence' | 'presence_or_interest'; export type UpdateApiV1AdsCampaignsCampaignIdTargetingArgs = {campaignId: string, body: {platform: 'google', targeting: {devices?: (Array<'MOBILE' | 'DESKTOP' | 'TABLET' | 'CONNECTED_TV' | {device: 'MOBILE' | 'DESKTOP' | 'TABLET' | 'CONNECTED_TV', bidModifier: number}>) | undefined, locations?: (Array | {countries?: Array | undefined, regions?: (Array) | undefined, cities?: (Array) | undefined, zips?: (Array) | undefined, metros?: (Array) | undefined}) | undefined, languages?: Array | undefined, excludedLocations?: (Array | {countries?: Array | undefined, regions?: (Array) | undefined, cities?: (Array) | undefined, zips?: (Array) | undefined, metros?: (Array) | undefined}) | undefined, locationTargetingType?: GoogleLocationTargetingType | undefined}}}; export type UpdateApiV1AdsCampaignsCampaignIdTargetingOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const updateApiV1AdsCampaignsCampaignIdTargetingResponse = z.object({campaignId: z.string().optional(), adGroupId: z.string().optional(), updated: z.array(z.enum(["devices", "locations", "excludedLocations", "languages", "locationTargetingType"])).optional(), locationTargetingType: z.enum(["presence", "presence_or_interest", "null"]).nullable().optional(), devices: z.array(z.object({device: z.enum(["MOBILE", "DESKTOP", "TABLET", "CONNECTED_TV"]).optional(), included: z.boolean().optional(), bidModifier: z.number().nullable().optional()})).optional(), locations: z.array(z.object({geoTargetId: z.string().optional(), negative: z.boolean().optional(), name: z.string().nullable().optional(), canonicalName: z.string().nullable().optional(), type: z.string().nullable().optional(), countryCode: z.string().nullable().optional()})).optional(), excludedLocations: z.array(z.object({geoTargetId: z.string().optional(), negative: z.boolean().optional(), name: z.string().nullable().optional(), canonicalName: z.string().nullable().optional(), type: z.string().nullable().optional(), countryCode: z.string().nullable().optional()})).optional(), languages: z.array(z.object({code: z.string().optional(), id: z.string().optional(), name: z.string().optional()})).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 }; /** * Edit a Google campaign's device, location, excluded location, or language targeting * * PUT /v1/ads/campaigns/{campaignId}/targeting */ export const updateApiV1AdsCampaignsCampaignIdTargeting = async (args: UpdateApiV1AdsCampaignsCampaignIdTargetingArgs, options?: UpdateApiV1AdsCampaignsCampaignIdTargetingOptions) => { 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))}/targeting${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(updateApiV1AdsCampaignsCampaignIdTargetingResponse, await res.json()) };