import {z} from 'zod' export type GetApiPrefillBusinessesArgs = {officer_name: string, business_name?: (string | null) | undefined, similarity_threshold?: number | undefined, limit?: number | undefined}; export type GetApiPrefillBusinessesOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const stateAbbreviation = 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 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 matchType = z.enum(["NO_MATCH", "SIMILAR", "EXACT"]); export const v1PrefillRegistrationResponse = z.object({name: z.string(), issue_date: z.string().nullable().optional(), state: stateAbbreviation, file_number: z.string(), primary_address: v1AddressSummaryResponse.nullable().optional(), officers: z.array(z.string()).optional(), alternative_names: z.array(z.string()).optional(), similarity_score: z.number(), business_name_match: matchType.nullable().optional()}); export const getApiPrefillBusinessesResponse = z.array(v1PrefillRegistrationResponse); 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 }; /** * Business Prefill * * GET /prefill/businesses */ export const getApiPrefillBusinesses = async (args: GetApiPrefillBusinessesArgs, options?: GetApiPrefillBusinessesOptions) => { 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.officer_name != null) query.append("officer_name", String(args.officer_name)) if (args.business_name != null) query.append("business_name", String(args.business_name)) if (args.similarity_threshold != null) query.append("similarity_threshold", String(args.similarity_threshold)) if (args.limit != null) query.append("limit", String(args.limit)) if (APIKeyHeader !== undefined) headers.set("X-API-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/prefill/businesses${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(getApiPrefillBusinessesResponse, await res.json()) };