import {z} from 'zod' export type GetApiNewStrategyGroupsArgs = {page_limit?: number | undefined, page_offset?: number | undefined, sort_by?: string | undefined, q?: string | undefined, full?: string | undefined, with?: string | undefined}; export type GetApiNewStrategyGroupsOptions = {baseUrl?: string | undefined, Auth0?: string | undefined, fetch?: typeof fetch | undefined}; export const newStrategyGroupStrategyEmbed = z.object({id: z.number().int().optional(), entity_type: z.string().optional(), name: z.string().optional(), status: z.boolean().optional(), pacing_type: z.enum(["even", "asap"]).optional(), pacing_amount: z.number().gte(0.01).lte(9999999.99).optional(), pacing_interval: z.enum(["hour", "day"]).optional(), impression_pacing_type: z.enum(["even", "asap", "no-limit"]).optional(), impression_pacing_amount: z.number().int().gte(1).nullable().optional(), impression_pacing_interval: z.enum(["hour", "day"]).optional()}); export const newStrategyGroupListItem = z.object({id: z.number().int().optional(), entity_type: z.string().optional(), name: z.string().optional(), campaign_id: z.number().int().optional(), strategy_count: z.number().int().optional(), strategies: z.array(newStrategyGroupStrategyEmbed).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 newStrategyGroupCollection = z.object({data: z.array(newStrategyGroupListItem).optional(), meta: listMetadata.optional()}); export const newStrategyGroupFull = z.object({name: z.string().min(1).max(255).optional(), description: z.string().max(1024).nullable().optional(), spend_cap_type: z.enum(["even", "asap", "no-limit"]).optional(), spend_cap_amount: z.number().gte(0.01).lte(9999999.99).nullable().optional(), impression_cap_type: z.enum(["even", "asap", "no-limit"]).optional(), impression_cap_amount: z.number().int().gte(1).nullable().optional(), external_identifier: z.string().max(256).nullable().optional(), id: z.number().int().optional(), entity_type: z.string().optional(), version: z.number().int().optional(), campaign_id: z.number().int().optional(), strategy_count: z.number().int().optional(), strategies: z.array(newStrategyGroupStrategyEmbed).optional(), created_on: z.string().optional(), updated_on: z.string().optional()}); export const newStrategyGroupCollectionFull = z.object({data: z.array(newStrategyGroupFull).optional(), meta: listMetadata.optional()}); export const getApiNewStrategyGroupsResponse = z.union([newStrategyGroupCollection, newStrategyGroupCollectionFull]); 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 New Strategy Groups * * GET /new_strategy_groups */ export const getApiNewStrategyGroups = async (args: GetApiNewStrategyGroupsArgs, options?: GetApiNewStrategyGroupsOptions) => { 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.full != null) query.append("full", String(args.full)) if (args.with != null) query.append("with", String(args.with)) if (Auth0 !== undefined) headers.set('authorization', `Bearer ${Auth0}`) const url = `${baseUrl.replace(/\/+$/, '')}/new_strategy_groups${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(getApiNewStrategyGroupsResponse, await res.json()) };