import {z} from 'zod' export type GetApiCustomersExternalCustomerIdCurrentUsageArgs = {external_customer_id: string, external_subscription_id: string, apply_taxes?: boolean | undefined, charge_id?: string | undefined, charge_code?: string | undefined, billable_metric_code?: string | undefined, group?: {[key: string]: string} | undefined, filter_by_charge_id?: string | undefined, filter_by_charge_code?: string | undefined, filter_by_group?: {[key: string]: string} | undefined, full_usage?: boolean | undefined, filter_by_presentation?: string | undefined}; export type GetApiCustomersExternalCustomerIdCurrentUsageOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const currency = z.enum(["AED", "AFN", "ALL", "AMD", "ANG", "AOA", "ARS", "AUD", "AWG", "AZN", "BAM", "BBD", "BDT", "BGN", "BIF", "BMD", "BND", "BOB", "BRL", "BSD", "BWP", "BYN", "BZD", "CAD", "CDF", "CHF", "CLF", "CLP", "CNY", "COP", "CRC", "CVE", "CZK", "DJF", "DKK", "DOP", "DZD", "EGP", "ETB", "EUR", "FJD", "FKP", "GBP", "GEL", "GHS", "GIP", "GMD", "GNF", "GTQ", "GYD", "HKD", "HNL", "HRK", "HTG", "HUF", "IDR", "ILS", "INR", "ISK", "JMD", "JPY", "KES", "KGS", "KHR", "KMF", "KRW", "KYD", "KZT", "LAK", "LBP", "LKR", "LRD", "LSL", "MAD", "MDL", "MGA", "MKD", "MMK", "MNT", "MOP", "MRO", "MUR", "MVR", "MWK", "MXN", "MYR", "MZN", "NAD", "NGN", "NIO", "NOK", "NPR", "NZD", "PAB", "PEN", "PGK", "PHP", "PKR", "PLN", "PYG", "QAR", "RON", "RSD", "RUB", "RWF", "SAR", "SBD", "SCR", "SEK", "SGD", "SHP", "SLL", "SOS", "SRD", "STD", "SYP", "SZL", "THB", "TJS", "TND", "TOP", "TRY", "TTD", "TWD", "TZS", "UAH", "UGX", "USD", "UYU", "UZS", "VND", "VUV", "WST", "XAF", "XCD", "XOF", "XPF", "YER", "ZAR", "ZMW"]); export const usagePricingUnitDetailsObject = z.object({amount_cents: z.number().int(), short_name: z.string(), conversion_rate: z.string()}).nullable(); export const chargeModelEnum = z.enum(["dynamic", "graduated", "graduated_percentage", "package", "percentage", "standard", "volume"]); export const presentationBreakdownsObject = z.array(z.object({presentation_by: z.record(z.string(), z.string()).optional(), units: z.string().optional()})); export const customerChargeFiltersUsageObject = z.array(z.object({units: z.string(), total_aggregated_units: z.string(), amount_cents: z.number().int(), events_count: z.number().int(), invoice_display_name: z.string().nullable().optional(), pricing_unit_details: usagePricingUnitDetailsObject.optional(), values: z.record(z.string(), z.array(z.string())).nullable(), presentation_breakdowns: presentationBreakdownsObject.optional()})); export const customerChargeGroupedUsageObject = z.array(z.object({amount_cents: z.number().int(), events_count: z.number().int(), units: z.string(), total_aggregated_units: z.string(), pricing_unit_details: usagePricingUnitDetailsObject.optional(), grouped_by: z.record(z.string(), z.string()), filters: customerChargeFiltersUsageObject.optional(), presentation_breakdowns: presentationBreakdownsObject.optional()})); export const customerChargeUsageObject = z.object({units: z.string(), total_aggregated_units: z.string(), events_count: z.number().int(), amount_cents: z.number().int(), amount_currency: currency, pricing_unit_details: usagePricingUnitDetailsObject.optional(), charge: z.object({lago_id: z.string(), code: z.string(), charge_model: chargeModelEnum, invoice_display_name: z.string().nullable().optional()}), billable_metric: z.object({lago_id: z.string(), name: z.string(), code: z.string(), aggregation_type: z.enum(["count_agg", "sum_agg", "max_agg", "unique_count_agg", "weighted_sum_agg", "latest_agg"])}), filters: customerChargeFiltersUsageObject.optional(), grouped_usage: customerChargeGroupedUsageObject.optional(), presentation_breakdowns: presentationBreakdownsObject.optional()}); export const customerUsageObject = z.object({from_datetime: z.string(), to_datetime: z.string(), issuing_date: z.string(), lago_invoice_id: z.string().nullable(), currency: currency, amount_cents: z.number().int(), taxes_amount_cents: z.number().int(), total_amount_cents: z.number().int(), charges_usage: z.array(customerChargeUsageObject)}); export const customerUsage = z.object({customer_usage: customerUsageObject}); 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 customer current usage * * GET /customers/{external_customer_id}/current_usage */ export const getApiCustomersExternalCustomerIdCurrentUsage = async (args: GetApiCustomersExternalCustomerIdCurrentUsageArgs, options?: GetApiCustomersExternalCustomerIdCurrentUsageOptions) => { 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 (args.external_subscription_id != null) query.append("external_subscription_id", String(args.external_subscription_id)) if (args.apply_taxes != null) query.append("apply_taxes", String(args.apply_taxes)) if (args.charge_id != null) query.append("charge_id", String(args.charge_id)) if (args.charge_code != null) query.append("charge_code", String(args.charge_code)) if (args.billable_metric_code != null) query.append("billable_metric_code", String(args.billable_metric_code)) if (args.group != null) query.append("group", JSON.stringify(args.group)) if (args.filter_by_charge_id != null) query.append("filter_by_charge_id", String(args.filter_by_charge_id)) if (args.filter_by_charge_code != null) query.append("filter_by_charge_code", String(args.filter_by_charge_code)) if (args.filter_by_group != null) query.append("filter_by_group", JSON.stringify(args.filter_by_group)) if (args.full_usage != null) query.append("full_usage", String(args.full_usage)) if (args.filter_by_presentation != null) query.append("filter_by_presentation", String(args.filter_by_presentation)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/customers/${encodeURIComponent(String(args.external_customer_id))}/current_usage${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(customerUsage, await res.json()) };