import {z} from 'zod' export type GetApiSearchesBatchesArgs = {q?: (string | 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 GetApiSearchesBatchesOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const taskState = z.enum(["PENDING", "EXECUTING", "COMPLETED", "FAILED", "CANCELLED"]); export const dataTestKYBAnswers = z.object({current_provider: z.string().nullable().optional(), new_applications_per_month: z.number().int().gte(0).nullable().optional(), avg_uw_time_hours: z.number().int().gte(0).nullable().optional(), avg_uw_time_minutes: z.number().int().gte(0).lte(59).nullable().optional(), match_rate_pct: z.number().gte(0).lte(100).nullable().optional(), approval_rate_pct: z.number().gte(0).lte(100).nullable().optional()}); export const dataTestWebPresenceAnswers = z.object({existing_workflow_step: z.boolean().nullable().optional(), current_provider: z.string().nullable().optional()}); export const dataTestLiensAnswers = z.object({current_provider: z.string().nullable().optional()}); export const dataTestLitigationsAnswers = z.object({current_provider: z.string().nullable().optional()}); export const dataTestQuestionnaire = z.object({kyb: dataTestKYBAnswers.nullable().optional(), web_presence: dataTestWebPresenceAnswers.nullable().optional(), liens: dataTestLiensAnswers.nullable().optional(), litigations: dataTestLitigationsAnswers.nullable().optional()}); export const v1BusinessSearchBatchResponse = z.object({id: z.string(), name: z.string(), state: taskState, progress: z.number().gte(0).lte(1).nullable().optional(), completed_count: z.number().int().gte(0).nullable().optional(), total_count: z.number().int().gte(0).nullable().optional(), created_at: z.string(), options: z.array(z.enum(["Order.WebsiteAnalysis", "Order.NaicsPrediction", "Order.Pep", "Order.Enhanced"])).optional(), warnings: z.array(z.string()).optional(), export_csv_uri: z.string().nullable().optional(), export_jsonl_uri: z.string().nullable().optional(), report_version: z.number().int().nullable().optional(), questionnaire: dataTestQuestionnaire.nullable().optional()}); export const getApiSearchesBatchesResponse = z.array(v1BusinessSearchBatchResponse); 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 }; /** * List search batches * * GET /searches/batches */ export const getApiSearchesBatches = async (args: GetApiSearchesBatchesArgs, options?: GetApiSearchesBatchesOptions) => { 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.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/batches${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(getApiSearchesBatchesResponse, await res.json()) };