import {z} from 'zod' export type GetApiPlansCodeFixedChargesFixedChargeCodeOptions = {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 }; /** * Retrieve a fixed charge * * GET /plans/{code}/fixed_charges/{fixed_charge_code} */ export const getApiPlansCodeFixedChargesFixedChargeCode = async (options?: GetApiPlansCodeFixedChargesFixedChargeCodeOptions) => { const {baseUrl = "https://api.getlago.com/api/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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: "GET", headers}) 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()) };