UNPKG

@smartsamurai/krapi-sdk

Version:

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

208 lines (193 loc) 7.08 kB
/** * Projects Adapter * * Unifies ProjectsHttpClient and ProjectsService behind a common interface. */ import { ActivityLog, ActivityLogger } from "../../activity-logger"; import { ProjectsHttpClient } from "../../http-clients/projects-http-client"; import { ProjectsService } from "../../projects-service"; import { Project, ProjectStats, ProjectSettings } from "../../types"; import { createAdapterInitError } from "./error-handler"; type Mode = "client" | "server"; export class ProjectsAdapter { private mode: Mode; private httpClient: ProjectsHttpClient | undefined; private service: ProjectsService | undefined; private activityLogger: ActivityLogger | undefined; constructor( mode: Mode, httpClient?: ProjectsHttpClient, service?: ProjectsService, activityLogger?: ActivityLogger ) { this.mode = mode; this.httpClient = httpClient; this.service = service; this.activityLogger = activityLogger; } async create(projectData: { name: string; description?: string; settings?: Record<string, unknown>; }): Promise<Project> { if (this.mode === "client") { if (!this.httpClient) { throw createAdapterInitError("HTTP client", this.mode); } const response = await this.httpClient.createProject(projectData); return (response.data as unknown as Project) || ({} as Project); } else { if (!this.service) { throw createAdapterInitError("Projects service", this.mode); } return this.service.createProject("system", projectData) as unknown as Project; } } async get(projectId: string): Promise<Project> { if (this.mode === "client") { if (!this.httpClient) { throw createAdapterInitError("HTTP client", this.mode); } const response = await this.httpClient.getProject(projectId); return (response.data as unknown as Project) || ({} as Project); } else { if (!this.service) { throw createAdapterInitError("Projects service", this.mode); } return this.service.getProjectById(projectId) as unknown as Project; } } async update(projectId: string, updates: Record<string, unknown>): Promise<Project> { if (this.mode === "client") { if (!this.httpClient) { throw createAdapterInitError("HTTP client", this.mode); } const response = await this.httpClient.updateProject(projectId, updates); return (response.data as unknown as Project) || ({} as Project); } else { if (!this.service) { throw createAdapterInitError("Projects service", this.mode); } return this.service.updateProject(projectId, updates) as unknown as Project; } } async delete(projectId: string): Promise<{ success: boolean }> { if (this.mode === "client") { if (!this.httpClient) { throw createAdapterInitError("HTTP client", this.mode); } const response = await this.httpClient.deleteProject(projectId); return response.data || { success: false }; } else { if (!this.service) { throw createAdapterInitError("Projects service", this.mode); } const success = await this.service.deleteProject(projectId); return { success }; } } async getAll(options?: { limit?: number; offset?: number; search?: string; status?: string; }): Promise<Project[]> { if (this.mode === "client") { if (!this.httpClient) { throw createAdapterInitError("HTTP client", this.mode); } const response = await this.httpClient.getAllProjects(options); return (response.data as unknown as Project[]) || []; } else { if (!this.service) { throw createAdapterInitError("Projects service", this.mode); } return this.service.getAllProjects(options) as unknown as Project[]; } } async getStatistics(projectId: string): Promise<ProjectStats> { if (this.mode === "client") { if (!this.httpClient) { throw createAdapterInitError("HTTP client", this.mode); } const response = await this.httpClient.getProjectStatistics(projectId); return (response.data as unknown as ProjectStats) || ({} as ProjectStats); } else { if (!this.service) { throw createAdapterInitError("Projects service", this.mode); } return this.service.getProjectStatistics(projectId) as unknown as ProjectStats; } } async getSettings(projectId: string): Promise<ProjectSettings> { if (this.mode === "client") { if (!this.httpClient) { throw createAdapterInitError("HTTP client", this.mode); } const response = await this.httpClient.getProjectSettings(projectId); return (response.data as unknown as ProjectSettings) || ({} as ProjectSettings); } else { if (!this.service) { throw createAdapterInitError("Projects service", this.mode); } const project = await this.service.getProjectById(projectId); return (project?.settings as unknown as ProjectSettings) || ({} as ProjectSettings); } } async updateSettings( projectId: string, settings: Record<string, unknown> ): Promise<ProjectSettings> { if (this.mode === "client") { if (!this.httpClient) { throw createAdapterInitError("HTTP client", this.mode); } const response = await this.httpClient.updateProjectSettings(projectId, settings); return (response.data as unknown as ProjectSettings) || ({} as ProjectSettings); } else { if (!this.service) { throw createAdapterInitError("Projects service", this.mode); } return this.service.updateProjectSettings(projectId, settings) as unknown as ProjectSettings; } } async getActivity( projectId: string, options?: { limit?: number; offset?: number; action_type?: string; start_date?: string; end_date?: string; } ): Promise<ActivityLog[]> { if (this.mode === "client") { if (!this.httpClient) { throw createAdapterInitError("HTTP client", this.mode); } const response = await this.httpClient.getProjectActivity(projectId, options); return (response.data as unknown as ActivityLog[]) || []; } else { if (!this.activityLogger) { throw createAdapterInitError("Activity logger", this.mode); } const queryOptions: { project_id: string; action?: string; start_date?: Date; end_date?: Date; limit?: number; offset?: number; } = { project_id: projectId, }; if (options?.action_type) queryOptions.action = options.action_type; if (options?.start_date) queryOptions.start_date = new Date(options.start_date); if (options?.end_date) queryOptions.end_date = new Date(options.end_date); if (options?.limit) queryOptions.limit = options.limit; if (options?.offset) queryOptions.offset = options.offset; const result = await this.activityLogger.query(queryOptions); return result.logs; } } }