import {z} from 'zod' export type ChargeObjectProperties = {grouped_by?: Array | undefined, pricing_group_keys?: Array | undefined, graduated_ranges?: (Array<{from_value: number, to_value: number | null, flat_amount: string, per_unit_amount: string}>) | undefined, graduated_percentage_ranges?: (Array<{from_value: number, to_value: number | null, rate: unknown, flat_amount: unknown}>) | undefined, amount?: string | undefined, free_units?: number | undefined, package_size?: number | undefined, rate?: string | undefined, fixed_amount?: string | undefined, free_units_per_events?: (number | null) | undefined, free_units_per_total_aggregation?: (string | null) | undefined, per_transaction_max_amount?: (unknown | null) | undefined, per_transaction_min_amount?: (unknown | null) | undefined, volume_ranges?: (Array<{from_value: number, to_value: number | null, flat_amount: string, per_unit_amount: string}>) | undefined, presentation_group_keys?: (Array<{value?: string | undefined, options?: ({display_in_invoice?: boolean | undefined}) | undefined}>) | undefined}; export type ChargeProperties = {grouped_by?: Array | undefined, pricing_group_keys?: Array | undefined, graduated_ranges?: (Array<{from_value: number, to_value: number | null, flat_amount: string, per_unit_amount: string}>) | undefined, graduated_percentage_ranges?: (Array<{from_value: number, to_value: number | null, rate: unknown, flat_amount: unknown}>) | undefined, amount?: string | undefined, free_units?: number | undefined, package_size?: number | undefined, rate?: string | undefined, fixed_amount?: string | undefined, free_units_per_events?: (number | null) | undefined, free_units_per_total_aggregation?: (string | null) | undefined, per_transaction_max_amount?: (unknown | null) | undefined, per_transaction_min_amount?: (unknown | null) | undefined, volume_ranges?: (Array<{from_value: number, to_value: number | null, flat_amount: string, per_unit_amount: string}>) | undefined}; export type ChargeFilterInput = {invoice_display_name?: (string | null) | undefined, properties: ChargeProperties, values: {[key: string]: Array}}; export type TaxCodes = Array; export type ChargeModelEnum = 'dynamic' | 'graduated' | 'graduated_percentage' | 'package' | 'percentage' | 'standard' | 'volume'; export type ChargeUpdateInput = {charge: {invoice_display_name?: string | undefined, min_amount_cents?: number | undefined, properties?: ChargeObjectProperties | undefined, filters?: Array | undefined, tax_codes?: TaxCodes | undefined, applied_pricing_unit?: ({code?: string | undefined, conversion_rate?: string | undefined}) | undefined, billable_metric_id?: string | undefined, code?: string | undefined, charge_model?: ChargeModelEnum | undefined, pay_in_advance?: boolean | undefined, invoiceable?: boolean | undefined, regroup_paid_fees?: ('null' | 'invoice' | null) | undefined, prorated?: boolean | undefined, accepts_target_wallet?: (boolean | null) | undefined, cascade_updates?: boolean | undefined}}; export type UpdateApiPlansCodeChargesChargeCodeArgs = {body: ChargeUpdateInput}; export type UpdateApiPlansCodeChargesChargeCodeOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const chargeModelEnum = z.enum(["dynamic", "graduated", "graduated_percentage", "package", "percentage", "standard", "volume"]); export const chargeObjectProperties = z.object({grouped_by: z.array(z.string()).optional(), pricing_group_keys: z.array(z.string()).optional(), graduated_ranges: z.array(z.object({from_value: z.number().int(), to_value: z.number().int().nullable(), flat_amount: z.string(), per_unit_amount: z.string()})).optional(), graduated_percentage_ranges: z.array(z.object({from_value: z.number().int(), to_value: z.number().int().nullable(), rate: z.string(), flat_amount: z.string()})).optional(), amount: z.string().optional(), free_units: z.number().int().optional(), package_size: z.number().int().optional(), rate: z.string().optional(), fixed_amount: z.string().optional(), free_units_per_events: z.number().int().nullable().optional(), free_units_per_total_aggregation: z.string().nullable().optional(), per_transaction_max_amount: z.string().nullable().optional(), per_transaction_min_amount: z.string().nullable().optional(), volume_ranges: z.array(z.object({from_value: z.number().int(), to_value: z.number().int().nullable(), flat_amount: z.string(), per_unit_amount: z.string()})).optional(), presentation_group_keys: z.array(z.object({value: z.string().optional(), options: z.object({display_in_invoice: z.boolean().optional()}).optional()})).optional()}); export const chargeProperties = z.object({grouped_by: z.array(z.string()).optional(), pricing_group_keys: z.array(z.string()).optional(), graduated_ranges: z.array(z.object({from_value: z.number().int(), to_value: z.number().int().nullable(), flat_amount: z.string(), per_unit_amount: z.string()})).optional(), graduated_percentage_ranges: z.array(z.object({from_value: z.number().int(), to_value: z.number().int().nullable(), rate: z.string(), flat_amount: z.string()})).optional(), amount: z.string().optional(), free_units: z.number().int().optional(), package_size: z.number().int().optional(), rate: z.string().optional(), fixed_amount: z.string().optional(), free_units_per_events: z.number().int().nullable().optional(), free_units_per_total_aggregation: z.string().nullable().optional(), per_transaction_max_amount: z.string().nullable().optional(), per_transaction_min_amount: z.string().nullable().optional(), volume_ranges: z.array(z.object({from_value: z.number().int(), to_value: z.number().int().nullable(), flat_amount: z.string(), per_unit_amount: z.string()})).optional()}); export const chargeFilterObject = z.object({lago_id: z.string().optional(), charge_code: z.string().optional(), invoice_display_name: z.string().nullable(), properties: chargeProperties, values: z.record(z.string(), z.array(z.string()))}); export const taxObject = z.object({lago_id: z.string(), name: z.string(), code: z.string(), description: z.string().nullable(), rate: z.number(), applied_to_organization: z.boolean(), applied_to_billing_entities_codes: z.array(z.string()).optional(), created_at: z.string()}); export const chargeObject = z.object({lago_id: z.string(), lago_billable_metric_id: z.string(), code: z.string().optional(), billable_metric_code: z.string(), invoice_display_name: z.string().nullable().optional(), created_at: z.string(), charge_model: chargeModelEnum, pay_in_advance: z.boolean(), invoiceable: z.boolean(), regroup_paid_fees: z.enum(["null", "invoice"]).nullable(), prorated: z.boolean(), min_amount_cents: z.number().int(), properties: chargeObjectProperties, filters: z.array(chargeFilterObject), taxes: z.array(taxObject).optional(), applied_pricing_unit: z.object({code: z.string(), conversion_rate: z.string()}).nullable().optional(), accepts_target_wallet: z.boolean().optional(), lago_parent_id: z.string().nullable().optional()}); export const charge = z.object({charge: chargeObject}); 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 }; /** * Update a charge * * PUT /plans/{code}/charges/{charge_code} */ export const updateApiPlansCodeChargesChargeCode = async (args: UpdateApiPlansCodeChargesChargeCodeArgs, options?: UpdateApiPlansCodeChargesChargeCodeOptions) => { const {baseUrl = "https://api.getlago.com/api/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/plans/${encodeURIComponent(String(args.code))}/charges/${encodeURIComponent(String(args.charge_code))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(charge, await res.json()) };