import {z} from 'zod' export type GetApiRrApiV1ContractsContractIdBundlesArgs = {contract_id: number, include_deleted?: boolean | undefined, last_modified_at__gte?: string | undefined, last_modified_at__lte?: string | undefined}; export type GetApiRrApiV1ContractsContractIdBundlesOptions = {baseUrl?: string | undefined, knoxApiToken?: string | undefined, fetch?: typeof fetch | undefined}; export const contractBundleLine = z.object({id: z.number().int(), is_deleted: z.boolean(), deleted_at: z.string().nullable(), contract_bundle: z.number().int().optional(), product_name: z.string(), amount: z.number().gt(-1000000000000000000).lt(1000000000000000000).nullable().optional(), created_at: z.string(), last_modified_at: z.string(), customer: z.number().int(), product: z.number().int().nullable().optional()}); export const prorationMethodEnum = z.enum(["standard", "thirty_day", "daily"]); export const contractBundle = z.object({id: z.number().int(), is_deleted: z.boolean(), deleted_at: z.string().nullable(), end_date: z.string().nullable().optional(), rebalance_journal_entries_indefinitely: z.boolean().optional(), lines: z.array(contractBundleLine), bundle_name: z.string().max(250), bundle_description: z.string().max(250).nullable().optional(), start_date: z.string(), use_daily_accounting: z.boolean().optional(), proration_method: prorationMethodEnum.optional(), use_catchup: z.boolean().optional(), catchup_date: z.string().nullable().optional(), created_at: z.string(), last_modified_at: z.string(), customer: z.number().int(), contract: z.number().int()}); export const getApiRrApiV1ContractsContractIdBundlesResponse = z.array(contractBundle); 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 Bundles * * GET /rr/api/v1/contracts/{contract_id}/bundles */ export const getApiRrApiV1ContractsContractIdBundles = async (args: GetApiRrApiV1ContractsContractIdBundlesArgs, options?: GetApiRrApiV1ContractsContractIdBundlesOptions) => { 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.include_deleted != null) query.append("include_deleted", String(args.include_deleted)) if (args.last_modified_at__gte != null) query.append("last_modified_at__gte", String(args.last_modified_at__gte)) if (args.last_modified_at__lte != null) query.append("last_modified_at__lte", String(args.last_modified_at__lte)) if (knoxApiToken !== undefined) headers.set("Authorization", knoxApiToken) const url = `${baseUrl.replace(/\/+$/, '')}/rr/api/v1/contracts/${encodeURIComponent(String(args.contract_id))}/bundles${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(getApiRrApiV1ContractsContractIdBundlesResponse, await res.json()) };