import {z} from 'zod' export type AutocompleteEntityType = 'business'; 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 GetApiAutocompleteArgs = {entity_type: AutocompleteEntityType, 'business.name': string, state?: (StateAbbreviation | null) | undefined, limit?: number | undefined}; export type GetApiAutocompleteOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const autocompleteEntityType = z.literal("business"); export const v1AutocompleteHighlight = z.object({field: z.string(), snippets: z.array(z.string()).optional()}); export const v1AutocompleteSuggestion = z.object({id: z.string(), entity_type: autocompleteEntityType, name: z.string(), states: z.array(z.string()).optional(), registration_count: z.number().int(), score: z.number(), highlights: z.array(v1AutocompleteHighlight).optional()}); export const v1AutocompleteResponse = z.object({query: z.string(), suggestions: z.array(v1AutocompleteSuggestion).optional()}); 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 }; /** * Autocomplete * * GET /autocomplete */ export const getApiAutocomplete = async (args: GetApiAutocompleteArgs, options?: GetApiAutocompleteOptions) => { 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.entity_type != null) query.append("entity_type", String(args.entity_type)) if (args['business.name'] != null) query.append("business.name", String(args['business.name'])) if (args.state != null) query.append("state", String(args.state)) if (args.limit != null) query.append("limit", String(args.limit)) if (APIKeyHeader !== undefined) headers.set("X-API-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/autocomplete${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(v1AutocompleteResponse, await res.json()) };