import {z} from 'zod' export type GetApiRrApiV1ContractsContractIdUsageIdArgs = {contract_id: number, id: number}; export type GetApiRrApiV1ContractsContractIdUsageIdOptions = {baseUrl?: string | undefined, knoxApiToken?: string | undefined, fetch?: typeof fetch | undefined}; export const contractProductBundleLineRead = z.object({id: z.number().int(), product: z.number().int(), product_name: z.string(), amount: z.number().gt(-1000000000000000000).lt(1000000000000000000), percentage: z.string(), original_percentage: z.number().gt(-1000).lt(1000).nullable().optional()}); export const contractProductBundleRead = z.object({id: z.number().int(), source_bundle: z.number().int().nullable().optional(), source_bundle_name: z.string().nullable(), total_amount: z.number().gt(-1000000000000000000).lt(1000000000000000000), currency: z.string().max(3).optional(), lines: z.array(contractProductBundleLineRead)}); export const contractUsage = z.object({id: z.number().int(), contract: z.number().int(), product: z.number().int().nullable().optional(), product_bundle: z.string(), contract_product_bundle: contractProductBundleRead, product_name: z.string(), product_bundle_name: z.string(), usage_group: z.number().int().nullable().optional(), usage_group_name: z.string(), department: z.number().int().nullable().optional(), tags: z.string().optional(), description: z.string().nullable().optional(), start_date: z.string().nullable().optional(), end_date: z.string().nullable().optional(), lines: z.string(), created_at: z.string().nullable(), last_modified_at: z.string().nullable()}); 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 Contract Usage Revenue * * GET /rr/api/v1/contracts/{contract_id}/usage/{id} */ export const getApiRrApiV1ContractsContractIdUsageId = async (args: GetApiRrApiV1ContractsContractIdUsageIdArgs, options?: GetApiRrApiV1ContractsContractIdUsageIdOptions) => { 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 (knoxApiToken !== undefined) headers.set("Authorization", knoxApiToken) const url = `${baseUrl.replace(/\/+$/, '')}/rr/api/v1/contracts/${encodeURIComponent(String(args.contract_id))}/usage/${encodeURIComponent(String(args.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(contractUsage, await res.json()) };