import {z} from 'zod' export type CashAccountTransaction = {id?: string | undefined, version?: string | undefined, accountId?: string | undefined, amount: unknown, cashAccountSheetId?: string | undefined, cleared?: boolean | undefined, createdById?: string | undefined, createdDate?: number | undefined, currencyId?: string | undefined, description?: string | undefined, effectiveDate?: number | undefined, externalRecordNumber?: string | undefined, lastModifiedDate?: number | undefined, origin?: ('AMAZON' | 'CAMT053' | 'EBAY' | 'FINAPI' | 'KLARNA' | 'LONDON_ZURICH' | 'MANUALLY' | 'MOLLIE' | 'MT940_KAPOTT' | 'MT940_PROVIDE' | 'PAYMENT_RUN' | 'PAYPAL' | 'POS' | 'QWIST' | 'SHOPIFY' | 'STRIPE' | 'TINK') | undefined, paymentMethodId?: string | undefined, paymentType: 'DISBURSEMENT' | 'RECEIPT', taxId?: string | undefined}; export type CreateApiCashAccountTransactionArgs = {dryRun?: boolean | undefined, body: CashAccountTransaction}; export type CreateApiCashAccountTransactionOptions = {baseUrl: string, APIToken?: string | undefined, fetch?: typeof fetch | undefined}; export const cashAccountTransaction = z.object({id: z.string().optional(), version: z.string().optional(), accountId: z.string().optional(), amount: z.string(), cashAccountSheetId: z.string().optional(), cleared: z.boolean().optional(), createdById: z.string().optional(), createdDate: z.number().int().optional(), currencyId: z.string().optional(), description: z.string().optional(), effectiveDate: z.number().int().optional(), externalRecordNumber: z.string().optional(), lastModifiedDate: z.number().int().optional(), origin: z.enum(["AMAZON", "CAMT053", "EBAY", "FINAPI", "KLARNA", "LONDON_ZURICH", "MANUALLY", "MOLLIE", "MT940_KAPOTT", "MT940_PROVIDE", "PAYMENT_RUN", "PAYPAL", "POS", "QWIST", "SHOPIFY", "STRIPE", "TINK"]).optional(), paymentMethodId: z.string().optional(), paymentType: z.enum(["DISBURSEMENT", "RECEIPT"]), taxId: 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 }; /** * create a cashAccountTransaction * * POST /cashAccountTransaction */ export const createApiCashAccountTransaction = async (args: CreateApiCashAccountTransactionArgs, options: CreateApiCashAccountTransactionOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.dryRun != null) query.append("dryRun", String(args.dryRun)) if (APIToken !== undefined) headers.set("AuthenticationToken", APIToken) const url = `${baseUrl.replace(/\/+$/, '')}/cashAccountTransaction${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(cashAccountTransaction, await res.json()) };