import {z} from 'zod' export type GetApiSubscriptionsEstimateArgs = {'X-Profile-Id': string, plan_id: string, customer_id?: (string | null) | undefined, coupon_id?: (string | null) | undefined, trial_days?: (number | null) | undefined}; export type GetApiSubscriptionsEstimateOptions = {baseUrl: string, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const minorUnit = z.number().int(); export const currency = z.enum(["AED", "AFN", "ALL", "AMD", "ANG", "AOA", "ARS", "AUD", "AWG", "AZN", "BAM", "BBD", "BDT", "BGN", "BHD", "BIF", "BMD", "BND", "BOB", "BRL", "BSD", "BTN", "BWP", "BYN", "BZD", "CAD", "CDF", "CHF", "CLF", "CLP", "CNY", "COP", "CRC", "CUC", "CUP", "CVE", "CZK", "DJF", "DKK", "DOP", "DZD", "EGP", "ERN", "ETB", "EUR", "FJD", "FKP", "GBP", "GEL", "GHS", "GIP", "GMD", "GNF", "GTQ", "GYD", "HKD", "HNL", "HRK", "HTG", "HUF", "IDR", "ILS", "INR", "IQD", "IRR", "ISK", "JMD", "JOD", "JPY", "KES", "KGS", "KHR", "KMF", "KPW", "KRW", "KWD", "KYD", "KZT", "LAK", "LBP", "LKR", "LRD", "LSL", "LYD", "MAD", "MDL", "MGA", "MKD", "MMK", "MNT", "MOP", "MRU", "MUR", "MVR", "MWK", "MXN", "MYR", "MZN", "NAD", "NGN", "NIO", "NOK", "NPR", "NZD", "OMR", "PAB", "PEN", "PGK", "PHP", "PKR", "PLN", "PYG", "QAR", "RON", "RSD", "RUB", "RWF", "SAR", "SBD", "SCR", "SDG", "SEK", "SGD", "SHP", "SLE", "SLL", "SOS", "SRD", "SSP", "STD", "STN", "SVC", "SYP", "SZL", "THB", "TJS", "TMT", "TND", "TOP", "TRY", "TTD", "TWD", "TZS", "UAH", "UGX", "USD", "UYU", "UZS", "VES", "VND", "VUV", "WST", "XAF", "XCD", "XOF", "XPF", "YER", "ZAR", "ZMW", "ZWL"]); export const customerId = z.string(); export const subscriptionLineItem = z.object({item_id: z.string(), item_type: z.string(), description: z.string(), amount: minorUnit, currency: currency, quantity: z.number().int()}); export const estimateSubscriptionResponse = z.object({amount: minorUnit, currency: currency, plan_id: z.string().nullable().optional(), item_price_id: z.string().nullable().optional(), coupon_code: z.string().nullable().optional(), customer_id: customerId.nullable().optional(), line_items: z.array(subscriptionLineItem)}); 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 }; /** * Subscription - Get Estimate * * GET /subscriptions/estimate */ export const getApiSubscriptionsEstimate = async (args: GetApiSubscriptionsEstimateArgs, options: GetApiSubscriptionsEstimateOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['X-Profile-Id'] != null) headers.set("X-Profile-Id", String(args['X-Profile-Id'])) if (args.plan_id != null) query.append("plan_id", String(args.plan_id)) if (args.customer_id != null) query.append("customer_id", String(args.customer_id)) if (args.coupon_id != null) query.append("coupon_id", String(args.coupon_id)) if (args.trial_days != null) query.append("trial_days", String(args.trial_days)) if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/subscriptions/estimate${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(estimateSubscriptionResponse, await res.json()) };