UNPKG

@smartsamurai/krapi-sdk

Version:

KRAPI TypeScript SDK - Easy-to-use client SDK for connecting to self-hosted KRAPI servers (like Appwrite SDK)

414 lines (383 loc) 13 kB
/** * Service Manager * * Manages adapter lifecycle and provides unified access to all services. */ import { ActivityLogger } from "../activity-logger"; import { AdminService } from "../admin-service"; import { AuthService } from "../auth-service"; import { BackupService } from "../backup-service"; import { CollectionsSchemaManager } from "../collections-schema-manager"; import { CollectionsService } from "../collections-service"; import { DatabaseConnection, Logger } from "../core"; import { EmailService } from "../email-service"; import { HealthService } from "../health-service"; import { ActivityHttpClient } from "../http-clients/activity-http-client"; import { AdminHttpClient } from "../http-clients/admin-http-client"; import { AuthHttpClient } from "../http-clients/auth-http-client"; import { BackupHttpClient } from "../http-clients/backup-http-client"; import { HttpClientConfig } from "../http-clients/base-http-client"; import { ChangelogHttpClient } from "../http-clients/changelog-http-client"; import { CollectionsHttpClient } from "../http-clients/collections-http-client"; import { EmailHttpClient } from "../http-clients/email-http-client"; import { HealthHttpClient } from "../http-clients/health-http-client"; import { MCPHttpClient } from "../http-clients/mcp-http-client"; import { ProjectsHttpClient } from "../http-clients/projects-http-client"; import { StorageHttpClient } from "../http-clients/storage-http-client"; import { SystemHttpClient } from "../http-clients/system-http-client"; import { TestingHttpClient } from "../http-clients/testing-http-client"; import { UsersHttpClient } from "../http-clients/users-http-client"; import { ClientConnectionConfig, ServerConnectionConfig } from "../krapi"; import { MCPService } from "../mcp-service"; import { ProjectsService } from "../projects-service"; import { StorageService } from "../storage-service"; import { SystemService } from "../system-service"; import { TestingService } from "../testing-service"; import { UsersService } from "../users-service"; import { ActivityAdapter } from "./adapters/activity-adapter"; import { AdminAdapter } from "./adapters/admin-adapter"; import { AuthAdapter } from "./adapters/auth-adapter"; import { BackupAdapter } from "./adapters/backup-adapter"; import { ChangelogAdapter } from "./adapters/changelog-adapter"; import { CollectionsAdapter } from "./adapters/collections-adapter"; import { DocumentsAdapter } from "./adapters/documents-adapter"; import { EmailAdapter } from "./adapters/email-adapter"; import { HealthAdapter } from "./adapters/health-adapter"; import { MCPAdapter } from "./adapters/mcp-adapter"; import { ProjectsAdapter } from "./adapters/projects-adapter"; import { StorageAdapter } from "./adapters/storage-adapter"; import { SystemAdapter } from "./adapters/system-adapter"; import { TestingAdapter } from "./adapters/testing-adapter"; import { UsersAdapter } from "./adapters/users-adapter"; import type { Mode } from "./connection-manager"; export class ServiceManager { private mode: Mode; private logger: Logger; private db?: DatabaseConnection; // HTTP clients (client mode) private authHttpClient?: AuthHttpClient; private projectsHttpClient?: ProjectsHttpClient; private collectionsHttpClient?: CollectionsHttpClient; private storageHttpClient?: StorageHttpClient; private emailHttpClient?: EmailHttpClient; private healthHttpClient?: HealthHttpClient; private systemHttpClient?: SystemHttpClient; private testingHttpClient?: TestingHttpClient; private adminHttpClient?: AdminHttpClient; private changelogHttpClient?: ChangelogHttpClient; private usersHttpClient?: UsersHttpClient; private activityHttpClient?: ActivityHttpClient; private backupHttpClient?: BackupHttpClient; private mcpHttpClient?: MCPHttpClient; // Services (server mode) private adminService?: AdminService; private authService?: AuthService; private projectsService?: ProjectsService; private collectionsService?: CollectionsService; private collectionsSchemaManager?: CollectionsSchemaManager; private usersService?: UsersService; private storageService?: StorageService; private emailService?: EmailService; private healthService?: HealthService; private testingService?: TestingService; private systemService?: SystemService; private activityLogger?: ActivityLogger; private backupService?: BackupService; private mcpService?: MCPService; // Adapters (lazy initialized) private _auth?: AuthAdapter; private _projects?: ProjectsAdapter; private _collections?: CollectionsAdapter; private _documents?: DocumentsAdapter; private _storage?: StorageAdapter; private _users?: UsersAdapter; private _email?: EmailAdapter; private _admin?: AdminAdapter; private _system?: SystemAdapter; private _health?: HealthAdapter; private _backup?: BackupAdapter; private _mcp?: MCPAdapter; private _activity?: ActivityAdapter; private _changelog?: ChangelogAdapter; private _testing?: TestingAdapter; constructor(mode: Mode, logger: Logger, db?: DatabaseConnection) { this.mode = mode; this.logger = logger; if (db) { this.db = db; } } /** * Initialize HTTP clients for client mode */ async initializeClientMode(config: ClientConnectionConfig): Promise<void> { if (this.mode !== "client") { return; } const httpConfig: HttpClientConfig = { baseUrl: config.endpoint, }; if (config.apiKey) httpConfig.apiKey = config.apiKey; if (config.sessionToken) httpConfig.sessionToken = config.sessionToken; if (config.timeout) httpConfig.timeout = config.timeout; if (config.retry) httpConfig.retry = config.retry; this.authHttpClient = new AuthHttpClient(httpConfig); this.projectsHttpClient = new ProjectsHttpClient(httpConfig); this.collectionsHttpClient = new CollectionsHttpClient(httpConfig); this.storageHttpClient = new StorageHttpClient(httpConfig); this.emailHttpClient = new EmailHttpClient(httpConfig); this.healthHttpClient = new HealthHttpClient(httpConfig); this.systemHttpClient = new SystemHttpClient(httpConfig); this.testingHttpClient = new TestingHttpClient(httpConfig); this.adminHttpClient = new AdminHttpClient(httpConfig); this.changelogHttpClient = new ChangelogHttpClient(httpConfig); this.usersHttpClient = new UsersHttpClient(httpConfig); this.activityHttpClient = new ActivityHttpClient(httpConfig); this.backupHttpClient = new BackupHttpClient(httpConfig); this.mcpHttpClient = new MCPHttpClient(httpConfig); } /** * Initialize services for server mode */ async initializeServerMode(_config: ServerConnectionConfig): Promise<void> { if (this.mode !== "server" || !this.db) { return; } // Import services dynamically to avoid circular dependencies const { AdminService } = await import("../admin-service"); const { UsersService } = await import("../users-service"); this.adminService = new AdminService(this.db, this.logger); this.authService = new AuthService(this.db, this.logger); this.projectsService = new ProjectsService(this.db, this.logger); this.collectionsService = new CollectionsService(this.db, this.logger as Console); this.collectionsSchemaManager = new CollectionsSchemaManager( this.db, this.logger as Console ); this.usersService = new UsersService(this.db, this.logger); this.storageService = new StorageService(this.db, this.logger); this.emailService = new EmailService(this.db, this.logger); this.healthService = new HealthService(this.db, this.logger); this.testingService = new TestingService(this.db, this.logger); // SystemService doesn't need database connection, it's a client-side service // For server mode, we'll use a placeholder - SystemService is primarily for client mode this.systemService = new SystemService("", ""); this.activityLogger = new ActivityLogger(this.db, this.logger); this.backupService = new BackupService(this.db, this.logger); this.mcpService = new MCPService(this.db, this.logger); } /** * Clear all HTTP clients */ clearHttpClients(): void { delete this.authHttpClient; delete this.projectsHttpClient; delete this.collectionsHttpClient; delete this.storageHttpClient; delete this.emailHttpClient; delete this.healthHttpClient; delete this.systemHttpClient; delete this.testingHttpClient; delete this.adminHttpClient; delete this.changelogHttpClient; delete this.usersHttpClient; delete this.activityHttpClient; delete this.backupHttpClient; delete this.mcpHttpClient; } /** * Get auth adapter */ get auth(): AuthAdapter { if (!this._auth) { this._auth = new AuthAdapter( this.mode as "client" | "server", this.authHttpClient, this.authService ); } return this._auth; } /** * Get projects adapter */ get projects(): ProjectsAdapter { if (!this._projects) { this._projects = new ProjectsAdapter( this.mode as "client" | "server", this.projectsHttpClient, this.projectsService ); } return this._projects; } /** * Get collections adapter */ get collections(): CollectionsAdapter { if (!this._collections) { this._collections = new CollectionsAdapter( this.mode as "client" | "server", this.collectionsHttpClient, this.collectionsService, this.collectionsSchemaManager ); } return this._collections; } /** * Get documents adapter */ get documents(): DocumentsAdapter { if (!this._documents) { this._documents = new DocumentsAdapter( this.mode as "client" | "server", this.collectionsHttpClient, this.collectionsService ); } return this._documents; } /** * Get storage adapter */ get storage(): StorageAdapter { if (!this._storage) { this._storage = new StorageAdapter( this.mode as "client" | "server", this.storageHttpClient, this.storageService ); } return this._storage; } /** * Get users adapter */ get users(): UsersAdapter { if (!this._users) { this._users = new UsersAdapter( this.mode as "client" | "server", this.usersHttpClient, this.usersService ); } return this._users; } /** * Get email adapter */ get email(): EmailAdapter { if (!this._email) { this._email = new EmailAdapter( this.mode as "client" | "server", this.emailHttpClient, this.emailService ); } return this._email; } /** * Get admin adapter */ get admin(): AdminAdapter { if (!this._admin) { this._admin = new AdminAdapter( this.mode as "client" | "server", this.adminHttpClient, this.adminService ); } return this._admin; } /** * Get system adapter */ get system(): SystemAdapter { if (!this._system) { this._system = new SystemAdapter( this.mode as "client" | "server", this.systemHttpClient, this.systemService ); } return this._system; } /** * Get health adapter */ get health(): HealthAdapter { if (!this._health) { this._health = new HealthAdapter( this.mode as "client" | "server", this.healthHttpClient, this.healthService ); } return this._health; } /** * Get backup adapter */ get backup(): BackupAdapter { if (!this._backup) { this._backup = new BackupAdapter( this.mode as "client" | "server", this.backupHttpClient, this.backupService ); } return this._backup; } /** * Get MCP adapter */ get mcp(): MCPAdapter { if (!this._mcp) { this._mcp = new MCPAdapter( this.mode as "client" | "server", this.mcpHttpClient, this.mcpService ); } return this._mcp; } /** * Get activity adapter */ get activity(): ActivityAdapter { if (!this._activity) { this._activity = new ActivityAdapter( this.mode as "client" | "server", this.activityHttpClient, this.activityLogger ); } return this._activity; } /** * Get changelog adapter */ get changelog(): ChangelogAdapter { if (!this._changelog) { this._changelog = new ChangelogAdapter( this.mode as "client" | "server", this.changelogHttpClient ); } return this._changelog; } /** * Get testing adapter */ get testing(): TestingAdapter { if (!this._testing) { this._testing = new TestingAdapter( this.mode as "client" | "server", this.testingHttpClient, this.testingService ); } return this._testing; } }