import {z} from 'zod' export type GetApiDemandAccountsAccountIdAllocationTemplatesArgs = {page_size?: number | undefined, start_after?: string | undefined, end_before?: string | undefined}; export type GetApiDemandAccountsAccountIdAllocationTemplatesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const templateID = z.string(); export const lastModifiedAt = z.string(); export const userID = z.string(); export const accountAllocationTemplateListItem = z.object({template_id: templateID, name: z.string(), last_modified_at: lastModifiedAt, last_modified_by: userID}); export const listAccountAllocationTemplatesResponse = z.object({templates: z.array(accountAllocationTemplateListItem), has_more: z.boolean(), next_cursor: z.string()}); 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 }; /** * Lists allocation templates for an account * * GET /demand/accounts/{account_id}/allocation-templates */ export const getApiDemandAccountsAccountIdAllocationTemplates = async (args: GetApiDemandAccountsAccountIdAllocationTemplatesArgs, options?: GetApiDemandAccountsAccountIdAllocationTemplatesOptions) => { 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.page_size != null) query.append("page_size", String(args.page_size)) if (args.start_after != null) query.append("start_after", String(args.start_after)) if (args.end_before != null) query.append("end_before", String(args.end_before)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/demand/accounts/${encodeURIComponent(String(args.account_id))}/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(listAccountAllocationTemplatesResponse, await res.json()) };