import {z} from 'zod' export type CreateApiV1SearchArgs = {body: {entity_name: string, jurisdiction?: string | undefined, entity_type?: ('llc' | 'corporation' | 'lp' | 'llp' | 'sole_proprietorship' | 'nonprofit' | 'general_partnership' | 'other') | undefined, limit?: number | undefined, offset?: number | undefined}}; export type CreateApiV1SearchOptions = {baseUrl: string, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1SearchResponse = z.object({results: z.array(z.unknown()), total: z.number(), limit: z.number(), offset: z.number(), jurisdictions_searched: z.array(z.string()), jurisdictions_failed: z.array(z.string()), credits_charged: z.number()}); 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 for business entities across one or multiple jurisdictions. Returns matching entities with confidence scores. Charges 2 credits per jurisdiction searched. Use the jurisdiction parameter to search a specific jurisdiction, or omit it to search all active jurisdictions. Supports pagination via limit and offset. Search is available in every active jurisdiction regardless of verification tier. * * POST /v1/search */ export const createApiV1Search = async (args: CreateApiV1SearchArgs, options: CreateApiV1SearchOptions) => { const {baseUrl, 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("X-API-Key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/search${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(createApiV1SearchResponse, await res.json()) };