import {z} from 'zod' export type BillingAddress = {street1: string, street2?: (string | null) | undefined, city: string, country: string, state: string, zip: string, phone?: (string | null) | undefined, salutation?: (string | null) | undefined, companyName?: (string | null) | undefined, fax?: (string | null) | undefined, faxExtension?: (string | null) | undefined, poBox?: (string | null) | undefined}; export type AppResellerPaymentMethodRequest = {ownerId: string, companyId: string, paymentMethodType: string, properties?: {[key: string]: string} | undefined, billingAddress: BillingAddress}; export type CreateApiAppResellerV1PaymentMethodsArgs = {body: AppResellerPaymentMethodRequest}; export type CreateApiAppResellerV1PaymentMethodsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const billingAddress = z.object({street1: z.string(), street2: z.string().nullable().optional(), city: z.string(), country: z.string(), state: z.string(), zip: z.string(), phone: z.string().nullable().optional(), salutation: z.string().nullable().optional(), companyName: z.string().nullable().optional(), fax: z.string().nullable().optional(), faxExtension: z.string().nullable().optional(), poBox: z.string().nullable().optional()}); export const userAssociation = z.object({id: z.string(), firstName: z.string(), lastName: z.string()}); export const appResellerPaymentMethod = z.object({id: z.string(), accountDisplay: z.string().optional(), default: z.boolean(), paymentMethodType: z.string(), properties: z.record(z.string(), z.string()).optional(), billingAddress: billingAddress, owner: userAssociation}); 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 }; /** * Create payment method * * POST /appReseller/v1/paymentMethods */ export const createApiAppResellerV1PaymentMethods = async (args: CreateApiAppResellerV1PaymentMethodsArgs, options?: CreateApiAppResellerV1PaymentMethodsOptions) => { const {baseUrl = "https://marketplace.appdirect.com/api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/appReseller/v1/paymentMethods${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(appResellerPaymentMethod, await res.json()) };