import {z} from 'zod' export type Payment = {id?: string | undefined, integration?: string | undefined, srcId?: string | undefined, paymentDate?: string | undefined, totalAmount?: (string | null) | undefined, status?: (string | null) | undefined, modified?: (string | null) | undefined, accountId?: (string | null) | undefined, customerId?: (string | null) | undefined, customerName?: (string | null) | undefined, linkedTxnId?: (string | null) | undefined, linkedTxnType?: (string | null) | undefined, created?: (string | null) | undefined, invoiceId?: (string | null) | undefined, currencyCode?: (string | null) | undefined, paymentType?: (string | null) | undefined}; export type CreateApiPaymentsArgs = {body: Payment}; export type CreateApiPaymentsOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const payment = z.object({id: z.string().optional(), integration: z.string().optional(), srcId: z.string().optional(), paymentDate: z.string().optional(), totalAmount: z.string().nullable().optional(), status: z.string().nullable().optional(), modified: z.string().nullable().optional(), accountId: z.string().nullable().optional(), customerId: z.string().nullable().optional(), customerName: z.string().nullable().optional(), linkedTxnId: z.string().nullable().optional(), linkedTxnType: z.string().nullable().optional(), created: z.string().nullable().optional(), invoiceId: z.string().nullable().optional(), currencyCode: z.string().nullable().optional(), paymentType: 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 }; /** * Creates a Payment resource. * * POST /payments */ export const createApiPayments = async (args: CreateApiPaymentsArgs, options?: CreateApiPaymentsOptions) => { const {baseUrl = "https://example.myintranetapps.com/api", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/payments${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(payment, await res.json()) };