import {z} from 'zod' export type PauseOption = 'immediately' | 'end_of_term' | 'specific_date'; export type PauseSubscriptionRequest = {pause_option?: (PauseOption | null) | undefined, pause_at?: (string | null) | undefined}; export type CreateApiSubscriptionsSubscriptionIdPauseArgs = {subscription_id: string, 'X-Profile-Id': string, body: PauseSubscriptionRequest}; export type CreateApiSubscriptionsSubscriptionIdPauseOptions = {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 pauseSubscriptionResponse = z.object({id: subscriptionId, status: subscriptionStatus, merchant_reference_id: z.string().nullable().optional(), profile_id: profileId, merchant_id: merchantId, customer_id: customerId, paused_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 - Pause Subscription * * POST /subscriptions/{subscription_id}/pause */ export const createApiSubscriptionsSubscriptionIdPause = async (args: CreateApiSubscriptionsSubscriptionIdPauseArgs, options: CreateApiSubscriptionsSubscriptionIdPauseOptions) => { 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))}/pause${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(pauseSubscriptionResponse, await res.json()) };