import {z} from 'zod' export type SettlementUpdateBySettlementIdInnerPayload = {id?: number | undefined, reason?: string | undefined, state?: ('PENDING_SETTLEMENT' | 'PS_TRANSFERS_RECORDED' | 'PS_TRANSFERS_RESERVED' | 'PS_TRANSFERS_COMMITTED' | 'SETTLED') | undefined, externalReference?: string | undefined}; export type SettlementUpdateBySettlementIDOuterPayload = {id?: number | undefined, accounts?: Array | undefined}; export type SettlementUpdateBySettlementIdPayload = {state?: ('ABORTED' | 'INVALID') | undefined, reason?: string | undefined, externalReference?: string | undefined, participants?: Array | undefined}; export type UpdateApiSettlementsIdArgs = {id: number, body: SettlementUpdateBySettlementIdPayload}; export type UpdateApiSettlementsIdOptions = {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 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 }; /** * updateSettlementById * * PUT /settlements/{id} */ export const updateApiSettlementsId = async (args: UpdateApiSettlementsIdArgs, options: UpdateApiSettlementsIdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/settlements/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(settlement, await res.json()) };