import {z} from 'zod' export type GetApiApiPaymentsPlansArgs = {country?: string | undefined}; export type GetApiApiPaymentsPlansOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiPaymentsPlansResponse = z.object({data: z.object({plans: z.array(z.object({tier: z.enum(["essential", "premium", "ultimate", "ultra"]), imageSrc: z.string().optional(), isFeatured: z.boolean(), yearlySavingsPercent: z.number(), aiUsage: z.union([z.object({kind: z.literal("multiple"), multiple: z.number(), versusTier: z.enum(["free", "essential", "premium", "ultimate", "ultra"])}), z.object({kind: z.literal("more-than"), versusTier: z.enum(["free", "essential"])}), z.object({kind: z.literal("included"), sizedToLocalPrice: z.boolean()})]).nullable().optional(), byInterval: z.object({monthly: z.object({isActive: z.boolean(), description: z.string(), featuresHeading: z.string(), features: z.array(z.string()), hierarchyOrder: z.number(), product: z.object({id: z.string()}), price: z.object({id: z.string(), currency: z.string(), amountInCents: z.object({monthly: z.object({regular: z.number(), discounted: z.number()}), yearly: z.object({regular: z.number(), discounted: z.number()})}), coupon: z.object({id: z.string(), name: z.string(), percentOff: z.number(), perks: z.object({purchase: z.object({freeMonths: z.number(), aiCredits: z.number()}).nullable(), stacking: z.array(z.object({years: z.number(), freeMonths: z.number(), aiCredits: z.number()})), maxButton: z.object({enabled: z.boolean(), bonusFreeMonths: z.number(), bonusAiCredits: z.number()})}).nullable().optional()}).nullable()})}), yearly: z.object({isActive: z.boolean(), description: z.string(), featuresHeading: z.string(), features: z.array(z.string()), hierarchyOrder: z.number(), product: z.object({id: z.string()}), price: z.object({id: z.string(), currency: z.string(), amountInCents: z.object({monthly: z.object({regular: z.number(), discounted: z.number()}), yearly: z.object({regular: z.number(), discounted: z.number()})}), coupon: z.object({id: z.string(), name: z.string(), percentOff: z.number(), perks: z.object({purchase: z.object({freeMonths: z.number(), aiCredits: z.number()}).nullable(), stacking: z.array(z.object({years: z.number(), freeMonths: z.number(), aiCredits: z.number()})), maxButton: z.object({enabled: z.boolean(), bonusFreeMonths: z.number(), bonusAiCredits: z.number()})}).nullable().optional()}).nullable()})})})}))}), errors: z.unknown()}); 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 }; /** * Get all the available payment plans * * GET /api/payments/plans */ export const getApiApiPaymentsPlans = async (args: GetApiApiPaymentsPlansArgs, options?: GetApiApiPaymentsPlansOptions) => { const {baseUrl = "https://app.luxalgo.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.country != null) query.append("country", String(args.country)) const url = `${baseUrl.replace(/\/+$/, '')}/api/payments/plans${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(getApiApiPaymentsPlansResponse, await res.json()) };