import {z} from 'zod' export type GetApiVendorsVendorIdArgs = {vendor_id: number}; export type GetApiVendorsVendorIdOptions = {baseUrl?: string | undefined, Auth0?: string | undefined, fetch?: typeof fetch | undefined}; export const vendorBase = z.object({id: z.number().int().optional(), version: z.number().int().optional(), name: z.string().optional(), adx_approved: z.boolean().optional(), adx_declaration_required: z.boolean().optional(), adx_vendor_identifier: z.string().max(16).optional(), mm_contract_available: z.boolean().optional(), rate_card_price: z.number().lte(999999999.9999).optional(), rate_card_type: z.enum(["CPM", "FIXED"]).optional(), wholesale_price: z.number().lte(999999999.9999).optional(), is_eligible: z.boolean().optional(), created_on: z.string().nullable().optional(), updated_on: z.string().nullable().optional(), description: z.string().max(4096).optional(), vendor_type: z.enum(["AD_SERVER", "AD_VERIFICATION", "AUDIENCE_VERIFICATION", "CONTEXTUAL", "DATA", "DSP", "DYNAMIC_CREATIVE", "MEASUREMENT", "NETWORK", "OBA_COMPLIANCE", "OTHER", "PIXEL_TRACKING", "RICH_MEDIA", "SURVEY", "VIEWABILITY", "VIEWABILITY_BYO_DISPLAY", "VIEWABILITY_BYO_VIDEO"]).optional(), code: z.string().max(64).optional(), adx_ssl_approved: z.boolean().optional(), adx_video_ssl_approved: z.boolean().optional(), adx_video_approved: z.boolean().optional(), auto_tag_display: z.boolean().optional(), provider_tax: z.number().lte(999999.999999).optional(), set_by: z.string().optional()}); export const singleMetadata = z.object({status: z.string()}); export const getApiVendorsVendorIdResponse = z.object({data: vendorBase.optional(), meta: singleMetadata.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 }; /** * Get a Vendor * * GET /vendors/{vendor_id} */ export const getApiVendorsVendorId = async (args: GetApiVendorsVendorIdArgs, options?: GetApiVendorsVendorIdOptions) => { 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 (Auth0 !== undefined) headers.set('authorization', `Bearer ${Auth0}`) const url = `${baseUrl.replace(/\/+$/, '')}/vendors/${encodeURIComponent(String(args.vendor_id))}${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(getApiVendorsVendorIdResponse, await res.json()) };