import {z} from 'zod' export type LocaleCode = string; export type GetApiDemandAllocationTemplatesTemplateIdArgs = {locale?: LocaleCode | undefined}; export type GetApiDemandAllocationTemplatesTemplateIdOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const templateID = z.string(); export const templateSupplierResponse = z.object({supplier_id: z.number().int().gte(0).optional(), name: z.string().min(1).optional(), type: z.enum(["loyalty_panels", "non_panel_or_affiliate", "survey_panels"]).optional()}); export const allocationTemplateExchangeSupplierResponse = z.object({type: z.literal("exchange"), percentage_max: z.number().int().gte(0).lte(100), percentage_min: z.number().int().gte(0).lte(100).nullable(), suppliers: z.array(templateSupplierResponse)}); export const allocationTemplateGroupSupplierResponse = z.object({type: z.literal("group"), name: z.string(), percentage_max: z.number().int().gte(0).lte(100), percentage_min: z.number().int().gte(0).lte(100).nullable(), suppliers: z.array(templateSupplierResponse)}); export const allocationTemplateBlockedSupplierResponse = z.object({type: z.literal("blocked"), suppliers: z.array(templateSupplierResponse)}); export const getGlobalAllocationTemplateByTemplateIdResponse = z.object({template_id: templateID, name: z.string(), supply_allocations: z.array(z.union([allocationTemplateExchangeSupplierResponse, allocationTemplateGroupSupplierResponse, allocationTemplateBlockedSupplierResponse]))}); 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 a global allocation template by ID * * GET /demand/allocation-templates/{template_id} */ export const getApiDemandAllocationTemplatesTemplateId = async (args: GetApiDemandAllocationTemplatesTemplateIdArgs, options?: GetApiDemandAllocationTemplatesTemplateIdOptions) => { 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.locale != null) query.append("locale", String(args.locale)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/demand/allocation-templates/${encodeURIComponent(String(args.template_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(getGlobalAllocationTemplateByTemplateIdResponse, await res.json()) };