import {z} from 'zod' export type GetApiRrApiV1ContractsContractIdTierPriceOverridesArgs = {contract_id: number, limit?: number | undefined, offset?: number | undefined}; export type GetApiRrApiV1ContractsContractIdTierPriceOverridesOptions = {baseUrl?: string | undefined, knoxApiToken?: string | undefined, fetch?: typeof fetch | undefined}; export const tierPriceOverrideSourceEnum = z.enum(["manual", "integration"]); export const tierPriceOverride = z.object({id: z.number().int(), tier: z.number().int(), tier_name: z.string(), contract: z.number().int(), deal_name: z.string(), usage_group_id: z.number().int(), usage_group_name: z.string(), price: z.number().gt(-100000000000000).lt(100000000000000).nullable().optional(), percentage: z.number().gt(-100000000000000).lt(100000000000000).nullable().optional(), source: tierPriceOverrideSourceEnum.optional(), integration_name: z.string().max(250).nullable().optional(), external_reference: z.string().max(250).nullable().optional(), notes: z.string().nullable().optional(), created_at: z.string(), last_modified_at: z.string()}); export const paginatedTierPriceOverrideList = z.object({count: z.number().int(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(tierPriceOverride)}); 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 Contract Tier Price Overrides * * GET /rr/api/v1/contracts/{contract_id}/tier-price-overrides */ export const getApiRrApiV1ContractsContractIdTierPriceOverrides = async (args: GetApiRrApiV1ContractsContractIdTierPriceOverridesArgs, options?: GetApiRrApiV1ContractsContractIdTierPriceOverridesOptions) => { const {baseUrl = "https://api.meetcampfire.com", fetch: fetchFn = fetch, knoxApiToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (knoxApiToken !== undefined) headers.set("Authorization", knoxApiToken) const url = `${baseUrl.replace(/\/+$/, '')}/rr/api/v1/contracts/${encodeURIComponent(String(args.contract_id))}/tier-price-overrides${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(paginatedTierPriceOverrideList, await res.json()) };