import {z} from 'zod' export type GetApiBillingV1PaymentInstrumentsDefaultArgs = {companyUuid?: string | undefined, userUuid?: string | undefined}; export type GetApiBillingV1PaymentInstrumentsDefaultOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const billingAddressWS = z.object({city: z.string().max(255), country: z.string().max(2), state: z.string().max(255).optional(), firstName: z.string().max(255), lastName: z.string().max(255), phone: z.string().max(255), street1: z.string().max(255), street2: z.string().max(255).optional(), zip: z.string().max(255), companyName: z.string().optional(), fax: z.string().optional(), faxExtension: z.string().optional(), phoneExtension: z.string().optional(), poBox: z.string().optional(), salutation: z.string().optional()}); export const linkWS = z.object({href: z.string().optional(), id: z.string().optional()}).nullable(); export const creditCardType = z.enum(["VISA", "MASTERCARD", "AMEX", "DISCOVER", "JCB", "GATEWAY"]); export const creditCardWS = z.object({expirationMonth: z.number(), expirationYear: z.number(), name: z.string().max(255), number: z.string().max(19).nullable(), securityCode: z.string().min(3).max(4).nullable(), type: creditCardType.optional()}); export const paymentMethod = z.enum(["CREDIT_CARD", "PAYPAL", "MANUAL", "EXTERNAL_INVOICE", "MARKETPLACE_CREDITS"]); export const paymentInstrumentWS = z.object({accountDisplay: z.string().max(255).optional(), billingAddress: billingAddressWS.optional(), company: linkWS.optional(), createdOn: z.number().optional(), creditCard: creditCardWS.optional(), id: z.string().optional(), isDefault: z.boolean().optional(), paymentMethod: paymentMethod.optional(), user: linkWS.optional(), vatId: z.string().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 }; /** * Retrieve the default payment instrument * * GET /billing/v1/paymentInstruments/default */ export const getApiBillingV1PaymentInstrumentsDefault = async (args: GetApiBillingV1PaymentInstrumentsDefaultArgs, options?: GetApiBillingV1PaymentInstrumentsDefaultOptions) => { const {baseUrl = "https://marketplace.appdirect.com/api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.companyUuid != null) query.append("companyUuid", String(args.companyUuid)) if (args.userUuid != null) query.append("userUuid", String(args.userUuid)) const url = `${baseUrl.replace(/\/+$/, '')}/billing/v1/paymentInstruments/default${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(paymentInstrumentWS, await res.json()) };