import {z} from 'zod' export type GetApiSettlementWindowsArgs = {participantId?: number | undefined, state?: ('OPEN' | 'CLOSED' | 'PENDING_SETTLEMENT' | 'SETTLED' | 'ABORTED') | undefined, fromDateTime?: string | undefined, toDateTime?: string | undefined, currency?: string | undefined}; export type GetApiSettlementWindowsOptions = {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 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 }; /** * getSettlementWindowsByParams * * GET /settlementWindows */ export const getApiSettlementWindows = async (args: GetApiSettlementWindowsArgs, options: GetApiSettlementWindowsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.participantId != null) query.append("participantId", String(args.participantId)) 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.currency != null) query.append("currency", String(args.currency)) const url = `${baseUrl.replace(/\/+$/, '')}/settlementWindows${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(settlementWindows, await res.json()) };