import {z} from 'zod' export type CompleteAccountLookupRequest = {account_lookup_id: string, result_code: string, result_status: 'PASS' | 'FAIL', result_description: string, redacted_first_name?: string | undefined, redacted_middle_name?: string | undefined, redacted_last_name?: string | undefined, redacted_org_name?: string | undefined, redacted_country_code?: string | undefined, redacted_account_id?: string | undefined, redacted_account_id_type?: ('AIIN' | 'BBAN' | 'CARD' | 'CUID' | 'UPIC' | 'EMAIL' | 'MSISDN' | 'DMST' | 'OTHER') | undefined, redacted_clearing_system_code?: string | undefined, redacted_branch_code?: string | undefined, redacted_account_currency?: string | undefined}; export type CreateApiAccountLookupsResponseArgs = {body: CompleteAccountLookupRequest}; export type CreateApiAccountLookupsResponseOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; 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 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 }; /** * Complete account lookup * * POST /account_lookups/response */ export const createApiAccountLookupsResponse = async (args: CreateApiAccountLookupsResponseArgs, options: CreateApiAccountLookupsResponseOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/account_lookups/response${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(accountLookup, await res.json()) };