import {z} from 'zod' export type BusinessUnitID = string; export type CountryCode = string; export type GetApiDemandAccountsAccountIdDefaultAllocationTemplatesArgs = {business_unit_id?: BusinessUnitID | undefined, country_code?: CountryCode | undefined}; export type GetApiDemandAccountsAccountIdDefaultAllocationTemplatesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const templateID = z.string(); export const defaultAllocationTemplate = z.object({id: templateID, type: z.enum(["account", "country"]), version: z.number().int()}); export const getDefaultAllocationTemplatesResponse = z.object({default_templates: z.array(defaultAllocationTemplate)}); 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 }; /** * Retrieve the IDs of relevant default allocation templates * * GET /demand/accounts/{account_id}/default-allocation-templates */ export const getApiDemandAccountsAccountIdDefaultAllocationTemplates = async (args: GetApiDemandAccountsAccountIdDefaultAllocationTemplatesArgs, options?: GetApiDemandAccountsAccountIdDefaultAllocationTemplatesOptions) => { const {baseUrl = "https://api.cint.com/v1", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.business_unit_id != null) query.append("business_unit_id", String(args.business_unit_id)) if (args.country_code != null) query.append("country_code", String(args.country_code)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/demand/accounts/${encodeURIComponent(String(args.account_id))}/default-allocation-templates${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(getDefaultAllocationTemplatesResponse, await res.json()) };