import {z} from 'zod' export type TargetingSpec = {userOs?: Array | undefined, userDevice?: Array | undefined, countries?: Array | undefined, regions?: (Array) | undefined, cities?: (Array) | undefined, zips?: (Array<{key: string, name?: string | undefined}>) | undefined, metros?: (Array<{key: string, name?: string | undefined}>) | undefined, countryGroups?: (Array<'africa' | 'asia' | 'europe' | 'north_america' | 'south_america' | 'oceania' | 'central_america' | 'caribbean' | 'eea' | 'euro_area' | 'nafta' | 'mercosur' | 'afta' | 'apec' | 'gcc' | 'cisfta' | 'emerging_markets' | 'itunes_app_store' | 'android_free_store' | 'android_paid_store'>) | undefined, customLocations?: (Array<{latitude: number, longitude: number, radius: number, distanceUnit: 'mile' | 'kilometer', name?: string | undefined, address?: string | undefined}>) | undefined, excludedLocations?: ({countries?: Array | undefined, countryGroups?: (Array<'africa' | 'asia' | 'europe' | 'north_america' | 'south_america' | 'oceania' | 'central_america' | 'caribbean' | 'eea' | 'euro_area' | 'nafta' | 'mercosur' | 'afta' | 'apec' | 'gcc' | 'cisfta' | 'emerging_markets' | 'itunes_app_store' | 'android_free_store' | 'android_paid_store'>) | undefined, regions?: (Array<{key: string, name?: string | undefined}>) | undefined, cities?: (Array<{key: string, radius?: number | undefined, distanceUnit?: ('mile' | 'kilometer') | undefined}>) | undefined, zips?: (Array<{key: string, name?: string | undefined}>) | undefined, places?: Array<{key: string}> | undefined, neighborhoods?: Array<{key: string}> | undefined, customLocations?: (Array<{latitude: number, longitude: number, radius: number, distanceUnit: 'mile' | 'kilometer', name?: string | undefined, address?: string | undefined}>) | undefined}) | undefined, ageMin?: number | undefined, ageMax?: number | undefined, gender?: ('all' | 'male' | 'female') | undefined, incomeTier?: ('top_5' | 'top_10' | 'top_10_25' | 'top_25_50') | undefined, languages?: Array | undefined, interests?: (Array<{id: string, name?: string | undefined}>) | undefined, behaviors?: (Array<{id: string, name?: string | undefined}>) | undefined, workPositions?: (Array<{id: string, name?: string | undefined}>) | undefined, workEmployers?: (Array<{id: string, name?: string | undefined}>) | undefined, workIndustries?: (Array<{id: string, name?: string | undefined}>) | undefined, industries?: Array | undefined, companySizes?: Array | undefined, seniorities?: Array | undefined, jobFunctions?: Array | undefined, skills?: Array | undefined, audienceInclude?: Array | undefined, audienceExclude?: Array | undefined, communities?: Array | undefined, excludedCommunities?: Array | undefined, conversationKeywords?: Array | undefined, excludedConversationKeywords?: Array | undefined}; export type CreateApiV1AdsAdSetsArgs = {'Idempotency-Key'?: string | undefined, body: {accountId: string, platform: 'facebook' | 'instagram' | 'tiktok' | 'linkedin' | 'pinterest' | 'google' | 'twitter' | 'openai' | 'reddit', campaignId: string, name: string, status?: ('ACTIVE' | 'PAUSED') | undefined, maxCpc?: number | undefined, adAccountId?: string | undefined, customerId?: string | undefined, budgetAmount?: number | undefined, budgetType?: ('daily' | 'lifetime') | undefined, startDate?: string | undefined, endDate?: string | undefined, targeting?: TargetingSpec | undefined, promotedObject?: ({pixelId?: string | undefined, customEventType?: string | undefined}) | undefined, optimizationGoal?: string | undefined, bidStrategy?: ('LOWEST_COST_WITHOUT_CAP' | 'LOWEST_COST_WITH_BID_CAP' | 'COST_CAP') | undefined, bidAmount?: number | undefined}}; export type CreateApiV1AdsAdSetsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1AdsAdSetsResponse = z.object({adSetId: z.string().optional(), campaignId: 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 standalone ad group * * POST /v1/ads/ad-sets */ export const createApiV1AdsAdSets = async (args: CreateApiV1AdsAdSetsArgs, options?: CreateApiV1AdsAdSetsOptions) => { 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 (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/ads/ad-sets${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(createApiV1AdsAdSetsResponse, await res.json()) };