import {z} from 'zod' export type GetApiPixelBundlesArgs = {page_limit?: number | undefined, page_offset?: number | undefined, sort_by?: string | undefined, q?: string | undefined, with?: 'organization' | undefined, full?: string | undefined}; export type GetApiPixelBundlesOptions = {baseUrl?: string | undefined, Auth0?: string | undefined, fetch?: typeof fetch | undefined}; 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 pixelBundleCollection = z.object({data: z.array(z.object({id: z.number().int().optional(), name: z.string().optional(), entity_type: z.string().optional()})).nullable().optional(), meta: listMetadata.optional()}); export const pixelBundleResponse = z.object({pixel_type: z.enum(["creative", "event", "data", "segment", "dynamic"]).optional(), advertiser_id: z.number().int().nullable().optional(), agency_id: z.number().int().nullable().optional(), id: z.number().int().gte(1).optional(), provider_id: z.number().int().gte(1).nullable().optional(), name: z.string().min(1).max(256).optional(), cost_pct_cpm: z.number().nullable().optional(), eligible: z.boolean().optional(), tags: z.string().nullable().optional(), cost_cpm: z.number().nullable().optional(), external_identifier: z.string().optional(), tag_type: z.enum(["dfa", "uat", "image", "iframe", "js"]).optional(), pricing: z.enum(["CPM", "CPTS"]).nullable().optional(), cost_cpts: z.number().nullable().optional(), segment_op: z.enum(["AND", "OR"]).nullable().optional(), rmx_friendly: z.boolean().optional(), rmx_merit: z.boolean().optional(), rmx_pc_window_minutes: z.number().int().nullable().optional(), rmx_pv_window_minutes: z.number().int().nullable().optional(), rmx_conversion_type: z.enum(["one", "variable"]).optional(), rmx_conversion_minutes: z.number().int().optional(), currency: z.enum(["S1", "S2", "V1", "V2"]).nullable().optional(), revenue: z.enum(["S1", "S2", "V1", "V2"]).nullable().optional(), currency_fixed: z.string().nullable().optional(), status: z.boolean().optional(), event_urls: z.array(z.string()).nullable().optional(), container_tags: z.array(z.string().max(1024)).nullable().optional(), version: z.number().int().optional()}); export const pixelBundleCollectionFull = z.object({data: pixelBundleResponse.nullable().optional(), meta: listMetadata.optional()}); export const getApiPixelBundlesResponse = z.union([pixelBundleCollection, pixelBundleCollectionFull]); 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 }; /** * List Pixel Bundles * * GET /pixel_bundles */ export const getApiPixelBundles = async (args: GetApiPixelBundlesArgs, options?: GetApiPixelBundlesOptions) => { const {baseUrl = "https://api.mediamath.com/api/v3.0", fetch: fetchFn = fetch, Auth0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page_limit != null) query.append("page_limit", String(args.page_limit)) if (args.page_offset != null) query.append("page_offset", String(args.page_offset)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.q != null) query.append("q", String(args.q)) if (args.with != null) query.append("with", String(args.with)) if (args.full != null) query.append("full", String(args.full)) if (Auth0 !== undefined) headers.set('authorization', `Bearer ${Auth0}`) const url = `${baseUrl.replace(/\/+$/, '')}/pixel_bundles${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(getApiPixelBundlesResponse, await res.json()) };