import {z} from 'zod' export type GetApiAgenciesArgs = {page_limit?: number | undefined, page_offset?: number | undefined, sort_by?: string | undefined, q?: string | undefined, with?: 'organization' | undefined, full?: string | undefined}; export type GetApiAgenciesOptions = {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 agencyCollection = z.object({data: z.array(z.object({id: z.number().int().optional(), name: z.string().optional(), entity_type: z.string().optional()})).optional(), meta: listMetadata.optional()}); export const agencyCollectionFull = z.object({data: z.object({ads_txt_verified: z.enum(["ANY", "AUTHORIZED_ONLY", "AUTHORIZED_DIRECT_ONLY", "NONE"]).optional(), allow_x_adv_optimization: z.boolean().optional(), allow_x_adv_pixels: z.boolean().optional(), dmp_enabled: z.enum(["disabled", "inherits"]).optional(), eligible_for_data_sharing: z.boolean().optional(), name: z.string().min(1).max(64).optional(), status: z.boolean().optional(), logo: z.string().max(1024).optional(), mfa_avoidance: z.enum(["INHERIT", "ALLOW_ALL", "EXCLUDE_MFA"]).optional(), id: z.number().int().optional(), created_on: z.string().optional(), entity_type: z.string().optional(), organization_id: z.number().int().gte(1).optional(), updated_on: z.string().optional(), version: z.number().int().optional(), organization: z.object({id: z.number().int().optional(), name: z.string().optional(), entity_type: z.string().optional(), rel: z.string().optional()}).optional(), sales_contact_id: z.number().int().optional(), billing_contact_id: z.number().int().optional(), traffic_contact_id: z.number().int().optional()}).optional(), meta: listMetadata.optional()}); export const getApiAgenciesResponse = z.union([agencyCollection, agencyCollectionFull]); 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 Agencies * * GET /agencies */ export const getApiAgencies = async (args: GetApiAgenciesArgs, options?: GetApiAgenciesOptions) => { 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(/\/+$/, '')}/agencies${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(getApiAgenciesResponse, await res.json()) };