import {z} from 'zod' export type DID = unknown; export type AssetIdentifier = string; export type TransferDirection = 'INCOMING' | 'OUTGOING'; export type GetApiEntitiesEntityDIDTxMatchArgs = {entityDID: DID, settlement_id: string, settlement_address: string, source_address?: string | undefined, asset?: Array | undefined, settlement_id_index?: string | undefined, memo_tag?: string | undefined, limit?: number | undefined, offset?: number | undefined, direction?: TransferDirection | undefined, fields?: string | undefined, sort_by?: string | undefined, sort_order?: ('asc' | 'desc') | undefined}; export type GetApiEntitiesEntityDIDTxMatchOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const searchTransfer = z.object({id: z.string().optional(), ref: z.string().nullable().optional(), asset: z.string().nullable().optional(), amount: z.string().nullable().optional(), status: z.string().nullable().optional(), entityDid: z.string().nullable().optional(), direction: z.string().nullable().optional(), initiator: z.string().nullable().optional(), createdAt: z.union([z.string(), z.string()]).nullable().optional(), updatedAt: z.union([z.string(), z.string()]).nullable().optional(), expiresTime: z.union([z.string(), z.string()]).nullable().optional(), settledTime: z.union([z.string(), z.string()]).nullable().optional(), rejectedTime: z.union([z.string(), z.string()]).nullable().optional(), settlementId: z.string().nullable().optional(), isTravelRule: z.boolean().nullable().optional(), notabeneAsset: z.string().nullable().optional(), authorizedTime: z.union([z.string(), z.string()]).nullable().optional(), settlementAddress: z.string().nullable().optional(), presentationDefinitionUrl: z.string().nullable().optional(), revertedTime: z.union([z.string(), z.string()]).nullable().optional(), settlementIdIndex: z.string().nullable().optional(), originator: z.object({"@id": z.string(), originatorPerson: z.array(z.record(z.string(), z.unknown())).nullable().optional()}).and(z.record(z.string(), z.unknown())).nullable().optional(), beneficiary: z.object({"@id": z.string(), beneficiaryPerson: z.array(z.record(z.string(), z.unknown())).nullable().optional()}).and(z.record(z.string(), z.unknown())).nullable().optional(), agents: z.array(z.object({"@id": z.string(), "@type": z.literal("TransferAgent").optional(), id: z.string().nullable().optional(), name: z.string().nullable().optional(), agent: z.object({"@id": z.string()}), logo: z.string().nullable().optional(), jurisdiction: z.string().nullable().optional(), did: z.string().nullable().optional(), transferId: z.string().nullable().optional(), role: z.enum(["VASP", "Custodian", "SettlementAddress", "SourceAddress", "Gateway", "Unknown"]).nullable().optional(), for: z.string().nullable().optional(), policies: z.array(z.object({"@type": z.string()}).and(z.record(z.string(), z.unknown()))).nullable().optional(), memoTag: z.string().nullable().optional(), status: z.enum(["PROCESSING", "AUTHORIZED", "REJECTED", "SETTLED", "FAILED", "REPLACED", "UNREACHABLE"]).nullable().optional(), relationship: z.object({"@id": z.string().nullable().optional(), from: z.string().nullable().optional(), to: z.string().nullable().optional(), status: z.string().nullable().optional(), confirmedBy: z.string().nullable().optional()}).and(z.record(z.string(), z.unknown())).nullable().optional(), replacement: z.object({"@id": z.string()}).and(z.record(z.string(), z.unknown())).nullable().optional()}).and(z.record(z.string(), z.unknown()))).optional(), matchedColumns: z.array(z.string()).optional()}).and(z.record(z.string(), z.unknown())); export const matchTransferList = z.object({data: z.array(searchTransfer), pagination: z.object({total: z.number(), limit: z.number(), offset: z.number()}), meta: z.object({match_strategy: z.enum(["strict", "broadened", "none"]), broadened_candidates: z.number().int().gte(0).lte(9007199254740991).optional()}).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 }; /** * Match transfers by settlement identifiers * * GET /entities/{entityDID}/tx/match */ export const getApiEntitiesEntityDIDTxMatch = async (args: GetApiEntitiesEntityDIDTxMatchArgs, options?: GetApiEntitiesEntityDIDTxMatchOptions) => { const {baseUrl = "https://api.eu1.notabene.id", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.settlement_id != null) query.append("settlement_id", String(args.settlement_id)) if (args.settlement_address != null) query.append("settlement_address", String(args.settlement_address)) if (args.source_address != null) query.append("source_address", String(args.source_address)) for (const item of args.asset ?? []) query.append("asset", String(item)) if (args.settlement_id_index != null) query.append("settlement_id_index", String(args.settlement_id_index)) if (args.memo_tag != null) query.append("memo_tag", String(args.memo_tag)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.direction != null) query.append("direction", String(args.direction)) if (args.fields != null) query.append("fields", String(args.fields)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/entities/${encodeURIComponent(String(args.entityDID))}/tx/match${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(matchTransferList, await res.json()) };