import {z} from 'zod' export type UpdateApiV1AdsCampaignsCampaignIdAdScheduleArgs = {campaignId: string, body: {schedule: Array<{dayOfWeek: 'MONDAY' | 'TUESDAY' | 'WEDNESDAY' | 'THURSDAY' | 'FRIDAY' | 'SATURDAY' | 'SUNDAY', startHour: number, startMinute?: (0 | 15 | 30 | 45) | undefined, endHour: number, endMinute?: (0 | 15 | 30 | 45) | undefined, bidModifier?: (number | null) | undefined}>}}; export type UpdateApiV1AdsCampaignsCampaignIdAdScheduleOptions = {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 updateApiV1AdsCampaignsCampaignIdAdScheduleResponse = z.object({campaignId: z.string().optional(), schedule: z.array(adScheduleWindow).optional(), servesAroundTheClock: z.boolean().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 }; /** * Replace a campaign's ad schedule (dayparting) * * PUT /v1/ads/campaigns/{campaignId}/ad-schedule */ export const updateApiV1AdsCampaignsCampaignIdAdSchedule = async (args: UpdateApiV1AdsCampaignsCampaignIdAdScheduleArgs, options?: UpdateApiV1AdsCampaignsCampaignIdAdScheduleOptions) => { 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))}/ad-schedule${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(updateApiV1AdsCampaignsCampaignIdAdScheduleResponse, await res.json()) };