@smartsamurai/krapi-sdk
Version:
KRAPI TypeScript SDK - Easy-to-use client SDK for connecting to self-hosted KRAPI servers (like Appwrite SDK)
95 lines (82 loc) • 2.79 kB
text/typescript
import { KrapiClientConfig } from "../client";
import { AdminHttpClient } from "../http-clients/admin-http-client";
import { AuthHttpClient } from "../http-clients/auth-http-client";
import { EmailHttpClient } from "../http-clients/email-http-client";
import { HealthHttpClient } from "../http-clients/health-http-client";
import { StorageHttpClient } from "../http-clients/storage-http-client";
import { HttpClientManager } from "./http-client-manager";
/**
* Service Client Manager
*
* Manages the initialization and lifecycle of all HTTP client services
* used by the KRAPI client SDK.
*/
export class ServiceClientManager {
private httpClientManager: HttpClientManager;
// Service clients
public auth!: AuthHttpClient;
public storage!: StorageHttpClient;
public admin!: AdminHttpClient;
public email!: EmailHttpClient;
public health!: HealthHttpClient;
constructor(config: KrapiClientConfig) {
this.httpClientManager = new HttpClientManager(config);
this.initializeServiceClients();
}
/**
* Get the HTTP client manager
*/
getHttpClientManager(): HttpClientManager {
return this.httpClientManager;
}
/**
* Update API key across all clients
*/
setApiKey(apiKey: string): void {
this.httpClientManager.setApiKey(apiKey);
}
/**
* Update session token across all clients
*/
setSessionToken(token: string): void {
this.httpClientManager.setSessionToken(token);
}
/**
* Update project ID across all clients
*/
setProjectId(projectId: string): void {
this.httpClientManager.setProjectId(projectId);
}
/**
* Get current configuration
*/
getConfig(): KrapiClientConfig {
return this.httpClientManager.getConfig();
}
/**
* Initialize all service clients with the HTTP client manager
*/
private initializeServiceClients(): void {
const config = this.httpClientManager.getConfig();
const httpConfig: {
baseUrl: string;
apiKey?: string;
sessionToken?: string;
projectId?: string;
timeout?: number;
} = {
baseUrl: config.endpoint.replace(/\/$/, ""),
};
// Only add defined properties to avoid type issues
if (config.apiKey !== undefined) httpConfig.apiKey = config.apiKey;
if (config.sessionToken !== undefined) httpConfig.sessionToken = config.sessionToken;
if (config.projectId !== undefined) httpConfig.projectId = config.projectId;
if (config.timeout !== undefined) httpConfig.timeout = config.timeout;
// Initialize service clients
this.auth = new AuthHttpClient(httpConfig);
this.storage = new StorageHttpClient(httpConfig);
this.admin = new AdminHttpClient(httpConfig);
this.email = new EmailHttpClient(httpConfig);
this.health = new HealthHttpClient(httpConfig);
}
}