import {z} from 'zod' export type ExtendFreeTrialRequest = {extend_to: string}; export type CreateApiBillingSubscriptionItemsSubscriptionItemIdExtendFreeTrialArgs = {subscription_item_id: string, body: ExtendFreeTrialRequest}; export type CreateApiBillingSubscriptionItemsSubscriptionItemIdExtendFreeTrialOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const commerceMoneyResponse = z.object({amount: z.number().int(), amount_formatted: z.string(), currency: z.string(), currency_symbol: z.string()}); export const commerceSubscriptionCreditResponse = z.object({amount: commerceMoneyResponse.nullable().optional(), cycle_remaining_percent: z.number().optional()}); export const featureResponse = z.object({object: z.literal("feature"), id: z.string(), name: z.string(), description: z.string(), slug: z.string(), avatar_url: z.string()}); export const commercePlan = z.object({object: z.literal("commerce_plan"), id: z.string(), name: z.string(), fee: commerceMoneyResponse.nullable(), annual_monthly_fee: commerceMoneyResponse.nullable(), annual_fee: commerceMoneyResponse.nullable(), amount: z.number().int(), amount_formatted: z.string(), annual_monthly_amount: z.number().int(), annual_monthly_amount_formatted: z.string(), annual_amount: z.number().int(), annual_amount_formatted: z.string(), currency_symbol: z.string(), currency: z.string(), description: z.string(), product_id: z.string(), is_default: z.boolean(), is_recurring: z.boolean(), publicly_visible: z.boolean(), has_base_fee: z.boolean(), payer_type: z.array(z.string()), for_payer_type: z.string(), slug: z.string(), avatar_url: z.string(), period: z.string().optional(), interval: z.number().int().optional(), features: z.array(featureResponse), free_trial_enabled: z.boolean().optional(), free_trial_days: z.number().int().nullable().optional()}); export const commercePaymentSourceResponse = z.object({object: z.literal("commerce_source"), id: z.string(), payer_id: z.string(), payment_method: z.enum(["card", "apple_pay", "google_pay"]), is_default: z.boolean().nullable().optional(), gateway: z.string(), gateway_external_id: z.string(), gateway_external_account_id: z.string().nullable().optional(), last4: z.string(), status: z.enum(["active", "disconnected"]), wallet_type: z.string(), card_type: z.string(), expiry_year: z.number().int().optional(), expiry_month: z.number().int().optional(), created_at: z.number().int(), updated_at: z.number().int(), is_removable: z.boolean().nullable().optional()}); export const commercePerUnitTotalTier = z.object({quantity: z.number().int().nullable().optional(), fee_per_block: commerceMoneyResponse, total: commerceMoneyResponse}); export const commercePerUnitTotal = z.object({name: z.string(), block_size: z.number().int(), tiers: z.array(commercePerUnitTotalTier)}); export const commerceProrationCreditDetailResponse = z.object({amount: commerceMoneyResponse, cycle_days_remaining: z.number().int(), cycle_days_total: z.number().int(), cycle_remaining_percent: z.number()}); export const commercePayerCreditResponse = z.object({remaining_balance: commerceMoneyResponse, applied_amount: commerceMoneyResponse}); export const commerceCreditsResponse = z.object({proration: commerceProrationCreditDetailResponse.nullable(), payer: commercePayerCreditResponse.nullable(), total: commerceMoneyResponse}); export const commerceProrationDiscountResponse = z.object({amount: commerceMoneyResponse, cycle_days_passed: z.number().int(), cycle_days_total: z.number().int(), cycle_passed_percent: z.number()}); export const commerceDiscountsResponse = z.object({proration: commerceProrationDiscountResponse.nullable(), total: commerceMoneyResponse}); export const commerceTotalsResponse = z.object({subtotal: commerceMoneyResponse, base_fee: commerceMoneyResponse, tax_total: commerceMoneyResponse, grand_total: commerceMoneyResponse, per_unit_totals: z.array(commercePerUnitTotal).optional(), credits: commerceCreditsResponse.nullable().optional(), discounts: commerceDiscountsResponse.nullable().optional()}); export const commerceSubscriptionItemNextPaymentResponse = z.object({amount: commerceMoneyResponse.nullable().optional(), date: z.number().int().nullable().optional(), per_unit_totals: z.array(commercePerUnitTotal).optional(), totals: commerceTotalsResponse.nullable().optional()}); export const commercePayerResponse = z.object({object: z.literal("commerce_payer"), id: z.string(), instance_id: z.string(), user_id: z.string().optional(), first_name: z.string(), last_name: z.string(), email: z.string(), organization_id: z.string().optional(), organization_name: z.string().optional(), image_url: z.string(), credits_balance: commerceMoneyResponse.optional(), created_at: z.number().int(), updated_at: z.number().int()}); export const commerceSubscriptionItem = z.object({object: z.literal("commerce_subscription_item"), id: z.string(), instance_id: z.string(), status: z.enum(["active", "ended", "past_due", "upcoming", "incomplete", "abandoned"]), credit: commerceSubscriptionCreditResponse.nullable().optional(), plan_id: z.string(), price_id: z.string().optional(), plan: commercePlan.nullable(), plan_period: z.enum(["month", "annual"]), payment_source_id: z.string(), payment_source: commercePaymentSourceResponse.nullable().optional(), lifetime_paid: commerceMoneyResponse.nullable().optional(), amount: commerceMoneyResponse.nullable().optional(), next_invoice: commerceSubscriptionItemNextPaymentResponse.nullable().optional(), next_payment: commerceSubscriptionItemNextPaymentResponse.nullable().optional(), payer_id: z.string(), payer: commercePayerResponse.nullable().optional(), is_free_trial: z.boolean(), period_start: z.number().int().nullable().optional(), period_end: z.number().int().nullable().optional(), proration_date: z.string(), canceled_at: z.number().int().nullable().optional(), past_due_at: z.number().int().nullable().optional(), ended_at: z.number().int().nullable().optional(), created_at: z.number().int(), updated_at: z.number().int()}); 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 }; /** * Extend free trial for a subscription item * * POST /billing/subscription_items/{subscription_item_id}/extend_free_trial */ export const createApiBillingSubscriptionItemsSubscriptionItemIdExtendFreeTrial = async (args: CreateApiBillingSubscriptionItemsSubscriptionItemIdExtendFreeTrialArgs, options?: CreateApiBillingSubscriptionItemsSubscriptionItemIdExtendFreeTrialOptions) => { const {baseUrl = "https://api.clerk.com/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/billing/subscription_items/${encodeURIComponent(String(args.subscription_item_id))}/extend_free_trial${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(commerceSubscriptionItem, await res.json()) };