import {z} from 'zod' export type ResumeOption = 'immediately' | 'specific_date'; export type ChargesHandling = 'invoice_immediately' | 'add_to_unbilled_charges'; export type UnpaidInvoicesHandling = 'no_action' | 'schedule_payment_collection'; export type ResumeSubscriptionRequest = {resume_option?: (ResumeOption | null) | undefined, resume_date?: (string | null) | undefined, charges_handling?: (ChargesHandling | null) | undefined, unpaid_invoices_handling?: (UnpaidInvoicesHandling | null) | undefined}; export type CreateApiSubscriptionsSubscriptionIdResumeArgs = {subscription_id: string, 'X-Profile-Id': string, body: ResumeSubscriptionRequest}; export type CreateApiSubscriptionsSubscriptionIdResumeOptions = {baseUrl: string, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const subscriptionId = z.string(); export const subscriptionStatus = z.enum(["active", "created", "in_active", "pending", "trial", "paused", "unpaid", "onetime", "cancelled", "failed"]); export const profileId = z.string(); export const merchantId = z.string(); export const customerId = z.string(); export const resumeSubscriptionResponse = z.object({id: subscriptionId, status: subscriptionStatus, merchant_reference_id: z.string().nullable().optional(), profile_id: profileId, merchant_id: merchantId, customer_id: customerId, next_billing_at: z.string().nullable().optional()}); 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 - Resume Subscription * * POST /subscriptions/{subscription_id}/resume */ export const createApiSubscriptionsSubscriptionIdResume = async (args: CreateApiSubscriptionsSubscriptionIdResumeArgs, options: CreateApiSubscriptionsSubscriptionIdResumeOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Profile-Id'] != null) headers.set("X-Profile-Id", String(args['X-Profile-Id'])) if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/subscriptions/${encodeURIComponent(String(args.subscription_id))}/resume${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(resumeSubscriptionResponse, await res.json()) };