import {z} from 'zod' 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 TaxCodes = Array; export type FixedChargeModelEnum = 'standard' | 'graduated' | 'volume'; export type FixedChargeUpdateInput = {fixed_charge: {invoice_display_name?: (string | null) | undefined, units?: string | undefined, apply_units_immediately?: boolean | undefined, properties?: ChargeProperties | undefined, tax_codes?: TaxCodes | undefined, add_on_id?: string | undefined, add_on_code?: string | undefined, code?: string | undefined, charge_model?: FixedChargeModelEnum | undefined, pay_in_advance?: boolean | undefined, prorated?: boolean | undefined, cascade_updates?: boolean | undefined}}; export type UpdateApiPlansCodeFixedChargesFixedChargeCodeArgs = {body: FixedChargeUpdateInput}; export type UpdateApiPlansCodeFixedChargesFixedChargeCodeOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const fixedChargeProperties = z.object({amount: 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(), 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 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 fixedChargeObject = z.object({lago_id: z.string(), lago_add_on_id: z.string(), invoice_display_name: z.string().nullable(), add_on_code: z.string(), created_at: z.string(), code: z.string(), charge_model: z.enum(["standard", "graduated", "volume"]), pay_in_advance: z.boolean(), prorated: z.boolean(), properties: fixedChargeProperties, units: z.string(), lago_parent_id: z.string().nullable().optional(), taxes: z.array(taxObject).optional()}); export const fixedCharge = z.object({fixed_charge: fixedChargeObject}); 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 fixed charge * * PUT /plans/{code}/fixed_charges/{fixed_charge_code} */ export const updateApiPlansCodeFixedChargesFixedChargeCode = async (args: UpdateApiPlansCodeFixedChargesFixedChargeCodeArgs, options?: UpdateApiPlansCodeFixedChargesFixedChargeCodeOptions) => { 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))}/fixed_charges/${encodeURIComponent(String(args.fixed_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(fixedCharge, await res.json()) };