import {z} from 'zod' export type CreateApiBillingSubscriptionCancellationPreviewArgs = {body: {organizationId?: string | undefined}}; export type CreateApiBillingSubscriptionCancellationPreviewOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiBillingSubscriptionCancellationPreviewResponse = z.object({currentPlan: z.object({slug: z.string(), name: z.string(), price: z.string().nullable(), currency: z.string().nullable(), cycleCredits: z.string().nullable(), targetCreditBalance: z.string().nullable(), subscriptionStatus: z.string(), cycleStartsAt: z.string().nullable(), cycleEndsAt: z.string().nullable(), renewalAt: z.string().nullable(), pendingSwitch: z.object({billingSubscriptionChangeId: z.string(), effectiveAt: z.string(), planSlug: z.string().nullable(), planName: z.string().nullable(), price: z.string().nullable(), currency: z.string().nullable(), cycleCredits: z.string().nullable(), targetCreditBalance: z.string().nullable()}).nullable(), pendingCancellation: z.object({effectiveAt: z.string()}).nullable()}), effectiveAt: z.string(), currentBalance: z.string(), servicePolicy: z.literal("active_until_period_end"), creditPolicy: z.literal("dormant_until_resubscription"), scheduledFinalCharge: z.string().nullable(), autoTopUpEnabled: z.boolean(), pendingDowngrade: z.object({billingSubscriptionChangeId: z.string(), effectiveAt: z.string(), planSlug: z.string().nullable(), planName: z.string().nullable(), price: z.string().nullable(), currency: z.string().nullable(), cycleCredits: z.string().nullable(), targetCreditBalance: z.string().nullable()}).nullable(), calculatedAt: z.string()}); 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 }; /** * Preview subscription cancellation * * POST /billing/subscription-cancellation/preview */ export const createApiBillingSubscriptionCancellationPreview = async (args: CreateApiBillingSubscriptionCancellationPreviewArgs, options?: CreateApiBillingSubscriptionCancellationPreviewOptions) => { const {baseUrl = "https://api.sazabi.com/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/billing/subscription-cancellation/preview${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiBillingSubscriptionCancellationPreviewResponse, await res.json()) };