import {z} from 'zod' export type CreateApiApiSubscriptionsIdPauseArgs = {id: string}; export type CreateApiApiSubscriptionsIdPauseOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const subscriptionCustomerResponse = z.object({id: z.string(), name: z.string(), email: z.string()}); export const subscriptionPlanResponse = z.object({id: z.string(), name: z.string(), billingInterval: z.enum(["HOURLY", "DAILY", "WEEKLY", "MONTHLY", "QUARTERLY", "YEARLY"])}); export const subscriptionResponse = z.object({id: z.string(), externalId: z.string().optional(), customerId: z.string(), planId: z.string(), previousPlanId: z.string().optional(), status: z.enum(["ACTIVE", "PAST_DUE", "CANCELED", "TRIALING", "PAUSED"]), currency: z.string(), billingTiming: z.enum(["IN_ADVANCE", "IN_ARREARS"]), currentPeriodStart: z.string(), currentPeriodEnd: z.string(), cancelAt: z.string().optional(), canceledAt: z.string().optional(), trialStart: z.string().optional(), trialEnd: z.string().optional(), startedAt: z.string(), metadata: z.record(z.string(), z.unknown()).optional(), customer: subscriptionCustomerResponse.optional(), plan: subscriptionPlanResponse.optional(), createdAt: z.string(), updatedAt: 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 }; /** * Pause a subscription * * POST /api/subscriptions/{id}/pause */ export const createApiApiSubscriptionsIdPause = async (args: CreateApiApiSubscriptionsIdPauseArgs, options?: CreateApiApiSubscriptionsIdPauseOptions) => { const {baseUrl = "https://api.novabilling.one", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/subscriptions/${encodeURIComponent(String(args.id))}/pause${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(subscriptionResponse, await res.json()) };