import {z} from 'zod' export type GetApiPathAudienceSegmentsArgs = {page_limit?: number | undefined, page_offset?: number | undefined, sort_by?: string | undefined, q: string, currency_code?: string | undefined, advertiser_id?: number | undefined, only_brain_enabled?: boolean | undefined, parent_id?: number | undefined, with?: 'audience_vendor' | undefined}; export type GetApiPathAudienceSegmentsOptions = {baseUrl?: string | undefined, Auth0?: string | undefined, fetch?: typeof fetch | undefined}; export const cpm = z.object({currency_code: z.string().optional(), value: z.number().optional()}); export const audienceVendor = z.object({id: z.number().int().optional(), name: z.string().optional(), version: z.number().int().optional(), vendor_id: z.number().int().optional(), mma_free_cm: z.boolean().optional(), created_on: z.string().optional(), updated_on: z.string().optional(), provides_batch_segments: z.boolean().optional(), campaign_brain_enabled: z.boolean().optional(), pin_top_search: z.boolean().optional(), bidder_code: z.string().optional(), namespace_code: z.string().optional()}); export const pathAudienceSegment = z.object({id: z.number().int().optional(), name: z.string().optional(), version: z.number().int().optional(), parent_audience_segment_id: z.number().int().nullable().optional(), full_path: z.string().optional(), uniques: z.number().int().optional(), retail_cpm: z.array(cpm).optional(), audience_vendor_id: z.number().int().optional(), code: z.string().nullable().optional(), buyable: z.boolean().optional(), child_count: z.number().int().optional(), wholesale_cpm: z.array(cpm).optional(), type: z.string().optional(), taxonomy_id: z.number().int().optional(), created_on: z.string().optional(), updated_on: z.string().optional(), audience_vendor: audienceVendor.nullable().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 pathAudienceSegmentsCollectionFull = z.object({data: z.array(pathAudienceSegment).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 }; /** * List Path Audience Segments * * GET /path_audience_segments */ export const getApiPathAudienceSegments = async (args: GetApiPathAudienceSegmentsArgs, options?: GetApiPathAudienceSegmentsOptions) => { 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.currency_code != null) query.append("currency_code", String(args.currency_code)) if (args.advertiser_id != null) query.append("advertiser_id", String(args.advertiser_id)) if (args.only_brain_enabled != null) query.append("only_brain_enabled", String(args.only_brain_enabled)) if (args.parent_id != null) query.append("parent_id", String(args.parent_id)) if (args.with != null) query.append("with", String(args.with)) if (Auth0 !== undefined) headers.set('authorization', `Bearer ${Auth0}`) const url = `${baseUrl.replace(/\/+$/, '')}/path_audience_segments${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(pathAudienceSegmentsCollectionFull, await res.json()) };