import {z} from 'zod' export type MatchType = 'NO_MATCH' | 'SIMILAR' | 'EXACT'; export type AddressMatchType = 'NO_MATCH' | 'CITY' | 'STATE' | 'SIMILAR' | 'EXACT'; export type GetApiSearchesArgs = {q?: (string | null) | undefined, tin_matched?: (boolean | null) | undefined, verified?: (boolean | null) | undefined, business_website_match?: (boolean | null) | undefined, business_name_match?: (MatchType | null) | undefined, business_officer_match?: (MatchType | null) | undefined, registered_agent_match?: (MatchType | null) | undefined, business_address_match?: (AddressMatchType | null) | undefined, limit?: number | undefined, offset?: (number | null) | undefined, cursor?: (string | null) | undefined, start_date?: (string | null) | undefined, end_date?: (string | null) | undefined, tz?: string | undefined}; export type GetApiSearchesOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const orderableType = z.enum(["WebsiteAnalysisRequest", "NAICSPredictionRequest"]); export const businessSearchOptionsOutput = z.enum(["Order.WebsiteAnalysis", "Order.NaicsPrediction", "Order.Pep", "Order.Enhanced"]); export const orderable = z.object({type: orderableType, id: z.string(), url: z.string(), option: businessSearchOptionsOutput}); export const v1UserAttributionResponse = z.object({id: z.string(), first_name: z.string(), last_name: z.string(), email: z.string()}); export const taskState = z.enum(["PENDING", "EXECUTING", "COMPLETED", "FAILED", "CANCELLED"]); export const v1StateAbbreviation = z.enum(["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 const v1AddressRDI = z.enum(["Residential", "Commercial"]); export const v1USPSDeliveryType = z.enum(["FIRM", "GENERAL_DELIVERY", "HIGH_RISE", "POST_OFFICE", "RURAL_ROUTE", "STREET"]); export const v1AddressSummaryResponse = z.object({id: z.string(), street: z.string(), city: z.string(), state: v1StateAbbreviation, zip: z.string(), latitude: z.number(), longitude: z.number(), rdi: v1AddressRDI.nullable().optional(), deliverable: z.boolean().nullable().optional(), cmra: z.boolean().nullable().optional(), url: z.string().min(1).max(2083).nullable().optional(), delivery_type: v1USPSDeliveryType.nullable().optional()}); export const iRSTINMatchType = z.enum(["SSN", "EIN", "UNKNOWN"]); export const v1WatchlistDataSource = z.enum(["Death Master File", "Department of Treasury, Office of Foreign Assets Control", "Politically Exposed Persons", "IRS Exempt Organizations List", "FBI Wanted List", "Department of Commerce, Consolidated Screening List", "Consolidated Canadian Autonomous Sanctions List", "HHS OIG List of Excluded Individuals/Entities", "Baselayer Fraud Consortium"]); export const v1WatchlistHitResponse = z.object({code: z.string(), name: v1WatchlistDataSource, count: z.number().int(), details: z.array(z.record(z.string(), z.unknown())).nullable().optional()}); export const matchType = z.enum(["NO_MATCH", "SIMILAR", "EXACT"]); export const addressMatchType = z.enum(["NO_MATCH", "CITY", "STATE", "SIMILAR", "EXACT"]); export const addressValidationLevel = z.enum(["FULL", "PARTIAL", "INVALID"]); export const v1ScoreType = z.enum(["fraud", "risk", "kyb"]); export const v1ScoreResponse = z.object({type: v1ScoreType, score: z.number(), rating: z.string()}); export const v1BusinessSummaryResponse = z.object({id: z.string(), name: z.string(), address: z.string().nullable().optional(), phone_number: z.string().nullable().optional(), email: z.string().nullable().optional(), website: z.string().nullable().optional(), url: z.string().min(1).max(2083).optional(), console_url: z.string().min(1).max(2083).optional()}); export const v1BusinessSearchResponse = z.object({id: z.string(), options: z.array(z.enum(["Order.WebsiteAnalysis", "Order.NaicsPrediction", "Order.Pep", "Order.Enhanced"])), orderables: z.array(orderable).optional(), user: v1UserAttributionResponse.nullable().optional(), state: taskState, name: z.string(), address: z.string(), search_address: v1AddressSummaryResponse.nullable().optional(), officer_names: z.array(z.string()).nullable().optional(), alternative_names: z.array(z.string()).nullable().optional(), website: z.string().nullable().optional(), phone_number: z.string().nullable().optional(), email: z.string().nullable().optional(), tin: z.string().nullable().optional(), reference_id: z.string().nullable().optional(), tin_matched: z.boolean().nullable().optional(), tin_match_type: iRSTINMatchType.nullable().optional(), tin_potential_match: z.string().nullable().optional(), watchlist_hits: z.array(v1WatchlistHitResponse).optional(), business_name_match: matchType.nullable().optional(), business_address_match: addressMatchType.nullable().optional(), business_officer_match: matchType.nullable().optional(), registered_agent_match: matchType.nullable().optional(), business_website_match: z.boolean().nullable().optional(), business_website_redirect_match: z.boolean().nullable().optional(), search_address_validation_level: addressValidationLevel.nullable().optional(), created_at: z.string(), updated_at: z.string().nullable().optional(), verified: z.boolean().nullable().optional(), scores: z.array(v1ScoreResponse).nullable().optional(), error: z.string().nullable().optional(), warnings: z.array(z.string()).optional(), business: v1BusinessSummaryResponse.nullable().optional(), url: z.string().min(1).max(2083), status_url: z.string().min(1).max(2083), business_url: z.string().min(1).max(2083).nullable(), console_url: z.string().min(1).max(2083).optional()}); export const getApiSearchesResponse = z.array(v1BusinessSearchResponse); 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 Searches * * GET /searches */ export const getApiSearches = async (args: GetApiSearchesArgs, options?: GetApiSearchesOptions) => { const {baseUrl = "https://api.baselayer.com", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.q != null) query.append("q", String(args.q)) if (args.tin_matched != null) query.append("tin_matched", String(args.tin_matched)) if (args.verified != null) query.append("verified", String(args.verified)) if (args.business_website_match != null) query.append("business_website_match", String(args.business_website_match)) if (args.business_name_match != null) query.append("business_name_match", String(args.business_name_match)) if (args.business_officer_match != null) query.append("business_officer_match", String(args.business_officer_match)) if (args.registered_agent_match != null) query.append("registered_agent_match", String(args.registered_agent_match)) if (args.business_address_match != null) query.append("business_address_match", String(args.business_address_match)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.tz != null) query.append("tz", String(args.tz)) if (APIKeyHeader !== undefined) headers.set("X-API-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/searches${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(getApiSearchesResponse, await res.json()) };