import {z} from 'zod' export type ScreeningKey = string; export type OffsetToken = string; export type GetApiScreeningsScreeningsKeyResultsArgs = {screeningKey: ScreeningKey, sortOrder?: ('asc' | 'desc') | undefined, limit?: number | undefined, offsetToken?: OffsetToken | undefined}; export type GetApiScreeningsScreeningsKeyResultsOptions = {baseUrl?: string | undefined, ApiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const offsetToken = z.string().min(1).max(2048); export const moreItems = z.boolean(); export const terminationReason = z.enum(["excessive-fraud", "excessive-chargebacks", "transaction-laundering", "illegal-transactions", "visa-compliance-program", "merchant-collusion", "common-point-of-purchase", "fraud-conviction", "insolvency", "violation-of-agreement", "violation-of-rules", "pci-non-compliance", "account-data-compromise", "merchant-identity-theft", "disqualified-from-network", "non-f2f-adult-content"]); export const bid = z.string(); export const matchType = z.enum(["none", "partial", "phonetic", "exact"]); export const recordMatchString = z.object({matchType: matchType, value: z.string().optional()}); export const recordMatchStringArray = z.object({matchType: matchType, value: z.array(z.string())}); export const addressMatch = z.object({line1: recordMatchString.optional(), city: recordMatchString.optional(), stateOrProvince: recordMatchString.optional(), countryCode: recordMatchString.optional(), postalCode: recordMatchString.optional()}); export const vmssPrincipalResult = z.object({firstName: recordMatchString, lastName: recordMatchString}); export const vmssScreeningResult = z.object({bid: bid, doingBusinessAsName: recordMatchString, businessPhoneNumbers: recordMatchStringArray, websiteUrl: recordMatchStringArray, address: addressMatch, principals: z.array(vmssPrincipalResult)}); export const terminatedByAcquirerIdCode = z.string(); export const matchPrincipalResult = z.object({firstName: recordMatchString, lastName: recordMatchString, address: addressMatch, phoneNumber: recordMatchString}); export const matchScreeningResult = z.object({acquirerIdCode: terminatedByAcquirerIdCode, legalName: recordMatchString, doingBusinessAsName: recordMatchString, address: addressMatch, websiteUrl: recordMatchStringArray, principals: z.array(matchPrincipalResult)}); export const matchProPrincipalResult = z.object({firstName: recordMatchString, lastName: recordMatchString, address: addressMatch, phoneNumber: recordMatchString, email: recordMatchString.optional()}); export const matchProScreeningResult = z.object({acquirerIdCode: terminatedByAcquirerIdCode, legalName: recordMatchString, doingBusinessAsName: recordMatchString, merchantCategoryCode: recordMatchString.optional(), merchantPhoneNumber: recordMatchString.optional(), address: addressMatch, websiteUrl: recordMatchStringArray, principals: z.array(matchProPrincipalResult)}); export const terminatedMerchant = z.union([vmssScreeningResult, z.union([matchScreeningResult, matchProScreeningResult])]); export const screeningResult = z.object({terminationReason: terminationReason, terminatedMerchant: terminatedMerchant}); export const screeningResultsPage = z.object({offsetToken: offsetToken.optional(), moreItems: moreItems.optional(), screeningResults: z.array(screeningResult)}); 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 Screening Results * * GET /screenings/{screeningsKey}/results * * Also accepts auth: BearerToken */ export const getApiScreeningsScreeningsKeyResults = async (args: GetApiScreeningsScreeningsKeyResultsArgs, options?: GetApiScreeningsScreeningsKeyResultsOptions) => { const {baseUrl = "https://eu-west-1.api.silverflow.com/v1", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offsetToken != null) query.append("offsetToken", String(args.offsetToken)) if (ApiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${ApiKey.username}:${ApiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/screenings/${encodeURIComponent(String(args.screeningsKey))}/results${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(screeningResultsPage, await res.json()) };