import {z} from 'zod' export type GetApiV1AdsCampaignsCampaignIdAdScheduleArgs = {campaignId: string, platform?: 'google' | undefined, includePerformance?: boolean | undefined, windowDays?: number | undefined, fromDate?: string | undefined, toDate?: string | undefined}; export type GetApiV1AdsCampaignsCampaignIdAdScheduleOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const adScheduleWindow = z.object({criterionId: z.string().optional(), resourceName: z.string().optional(), dayOfWeek: z.enum(["MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY"]).optional(), startHour: z.number().int().gte(0).lte(23).optional(), startMinute: z.union([z.literal(0), z.literal(15), z.literal(30), z.literal(45)]).optional(), endHour: z.number().int().gte(0).lte(24).optional(), endMinute: z.union([z.literal(0), z.literal(15), z.literal(30), z.literal(45)]).optional(), bidModifier: z.number().nullable().optional()}); export const getApiV1AdsCampaignsCampaignIdAdScheduleResponse = z.object({campaignId: z.string().optional(), schedule: z.array(adScheduleWindow).optional(), servesAroundTheClock: z.boolean().optional(), cachedAt: z.string().nullable().optional(), stale: z.boolean().optional(), performance: z.object({windowDays: z.number().int().nullable().optional(), byDayOfWeek: z.array(z.object({dayOfWeek: z.enum(["MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY"]).optional(), impressions: z.number().int().optional(), clicks: z.number().int().optional(), cost: z.number().optional(), conversions: z.number().optional()})).optional(), byHour: z.array(z.object({hour: z.number().int().gte(0).lte(23).optional(), impressions: z.number().int().optional(), clicks: z.number().int().optional(), cost: z.number().optional(), conversions: z.number().optional()})).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 }; /** * Read a campaign's ad schedule (dayparting) * * GET /v1/ads/campaigns/{campaignId}/ad-schedule */ export const getApiV1AdsCampaignsCampaignIdAdSchedule = async (args: GetApiV1AdsCampaignsCampaignIdAdScheduleArgs, options?: GetApiV1AdsCampaignsCampaignIdAdScheduleOptions) => { const {baseUrl = "https://zernio.com/api", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.platform != null) query.append("platform", String(args.platform)) if (args.includePerformance != null) query.append("includePerformance", String(args.includePerformance)) if (args.windowDays != null) query.append("windowDays", String(args.windowDays)) if (args.fromDate != null) query.append("fromDate", String(args.fromDate)) if (args.toDate != null) query.append("toDate", String(args.toDate)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/ads/campaigns/${encodeURIComponent(String(args.campaignId))}/ad-schedule${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV1AdsCampaignsCampaignIdAdScheduleResponse, await res.json()) };