import {z} from 'zod' export type GetApiAmlMonitoringsArgs = {created?: Array | undefined, expiration?: Array | undefined, isExpired?: boolean | undefined, name?: string | undefined, orderBy?: (Array<'-created' | '-expiration' | 'created' | 'expiration'>) | undefined, page?: number | undefined, perPage?: number | undefined, status?: ('ACTIVE' | 'ALERT' | 'PENDING' | 'STOPPED') | undefined, tags?: Array> | undefined, userType?: ('COMPANY' | 'PERSON') | undefined}; export type GetApiAmlMonitoringsOptions = {baseUrl?: string | undefined, apiKeyPair?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const monitoringStatusEnum = z.enum(["ACTIVE", "STOPPED", "PENDING", "ALERT"]); export const userMonitoringTypeEnum = z.enum(["COMPANY", "PERSON"]); export const amlMonitoringList = z.object({id: z.string(), name: z.string().max(100), status: monitoringStatusEnum.optional(), userType: userMonitoringTypeEnum, isSubscribed: z.boolean().optional(), checkedAt: z.string(), expiration: z.string().optional(), tags: z.array(z.string().max(32)).optional(), created: z.string()}); export const paginatedAmlMonitoringListList = z.object({count: z.number().int(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(amlMonitoringList)}); 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 AML monitorings * * GET /aml/monitorings/ */ export const getApiAmlMonitorings = async (args: GetApiAmlMonitoringsArgs, options?: GetApiAmlMonitoringsOptions) => { const {baseUrl = "https://ivs.idenfy.com", fetch: fetchFn = fetch, apiKeyPair} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.created != null) query.append("created", args.created.join(',')) if (args.expiration != null) query.append("expiration", args.expiration.join(',')) if (args.isExpired != null) query.append("isExpired", String(args.isExpired)) if (args.name != null) query.append("name", String(args.name)) if (args.orderBy != null) query.append("orderBy", args.orderBy.join(',')) if (args.page != null) query.append("page", String(args.page)) if (args.perPage != null) query.append("perPage", String(args.perPage)) if (args.status != null) query.append("status", String(args.status)) if (args.tags != null) query.append("tags", args.tags.join(',')) if (args.userType != null) query.append("userType", String(args.userType)) if (apiKeyPair !== undefined) headers.set('authorization', `Basic ${btoa(`${apiKeyPair.username}:${apiKeyPair.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/aml/monitorings/${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(paginatedAmlMonitoringListList, await res.json()) };