import {z} from 'zod' export type MerchantKey = string; export type SchemasBinKey = string; export type BusinessPhoneNumber = string; export type MatchPrincipalName = string; export type CountryCodeAlpha3 = string; export type CountryCodeAlpha2 = string; export type CountryCodeNumeric = string; export type CountryCodeInput = CountryCodeAlpha3 | CountryCodeAlpha2 | CountryCodeNumeric; export type SchemasStateOrProvince = string; export type SchemasPostalCode = string; export type MatchProAddressInput = {countryCode: CountryCodeInput, stateOrProvince?: SchemasStateOrProvince | undefined, city: string, line1: string, line2?: string | undefined, postalCode: SchemasPostalCode}; export type MatchProEmail = string; export type MatchProPrincipalInput = {firstName: MatchPrincipalName, lastName: MatchPrincipalName, phoneNumber: BusinessPhoneNumber, address: MatchProAddressInput, email: MatchProEmail}; export type MatchProScreeningRequest = {screeningType: 'MATCH_PRO', merchantKey: MerchantKey, binKey: SchemasBinKey, doingBusinessAsName: string, merchantCategoryCode: string, merchantPhoneNumber: BusinessPhoneNumber, principals: Array}; export type VmssDoingBusinessAsName = string; export type TradeOverInternet = boolean; export type BusinessCategory = 'merchant' | 'paymentFacilitator' | 'marketplace'; export type VmssPrincipal = {firstName: string, lastName: string}; export type Bid = string; export type VmssScreeningRequest = {screeningType: 'VMSS', merchantKey: MerchantKey, binKey: SchemasBinKey, doingBusinessAsName: VmssDoingBusinessAsName, businessPhoneNumbers: Array, tradeOverInternet: TradeOverInternet, businessCategory: BusinessCategory, principals: Array, bid?: Bid | undefined}; export type ScreeningRequest = MatchProScreeningRequest | VmssScreeningRequest; export type CreateApiScreeningsArgs = {body: ScreeningRequest}; export type CreateApiScreeningsOptions = {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 }; /** * Create Screening Request * * POST /screenings * * Also accepts auth: BearerToken */ export const createApiScreenings = async (args: CreateApiScreeningsArgs, options?: CreateApiScreeningsOptions) => { 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('content-type', "application/json") headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${ApiKey.username}:${ApiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/screenings${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(screening, await res.json()) };