import {z} from 'zod' export type PlatformEscrowStatus = 'draft' | 'authorized' | 'charged' | 'partiallyCaptured' | 'captured' | 'voided' | 'reclaimed' | 'partiallyRefunded' | 'refunded'; export type GetApiEscrowArgs = {status?: (Array | null) | undefined, senderClientId?: (string | null) | undefined, receiverClientId?: (string | null) | undefined, limit?: number | undefined, cursor?: (string | null) | undefined, 'X-Hevn-Account'?: (string | null) | undefined}; export type GetApiEscrowOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const platformEscrowStatus = z.enum(["draft", "authorized", "charged", "partiallyCaptured", "captured", "voided", "reclaimed", "partiallyRefunded", "refunded"]); export const escrowAction = z.enum(["approve", "authorize", "charge", "capture", "reject", "void", "reclaim", "refund"]); export const escrowPartyView = z.object({clientId: z.string()}); export const escrowWindowsView = z.object({approveBy: z.string(), holdUntil: z.string(), refundableUntil: z.string()}); export const platformEscrowActionStatus = z.enum(["awaitingSignature", "signing", "submitted", "confirmed", "reverted", "failed", "superseded", "dropped"]); export const escrowApprovalView = z.object({id: z.string(), payload: z.string(), expiresAt: z.string()}); export const escrowActionView = z.object({action: escrowAction, idempotencyKey: z.string(), status: platformEscrowActionStatus, amount: z.string().nullable().optional(), feeBps: z.number().int().nullable().optional(), transactionHash: z.string().nullable().optional(), approval: escrowApprovalView.nullable().optional()}); export const escrowView = z.object({id: z.string(), status: platformEscrowStatus, availableActions: z.array(escrowAction), sender: escrowPartyView, receiver: escrowPartyView, onchainEscrowUrl: z.string(), description: z.string().nullable().optional(), completionConditions: z.string().nullable().optional(), token: z.string(), maxAmount: z.string(), authorizedAmount: z.string(), capturableAmount: z.string(), refundableAmount: z.string(), windows: escrowWindowsView, action: escrowActionView.nullable().optional(), consistent: z.boolean().nullable().optional(), createdAt: z.string(), updatedAt: z.string()}); export const pageEscrowView = z.object({items: z.array(escrowView), nextCursor: 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 }; /** * List the escrows this account operates * * GET /escrow */ export const getApiEscrow = async (args: GetApiEscrowArgs, options: GetApiEscrowOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.status ?? []) query.append("status", String(item)) if (args.senderClientId != null) query.append("senderClientId", String(args.senderClientId)) if (args.receiverClientId != null) query.append("receiverClientId", String(args.receiverClientId)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args['X-Hevn-Account'] != null) headers.set("X-Hevn-Account", String(args['X-Hevn-Account'])) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/escrow${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(pageEscrowView, await res.json()) };