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 CreateScopedAuthTokenRequest = {application_origin: string, provider_id: string, source: SourceOptions}; export type CreateApiActionsGenerateScopedAuthTokenArgs = {body: CreateScopedAuthTokenRequest}; export type CreateApiActionsGenerateScopedAuthTokenOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createScopedAuthTokenResponse = z.object({scoped_token: z.string()}); export const createApiActionsGenerateScopedAuthTokenResponse = z.object({status: z.literal("success"), data: createScopedAuthTokenResponse, 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 }; /** * Generate Scoped Auth Token * * POST /actions/generate-scoped-auth-token */ export const createApiActionsGenerateScopedAuthToken = async (args: CreateApiActionsGenerateScopedAuthTokenArgs, options?: CreateApiActionsGenerateScopedAuthTokenOptions) => { 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(/\/+$/, '')}/actions/generate-scoped-auth-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(createApiActionsGenerateScopedAuthTokenResponse, await res.json()) };