import {z} from 'zod' export type AudienceSegmentsGroup = {operator?: ('AND' | 'OR') | undefined, segments?: (Array<{audience_segment_id?: number | undefined, user_cpm?: number | undefined}>) | undefined}; export type CreateApiAudienceSegmentsCpmEstimateArgs = {extended?: boolean | undefined, body: {currency_code: string, include?: Array | undefined, exclude?: AudienceSegmentsGroup | undefined}}; export type CreateApiAudienceSegmentsCpmEstimateOptions = {baseUrl?: string | undefined, Auth0?: string | undefined, fetch?: typeof fetch | undefined}; export const audienceSegmentShort = z.object({id: z.number().int().optional(), name: z.string().optional(), full_path: z.string().optional(), cpm: z.number().optional(), user_cpm: z.number().nullable().optional(), uniques: z.number().int().nullable().optional(), code: z.string().nullable().optional()}); export const singleMetadata = z.object({status: z.string()}); export const createApiAudienceSegmentsCpmEstimateResponse = z.object({data: z.object({cpm: z.object({currency_code: z.string().min(3).max(3).optional(), includes_taxed_segments: z.boolean().optional(), audience_cpm_min: z.number().gte(0).lte(999999999999.99), audience_cpm_max: z.number().gte(0).lte(999999999999.99)}).optional(), segments: z.array(audienceSegmentShort).optional()}).optional(), meta: singleMetadata.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 }; /** * Audience Segments CPM Estimate * * POST /audience_segments/cpm_estimate */ export const createApiAudienceSegmentsCpmEstimate = async (args: CreateApiAudienceSegmentsCpmEstimateArgs, options?: CreateApiAudienceSegmentsCpmEstimateOptions) => { 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 (args.extended != null) query.append("extended", String(args.extended)) if (Auth0 !== undefined) headers.set('authorization', `Bearer ${Auth0}`) const url = `${baseUrl.replace(/\/+$/, '')}/audience_segments/cpm_estimate${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(createApiAudienceSegmentsCpmEstimateResponse, await res.json()) };