import {z} from 'zod' export type GetApiAtomicCreativesHealthcheckArgs = {q: string}; export type GetApiAtomicCreativesHealthcheckOptions = {baseUrl?: string | undefined, Auth0?: string | undefined, fetch?: typeof fetch | undefined}; export const atomicCreativeHealthcheckResponse = z.object({atomic_creative_id: z.number().int().optional(), creative_name: z.string().optional(), advertiser_id: z.number().int().optional(), advertiser_name: z.string().optional(), entity_type: z.string().optional(), net_status: z.enum(["APPROVED", "REJECTED", "PENDING", "PENDING > 48 Hours", "N/A"]).optional(), adx_open_auction: z.string().optional(), adx_deals: z.string().optional(), app_nexus: z.string().optional(), microsoft_ad_exchange: z.string().optional(), right_media_exchange: z.string().optional(), brightroll_for_display: z.string().optional(), mopub_direct: z.string().optional(), freakout: z.string().optional(), freewheel: z.string().optional(), adx_open_auction_rejected_reason: z.string().optional(), adx_deals_rejected_reason: z.string().optional(), app_nexus_rejected_reason: z.string().optional(), microsoft_ad_exchange_rejected_reason: z.string().optional(), right_media_exchange_rejected_reason: z.string().optional(), brightroll_for_display_rejected_reason: z.string().optional(), mopub_direct_rejected_reason: z.string().optional(), freakout_rejected_reason: z.string().optional(), freewheel_rejected_reason: z.string().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 getApiAtomicCreativesHealthcheckResponse = z.object({data: z.array(atomicCreativeHealthcheckResponse).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 }; /** * Healthcheck for Atomic Creatives * * GET /atomic_creatives/healthcheck */ export const getApiAtomicCreativesHealthcheck = async (args: GetApiAtomicCreativesHealthcheckArgs, options?: GetApiAtomicCreativesHealthcheckOptions) => { 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.q != null) query.append("q", String(args.q)) if (Auth0 !== undefined) headers.set('authorization', `Bearer ${Auth0}`) const url = `${baseUrl.replace(/\/+$/, '')}/atomic_creatives/healthcheck${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(getApiAtomicCreativesHealthcheckResponse, await res.json()) };