import {z} from 'zod' export type CreateApiV2PreauthTransactionArgs = {'Content-Type': string, authorization: string, 'Idempotency-Key'?: string | undefined, body: {bankAccountId: number, merchantId: number, amount: {amount?: number | undefined, currency?: string | undefined}, attributes?: ({'CUSTOM ATTRIBUTE'?: ({value?: string | undefined, description?: string | undefined}) | undefined, invoice?: ({value?: string | undefined, description?: string | undefined}) | undefined, tip?: ({tipAmount?: string | undefined, label?: string | undefined, totalAmount?: string | undefined}) | undefined}) | undefined, referenceId?: string | undefined}}; export type CreateApiV2PreauthTransactionOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const preauthTransactionResponse = z.object({transaction: z.object({id: z.string().optional(), amount: z.object({currency: z.string().optional(), amount: z.number().int().optional()}).optional(), status: z.string().optional(), userId: z.string().optional(), description: z.string().optional(), attributes: z.object({}).optional(), createdDate: z.string().optional(), expiryDate: z.string().optional(), userName: z.string().optional(), userEmail: z.string().optional(), merchantId: z.number().int().optional(), userAccountId: z.number().int().optional(), locationId: z.number().int().optional(), referenceId: z.string().optional()}).optional()}); export const schema200failure = z.object({error: z.object({code: z.string().optional(), message: z.string().optional(), help: z.string().optional()}).optional(), reasonCodes: z.array(z.string()).optional()}); export const createApiV2PreauthTransactionResponse = z.union([preauthTransactionResponse, schema200failure]); 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 }; /** * preauthTransaction * * POST /v2/preauthTransaction */ export const createApiV2PreauthTransaction = async (args: CreateApiV2PreauthTransactionArgs, options?: CreateApiV2PreauthTransactionOptions) => { const {baseUrl = "https://api.sandbox-pay.aero.inc", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) if (args.authorization != null) headers.set("authorization", String(args.authorization)) if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) const url = `${baseUrl.replace(/\/+$/, '')}/v2/preauthTransaction${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(createApiV2PreauthTransactionResponse, await res.json()) };