import {z} from 'zod' export type DeleteApiV1BillingAccountsIdArgs = {id: string}; export type DeleteApiV1BillingAccountsIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const billingAddress = z.object({city: z.string().optional(), country: z.string().optional(), line1: z.string().optional(), line2: z.string().optional(), name: z.string().optional(), phone: z.string().optional(), postalCode: z.string().optional(), state: z.string().optional()}); export const decimal = z.object({value: z.string().optional()}); export const lastPaymentError = z.object({code: z.string(), declineCode: z.string()}); export const charge = z.object({amount: decimal, createTime: z.string(), failureCode: z.string().optional(), paid: z.boolean(), refunded: z.boolean(), status: z.string()}); export const transaction = z.object({amount: decimal, createTime: z.string(), description: z.string(), id: z.string(), lastPaymentError: lastPaymentError, latestCharge: charge, status: z.string(), succeeded: z.boolean()}); export const creditBalanceRecharge = z.object({autoRecharge: z.boolean(), high: decimal, low: decimal, transaction: transaction.optional()}); export const paymentTerms = z.enum(["PAYMENT_TERMS_UNSPECIFIED", "PAYMENT_TERMS_PREPAID", "PAYMENT_TERMS_INVOICE"]); export const billingAccountState = z.enum(["STATE_UNSPECIFIED", "STATE_ACTIVE", "STATE_SUSPENDED_ABUSE", "STATE_SUSPENDED_PAYMENT", "STATE_DELETED"]); export const taxId = z.object({id: z.string().optional(), type: z.string().optional(), value: z.string().optional()}); export const billingAccount = z.object({billingAddress: billingAddress.optional(), billingThreshold: decimal.optional(), createBy: z.string().optional(), createTime: z.string().optional(), creditBalance: decimal.optional(), creditBalanceRecharge: creditBalanceRecharge.optional(), deleteTime: z.string().optional(), displayName: z.string().optional(), hourlySpendLimit: decimal.optional(), hourlySpendRate: decimal.optional(), id: z.string().optional(), invoiceTime: z.string().optional(), monthlySpend: z.string().optional(), monthlySpendLimit: decimal.optional(), nextInvoiceTotal: decimal.optional(), paymentTerms: paymentTerms.optional(), purgeTime: z.string().optional(), state: billingAccountState.optional(), stripeRef: z.string().optional(), taxId: taxId.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 }; /** * Delete a billing account * * DELETE /v1/billing-accounts/{id} */ export const deleteApiV1BillingAccountsId = async (args: DeleteApiV1BillingAccountsIdArgs, options?: DeleteApiV1BillingAccountsIdOptions) => { const {baseUrl = "https://rest.compute.cudo.org", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set("Authorization", bearerAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/billing-accounts/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "DELETE", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(billingAccount, await res.json()) };