import {z} from 'zod' export type ScreeningKey = string; export type GetApiScreeningsScreeningsKeyArgs = {screeningKey: ScreeningKey}; export type GetApiScreeningsScreeningsKeyOptions = {baseUrl?: string | undefined, ApiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const screeningKey = z.string().min(5).max(120); export const merchantKey = z.string().min(5).max(120); export const schemasBinKey = z.string().min(5).max(120); export const agentKey = z.string(); export const screeningStatus = z.enum(["pending", "completed", "failed"]); export const errorMessage = z.string(); export const screeningType = z.enum(["MATCH", "VMSS", "MATCH_PRO"]); export const created = z.string(); export const lastModified = z.string(); export const status = z.enum(["active", "archived"]); export const version = z.number().int(); export const totalNumberOfResults = z.number(); export const schemasAcquirerIdCode = z.string().min(6).max(6); export const schemasDoingBusinessAsName = z.string().min(1).max(120); export const countryCodeAlpha3 = z.string().min(3).max(3); export const countryCodeAlpha2 = z.string().min(2).max(2); export const countryCodeNumeric = z.string().min(3).max(3); export const countryCode = z.object({alpha3: countryCodeAlpha3, alpha2: countryCodeAlpha2, numeric: countryCodeNumeric}); export const schemasStateOrProvince = z.string().min(1).max(120); export const schemasCity = z.string().min(1).max(120); export const schemasLine1 = z.string().min(1).max(120); export const schemasLine2 = z.string().min(1).max(120); export const schemasPostalCode = z.string().min(1).max(10); export const schemasAddress = z.object({countryCode: countryCode, stateOrProvince: schemasStateOrProvince.optional(), city: schemasCity, line1: schemasLine1, line2: schemasLine2.optional(), postalCode: schemasPostalCode}); export const schemasUrl = z.string().min(8).max(2048); export const businessPhoneNumber = z.string().min(10).max(12); export const matchPrincipal = z.object({firstName: z.string().min(1).max(120), lastName: z.string().min(1).max(120), phoneNumber: businessPhoneNumber, address: schemasAddress}); export const matchScreeningDetail = z.object({acquirerIdCode: schemasAcquirerIdCode, legalName: z.string().min(1).max(120), doingBusinessAsName: schemasDoingBusinessAsName, address: schemasAddress, websiteUrl: schemasUrl, principals: z.array(matchPrincipal)}); export const matchProEmail = z.string().max(100); export const matchProPrincipal = z.object({firstName: z.string().min(1).max(120), lastName: z.string().min(1).max(120), phoneNumber: businessPhoneNumber, address: schemasAddress, email: matchProEmail}); export const matchProScreeningDetail = z.object({acquirerIdCode: schemasAcquirerIdCode, legalName: z.string().min(1).max(120), doingBusinessAsName: schemasDoingBusinessAsName, merchantCategoryCode: z.string().min(4).max(4), merchantPhoneNumber: businessPhoneNumber, address: schemasAddress, websiteUrl: schemasUrl, principals: z.array(matchProPrincipal)}); export const bid = z.string(); export const vmssDoingBusinessAsName = z.string(); export const tradeOverInternet = z.boolean(); export const businessCategory = z.enum(["merchant", "paymentFacilitator", "marketplace"]); export const vmssPrincipal = z.object({firstName: z.string().min(1).max(15), lastName: z.string().min(1).max(15)}); export const vmssScreeningDetail = z.object({bid: bid, doingBusinessAsName: vmssDoingBusinessAsName, address: schemasAddress, businessPhoneNumbers: z.array(businessPhoneNumber), websiteUrl: schemasUrl, tradeOverInternet: tradeOverInternet, businessCategory: businessCategory, principals: z.array(vmssPrincipal)}); export const screeningDetail = z.union([matchScreeningDetail, matchProScreeningDetail, vmssScreeningDetail]); export const screening = z.object({key: screeningKey, merchantKey: merchantKey, binKey: schemasBinKey, agentKey: agentKey, screeningStatus: screeningStatus, errorMessage: errorMessage.optional(), screeningType: screeningType, created: created, lastModified: lastModified.optional(), status: status, version: version, totalNumberOfResults: totalNumberOfResults.optional(), screeningDetail: screeningDetail}); 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 a Screening * * GET /screenings/{screeningsKey} * * Also accepts auth: BearerToken */ export const getApiScreeningsScreeningsKey = async (args: GetApiScreeningsScreeningsKeyArgs, options?: GetApiScreeningsScreeningsKeyOptions) => { 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 (ApiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${ApiKey.username}:${ApiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/screenings/${encodeURIComponent(String(args.screeningsKey))}${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(screening, await res.json()) };