import {z} from 'zod' export type CreateApiBulkUpdateBudgetFlightsArgs = {body: {budget_flights?: ({create?: (Array<{start_date: string, end_date: string, total_budget: number, total_impression_budget?: (number | null) | undefined, campaign_id: number}>) | undefined, update?: (Array<{id: number, start_date?: string | undefined, end_date?: string | undefined, total_budget?: number | undefined, total_impression_budget?: (number | null) | undefined, is_relevant?: boolean | undefined}>) | undefined, delete?: (Array<{id?: number | undefined}>) | undefined}) | undefined}}; export type CreateApiBulkUpdateBudgetFlightsOptions = {baseUrl?: string | undefined, Auth0?: string | undefined, fetch?: typeof fetch | undefined}; export const campaignBudgetFlights = z.object({id: z.number().int().optional(), start_date: z.string().optional(), end_date: z.string().optional(), total_budget: z.number().optional(), total_impression_budget: z.number().int().gte(1).nullable().optional(), is_relevant: z.boolean().optional()}); export const listMetadata = z.object({status: z.string(), count: z.number().int(), total_count: z.number().int().optional(), offset: z.number().int().optional(), next_page: z.string().optional(), prev_page: z.string().optional()}); export const createApiBulkUpdateBudgetFlightsResponse = z.object({data: z.array(z.array(campaignBudgetFlights)).optional(), meta: listMetadata.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 }; /** * Bulk Update Budget Flights * * POST /bulk_update/budget_flights */ export const createApiBulkUpdateBudgetFlights = async (args: CreateApiBulkUpdateBudgetFlightsArgs, options?: CreateApiBulkUpdateBudgetFlightsOptions) => { const {baseUrl = "https://api.mediamath.com/api/v3.0", fetch: fetchFn = fetch, Auth0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Auth0 !== undefined) headers.set('authorization', `Bearer ${Auth0}`) const url = `${baseUrl.replace(/\/+$/, '')}/bulk_update/budget_flights${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(createApiBulkUpdateBudgetFlightsResponse, await res.json()) };