import {z} from 'zod' export type GetApiSettlementsArgs = {currency?: string | undefined, participantId?: number | undefined, settlementWindowId?: number | undefined, accountId?: number | undefined, state?: ('PENDING_SETTLEMENT' | 'PS_TRANSFERS_RECORDED' | 'PS_TRANSFERS_RESERVED' | 'PS_TRANSFERS_COMMITTED' | 'SETTLING' | 'SETTLED' | 'ABORTED') | undefined, fromDateTime?: string | undefined, toDateTime?: string | undefined, fromSettlementWindowDateTime?: string | undefined, toSettlementWindowDateTime?: string | undefined}; export type GetApiSettlementsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const settlementWindowContent = z.object({id: z.number().int(), state: z.string(), ledgerAccountType: z.string(), currencyId: z.string(), createdDate: z.string(), changedDate: z.string().optional(), settlementId: z.number().int().optional()}); export const settlementWindow = z.object({id: z.number().int(), reason: z.string().optional(), state: z.string(), createdDate: z.string(), changedDate: z.string().optional(), content: z.array(settlementWindowContent).optional()}); export const settlementWindows = z.array(settlementWindow); export const netSettlementAmount = z.object({amount: z.number().optional(), currency: z.string().optional()}); export const accounts = z.object({id: z.number().int().optional(), reason: z.string().optional(), state: z.string().optional(), netSettlementAmount: netSettlementAmount.optional()}); export const participant = z.object({id: z.number().int().optional(), accounts: z.array(accounts).optional()}); export const settlement = z.object({id: z.number().int().optional(), state: z.string().optional(), settlementWindows: z.array(settlementWindows).optional(), participants: z.array(participant).optional()}); export const settlements = z.array(settlement); 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 }; /** * getSettlementsByParams * * GET /settlements */ export const getApiSettlements = async (args: GetApiSettlementsArgs, options: GetApiSettlementsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.currency != null) query.append("currency", String(args.currency)) if (args.participantId != null) query.append("participantId", String(args.participantId)) if (args.settlementWindowId != null) query.append("settlementWindowId", String(args.settlementWindowId)) if (args.accountId != null) query.append("accountId", String(args.accountId)) if (args.state != null) query.append("state", String(args.state)) if (args.fromDateTime != null) query.append("fromDateTime", String(args.fromDateTime)) if (args.toDateTime != null) query.append("toDateTime", String(args.toDateTime)) if (args.fromSettlementWindowDateTime != null) query.append("fromSettlementWindowDateTime", String(args.fromSettlementWindowDateTime)) if (args.toSettlementWindowDateTime != null) query.append("toSettlementWindowDateTime", String(args.toSettlementWindowDateTime)) const url = `${baseUrl.replace(/\/+$/, '')}/settlements${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(settlements, await res.json()) };