import {z} from 'zod' export type GetApiV1MatchingContextsContextIdOpenItemsArgs = {contextId: string, status?: ('OPEN' | 'PARTIALLY_CLEARED' | 'CLEARED' | 'AGED') | undefined, limit?: number | undefined, cursor?: string | undefined, sort_by?: ('ageDays' | 'runningBalance' | 'obligationDate') | undefined, sort_order?: ('asc' | 'desc') | undefined}; export type GetApiV1MatchingContextsContextIdOpenItemsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const openItemResponse = z.object({ageDays: z.number().int().gte(0), contextId: z.string(), createdAt: z.string(), currency: z.string(), expectedBalance: z.string(), firstSeenAt: z.string(), id: z.string(), identityKey: z.string(), lastActivityAt: z.string(), obligationDate: z.string().optional(), runId: z.string().optional(), runningBalance: z.string(), status: z.string(), transactionId: z.string().optional(), updatedAt: z.string()}); export const listOpenItemsResponse = z.object({hasMore: z.boolean(), items: z.array(openItemResponse).nullable(), limit: z.number().int().gte(1).lte(200), nextCursor: z.string().optional(), prevCursor: z.string().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 open items * * GET /v1/matching/contexts/{contextId}/open-items */ export const getApiV1MatchingContextsContextIdOpenItems = async (args: GetApiV1MatchingContextsContextIdOpenItemsArgs, options?: GetApiV1MatchingContextsContextIdOpenItemsOptions) => { const {baseUrl = "https://identity.sandbox.lerian.net", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.status != null) query.append("status", String(args.status)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) 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(/\/+$/, '')}/v1/matching/contexts/${encodeURIComponent(String(args.contextId))}/open-items${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(listOpenItemsResponse, await res.json()) };