s3-csv-autosigner
Version:
A TypeScript library for uploading CSV files to S3 and generating presigned URLs, with automatic AWS configuration discovery
194 lines (193 loc) • 7.16 kB
TypeScript
interface CsvUploadResult {
localPath: string;
fileName: string;
presignedUrl: string;
}
interface UploadOptions {
/** S3 bucket name (defaults to S3_BUCKET environment variable) */
bucket?: string;
keyPrefix?: string;
expiresIn?: number;
region?: string;
accessKeyId?: string;
secretAccessKey?: string;
sessionToken?: string;
/** Enable verbose logging for configuration discovery and upload process */
verbose?: boolean;
/** Directory to start .env file discovery from */
startDir?: string;
}
interface CsvFile {
localPath: string;
fileName: string;
}
/**
* Uploads CSV files to S3 and generates presigned download URLs.
*
* This function is a volatile little bastard that takes your CSV files,
* throws them at S3, and gives you back download URLs that'll work for an hour.
* Perfect for when you need to share data but don't want to deal with
* authentication nightmares.
*/
declare function uploadCsvFiles(csvFilePaths: string[], options?: UploadOptions): Promise<CsvUploadResult[]>;
/**
* Convenience function for uploading a single CSV file.
* Because sometimes you just have one file and don't want to wrap it in an array.
*/
declare function uploadSingleCsv(csvFilePath: string, options?: UploadOptions): Promise<CsvUploadResult>;
import { S3Client } from "@aws-sdk/client-s3";
/**
* AWS configuration that can be explicitly provided or discovered from environment
*/
interface AwsConfig {
region?: string;
accessKeyId?: string;
secretAccessKey?: string;
sessionToken?: string;
profile?: string;
}
/**
* Result of AWS configuration discovery and validation
*/
interface AwsConfigResult {
config: AwsConfig;
isValid: boolean;
missingVars: string[];
source: "explicit" | "environment" | "aws-sdk-default";
envFileUsed?: string;
}
/**
* Discovers and loads AWS configuration from environment files and variables
*
* This function is smarter than your average config loader:
* 1. Discovers .env files up the directory tree
* 2. Loads environment variables
* 3. Validates AWS credential configuration
* 4. Provides helpful error messages when shit goes wrong
*
* @param explicitConfig - Explicitly provided AWS configuration
* @param options - Discovery options
* @returns Promise<AwsConfigResult> - Complete AWS configuration result
*/
declare function discoverAwsConfig(explicitConfig?: AwsConfig, options?: {
verbose?: boolean
startDir?: string
}): Promise<AwsConfigResult>;
/**
* Validates AWS configuration and throws helpful errors
*
* @param configResult - Result from discoverAwsConfig
* @throws Error with helpful message if configuration is invalid
*/
declare function validateAwsConfig(configResult: AwsConfigResult): void;
/**
* Shows current AWS configuration in a nice format
* Masks sensitive values for security
*
* @param config - AWS configuration to display
* @param source - Configuration source
*/
declare function showAwsConfiguration(config: AwsConfig, source: AwsConfigResult["source"]): void;
/**
* Quick utility to show all AWS-related environment variables
* Useful for debugging configuration issues
*/
declare function showAwsEnvironmentVariables(): void;
/**
* S3 client configuration options
*/
interface S3ClientOptions {
/** AWS configuration (will be merged with discovered environment config) */
awsConfig?: AwsConfig;
/** Enable verbose logging for configuration discovery */
verbose?: boolean;
/** Directory to start .env file discovery from */
startDir?: string;
}
/**
* Creates and configures an S3 client instance with intelligent configuration discovery.
*
* This function is a configuration wizard that:
* 1. Discovers .env files up the directory tree
* 2. Loads and validates AWS credentials
* 3. Provides helpful error messages when configuration is missing
* 4. Falls back to AWS SDK default credential provider chain
*
* @param options - S3 client configuration options
* @returns Promise<S3Client> - Configured S3 client
*/
declare function createS3Client(options?: S3ClientOptions): Promise<S3Client>;
/**
* Synchronous version of createS3Client for backwards compatibility
*
* WARNING: This bypasses the intelligent configuration discovery.
* Use createS3Client() for the full experience.
*
* @param config - Basic S3 configuration
* @returns S3Client - Configured S3 client
*/
declare function createS3ClientSync(config?: AwsConfig): S3Client;
/**
* Configuration for .env file discovery
*/
interface EnvDiscoveryConfig {
/** Starting directory for search (defaults to process.cwd()) */
startDir?: string;
/** Maximum levels to search up the directory tree */
maxLevels?: number;
/** Names of .env files to look for (in priority order) */
envFileNames?: string[];
/** Whether to show verbose output during discovery */
verbose?: boolean;
}
/**
* Result of .env file discovery
*/
interface EnvDiscoveryResult {
/** Path to the discovered .env file (null if none found) */
envFilePath: string | null;
/** Directory containing the .env file */
envDirectory: string | null;
/** Name of the .env file that was found */
envFileName: string | null;
/** All .env files found during search (for debugging) */
allFound: string[];
/** Number of directories searched */
searchedLevels: number;
}
/**
* Discovers .env files by crawling up the directory tree
* Starts from current directory and searches upward until it finds an .env file
* or reaches the filesystem root or max search levels
*
* @param config - Configuration for the discovery process
* @returns Promise<EnvDiscoveryResult> - Results of the discovery process
*/
declare function discoverEnvFile(config?: EnvDiscoveryConfig): Promise<EnvDiscoveryResult>;
/**
* Loads and applies .env file variables to process.env
* Uses the discovery mechanism to find the closest .env file
*
* @param config - Configuration for discovery and loading
* @returns Promise<EnvDiscoveryResult> - Results including which file was loaded
*/
declare function loadDiscoveredEnvFile(config?: EnvDiscoveryConfig): Promise<EnvDiscoveryResult>;
/**
* Quick utility to just get the path of the closest .env file
* For when you don't need all the bells and whistles
*
* @param startDir - Directory to start searching from
* @returns Promise<string | null> - Path to .env file or null if not found
*/
declare function findEnvFile(startDir?: string): Promise<string | null>;
/**
* Validates that required environment variables are present
* Useful for checking configuration after loading .env files
*
* @param requiredVars - Array of required environment variable names
* @param verbose - Whether to show detailed output
* @returns boolean - True if all required variables are present
*/
declare function validateRequiredEnvVars(requiredVars: string[], verbose?: boolean): boolean;
import { S3Client as S3Client2 } from "@aws-sdk/client-s3";
export { validateRequiredEnvVars, validateAwsConfig, uploadSingleCsv, uploadCsvFiles, showAwsEnvironmentVariables, showAwsConfiguration, loadDiscoveredEnvFile, findEnvFile, discoverEnvFile, discoverAwsConfig, createS3ClientSync, createS3Client, UploadOptions, S3Client2 as S3Client, EnvDiscoveryResult, EnvDiscoveryConfig, CsvUploadResult, CsvFile, AwsConfigResult, AwsConfig };