import {z} from 'zod' export type StreamMetadata = {id?: string | undefined, name?: string | undefined, connector?: string | undefined, schema_fields?: {[key: string]: string} | undefined, ddl_properties: unknown, stream_resource_properties: unknown, stream_type?: string | undefined, watermark?: string | undefined}; export type FieldKind = 'physical' | 'computed' | 'metadata'; export type PhysicalField = {kind?: FieldKind | undefined, name?: string | undefined, type?: string | undefined}; export type ComputedField = {kind?: FieldKind | undefined, name?: string | undefined, expression: unknown}; export type MetadataField = {kind?: FieldKind | undefined, name?: string | undefined, type?: string | undefined, key?: string | undefined}; export type SchemaV2 = {fields?: (Array) | undefined, watermarks?: (Array<{name?: string | undefined, expression?: string | undefined}>) | undefined, constraints?: ({primary_key?: Array | undefined}) | undefined}; export type OutputMetadata = {output_stream_type?: string | undefined, name?: string | undefined, schema?: SchemaV2 | undefined, ddl_properties: unknown}; export type SqlPreviewCreateRequest = {preview_id: string, rewritten_sql: string, stream_metadata: Array<{[key: string]: StreamMetadata}>, output_metadata: OutputMetadata}; export type CreateApiSqlPreviewArgs = {body: SqlPreviewCreateRequest}; export type CreateApiSqlPreviewOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const resourceId = z.string(); export const preview = z.object({id: resourceId, output_stream_type: z.enum(["APPEND", "CHANGE"]).optional(), results: z.array(z.object({})).optional(), next_token: z.string().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 }; /** * Create a SQL preview. * * POST /sql/preview */ export const createApiSqlPreview = async (args: CreateApiSqlPreviewArgs, options?: CreateApiSqlPreviewOptions) => { const {baseUrl = "https://replace-with-account.api.data.decodable.co/v1alpha2", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/sql/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(preview, await res.json()) };