import {z} from 'zod' export type BidStrategy = 'LOWEST_COST_WITHOUT_CAP' | 'LOWEST_COST_WITH_BID_CAP' | 'COST_CAP' | 'LOWEST_COST_WITH_MIN_ROAS'; export type GoogleTargetImpressionShare = {location: 'ANYWHERE_ON_PAGE' | 'TOP_OF_PAGE' | 'ABSOLUTE_TOP_OF_PAGE', percent: number, maxCpc: number}; export type GoogleManualCpc = {maxCpc: number}; export type GoogleNetworkSettings = {searchPartners?: boolean | undefined, displayNetwork?: boolean | undefined}; export type UpdateApiV1AdsCampaignsCampaignIdArgs = {campaignId: string, body: {platform: 'facebook' | 'instagram' | 'tiktok' | 'linkedin' | 'pinterest' | 'google' | 'twitter' | 'openai' | 'reddit' | 'microsoft', accountId?: string | undefined, bidStrategy?: BidStrategy | undefined, bidAmount?: number | undefined, roasAverageFloor?: number | undefined, portfolioBidStrategyId?: string | undefined, allowSharedBudgetUpdate?: boolean | undefined, targetImpressionShare?: GoogleTargetImpressionShare | undefined, manualCpc?: GoogleManualCpc | undefined, networkSettings?: GoogleNetworkSettings | undefined, trackingUrlTemplate?: string | undefined, finalUrlSuffix?: string | undefined, sharedBudgetId?: (string | null) | undefined, budget?: ({amount: number, type: 'daily' | 'lifetime'}) | undefined, name?: string | undefined, platformSpecificData?: ({spendCap?: (number | null) | undefined}) | undefined}}; export type UpdateApiV1AdsCampaignsCampaignIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const adCampaignBudget = z.object({amount: z.number(), type: z.enum(["daily", "lifetime"]), amountMicros: z.string().optional(), explicitlyShared: z.boolean().nullable().optional(), resourceName: z.string().nullable().optional(), deliveryMethod: z.string().nullable().optional()}); export const bidStrategy = z.enum(["LOWEST_COST_WITHOUT_CAP", "LOWEST_COST_WITH_BID_CAP", "COST_CAP", "LOWEST_COST_WITH_MIN_ROAS"]); export const googleTargetImpressionShare = z.object({location: z.enum(["ANYWHERE_ON_PAGE", "TOP_OF_PAGE", "ABSOLUTE_TOP_OF_PAGE"]), percent: z.number().gt(0).lte(100), maxCpc: z.number().gt(0)}); export const googleManualCpc = z.object({maxCpc: z.number().gt(0)}); export const googleNetworkSettings = z.object({searchPartners: z.boolean().optional(), displayNetwork: z.boolean().optional()}); export const updateApiV1AdsCampaignsCampaignIdResponse = z.object({updated: z.number().int().optional(), budget: adCampaignBudget.optional(), budgetLevel: z.literal("campaign").optional(), bidStrategy: bidStrategy.optional(), bidAmount: z.number().optional(), roasAverageFloor: z.number().optional(), portfolioBidStrategyId: z.string().optional(), targetImpressionShare: googleTargetImpressionShare.optional(), manualCpc: googleManualCpc.optional(), networkSettings: googleNetworkSettings.optional(), trackingUrlTemplate: z.string().optional(), finalUrlSuffix: z.string().optional(), sharedBudgetId: z.string().nullable().optional(), platformSpecificData: z.object({}).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 a campaign * * PUT /v1/ads/campaigns/{campaignId} */ export const updateApiV1AdsCampaignsCampaignId = async (args: UpdateApiV1AdsCampaignsCampaignIdArgs, options?: UpdateApiV1AdsCampaignsCampaignIdOptions) => { 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))}${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(updateApiV1AdsCampaignsCampaignIdResponse, await res.json()) };