import {z} from 'zod' export type ResourceId = string; export type RecordSchema = Array<{name?: string | undefined, type?: string | undefined}>; export type PhysicalField = {kind: 'physical', name?: string | undefined, type?: string | undefined}; export type ComputedField = {kind: 'computed', name?: string | undefined, expression?: string | undefined}; export type MetadataField = {kind: 'metadata', 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 AutoscaleConfiguration = {pause_conditions?: ({[key: string]: string | null} | null) | undefined, resume_conditions?: ({[key: string]: string | null} | null) | undefined} | null; export type ConnectionCommon = {name?: string | undefined, description?: string | undefined, connector?: ('astra-streaming' | 'clickhouse' | 'confluent-cloud' | 'datadog' | 'datagen' | 'delta-lake' | 'druid' | 'dynamodb-cdc' | 'elasticsearch' | 'gcp-pubsub' | 'hudi' | 'influxdb' | 'kafka' | 'kinesis' | 'mongodb' | 'mongodb-cdc' | 'ms-sql' | 'mysql-cdc' | 'oracle' | 'oracle-cdc' | 'pinot' | 'polaris' | 'postgres' | 'postgres-cdc' | 'pulsar' | 'redis' | 'redpanda' | 'redshift' | 'rest' | 'rockset' | 's3' | 's3-lambda' | 's3-v2' | 'singlestore-db' | 'snowflake' | 'startree' | 'streamnative' | 'timescale') | undefined, type?: ('source' | 'sink') | undefined, stream_mappings?: (Array<{stream_id?: ResourceId | undefined, external_resource_specifier: unknown}>) | undefined, stream_id?: ResourceId | undefined, schema?: RecordSchema | undefined, schema_v2?: SchemaV2 | undefined, properties?: ({[key: string]: string | null}) | undefined, autoscale_configuration?: AutoscaleConfiguration | undefined}; export type CreateApiConnectivityCheckTokenArgs = {body: ConnectionCommon}; export type CreateApiConnectivityCheckTokenOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const dataPlaneJwtToken = z.string(); export const createApiConnectivityCheckTokenResponse = z.object({token: dataPlaneJwtToken, data_plane_request: 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 JWT token to perform a connectivity check in the data plane * * POST /connectivity-check/token */ export const createApiConnectivityCheckToken = async (args: CreateApiConnectivityCheckTokenArgs, options?: CreateApiConnectivityCheckTokenOptions) => { const {baseUrl = "https://replace-with-account.api.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(/\/+$/, '')}/connectivity-check/token${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(createApiConnectivityCheckTokenResponse, await res.json()) };