import {z} from 'zod' export type EnrichmentsEscapeType = 'json' | 'none'; export type EnrichmentsBulkProperties = {batch_size?: number | undefined, results_path?: Array | undefined}; export type ColumnsColumn = {FieldType?: string | undefined, Id?: string | undefined, Name?: string | undefined, Origin?: string | undefined}; export type HttpHeader = {[key: string]: Array}; export type EnrichmentsOutputMapping = {name?: string | undefined, path?: Array | undefined}; export type EnrichmentsResponseType = 'json' | 'raw' | 'unknown'; export type EnrichmentsRetryProperties = {maximum_attempts?: number | undefined, minimum_wait?: number | undefined, status_codes?: Array | undefined}; export type ClientstateSortModel = {colId?: string | undefined, pivotFieldGroupId?: string | undefined, sort?: string | undefined}; export type EnrichmentsRequest = {LaxValidation?: boolean | undefined, body_escape_format?: EnrichmentsEscapeType | undefined, body_template?: string | undefined, bulk?: EnrichmentsBulkProperties | undefined, columns?: Array | undefined, email_on_completion?: Array | undefined, filter_model?: {[key: string]: unknown} | undefined, headers?: HttpHeader | undefined, maximum_requests_per_second?: number | undefined, method?: string | undefined, output_path_map?: Array | undefined, recipe_id?: string | undefined, response_type?: EnrichmentsResponseType | undefined, retry_properties?: EnrichmentsRetryProperties | undefined, sort_model?: Array | undefined, url_template?: string | undefined}; export type CreateApiEnrichUserDefinedHttpHandlePreviewArgs = {handle: string, body: EnrichmentsRequest}; export type CreateApiEnrichUserDefinedHttpHandlePreviewOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const httpResponseValue = z.object({path: z.array(z.unknown()).optional(), value: z.unknown()}); export const enrichmentsResponseType = z.enum(["json", "raw", "unknown"]); export const httpHttpEnrichmentPreviewResponse = z.object({execution_ms: z.number().int().optional(), response_data: z.array(z.array(httpResponseValue)).optional(), response_failures: z.array(z.array(httpResponseValue)).optional(), response_type: enrichmentsResponseType.optional(), success: z.boolean().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 }; /** * Preview of a generic HTTP enrichment * * POST /enrich/user-defined-http/{handle}/preview */ export const createApiEnrichUserDefinedHttpHandlePreview = async (args: CreateApiEnrichUserDefinedHttpHandlePreviewArgs, options: CreateApiEnrichUserDefinedHttpHandlePreviewOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-GIGASHEET-TOKEN", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/enrich/user-defined-http/${encodeURIComponent(String(args.handle))}/preview${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(httpHttpEnrichmentPreviewResponse, await res.json()) };