import {z} from 'zod' export type GetApiQuotesLagoIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const quoteOrderTypeEnum = z.enum(["subscription_creation", "subscription_amendment", "one_off"]); export const quoteVersionStatusEnum = z.enum(["draft", "approved", "voided"]); export const currencyOrNull = z.enum(["null", "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", "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"]).nullable(); export const quoteVersionVoidReasonEnum = z.enum(["null", "manual", "superseded", "cascade_of_expired", "cascade_of_voided"]).nullable(); export const quoteVersionObject = z.object({lago_id: z.string(), lago_quote_id: z.string(), lago_organization_id: z.string(), version: z.number().int(), status: quoteVersionStatusEnum, currency: currencyOrNull, billing_entity_code: z.string().nullable(), void_reason: quoteVersionVoidReasonEnum, approved_at: z.string().nullable(), voided_at: z.string().nullable(), created_at: z.string(), updated_at: z.string()}); export const quoteOwnerObject = z.object({lago_id: z.string(), email: z.string().nullable()}); export const quoteObjectExtended = z.object({lago_id: z.string(), number: z.string(), order_type: quoteOrderTypeEnum, lago_customer_id: z.string(), lago_subscription_id: z.string().nullable(), lago_organization_id: z.string(), created_at: z.string(), updated_at: z.string(), current_version: quoteVersionObject.nullable(), owners: z.array(quoteOwnerObject)}); export const quote = z.object({quote: quoteObjectExtended}); 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 quote * * GET /quotes/{lago_id} */ export const getApiQuotesLagoId = async (options?: GetApiQuotesLagoIdOptions) => { 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(/\/+$/, '')}/quotes/${encodeURIComponent(String(args.lago_id))}${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(quote, await res.json()) };