import {z} from 'zod' export type UpdatePasswordAuth = {username?: string | undefined, password?: string | undefined}; 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 UpdatePostgresSourceOptions = {host?: string | undefined, port?: number | undefined, database?: string | undefined, auth_method?: 'password_auth' | undefined, password_auth?: UpdatePasswordAuth | undefined, use_ssh_tunnel?: boolean | undefined, ssh_tunnel?: SSHTunnelParams | undefined, disable_ssl?: boolean | undefined}; export type UpdateRedshiftAccessKeys = {password: string, access_id: string, secret_key: string}; export type AWSIAMRole = {aws_iam_role_arn: string}; export type UpdateRedshiftSourceOptions = {host?: string | undefined, port?: number | undefined, database?: string | undefined, cluster?: string | undefined, region?: string | undefined, workgroup?: string | undefined, username?: string | undefined, auth_method?: ('aws_iam_role' | 'aws_access_keys') | undefined, aws_access_keys?: UpdateRedshiftAccessKeys | undefined, aws_iam_role?: AWSIAMRole | undefined, use_ssh_tunnel?: boolean | undefined, ssh_tunnel?: SSHTunnelParams | undefined, disable_ssl?: boolean | undefined, connection_timeout_in_seconds?: number | undefined}; export type UpdateRedshiftServerlessSourceOptions = {host?: string | undefined, port?: number | undefined, database?: string | undefined, workgroup?: string | undefined, region?: string | undefined, auth_method?: ('aws_iam_role' | 'aws_access_keys') | undefined, aws_access_keys?: UpdateRedshiftAccessKeys | undefined, aws_iam_role?: AWSIAMRole | undefined, use_ssh_tunnel?: boolean | undefined, ssh_tunnel?: SSHTunnelParams | undefined, disable_ssl?: boolean | undefined, connection_timeout_in_seconds?: number | undefined}; 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 UpdateGCPServiceAccountRole = {service_account_email?: string | undefined, workload_identity_federation_metadata?: GcpExternalAccountMetadata | undefined}; export type UpdateFederatedGCPServiceAccountRole = {service_account_email: string, workload_identity_federation_metadata?: GcpExternalAccountMetadata | undefined}; export type UpdateGCPSourceServiceAccountKey = {service_account_email?: string | undefined, service_account_key: unknown}; export type UpdateBigQuerySourceOptions = {project_id?: string | undefined, region?: string | undefined, auth_method?: ('gcp_service_account_role' | 'gcp_service_account_key' | 'federated_gcp_service_account_role') | undefined, gcp_service_account_role?: UpdateGCPServiceAccountRole | undefined, federated_gcp_service_account_role?: UpdateFederatedGCPServiceAccountRole | undefined, gcp_service_account_key?: UpdateGCPSourceServiceAccountKey | undefined}; export type UpdateAWSAccessKeys = {access_id?: string | undefined, secret_key?: string | undefined}; export type UpdateS3SourceOptions = {bucket_name?: string | undefined, region?: string | undefined, auth_method?: ('aws_iam_role' | 'aws_access_keys') | undefined, aws_iam_role?: AWSIAMRole | undefined, aws_access_keys?: UpdateAWSAccessKeys | undefined, bucket_host?: string | undefined, use_ssh_tunnel?: boolean | undefined, ssh_tunnel?: SSHTunnelParams | undefined}; export type UpdateAzureServiceSharedAccessSignature = {shared_access_signature_token?: string | undefined}; export type UpdateABSSourceOptions = {bucket_name?: string | undefined, storage_account_name?: string | undefined, auth_method?: 'azure_service_shared_access_signature' | undefined, azure_service_shared_access_signature?: UpdateAzureServiceSharedAccessSignature | undefined}; export type UpdatePublicKeyAuth = {public_key?: string | undefined}; export type UpdateSFTPSourceOptions = {username?: string | undefined, host?: string | undefined, port?: number | undefined, auth_method?: 'public_key_auth' | undefined, public_key_auth?: UpdatePublicKeyAuth | undefined}; export type UpdateGCSAccessKeys = {access_key?: string | undefined, secret?: string | undefined}; export type UpdateGCSSourceOptions = {bucket_name?: string | undefined, region?: string | undefined, auth_method?: ('gcp_service_account_role' | 'federated_gcp_service_account_role' | 'gcs_hmac_keys' | 'gcp_service_account_key') | undefined, gcp_service_account_role?: UpdateGCPServiceAccountRole | undefined, federated_gcp_service_account_role?: UpdateFederatedGCPServiceAccountRole | undefined, gcs_hmac_keys?: UpdateGCSAccessKeys | undefined, gcp_service_account_key?: UpdateGCPSourceServiceAccountKey | undefined}; export type UpdateS3CompatibleSourceOptions = {host?: string | undefined, port?: number | undefined, bucket_name?: string | undefined, auth_method?: 'access_keys' | undefined, aws_access_keys?: UpdateAWSAccessKeys | undefined, disable_ssl?: boolean | undefined, use_ssh_tunnel?: boolean | undefined, ssh_tunnel?: SSHTunnelParams | undefined}; export type UpdateSnowflakeSourceOptions = {host?: string | undefined, port?: number | undefined, database?: string | undefined, username?: string | undefined, auth_method?: 'public_key_auth' | undefined, public_key_auth?: UpdatePublicKeyAuth | undefined}; export type BearerTokenAuth = {bearer_token: string}; export type OAuthAuth = {client_id: string, client_secret: string}; export type UpdateDeltaSharingSourceOptions = {host?: string | undefined, port?: number | undefined, auth_method?: ('bearer_token' | 'oauth2') | undefined, bearer_token?: BearerTokenAuth | undefined, oauth2?: OAuthAuth | undefined}; export type UpdateSourceOptions = {name?: string | undefined, vendor?: string | undefined, postgres?: UpdatePostgresSourceOptions | undefined, redshift?: UpdateRedshiftSourceOptions | undefined, redshift_serverless?: UpdateRedshiftServerlessSourceOptions | undefined, bigquery?: UpdateBigQuerySourceOptions | undefined, s3?: UpdateS3SourceOptions | undefined, abs?: UpdateABSSourceOptions | undefined, sftp?: UpdateSFTPSourceOptions | undefined, gcs?: UpdateGCSSourceOptions | undefined, s3_compatible?: UpdateS3CompatibleSourceOptions | undefined, snowflake?: UpdateSnowflakeSourceOptions | undefined, delta_sharing?: UpdateDeltaSharingSourceOptions | undefined}; export type UpdateSourceRequest = {source: UpdateSourceOptions}; export type PatchApiSourcesSourceIdArgs = {source_id: string, body: UpdateSourceRequest}; export type PatchApiSourcesSourceIdOptions = {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 patchApiSourcesSourceIdResponse = 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 }; /** * Update source * * PATCH /sources/{source_id} */ export const patchApiSourcesSourceId = async (args: PatchApiSourcesSourceIdArgs, options?: PatchApiSourcesSourceIdOptions) => { 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/${encodeURIComponent(String(args.source_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(patchApiSourcesSourceIdResponse, await res.json()) };