import {z} from 'zod' export type PasswordAuth = {username: string, password: string}; export type SSHTunnelParams = {ssh_tunnel_host: string, ssh_tunnel_port: number, ssh_tunnel_username: string, ssh_public_key?: ('primary_public_key' | 'secondary_public_key') | undefined}; export type PostgresSourceOptions = {host: string, port: number, database: string, auth_method: 'password_auth', password_auth: PasswordAuth, use_ssh_tunnel?: boolean | undefined, ssh_tunnel?: SSHTunnelParams | undefined, disable_ssl?: boolean | undefined}; export type RedshiftAccessKeys = {password: string, access_id: string, secret_key: string}; export type RedshiftSourceOptionsAWSAccessKeys = {host: string, port: number, database: string, cluster: string, region: string, username: string, use_ssh_tunnel?: boolean | undefined, ssh_tunnel?: SSHTunnelParams | undefined, disable_ssl?: boolean | undefined, connection_timeout_in_seconds?: number | undefined, auth_method: 'aws_access_keys', aws_access_keys: RedshiftAccessKeys}; export type AWSIAMRole = {aws_iam_role_arn: string}; export type RedshiftSourceOptionsAWSIAMRole = {host: string, port: number, database: string, cluster: string, region: string, username: string, use_ssh_tunnel?: boolean | undefined, ssh_tunnel?: SSHTunnelParams | undefined, disable_ssl?: boolean | undefined, connection_timeout_in_seconds?: number | undefined, auth_method: 'aws_iam_role', aws_iam_role: AWSIAMRole}; export type RedshiftSourceOptions = RedshiftSourceOptionsAWSAccessKeys | RedshiftSourceOptionsAWSIAMRole; export type RedshiftServerlessSourceOptionsAWSAccessKeys = {host: string, port: number, database: string, workgroup: string, region: string, use_ssh_tunnel?: boolean | undefined, ssh_tunnel?: SSHTunnelParams | undefined, disable_ssl?: boolean | undefined, connection_timeout_in_seconds?: number | undefined, auth_method: 'aws_access_keys', aws_access_keys: RedshiftAccessKeys}; export type RedshiftServerlessSourceOptionsAWSIAMRole = {host: string, port: number, database: string, workgroup: string, region: string, use_ssh_tunnel?: boolean | undefined, ssh_tunnel?: SSHTunnelParams | undefined, disable_ssl?: boolean | undefined, connection_timeout_in_seconds?: number | undefined, auth_method: 'aws_iam_role', aws_iam_role: AWSIAMRole}; export type RedshiftServerlessSourceOptions = RedshiftServerlessSourceOptionsAWSAccessKeys | RedshiftServerlessSourceOptionsAWSIAMRole; export type GCPServiceAccountRole = {service_account_email: string}; export type BigQuerySourceOptionsGCPServiceAccountRole = {project_id: string, region: string, auth_method: 'gcp_service_account_role', gcp_service_account_role: GCPServiceAccountRole}; export type CredentialSource = {environment_id: string, region_url: string, url: string, regional_cred_verification_url: string}; export type GcpExternalAccountMetadata = {type: string, audience: string, subject_token_type: string, service_account_impersonation_url: string, token_url: string, credential_source: CredentialSource}; export type FederatedGCPServiceAccountRole = {service_account_email: string, workload_identity_federation_metadata?: GcpExternalAccountMetadata | undefined}; export type BigQuerySourceOptionsFederatedGCPServiceAccountRole = {project_id: string, region: string, auth_method: 'federated_gcp_service_account_role', federated_gcp_service_account_role: FederatedGCPServiceAccountRole}; export type GCPSourceServiceAccountKey = {service_account_email: string, service_account_key: unknown}; export type BigQuerySourceOptionsGCPServiceAccountKey = {project_id: string, region: string, auth_method: 'gcp_service_account_key', gcp_service_account_key: GCPSourceServiceAccountKey}; export type BigQuerySourceOptions = BigQuerySourceOptionsGCPServiceAccountRole | BigQuerySourceOptionsFederatedGCPServiceAccountRole | BigQuerySourceOptionsGCPServiceAccountKey; export type S3SourceOptionsAWSIAMRole = {bucket_name: string, region: string, bucket_host?: string | undefined, use_ssh_tunnel?: boolean | undefined, ssh_tunnel?: SSHTunnelParams | undefined, auth_method: 'aws_iam_role', aws_iam_role: AWSIAMRole}; export type AWSAccessKeys = {access_id: string, secret_key: string}; export type S3SourceOptionsAWSAccessKeys = {bucket_name: string, region: string, bucket_host?: string | undefined, use_ssh_tunnel?: boolean | undefined, ssh_tunnel?: SSHTunnelParams | undefined, auth_method: 'aws_access_keys', aws_access_keys: AWSAccessKeys}; export type S3SourceOptions = S3SourceOptionsAWSIAMRole | S3SourceOptionsAWSAccessKeys; export type AzureServiceSharedAccessSignature = {shared_access_signature_token: string}; export type ABSSourceOptions = {bucket_name: string, storage_account_name: string, auth_method: 'azure_service_shared_access_signature', azure_service_shared_access_signature: AzureServiceSharedAccessSignature}; export type PublicKeyAuth = {public_key: string}; export type SFTPSourceOptions = {username: string, host: string, port: number, auth_method: 'public_key_auth', public_key_auth: PublicKeyAuth}; export type GCSSourceOptionsGCPServiceAccountRole = {bucket_name: string, region: string, auth_method: 'gcp_service_account_role', gcp_service_account_role: GCPServiceAccountRole}; export type GCSSourceOptionsFederatedGCPServiceAccountRole = {bucket_name: string, region: string, auth_method: 'federated_gcp_service_account_role', federated_gcp_service_account_role: FederatedGCPServiceAccountRole}; export type GCSAccessKeys = {access_key: string, secret: string}; export type GCSSourceOptionsGCSHMACKeys = {bucket_name: string, region: string, auth_method: 'gcs_hmac_keys', gcs_hmac_keys: GCSAccessKeys}; export type GCSSourceOptionsGCPServiceAccountKey = {bucket_name: string, region: string, auth_method: 'gcp_service_account_key', gcp_service_account_key: GCPSourceServiceAccountKey}; export type GCSSourceOptions = GCSSourceOptionsGCPServiceAccountRole | GCSSourceOptionsFederatedGCPServiceAccountRole | GCSSourceOptionsGCSHMACKeys | GCSSourceOptionsGCPServiceAccountKey; export type S3CompatibleSourceOptions = {host: string, port: number, bucket_name: string, auth_method: 'access_keys', aws_access_keys: AWSAccessKeys, disable_ssl?: boolean | undefined, use_ssh_tunnel?: boolean | undefined, ssh_tunnel?: SSHTunnelParams | undefined}; export type SnowflakeSourceOptions = {host: string, port: number, database: string, username: string, auth_method: 'public_key_auth', public_key_auth: PublicKeyAuth}; export type BearerTokenAuth = {bearer_token: string}; export type DeltaSharingSourceOptionsBearerToken = {host: string, port: number, auth_method: 'bearer_token', bearer_token: BearerTokenAuth}; export type OAuthAuth = {client_id: string, client_secret: string}; export type DeltaSharingSourceOptionsOAuth2 = {host: string, port: number, auth_method: 'oauth2', oauth2: OAuthAuth}; export type DeltaSharingSourceOptions = DeltaSharingSourceOptionsBearerToken | DeltaSharingSourceOptionsOAuth2; export type SourceOptions = {vendor: 'postgres', postgres: PostgresSourceOptions, provider_id: string, name: string} | {vendor: 'redshift', redshift: RedshiftSourceOptions, provider_id: string, name: string} | {vendor: 'redshift_serverless', redshift_serverless: RedshiftServerlessSourceOptions, provider_id: string, name: string} | {vendor: 'bigquery', bigquery: BigQuerySourceOptions, provider_id: string, name: string} | {vendor: 's3', s3: S3SourceOptions, provider_id: string, name: string} | {vendor: 'abs', abs: ABSSourceOptions, provider_id: string, name: string} | {vendor: 'sftp', sftp: SFTPSourceOptions, provider_id: string, name: string} | {vendor: 'gcs', gcs: GCSSourceOptions, provider_id: string, name: string} | {vendor: 's3_compatible', s3_compatible: S3CompatibleSourceOptions, provider_id: string, name: string} | {vendor: 'snowflake', snowflake: SnowflakeSourceOptions, provider_id: string, name: string} | {vendor: 'delta_sharing', delta_sharing: DeltaSharingSourceOptions, provider_id: string, name: string}; export type CreateSourceRequest = {source: SourceOptions}; export type CreateApiSourcesArgs = {body: CreateSourceRequest}; export type CreateApiSourcesOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const sSHTunnelResource = z.object({ssh_tunnel_host: z.string().optional(), ssh_tunnel_port: z.number().int().optional(), ssh_tunnel_username: z.string().optional(), ssh_public_key: z.string().optional()}); export const postgresSourceResource = z.object({is_valid: z.boolean(), host: z.string(), port: z.number().int(), database: z.string(), username: z.string(), auth_method: z.string(), use_ssh_tunnel: z.boolean(), ssh_tunnel: sSHTunnelResource.optional(), disable_ssl: z.boolean(), connection_timeout_in_seconds: z.number().int()}); export const aWSIAMRoleResource = z.object({aws_iam_role: z.string()}); export const redshiftAccessKeysResource = z.object({access_id: z.string()}); export const redshiftSourceResource = z.object({is_valid: z.boolean(), host: z.string(), port: z.number().int(), database: z.string(), cluster: z.string().optional(), region: z.string().optional(), workgroup: z.string().optional(), username: z.string().optional(), auth_method: z.string(), aws_iam_role: aWSIAMRoleResource.optional(), aws_access_keys: redshiftAccessKeysResource.optional(), use_ssh_tunnel: z.boolean(), ssh_tunnel: sSHTunnelResource.optional(), disable_ssl: z.boolean(), connection_timeout_in_seconds: z.number().int()}); export const redshiftServerlessSourceResource = z.object({is_valid: z.boolean(), host: z.string(), port: z.number().int(), database: z.string(), region: z.string().optional(), workgroup: z.string(), auth_method: z.string(), aws_iam_role: aWSIAMRoleResource.optional(), aws_access_keys: redshiftAccessKeysResource.optional(), use_ssh_tunnel: z.boolean(), ssh_tunnel: sSHTunnelResource.optional(), disable_ssl: z.boolean(), connection_timeout_in_seconds: z.number().int()}); export const gCPServiceAccountRoleResource = z.object({service_account_email: z.string()}); export const bigQuerySourceResourceGCPServiceAccountRole = z.object({project_id: z.string(), region: z.string(), auth_method: z.literal("gcp_service_account_role"), gcp_service_account_role: gCPServiceAccountRoleResource}); export const federatedGCPServiceAccountRoleResource = z.object({service_account_email: z.string()}); export const bigQuerySourceResourceFederatedGCPServiceAccountRole = z.object({project_id: z.string(), region: z.string(), auth_method: z.literal("federated_gcp_service_account_role"), federated_gcp_service_account_role: federatedGCPServiceAccountRoleResource}); export const gCPServiceAccountKeyResource = z.object({service_account_email: z.string()}); export const bigQuerySourceResourceGCPServiceAccountKey = z.object({project_id: z.string(), region: z.string(), auth_method: z.literal("gcp_service_account_key"), gcp_service_account_key: gCPServiceAccountKeyResource}); export const bigQuerySourceResource = z.discriminatedUnion("auth_method", [bigQuerySourceResourceGCPServiceAccountRole, bigQuerySourceResourceFederatedGCPServiceAccountRole, bigQuerySourceResourceGCPServiceAccountKey]); export const aWSAccessKeysResource = z.object({access_key_id: z.string()}); export const s3SourceResource = z.object({is_valid: z.boolean(), bucket_name: z.string(), region: z.string(), auth_method: z.string(), aws_iam_role: aWSIAMRoleResource.optional(), aws_access_keys: aWSAccessKeysResource.optional(), bucket_host: z.string().optional(), use_ssh_tunnel: z.boolean(), ssh_tunnel: sSHTunnelResource.optional()}); export const aBSSourceResource = z.object({is_valid: z.boolean(), bucket_name: z.string(), storage_account_name: z.string(), auth_method: z.string()}); export const sFTPSourceResource = z.object({is_valid: z.boolean(), username: z.string(), host: z.string(), port: z.number().int(), auth_method: z.string()}); export const gCSAccessKeysResource = z.object({access_id: z.string()}); export const gCSSourceResource = z.object({is_valid: z.boolean(), bucket_name: z.string(), region: z.string(), auth_method: z.string(), gcp_service_account_role: gCPServiceAccountRoleResource.optional(), federated_gcp_service_account_role: federatedGCPServiceAccountRoleResource.optional(), gcs_hmac_keys: gCSAccessKeysResource.optional(), gcp_service_account_key: gCPServiceAccountKeyResource.optional()}); export const s3CompatibleSourceResource = z.object({is_valid: z.boolean(), host: z.string(), port: z.number().int(), bucket_name: z.string(), auth_method: z.string(), aws_access_keys: aWSAccessKeysResource.optional(), disable_ssl: z.boolean(), use_ssh_tunnel: z.boolean(), ssh_tunnel: sSHTunnelResource.optional()}); export const publicKeyAuthResource = z.object({public_key: z.string()}); export const snowflakeSourceResource = z.object({is_valid: z.boolean(), host: z.string(), port: z.number().int(), database: z.string(), username: z.string(), auth_method: z.literal("public_key_auth"), public_key_auth: publicKeyAuthResource.optional()}); export const deltaSharingSourceResource = z.object({is_valid: z.boolean(), host: z.string(), port: z.number().int(), auth_method: z.string(), oauth_client_id: z.string().optional()}); export const sourceResource = z.union([z.object({vendor: z.literal("postgres"), postgres: postgresSourceResource, id: z.string(), provider_id: z.string(), name: z.string(), created_at: z.string(), updated_at: z.string()}), z.object({vendor: z.literal("redshift"), redshift: redshiftSourceResource, id: z.string(), provider_id: z.string(), name: z.string(), created_at: z.string(), updated_at: z.string()}), z.object({vendor: z.literal("redshift_serverless"), redshift_serverless: redshiftServerlessSourceResource, id: z.string(), provider_id: z.string(), name: z.string(), created_at: z.string(), updated_at: z.string()}), z.object({vendor: z.literal("bigquery"), bigquery: bigQuerySourceResource, id: z.string(), provider_id: z.string(), name: z.string(), created_at: z.string(), updated_at: z.string()}), z.object({vendor: z.literal("s3"), s3: s3SourceResource, id: z.string(), provider_id: z.string(), name: z.string(), created_at: z.string(), updated_at: z.string()}), z.object({vendor: z.literal("abs"), abs: aBSSourceResource, id: z.string(), provider_id: z.string(), name: z.string(), created_at: z.string(), updated_at: z.string()}), z.object({vendor: z.literal("sftp"), sftp: sFTPSourceResource, id: z.string(), provider_id: z.string(), name: z.string(), created_at: z.string(), updated_at: z.string()}), z.object({vendor: z.literal("gcs"), gcs: gCSSourceResource, id: z.string(), provider_id: z.string(), name: z.string(), created_at: z.string(), updated_at: z.string()}), z.object({vendor: z.literal("s3_compatible"), s3_compatible: s3CompatibleSourceResource, id: z.string(), provider_id: z.string(), name: z.string(), created_at: z.string(), updated_at: z.string()}), z.object({vendor: z.literal("snowflake"), snowflake: snowflakeSourceResource, id: z.string(), provider_id: z.string(), name: z.string(), created_at: z.string(), updated_at: z.string()}), z.object({vendor: z.literal("delta_sharing"), delta_sharing: deltaSharingSourceResource, id: z.string(), provider_id: z.string(), name: z.string(), created_at: z.string(), updated_at: z.string()})]); export const sourceResponse = z.object({ssh_public_key: z.string().optional(), source: sourceResource}); export const createApiSourcesResponse = z.object({status: z.literal("success"), data: sourceResponse, message: z.string()}); 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 source * * POST /sources */ export const createApiSources = async (args: CreateApiSourcesArgs, options?: CreateApiSourcesOptions) => { const {baseUrl = "https://api.prequel.co", 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-API-KEY", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/sources${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(createApiSourcesResponse, await res.json()) };