import {z} from 'zod' export type GetApiAudienceSegmentsFrequentlyUsedArgs = {advertiser_id?: number | undefined, campaign_id?: number | undefined, top?: number | undefined}; export type GetApiAudienceSegmentsFrequentlyUsedOptions = {baseUrl?: string | undefined, Auth0?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiAudienceSegmentsFrequentlyUsedResponse = z.object({data: z.array(z.object({id: z.number().optional(), external_code: z.string().optional(), friendly_name: z.string().optional(), vendor_id: z.number().optional(), vendor_name: z.string().optional(), dimension_id: z.number().optional(), code: z.string().optional(), frequency: z.number().optional(), name: z.string().optional(), retail_cpm: z.number().optional(), objective_id: z.number().optional(), targeting_vendor_id: z.number().optional(), segment_objective_name: z.string().optional()})).nullable().optional(), meta: z.object({status: z.string().optional()}).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 }; /** * Frequently Used Audience Segments * * GET /audience_segments/frequently_used */ export const getApiAudienceSegmentsFrequentlyUsed = async (args: GetApiAudienceSegmentsFrequentlyUsedArgs, options?: GetApiAudienceSegmentsFrequentlyUsedOptions) => { 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.advertiser_id != null) query.append("advertiser_id", String(args.advertiser_id)) if (args.campaign_id != null) query.append("campaign_id", String(args.campaign_id)) if (args.top != null) query.append("top", String(args.top)) if (Auth0 !== undefined) headers.set('authorization', `Bearer ${Auth0}`) const url = `${baseUrl.replace(/\/+$/, '')}/audience_segments/frequently_used${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(getApiAudienceSegmentsFrequentlyUsedResponse, await res.json()) };