import {z} from 'zod' export type StateAbbreviation = 'AL' | 'AK' | 'AZ' | 'AR' | 'CA' | 'CO' | 'CT' | 'DE' | 'DC' | 'FL' | 'GA' | 'HI' | 'ID' | 'IL' | 'IN' | 'IA' | 'KS' | 'KY' | 'LA' | 'ME' | 'MD' | 'MA' | 'MI' | 'MN' | 'MS' | 'MO' | 'MT' | 'NE' | 'NV' | 'NH' | 'NJ' | 'NM' | 'NY' | 'NC' | 'ND' | 'OH' | 'OK' | 'OR' | 'PA' | 'RI' | 'SC' | 'SD' | 'TN' | 'TX' | 'UT' | 'VT' | 'VA' | 'WA' | 'WV' | 'WI' | 'WY' | 'PR' | 'VI' | 'AE' | 'AA' | 'AP' | 'GU' | 'AS'; export type V1ConsortiumCheckOfficerName = {first_name: string, last_name: string, state?: (StateAbbreviation | null) | undefined}; export type V1ConsortiumCheckAddress = {street?: (string | null) | undefined, city?: (string | null) | undefined, state?: (StateAbbreviation | null) | undefined, zip?: (string | null) | undefined}; export type V1ConsortiumCheckRequest = {tin?: (string | null) | undefined, phones?: Array | undefined, emails?: Array | undefined, website?: (string | null) | undefined, business_name?: (string | null) | undefined, business_id?: (string | null) | undefined, officers?: Array | undefined, address?: (V1ConsortiumCheckAddress | null) | undefined}; export type CreateApiConsortiumSearchesArgs = {body: V1ConsortiumCheckRequest}; export type CreateApiConsortiumSearchesOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const entityType = z.enum(["CORPORATION", "LLC", "PARTNERSHIP", "SOLE PROPRIETORSHIP", "NON-PROFIT", "TRUST", "AGENT", "UNKNOWN"]); export const v1ExclusionOwnerDetail = z.object({first_name: z.string(), middle_name: z.string().nullable().optional(), last_name: z.string(), phone_number: z.string().nullable().optional(), address: z.string().nullable().optional()}); export const productType = z.enum(["BNK", "CC", "WC", "LOC", "EQF", "MCA", "PMT", "Other"]); export const applicationChannel = z.enum(["OG", "BR", "ISO", "Other"]); export const closureStatus = z.enum(["PIF", "WO", "SD"]); export const closureReason = z.enum(["PIF", "WBC", "WUP", "WPF", "WIF", "WSF"]); export const paymentFrequency = z.enum(["DY", "WY", "BW", "MY", "QY", "AY"]); export const v1ConsortiumMatchType = z.enum(["TIN", "PHONE", "EMAIL", "WEBSITE", "BUSINESS_NAME", "OFFICER", "ADDRESS", "BUSINESS_ID"]); export const v1ConsortiumMatchedField = z.object({type: v1ConsortiumMatchType, value: z.string()}); export const v1ConsortiumMatch = z.object({exclusion_id: z.string(), reported_on: z.string().nullable().optional(), business_name: z.string(), doing_business_as: z.string().nullable().optional(), business_address: z.string(), entity_type: entityType.nullable().optional(), tin: z.string().nullable().optional(), email: z.string().nullable().optional(), website: z.string().nullable().optional(), primary_owner: v1ExclusionOwnerDetail, secondary_owner: v1ExclusionOwnerDetail.nullable().optional(), product_type: productType, application_channel: applicationChannel.nullable().optional(), application_channel_name: z.string().nullable().optional(), application_ip_address: z.string().nullable().optional(), application_date: z.string(), opened_on: z.string(), closed_on: z.string(), matures_on: z.string().nullable().optional(), closure_status: closureStatus, closure_reason: closureReason, first_payment_default: z.boolean().nullable().optional(), term_in_months: z.number().int().nullable().optional(), amount: z.number().nullable().optional(), charge_off_amount: z.number().nullable().optional(), payment_frequency: paymentFrequency.nullable().optional(), score: z.number().gte(0).lte(1), matched_on: z.array(v1ConsortiumMatchedField)}); export const v1ConsortiumCheckResult = z.object({matches: z.array(v1ConsortiumMatch).optional(), total_matches: z.number().int()}); 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 }; /** * Search Consortium * * POST /consortium/searches */ export const createApiConsortiumSearches = async (args: CreateApiConsortiumSearchesArgs, options?: CreateApiConsortiumSearchesOptions) => { const {baseUrl = "https://api.baselayer.com", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("X-API-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/consortium/searches${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(v1ConsortiumCheckResult, await res.json()) };