/** * Base interface and types for platform service providers */ import type { IService, IPlatformService, IPlatformResource, IPlatformServiceConfig, IProvisionedResource, IEnvVarMapping, TPlatformServiceType, TPlatformResourceType, } from '../../../types.ts'; import type { Onebox } from '../../onebox.ts'; /** * Interface that all platform service providers must implement */ export interface IPlatformServiceProvider { /** Unique identifier for this provider type */ readonly type: TPlatformServiceType; /** Human-readable display name */ readonly displayName: string; /** Resource types this provider can provision */ readonly resourceTypes: TPlatformResourceType[]; /** * Get the default configuration for this platform service */ getDefaultConfig(): IPlatformServiceConfig; /** * Deploy the platform service container * @returns The container ID */ deployContainer(): Promise; /** * Stop the platform service container */ stopContainer(containerId: string): Promise; /** * Check if the platform service is healthy and ready to accept connections */ healthCheck(): Promise; /** * Provision a resource for a user service (e.g., create database, bucket) * @param userService The user service requesting the resource * @returns Provisioned resource with credentials and env var mappings */ provisionResource(userService: IService): Promise; /** * Deprovision a resource (e.g., drop database, delete bucket) * @param resource The resource to deprovision */ deprovisionResource(resource: IPlatformResource, credentials: Record): Promise; /** * Get the environment variable mappings for this provider */ getEnvVarMappings(): IEnvVarMapping[]; } /** * Base class for platform service providers with common functionality */ export abstract class BasePlatformServiceProvider implements IPlatformServiceProvider { abstract readonly type: TPlatformServiceType; abstract readonly displayName: string; abstract readonly resourceTypes: TPlatformResourceType[]; protected oneboxRef: Onebox; constructor(oneboxRef: Onebox) { this.oneboxRef = oneboxRef; } abstract getDefaultConfig(): IPlatformServiceConfig; abstract deployContainer(): Promise; abstract stopContainer(containerId: string): Promise; abstract healthCheck(): Promise; abstract provisionResource(userService: IService): Promise; abstract deprovisionResource(resource: IPlatformResource, credentials: Record): Promise; abstract getEnvVarMappings(): IEnvVarMapping[]; /** * Get the internal Docker network name for platform services */ protected getNetworkName(): string { return 'onebox-network'; } /** * Get the container name for this platform service */ protected getContainerName(): string { return `onebox-${this.type}`; } /** * Generate a resource name from a user service name */ protected generateResourceName(serviceName: string, prefix: string = 'onebox'): string { // Replace dashes with underscores for database compatibility const sanitized = serviceName.replace(/-/g, '_'); return `${prefix}_${sanitized}`; } /** * Generate a bucket name from a user service name */ protected generateBucketName(serviceName: string, prefix: string = 'onebox'): string { // Buckets use dashes, lowercase const sanitized = serviceName.toLowerCase().replace(/_/g, '-'); return `${prefix}-${sanitized}`; } }