import {z} from 'zod' export type AccountLookupStatus = 'REQUESTED' | 'PROCESSED' | 'FAILED' | 'EXPIRED'; export type GetApiAccountLookupsArgs = {page?: number | undefined, size?: number | undefined, account_lookup_status?: Array | undefined, 'start-date'?: string | undefined, 'end-date'?: string | undefined, sort_field?: ('CREATED_DATE' | 'LAST_MODIFIED_DATE') | undefined, sort_direction?: ('ASC' | 'DESC') | undefined}; export type GetApiAccountLookupsOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const sort = z.object({direction: z.string().optional(), property: z.string().optional(), ignoreCase: z.boolean().optional(), nullHandling: z.string().optional(), ascending: z.boolean().optional(), descending: z.boolean().optional()}); export const accountLookup = z.object({account_lookup_id: z.string(), account_lookup_status: z.enum(["REQUESTED", "PROCESSED", "FAILED", "EXPIRED"]), first_name: z.string().max(40).optional(), middle_name: z.string().max(40).optional(), last_name: z.string().max(40).optional(), org_name: z.string().max(140).optional(), country_code: z.string().optional(), account_currency: z.string().optional(), account_id: z.string().max(40).optional(), account_id_type: z.enum(["AIIN", "BBAN", "CARD", "CUID", "UPIC", "EMAIL", "MSISDN", "DMST", "OTHER"]).optional(), result_code: z.string().optional(), result_status: z.enum(["PASS", "FAIL"]).optional(), result_description: z.string().optional(), redacted_first_name: z.string().max(40).optional(), redacted_middle_name: z.string().max(40).optional(), redacted_last_name: z.string().max(40).optional(), redacted_org_name: z.string().max(140).optional(), redacted_country_code: z.string().optional(), redacted_account_id: z.string().max(40).optional(), redacted_account_id_type: z.enum(["AIIN", "BBAN", "CARD", "CUID", "UPIC", "EMAIL", "MSISDN", "DMST", "OTHER"]).optional(), redacted_account_currency: z.string().optional(), clearing_system_code: z.string().optional(), branch_code: z.string().optional()}); export const accountLookups = z.object({first: z.boolean().optional(), last: z.boolean().optional(), number: z.number().int().optional(), numberOfElements: z.number().int().optional(), size: z.number().int().optional(), totalElements: z.number().int().optional(), totalPages: z.number().int().optional(), sort: z.array(sort).optional(), content: z.array(accountLookup).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 }; /** * Get account lookup by status * * GET /account_lookups */ export const getApiAccountLookups = async (args: GetApiAccountLookupsArgs, options: GetApiAccountLookupsOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (args.account_lookup_status != null) query.append("account_lookup_status", args.account_lookup_status.join(',')) if (args['start-date'] != null) query.append("start-date", String(args['start-date'])) if (args['end-date'] != null) query.append("end-date", String(args['end-date'])) if (args.sort_field != null) query.append("sort_field", String(args.sort_field)) if (args.sort_direction != null) query.append("sort_direction", String(args.sort_direction)) if (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/account_lookups${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(accountLookups, await res.json()) };